// File: /workspace/github_c_makefile_star2_O1/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 $0x31d8, %rsp           # imm = 0x31D8
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %r12
leaq 0x11d0(%rsp), %rdi
xorl %r13d, %r13d
movl $0x2000, %edx           # imm = 0x2000
xorl %esi, %esi
callq 0x1110
leaq 0x1d0(%rsp), %rdi
movl $0x1000, %edx           # imm = 0x1000
xorl %esi, %esi
callq 0x1110
leaq 0x40(%rsp), %rdi
movl $0x190, %edx            # imm = 0x190
xorl %esi, %esi
callq 0x1110
callq 0x5525
leaq 0x14781(%rip), %rbp     # 0x181c0
testl %eax, %eax
je 0x3bea
movslq 0x167d2(%rip), %rcx     # 0x1a220
leaq 0x1(%rcx), %r13
movq 0x167cf(%rip), %rbx     # 0x1a228
movq %rcx, 0x18(%rsp)
testq %rcx, %rcx
js 0x3a7b
leal (,%r13,2), %edx
leaq 0x11d0(%rsp), %rdi
movq %rbx, %rsi
callq 0x1170
movq %r13, 0x28(%rsp)
movq 0x18(%rsp), %rcx
movw $0x0, 0x11d0(%rsp,%rcx,2)
xorl %r13d, %r13d
leaq 0x1d0(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %rbx, %rdx
xorl %r8d, %r8d
callq 0x16c6
testl %eax, %eax
je 0x3c18
movq %r14, 0x38(%rsp)
movl %eax, 0xc(%rsp)
cltq
incq %rax
movq %rax, 0x30(%rsp)
movslq 0x146ec(%rip), %rax     # 0x181b8
movl (%rbp,%rax,4), %r13d
xorl %r14d, %r14d
leaq 0xf8bf(%rip), %rbx      # 0x1339a
movq %r15, %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %r12, %rdx
callq 0x1140
testq %rax, %rax
je 0x3ba7
movslq 0x146bd(%rip), %rax     # 0x181b8
incl (%rbp,%rax,4)
movl $0x3, %edx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x1070
testl %eax, %eax
je 0x3ba7
cmpq $0x31, %r14
ja 0x3adb
movl %r13d, 0x24(%rsp)
leaq 0x146f7(%rip), %rdi     # 0x18220
movl $0x1000, %esi           # imm = 0x1000
movq %r15, %rdx
movl $0x1000, %ecx           # imm = 0x1000
xorl %r8d, %r8d
callq 0x147f
movq %r14, 0x10(%rsp)
movslq %eax, %r14
leaq 0x4(,%r14,2), %rdi
callq 0x1180
movq %rax, %r13
movq 0x10(%rsp), %rax
movq %r13, 0x40(%rsp,%rax,8)
testq %r13, %r13
je 0x3e6e
leaq 0x2(%r14), %rax
cmpl $-0x1, %r14d
jl 0x3b8d
movl %eax, %edx
addq %rdx, %rdx
movq %r13, %rdi
leaq 0x1469e(%rip), %rsi     # 0x18220
movq %rax, %r14
callq 0x1170
movq %r14, %rax
movw $0x0, -0x2(%r13,%rax,2)
movq 0x10(%rsp), %r14
incq %r14
movl 0x24(%rsp), %r13d
jmp 0x3adb
cmpq $0x32, %r14
jb 0x3c54
movslq 0x14600(%rip), %rax     # 0x181b8
leaq 0x14621(%rip), %rcx     # 0x181e0
movq (%rcx,%rax,8), %rdx
movl (%rbp,%rax,4), %ecx
leaq 0xf7d0(%rip), %rsi      # 0x1339e
xorl %r15d, %r15d
leaq 0x1d0(%rsp), %r8
movl $0x6, %edi
xorl %eax, %eax
callq 0xa36c
jmp 0x3e27
movslq 0x145c7(%rip), %rax     # 0x181b8
leaq 0x145e8(%rip), %rcx     # 0x181e0
movq (%rcx,%rax,8), %rdx
movl (%rbp,%rax,4), %ecx
leaq 0xf75d(%rip), %rsi      # 0x13364
movl $0x6, %edi
xorl %eax, %eax
callq 0xa36c
jmp 0x3e59
movslq 0x14599(%rip), %rax     # 0x181b8
leaq 0x145ba(%rip), %rcx     # 0x181e0
movq (%rcx,%rax,8), %rdx
movl (%rbp,%rax,4), %ecx
movq 0x165f3(%rip), %r8      # 0x1a228
movl 0x165e4(%rip), %r9d     # 0x1a220
leaq 0xf73a(%rip), %rsi      # 0x1337d
movl $0x6, %edi
xorl %eax, %eax
callq 0xa36c
jmp 0x3e59
movl $0x40, %edi
callq 0x1180
testq %rax, %rax
je 0x3c7a
movq %rax, %r15
xorps %xmm0, %xmm0
movups %xmm0, 0x30(%rax)
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
jmp 0x3c9c
leaq 0xf573(%rip), %rsi      # 0x131f4
leaq 0xf619(%rip), %rdx      # 0x132a1
xorl %r15d, %r15d
movl $0x7, %edi
movl $0x673, %ecx            # imm = 0x673
xorl %eax, %eax
callq 0xa36c
movl 0xc(%rsp), %eax
testq %r15, %r15
je 0x3db5
addl $0x2, %eax
movslq %eax, %rdi
callq 0x1180
movq %rax, 0x8(%r15)
testq %rax, %rax
je 0x3dba
movq %rax, %rbx
leaq 0x1d0(%rsp), %rsi
movq %rax, %rdi
movq 0x30(%rsp), %r12
movq %r12, %rdx
callq 0x1170
movb $0x0, (%rbx,%r12)
movq 0x38(%rsp), %rbx
movq %rbx, %rdi
callq 0x10b0
incl %eax
movslq %eax, %rdi
callq 0x1180
movq %rax, 0x30(%r15)
testq %rax, %rax
je 0x3dd4
movq %rax, %rdi
movq %rbx, %rsi
callq 0x1080
movq 0x28(%rsp), %rdi
addq %rdi, %rdi
callq 0x1180
movq %rax, 0x10(%r15)
testq %rax, %rax
je 0x3dee
movq %rax, %rbx
cmpl $0x0, 0x18(%rsp)
js 0x3d4c
movq 0x28(%rsp), %rax
leal (%rax,%rax), %edx
leaq 0x11d0(%rsp), %rsi
movq %rbx, %rdi
callq 0x1170
movq 0x18(%rsp), %rax
movw $0x0, (%rbx,%rax,2)
movl %eax, 0x20(%r15)
leaq (,%r14,8), %rdi
callq 0x1180
movq %rax, 0x18(%r15)
testq %rax, %rax
je 0x3e08
testq %r14, %r14
je 0x3d91
xorl %eax, %eax
movq 0x40(%rsp,%rax,8), %rcx
movq 0x18(%r15), %rdx
movq %rcx, (%rdx,%rax,8)
incq %rax
cmpq %rax, %r14
jne 0x3d7c
movl %r14d, 0x24(%r15)
movl %r13d, 0x38(%r15)
movq 0x14408(%rip), %rax     # 0x181a8
movq %rax, (%r15)
movq %r15, 0x143fe(%rip)     # 0x181a8
movl $0x1, %r13d
jmp 0x3e59
xorl %r15d, %r15d
jmp 0x3e27
leaq 0xf433(%rip), %rsi      # 0x131f4
leaq 0xf4d9(%rip), %rdx      # 0x132a1
movl $0x7, %edi
movl $0x6e0, %ecx            # imm = 0x6E0
jmp 0x3e20
leaq 0xf419(%rip), %rsi      # 0x131f4
leaq 0xf4bf(%rip), %rdx      # 0x132a1
movl $0x7, %edi
movl $0x6ea, %ecx            # imm = 0x6EA
jmp 0x3e20
leaq 0xf3ff(%rip), %rsi      # 0x131f4
leaq 0xf4a5(%rip), %rdx      # 0x132a1
movl $0x7, %edi
movl $0x6f2, %ecx            # imm = 0x6F2
jmp 0x3e20
leaq 0xf3e5(%rip), %rsi      # 0x131f4
leaq 0xf48b(%rip), %rdx      # 0x132a1
movl $0x7, %edi
movl $0x6fb, %ecx            # imm = 0x6FB
xorl %eax, %eax
callq 0xa36c
testq %r15, %r15
je 0x3e34
movq %r15, %rdi
callq 0x34d7
testl %r14d, %r14d
je 0x3e56
movl %r14d, %ebx
xorl %r13d, %r13d
xorl %r14d, %r14d
movq 0x40(%rsp,%r14,8), %rdi
callq 0x1040
incq %r14
cmpq %r14, %rbx
jne 0x3e42
jmp 0x3e59
xorl %r13d, %r13d
movl %r13d, %eax
addq $0x31d8, %rsp           # imm = 0x31D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xf37f(%rip), %rsi      # 0x131f4
leaq 0xf425(%rip), %rdx      # 0x132a1
xorl %r15d, %r15d
movl $0x7, %edi
movl $0x6cb, %ecx            # imm = 0x6CB
xorl %eax, %eax
callq 0xa36c
movq 0x10(%rsp), %r14
jmp 0x3e27