#include <stdio.h>

char *merge_spaces(char *str) {
	char *saved_str, *pos;
	saved_str = pos = str;
	bool prev_was_ch = false;
	while (*str) {
		if (*str != ' ' || prev_was_ch) {
			*pos++ = *str;
			prev_was_ch = *str != ' ';
		}
		str++;
	}
	pos[-(!prev_was_ch && pos != saved_str)] = '\0';
	return saved_str;
}

int main(void) {
	char str[] = "  hello   world this  is          a test  ";
	printf("[%s]\n", merge_spaces(str));
	return 0;
}
