// File: /workspace/github_c_makefile_star2_O3/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, %r15
movq %rdi, %r12
leaq 0x11c0(%rsp), %rdi
xorl %r13d, %r13d
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 0x53ae
testl %eax, %eax
je 0x3b56
movslq 0x16849(%rip), %rbp     # 0x1a220
leaq 0x1(%rbp), %r13
movq 0x16846(%rip), %rbx     # 0x1a228
testq %rbp, %rbp
js 0x39ff
leal (,%r13,2), %edx
leaq 0x11c0(%rsp), %rdi
movq %rbx, %rsi
callq 0x1180
movq %r13, 0x10(%rsp)
movw $0x0, 0x11c0(%rsp,%rbp,2)
xorl %r13d, %r13d
leaq 0x1c0(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %rbx, %rdx
movl %ebp, %ecx
xorl %r8d, %r8d
callq 0x170a
testl %eax, %eax
je 0x3b8a
movq %r14, 0x20(%rsp)
movl %eax, 0xc(%rsp)
cltq
incq %rax
movq %rax, 0x18(%rsp)
movslq 0x1476b(%rip), %rax     # 0x181b8
leaq 0x1476c(%rip), %r14     # 0x181c0
movl (%r14,%rax,4), %eax
movl %eax, 0x8(%rsp)
xorl %r13d, %r13d
movq %r15, %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %r12, %rdx
callq 0x1140
testq %rax, %rax
je 0x3b16
movslq 0x14739(%rip), %rax     # 0x181b8
incl (%r14,%rax,4)
cmpb $0x65, (%r15)
jne 0x3a97
cmpb $0x6f, 0x1(%r15)
jne 0x3a97
cmpb $0x6d, 0x2(%r15)
je 0x3b16
cmpq $0x31, %r13
ja 0x3a5f
movq %rbp, 0x28(%rsp)
leaq 0x14777(%rip), %rdi     # 0x18220
movl $0x1000, %esi           # imm = 0x1000
movq %r15, %rdx
movl $0x1000, %ecx           # imm = 0x1000
xorl %r8d, %r8d
callq 0x1491
movslq %eax, %rbp
leaq 0x4(,%rbp,2), %rdi
callq 0x1190
movq %rax, 0x30(%rsp,%r13,8)
testq %rax, %rax
je 0x3d86
movq %rax, %rbx
leaq 0x2(%rbp), %rax
cmpl $-0x1, %ebp
jl 0x3b02
movl %eax, %edx
addq %rdx, %rdx
movq %rbx, %rdi
leaq 0x14729(%rip), %rsi     # 0x18220
movq %rax, %rbp
callq 0x1180
movq %rbp, %rax
movw $0x0, -0x2(%rbx,%rax,2)
incq %r13
movq 0x28(%rsp), %rbp
jmp 0x3a5f
cmpq $0x32, %r13
jb 0x3bcc
movslq 0x14691(%rip), %rax     # 0x181b8
leaq 0x146b2(%rip), %rcx     # 0x181e0
movq (%rcx,%rax,8), %rdx
movl (%r14,%rax,4), %ecx
leaq 0xf8fd(%rip), %rsi      # 0x1343a
leaq 0x1c0(%rsp), %r8
movl $0x6, %edi
xorl %eax, %eax
callq 0xa278
jmp 0x3d51
movslq 0x1465b(%rip), %rax     # 0x181b8
leaq 0x1467c(%rip), %rcx     # 0x181e0
movq (%rcx,%rax,8), %rdx
leaq 0x14651(%rip), %rcx     # 0x181c0
movl (%rcx,%rax,4), %ecx
leaq 0xf88b(%rip), %rsi      # 0x13404
movl $0x6, %edi
xorl %eax, %eax
callq 0xa278
jmp 0x3d6c
movslq 0x14627(%rip), %rax     # 0x181b8
leaq 0x14648(%rip), %rcx     # 0x181e0
movq (%rcx,%rax,8), %rdx
leaq 0x1461d(%rip), %rcx     # 0x181c0
movl (%rcx,%rax,4), %ecx
movq 0x1667b(%rip), %r8      # 0x1a228
movl 0x1666c(%rip), %r9d     # 0x1a220
leaq 0xf862(%rip), %rsi      # 0x1341d
movl $0x6, %edi
xorl %eax, %eax
callq 0xa278
jmp 0x3d6c
movl $0x1, %edi
movl $0x40, %esi
callq 0x1150
testq %rax, %rax
je 0x3cef
movq %rax, %r15
movl 0xc(%rsp), %eax
addl $0x2, %eax
movslq %eax, %rdi
callq 0x1190
movq %rax, 0x8(%r15)
testq %rax, %rax
je 0x3d10
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(%r15)
testq %rax, %rax
je 0x3d17
movq %rax, %rdi
movq %rbx, %rsi
callq 0x1080
movq 0x10(%rsp), %rdi
addq %rdi, %rdi
callq 0x1190
movq %rax, 0x10(%r15)
testq %rax, %rax
je 0x3d1e
movq %rax, %rbx
testl %ebp, %ebp
js 0x3c8b
movq 0x10(%rsp), %rax
leal (%rax,%rax), %edx
leaq 0x11c0(%rsp), %rsi
movq %rbx, %rdi
callq 0x1180
movw $0x0, (%rbx,%rbp,2)
movl %ebp, 0x20(%r15)
leaq (,%r13,8), %rdi
callq 0x1190
movq %rax, 0x18(%r15)
testq %rax, %rax
je 0x3d25
testq %r13, %r13
je 0x3cca
movl %r13d, %eax
xorl %ecx, %ecx
movq 0x30(%rsp,%rcx,8), %rdx
movq 0x18(%r15), %rsi
movq %rdx, (%rsi,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x3cb5
movl %r13d, 0x24(%r15)
movl 0x8(%rsp), %eax
movl %eax, 0x38(%r15)
movq 0x144cb(%rip), %rax     # 0x181a8
movq %rax, (%r15)
movq %r15, 0x144c1(%rip)     # 0x181a8
movl $0x1, %r13d
jmp 0x3d6c
leaq 0xf546(%rip), %rsi      # 0x1323c
leaq 0xf644(%rip), %rdx      # 0x13341
movl $0x7, %edi
movl $0x673, %ecx            # imm = 0x673
xorl %eax, %eax
callq 0xa278
jmp 0x3d4c
movl $0x6e0, %ecx            # imm = 0x6E0
jmp 0x3d2a
movl $0x6ea, %ecx            # imm = 0x6EA
jmp 0x3d2a
movl $0x6f2, %ecx            # imm = 0x6F2
jmp 0x3d2a
movl $0x6fb, %ecx            # imm = 0x6FB
leaq 0xf50b(%rip), %rsi      # 0x1323c
leaq 0xf609(%rip), %rdx      # 0x13341
movl $0x7, %edi
xorl %eax, %eax
callq 0xa278
movq %r15, %rdi
callq 0x3465
testq %r13, %r13
je 0x3d81
movl %r13d, %ebx
xorl %r13d, %r13d
xorl %r14d, %r14d
movq 0x30(%rsp,%r14,8), %rdi
callq 0x1040
incq %r14
cmpq %r14, %rbx
jne 0x3d5a
movl %r13d, %eax
addq $0x31c8, %rsp           # imm = 0x31C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r13d, %r13d
jmp 0x3d6c
leaq 0xf4af(%rip), %rsi      # 0x1323c
leaq 0xf5ad(%rip), %rdx      # 0x13341
movl $0x7, %edi
movl $0x6cb, %ecx            # imm = 0x6CB
jmp 0x3d07