// File: /workspace/github_c_makefile_star2_O2/MikeGray-APH[P]LibLouisAPH/source/compile.c
// Function: compile_macro
static int compile_macro(FILE *file, char cchars[INPUT_LINE_MAX], const char *file_name)
{
	struct macro *macro;
	unichar tag[INPUT_LINE_MAX], *lines[MACRO_LINES_MAX];
	char ctag[INPUT_LINE_MAX];
	int ctag_len, file_name_len, tag_len, line_len, lines_cnt, table_line_begin, i;

	DB_MEMSET(tag, 0, INPUT_LINE_MAX * sizeof(unichar));
	DB_MEMSET(ctag, 0, INPUT_LINE_MAX);
	DB_MEMSET(lines, 0, MACRO_LINES_MAX * sizeof(unichar*));

	if(!token_parse())
	{
		log_message(LOG_ERROR, "%s:%d  missing macro tag", table_file_names[include_depth], table_file_lines[include_depth]);
		return 0;
	}

	/*   tag array is same length of cchars array,
	     so there should always be enough space to copy   */
	tag_len = token_len;
	utf16_copy(tag, token_crs, token_len + 1);

	ctag_len = utf16_convert_to_utf8(ctag, INPUT_LINE_MAX, token_crs, token_len, NULL);
	if(!ctag_len)
	{
		log_message(LOG_ERROR, "%s:%d  invalid macro tag %#S", table_file_names[include_depth], table_file_lines[include_depth], token_crs, token_len);
		return 0;
	}

	/*   make room for terminating NULL char   */
	ctag_len++;

	/*   use return_fail_free from here on out   */
	macro = NULL;
	lines_cnt = 0;

	table_line_begin = table_file_lines[include_depth];
	while(fgets(cchars, INPUT_LINE_MAX, file))
	{
		table_file_lines[include_depth]++;

		if(!strncmp(cchars, "eom", 3))
			break;

		/*   let continue to try to find eom   */
		if(lines_cnt >= MACRO_LINES_MAX)
			continue;

		line_len = utf8_convert_to_utf16(token_line, INPUT_LINE_MAX, cchars, INPUT_LINE_MAX, NULL) + 1;

		lines[lines_cnt] = MALLOC((line_len + 1) * sizeof(unichar));
		if(!lines[lines_cnt])
		{
			LOG_ALLOCATE_FAIL
			goto return_fail_free;
		}
		utf16_copy(lines[lines_cnt], token_line, line_len + 1);

		lines_cnt++;
	}

	if(lines_cnt >= MACRO_LINES_MAX)
	{
		log_message(LOG_ERROR, "%s:%d  too many lines for macro %s", table_file_names[include_depth], table_file_lines[include_depth], ctag);
		goto return_fail_free;
	}

	macro = macro_allocate();
	if(!macro)
		goto return_fail_free;

	macro->ctag = MALLOC(ctag_len + 1);
	if(!macro->ctag)
	{
		LOG_ALLOCATE_FAIL
		goto return_fail_free;
	}
	memcpy(macro->ctag, ctag, ctag_len);
	macro->ctag[ctag_len] = 0;

	file_name_len = strlen(file_name);
	macro->file_name = MALLOC(file_name_len + 1);
	if(!macro->file_name)
	{
		LOG_ALLOCATE_FAIL
		goto return_fail_free;
	}
	strcpy(macro->file_name, file_name);

	macro->tag = MALLOC((tag_len + 1) * sizeof(unichar));
	if(!macro->tag)
	{
		LOG_ALLOCATE_FAIL
		goto return_fail_free;
	}
	utf16_copy(macro->tag, tag, tag_len + 1);
	macro->tag_len = tag_len;

	macro->lines = MALLOC(lines_cnt * sizeof(unichar*));
	if(!macro->lines)
	{
		LOG_ALLOCATE_FAIL
		goto return_fail_free;
	}
	for(i = 0; i < lines_cnt; i++)
		macro->lines[i] = lines[i];
	macro->lines_cnt = lines_cnt;

	macro->file_line = table_line_begin;

	/*   add to macro list   */
	macro->nxt = macros;
	macros = macro;
	return 1;

	return_fail_free:
	if(macro)
		macro_free(macro);
	for(i = 0; i < lines_cnt; i++)
		FREE(lines[i]);

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x31c8, %rsp           # imm = 0x31C8
movq %rdx, %r14
movq %rsi, %r12
movq %rdi, %r13
leaq 0x11c0(%rsp), %rdi
xorl %ebp, %ebp
movl $0x2000, %edx           # imm = 0x2000
xorl %esi, %esi
callq 0x1110
leaq 0x1c0(%rsp), %rdi
movl $0x1000, %edx           # imm = 0x1000
xorl %esi, %esi
callq 0x1110
leaq 0x30(%rsp), %rdi
movl $0x190, %edx            # imm = 0x190
xorl %esi, %esi
callq 0x1110
callq 0x51da
testl %eax, %eax
je 0x389d
movslq 0x15ade(%rip), %r15     # 0x19230
movq 0x15adf(%rip), %rbx     # 0x19238
movq %r15, %rdx
incq %rdx
leaq 0x11c0(%rsp), %rdi
movq %rbx, %rsi
movq %rdx, 0x28(%rsp)
callq 0x5277
xorl %ebp, %ebp
leaq 0x1c0(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %rbx, %rdx
movl %r15d, %ecx
xorl %r8d, %r8d
callq 0x16db
testl %eax, %eax
je 0x38cc
movq %r15, 0x10(%rsp)
movq %r14, 0x20(%rsp)
movl %eax, 0xc(%rsp)
cltq
incq %rax
movq %rax, 0x18(%rsp)
movslq 0x13a10(%rip), %rax     # 0x171c8
leaq 0x13a11(%rip), %r15     # 0x171d0
movl (%r15,%rax,4), %eax
movl %eax, 0x8(%rsp)
xorl %ebp, %ebp
leaq 0x13a60(%rip), %rbx     # 0x17230
movq %r12, %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %r13, %rdx
callq 0x1140
testq %rax, %rax
je 0x385f
movslq 0x139dc(%rip), %rax     # 0x171c8
incl (%r15,%rax,4)
cmpb $0x65, (%r12)
jne 0x3807
cmpb $0x6f, 0x1(%r12)
jne 0x3807
cmpb $0x6d, 0x2(%r12)
je 0x385f
cmpq $0x31, %rbp
ja 0x37d0
movq %rbx, %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %r12, %rdx
movl $0x1000, %ecx           # imm = 0x1000
xorl %r8d, %r8d
callq 0x146a
movl %eax, %r14d
cltq
leaq 0x4(,%rax,2), %rdi
callq 0x1190
movq %rax, 0x30(%rsp,%rbp,8)
testq %rax, %rax
je 0x3aac
addl $0x2, %r14d
movq %rax, %rdi
movq %rbx, %rsi
movl %r14d, %edx
callq 0x5277
incq %rbp
jmp 0x37d0
cmpq $0x32, %rbp
jb 0x391b
movslq 0x13958(%rip), %rax     # 0x171c8
leaq 0x13979(%rip), %rcx     # 0x171f0
movq (%rcx,%rax,8), %rdx
movl (%r15,%rax,4), %ecx
leaq 0xdb54(%rip), %rsi      # 0x113da
pushq $0x6
popq %rdi
leaq 0x1c0(%rsp), %r8
xorl %eax, %eax
callq 0x9174
jmp 0x3a8d
movslq 0x13924(%rip), %rax     # 0x171c8
leaq 0x13945(%rip), %rcx     # 0x171f0
movq (%rcx,%rax,8), %rdx
leaq 0x1391a(%rip), %rcx     # 0x171d0
movl (%rcx,%rax,4), %ecx
leaq 0xdae4(%rip), %rsi      # 0x113a4
pushq $0x6
popq %rdi
xorl %eax, %eax
callq 0x9174
jmp 0x3907
movslq 0x138f5(%rip), %rax     # 0x171c8
leaq 0x13916(%rip), %rcx     # 0x171f0
movq (%rcx,%rax,8), %rdx
leaq 0x138eb(%rip), %rcx     # 0x171d0
movl (%rcx,%rax,4), %ecx
movq 0x15949(%rip), %r8      # 0x19238
movl 0x1593a(%rip), %r9d     # 0x19230
leaq 0xdac0(%rip), %rsi      # 0x113bd
pushq $0x6
popq %rdi
xorl %eax, %eax
callq 0x9174
movl %ebp, %eax
addq $0x31c8, %rsp           # imm = 0x31C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x1
popq %rdi
pushq $0x40
popq %rsi
callq 0x1150
testq %rax, %rax
je 0x3a0c
movq %rax, %r12
movl 0xc(%rsp), %eax
addl $0x2, %eax
movslq %eax, %rdi
callq 0x1190
movq %rax, 0x8(%r12)
testq %rax, %rax
je 0x3a2b
movq %rax, %rbx
leaq 0x1c0(%rsp), %rsi
movq %rax, %rdi
movq 0x18(%rsp), %r14
movq %r14, %rdx
callq 0x1180
movb $0x0, (%rbx,%r14)
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
callq 0x10b0
incl %eax
movslq %eax, %rdi
callq 0x1190
movq %rax, 0x30(%r12)
testq %rax, %rax
je 0x3a32
movq %rax, %rdi
movq %rbx, %rsi
callq 0x1080
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
addq %rbx, %rdi
callq 0x1190
movq %rax, 0x10(%r12)
testq %rax, %rax
je 0x3a61
leaq 0x11c0(%rsp), %rsi
movq %rax, %rdi
movl %ebx, %edx
callq 0x5277
movq 0x10(%rsp), %rax
movl %eax, 0x20(%r12)
leaq (,%rbp,8), %rdi
callq 0x1190
movq %rax, 0x18(%r12)
testq %rax, %rax
je 0x3a68
movl %ebp, %eax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x3a39
movq 0x30(%rsp,%rcx,8), %rdx
movq 0x18(%r12), %rsi
movq %rdx, (%rsi,%rcx,8)
incq %rcx
jmp 0x39f4
leaq 0xd829(%rip), %rsi      # 0x1123c
leaq 0xd8c7(%rip), %rdx      # 0x112e1
pushq $0x7
popq %rdi
movl $0x673, %ecx            # imm = 0x673
xorl %eax, %eax
callq 0x9174
jmp 0x3a8d
movl $0x6e0, %ecx            # imm = 0x6E0
jmp 0x3a6d
movl $0x6ea, %ecx            # imm = 0x6EA
jmp 0x3a6d
movl %ebp, 0x24(%r12)
movl 0x8(%rsp), %eax
movl %eax, 0x38(%r12)
movq 0x1376a(%rip), %rax     # 0x171b8
movq %rax, (%r12)
movq %r12, 0x1375f(%rip)     # 0x171b8
pushq $0x1
popq %rbp
jmp 0x3907
movl $0x6f2, %ecx            # imm = 0x6F2
jmp 0x3a6d
movl $0x6fb, %ecx            # imm = 0x6FB
leaq 0xd7c8(%rip), %rsi      # 0x1123c
leaq 0xd866(%rip), %rdx      # 0x112e1
pushq $0x7
popq %rdi
xorl %eax, %eax
callq 0x9174
movq %r12, %rdi
callq 0x326f
movl %ebp, %ebx
xorl %ebp, %ebp
xorl %r14d, %r14d
cmpq %r14, %rbx
je 0x3907
movq 0x30(%rsp,%r14,8), %rdi
callq 0x1040
incq %r14
jmp 0x3a94
leaq 0xd789(%rip), %rsi      # 0x1123c
leaq 0xd827(%rip), %rdx      # 0x112e1
pushq $0x7
popq %rdi
movl $0x6cb, %ecx            # imm = 0x6CB
jmp 0x3a22