// File: /workspace/github_c_makefile_star2_O0/hsjoihs[P]c-compiler/cpp.c
// Function: preprocess
struct Tokvec preprocess(const char *str,
                         struct Map2 /*<struct Vector<struct Token>*/ *def_map)
{
	const struct Tokvec t = read_all_tokens(str);
	const struct Token *src = t.v;

	int total_token_num = t.tok_num;
	int ifdef_depth = 0;

	struct Token *dst_initial = calloc(total_token_num, sizeof(struct Token));

	int dst_offset = 0;

	int flag = 1;
	while (1) {
		for (enum LineState s = LINE_HAS_JUST_STARTED;
		     flag == 0 || s != LINE_HAS_JUST_STARTED || src[0].kind != HASH;
		     dst_offset++, src++) {
			flag = 1;

			set_line_state(&s, src[0].kind);

			struct Map2 *used_map = init_map();
			replace_recursively(def_map, used_map, src,
			                    &dst_initial[dst_offset]);

			if (dst_initial[dst_offset].kind == END) {
				if (ifdef_depth) {
					fprintf(stderr, "insufficient number of `#endif`s.\n");
					exit(EXIT_FAILURE);
				}

				struct Tokvec u;
				u.tok_num = dst_offset + 1;
				u.v = dst_initial;

				return u;
			}
		}

		src++; /* HASH */

		consume_spaces(&src);

		if (src[0].kind == NEWLINE) { /* empty directive */
			src++;
			continue;
		}
		expect_and_consume(&src, IDENT_OR_RESERVED,
		                   "identifier after `#` for preprocessor directive");

		const char *directive = src[-1].ident_str;
		consume_spaces(&src);

		if (strcmp(directive, "define") == 0) {
			flag = handle_define(&src, def_map);
		} else if (strcmp(directive, "include") == 0) {
			/* dst_initial may be realloc'd; total_token_num could be modified
			 */
			handle_include(&dst_initial, &src, &dst_offset, def_map,
			               &total_token_num);
			flag = 1;
		} else if (strcmp(directive, "ifdef") == 0 ||
		           strcmp(directive, "ifndef") == 0) {
			flag = handle_ifdef(strcmp(directive, "ifdef") == 0, &src, def_map,
			                    &ifdef_depth);
		} else if (strcmp(directive, "endif") ==
		           0) { /* passes only when the #if(n)?def condition was
			               true. If false, it will be handled in
			               #if(n)?def.*/
			if (ifdef_depth < 1) {
				fprintf(stderr,
				        "mismatch of `#endif` directive was detected.\n");
				exit(EXIT_FAILURE);
			}

			ifdef_depth--;
			expect_and_consume(&src, NEWLINE, "newline after `#endif`");
			flag = 1;
		} else {
			unsupported("unknown directive");
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x18(%rbp)
movq %rsi, -0x20(%rbp)
movq -0x18(%rbp), %rdi
callq 0xf600
movl %eax, -0x30(%rbp)
movq %rdx, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x38(%rbp)
movl -0x30(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl $0x0, -0x40(%rbp)
movslq -0x3c(%rbp), %rdi
movl $0x20, %esi
callq 0x10b0
movq %rax, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movl $0x1, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
movb $0x1, %al
cmpl $0x0, -0x50(%rbp)
movb %al, -0x69(%rbp)
je 0x1183f
movb $0x1, %al
cmpl $0x0, -0x54(%rbp)
movb %al, -0x69(%rbp)
jne 0x1183f
movq -0x38(%rbp), %rax
cmpl $0x4b, (%rax)
setne %al
movb %al, -0x69(%rbp)
movb -0x69(%rbp), %al
testb $0x1, %al
jne 0x1184b
jmp 0x118ff
movl $0x1, -0x50(%rbp)
movq -0x38(%rbp), %rax
movl (%rax), %esi
leaq -0x54(%rbp), %rdi
callq 0x11aa0
callq 0xccc0
movq %rax, -0x60(%rbp)
movq -0x20(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x48(%rbp), %rcx
movslq -0x4c(%rbp), %rax
shlq $0x5, %rax
addq %rax, %rcx
callq 0x11ae0
movq -0x48(%rbp), %rax
movslq -0x4c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $0x2, (%rax)
jne 0x118e3
cmpl $0x0, -0x40(%rbp)
je 0x118c6
movq 0xa74d(%rip), %rax      # 0x1bff8
movq (%rax), %rdi
leaq 0x5d5e(%rip), %rsi      # 0x17613
movb $0x0, %al
callq 0x10d0
movl $0x1, %edi
callq 0x1150
movl -0x4c(%rbp), %eax
incl %eax
movl %eax, -0x10(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x8(%rbp)
movl -0x10(%rbp), %eax
movq -0x8(%rbp), %rdx
addq $0x70, %rsp
popq %rbp
retq
jmp 0x118e5
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
movq -0x38(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x38(%rbp)
jmp 0x1181c
movq -0x38(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x38(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x11bb0
movq -0x38(%rbp), %rax
cmpl $0x4c, (%rax)
jne 0x1192e
movq -0x38(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x38(%rbp)
jmp 0x11815
leaq -0x38(%rbp), %rdi
movl $0x18, %esi
leaq 0x5cf8(%rip), %rdx      # 0x17636
callq 0xb190
movq -0x38(%rbp), %rax
movq -0x18(%rax), %rax
movq %rax, -0x68(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x11bb0
movq -0x68(%rbp), %rdi
leaq 0x5d03(%rip), %rsi      # 0x17666
callq 0x10c0
cmpl $0x0, %eax
jne 0x11982
movq -0x20(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x11be0
movl %eax, -0x50(%rbp)
jmp 0x11a8d
movq -0x68(%rbp), %rdi
leaq 0x5ce0(%rip), %rsi      # 0x1766d
callq 0x10c0
cmpl $0x0, %eax
jne 0x119bc
movq -0x20(%rbp), %rcx
leaq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rsi
leaq -0x4c(%rbp), %rdx
leaq -0x3c(%rbp), %r8
callq 0x11d50
movl $0x1, -0x50(%rbp)
jmp 0x11a8b
movq -0x68(%rbp), %rdi
leaq 0x5cae(%rip), %rsi      # 0x17675
callq 0x10c0
cmpl $0x0, %eax
je 0x119e6
movq -0x68(%rbp), %rdi
leaq 0x5c9f(%rip), %rsi      # 0x1767b
callq 0x10c0
cmpl $0x0, %eax
jne 0x11a17
movq -0x68(%rbp), %rdi
leaq 0x5c84(%rip), %rsi      # 0x17675
callq 0x10c0
cmpl $0x0, %eax
sete %al
andb $0x1, %al
movzbl %al, %edi
movq -0x20(%rbp), %rdx
leaq -0x38(%rbp), %rsi
leaq -0x40(%rbp), %rcx
callq 0x11f10
movl %eax, -0x50(%rbp)
jmp 0x11a89
movq -0x68(%rbp), %rdi
leaq 0x5c60(%rip), %rsi      # 0x17682
callq 0x10c0
cmpl $0x0, %eax
jne 0x11a7b
cmpl $0x1, -0x40(%rbp)
jge 0x11a54
movq 0xa5bf(%rip), %rax      # 0x1bff8
movq (%rax), %rdi
leaq 0x5c45(%rip), %rsi      # 0x17688
movb $0x0, %al
callq 0x10d0
movl $0x1, %edi
callq 0x1150
movl -0x40(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x40(%rbp)
leaq -0x38(%rbp), %rdi
movl $0x4c, %esi
leaq 0x5c49(%rip), %rdx      # 0x176b6
callq 0xb190
movl $0x1, -0x50(%rbp)
jmp 0x11a87
leaq 0x5c4b(%rip), %rdi      # 0x176cd
callq 0xb1f0
jmp 0x11a89
jmp 0x11a8b
jmp 0x11a8d
jmp 0x11815
nopw %cs:(%rax,%rax)