// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x31e0, %rsp           # imm = 0x31E0
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
leaq -0x2030(%rbp), %rdi
xorl %esi, %esi
movl $0x2000, %edx           # imm = 0x2000
callq 0x1100
leaq -0x31c0(%rbp), %rdi
xorl %esi, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x1100
leaq -0x21c0(%rbp), %rdi
xorl %esi, %esi
movl $0x190, %edx            # imm = 0x190
callq 0x1100
callq 0x6190
cmpl $0x0, %eax
jne 0x5a4c
movslq 0x1e787(%rip), %rcx     # 0x24198
leaq 0x1e7a8(%rip), %rax     # 0x241c0
movq (%rax,%rcx,8), %rdx
movslq 0x1e775(%rip), %rcx     # 0x24198
leaq 0x1e776(%rip), %rax     # 0x241a0
movl (%rax,%rcx,4), %ecx
movl $0x6, %edi
leaq 0x189ab(%rip), %rsi     # 0x1e3e4
movb $0x0, %al
callq 0xf610
movl $0x0, -0x4(%rbp)
jmp 0x5f2c
movl 0x207ae(%rip), %eax     # 0x26200
movl %eax, -0x31cc(%rbp)
leaq -0x2030(%rbp), %rdi
movq 0x207a2(%rip), %rsi     # 0x26208
movl 0x20794(%rip), %edx     # 0x26200
addl $0x1, %edx
callq 0x6290
leaq -0x31c0(%rbp), %rdi
movq 0x20786(%rip), %rdx     # 0x26208
movl 0x20778(%rip), %ecx     # 0x26200
movl $0x1000, %esi           # imm = 0x1000
xorl %eax, %eax
movl %eax, %r8d
callq 0x1ab0
movl %eax, -0x31c4(%rbp)
cmpl $0x0, -0x31c4(%rbp)
jne 0x5af6
movslq 0x1e6eb(%rip), %rcx     # 0x24198
leaq 0x1e70c(%rip), %rax     # 0x241c0
movq (%rax,%rcx,8), %rdx
movslq 0x1e6d9(%rip), %rcx     # 0x24198
leaq 0x1e6da(%rip), %rax     # 0x241a0
movl (%rax,%rcx,4), %ecx
movq 0x20738(%rip), %r8      # 0x26208
movl 0x20729(%rip), %r9d     # 0x26200
movl $0x6, %edi
leaq 0x1891a(%rip), %rsi     # 0x1e3fd
movb $0x0, %al
callq 0xf610
movl $0x0, -0x4(%rbp)
jmp 0x5f2c
movl -0x31c4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x31c4(%rbp)
movq $0x0, -0x28(%rbp)
movl $0x0, -0x31d4(%rbp)
movslq 0x1e67a(%rip), %rcx     # 0x24198
leaq 0x1e67b(%rip), %rax     # 0x241a0
movl (%rax,%rcx,4), %eax
movl %eax, -0x31d8(%rbp)
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rdx
movl $0x1000, %esi           # imm = 0x1000
callq 0x1120
cmpq $0x0, %rax
je 0x5c4b
movslq 0x1e647(%rip), %rcx     # 0x24198
leaq 0x1e648(%rip), %rax     # 0x241a0
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
leaq 0x1e63b(%rip), %rax     # 0x241a0
movl %edx, (%rax,%rcx,4)
movq -0x18(%rbp), %rdi
leaq 0x188a7(%rip), %rsi     # 0x1e41a
movl $0x3, %edx
callq 0x1060
cmpl $0x0, %eax
jne 0x5b87
jmp 0x5c4b
cmpl $0x32, -0x31d4(%rbp)
jl 0x5b92
jmp 0x5b2e
movq -0x18(%rbp), %rdx
leaq 0x1e663(%rip), %rdi     # 0x24200
movl $0x1000, %ecx           # imm = 0x1000
xorl %eax, %eax
movl %eax, %r8d
movl %ecx, %esi
callq 0x16c0
addl $0x1, %eax
movl %eax, -0x31d0(%rbp)
movl -0x31d0(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rdi
shlq %rdi
callq 0x1160
movq %rax, %rcx
movslq -0x31d4(%rbp), %rax
movq %rcx, -0x21c0(%rbp,%rax,8)
movslq -0x31d4(%rbp), %rax
cmpq $0x0, -0x21c0(%rbp,%rax,8)
jne 0x5c13
movl $0x7, %edi
leaq 0x1867d(%rip), %rsi     # 0x1e278
leaq 0x1871f(%rip), %rdx     # 0x1e321
movl $0x6cb, %ecx            # imm = 0x6CB
movb $0x0, %al
callq 0xf610
jmp 0x5ed8
movslq -0x31d4(%rbp), %rax
movq -0x21c0(%rbp,%rax,8), %rdi
movl -0x31d0(%rbp), %edx
addl $0x1, %edx
leaq 0x1e5ce(%rip), %rsi     # 0x24200
callq 0x6290
movl -0x31d4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x31d4(%rbp)
jmp 0x5b2e
cmpl $0x32, -0x31d4(%rbp)
jl 0x5c96
movslq 0x1e53d(%rip), %rcx     # 0x24198
leaq 0x1e55e(%rip), %rax     # 0x241c0
movq (%rax,%rcx,8), %rdx
movslq 0x1e52b(%rip), %rcx     # 0x24198
leaq 0x1e52c(%rip), %rax     # 0x241a0
movl (%rax,%rcx,4), %ecx
leaq -0x31c0(%rbp), %r8
movl $0x6, %edi
leaq 0x18794(%rip), %rsi     # 0x1e41e
movb $0x0, %al
callq 0xf610
jmp 0x5ed8
callq 0x62f0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x5cab
jmp 0x5ed8
movl -0x31c4(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rdi
callq 0x1160
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x5cf6
movl $0x7, %edi
leaq 0x1859a(%rip), %rsi     # 0x1e278
leaq 0x1863c(%rip), %rdx     # 0x1e321
movl $0x6e0, %ecx            # imm = 0x6E0
movb $0x0, %al
callq 0xf610
jmp 0x5ed8
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rdi
leaq -0x31c0(%rbp), %rsi
movslq -0x31c4(%rbp), %rdx
callq 0x1150
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x31c4(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq -0x20(%rbp), %rdi
callq 0x10a0
movl %eax, -0x31c8(%rbp)
movl -0x31c8(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rdi
callq 0x1160
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x28(%rbp), %rax
cmpq $0x0, 0x30(%rax)
jne 0x5d7e
movl $0x7, %edi
leaq 0x18512(%rip), %rsi     # 0x1e278
leaq 0x185b4(%rip), %rdx     # 0x1e321
movl $0x6ea, %ecx            # imm = 0x6EA
movb $0x0, %al
callq 0xf610
jmp 0x5ed8
movq -0x28(%rbp), %rax
movq 0x30(%rax), %rdi
movq -0x20(%rbp), %rsi
callq 0x1070
movl -0x31cc(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rdi
shlq %rdi
callq 0x1160
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x28(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x5ddd
movl $0x7, %edi
leaq 0x184b3(%rip), %rsi     # 0x1e278
leaq 0x18555(%rip), %rdx     # 0x1e321
movl $0x6f2, %ecx            # imm = 0x6F2
movb $0x0, %al
callq 0xf610
jmp 0x5ed8
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rdi
leaq -0x2030(%rbp), %rsi
movl -0x31cc(%rbp), %edx
addl $0x1, %edx
callq 0x6290
movl -0x31cc(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x20(%rax)
movslq -0x31d4(%rbp), %rdi
shlq $0x3, %rdi
callq 0x1160
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x28(%rbp), %rax
cmpq $0x0, 0x18(%rax)
jne 0x5e51
movl $0x7, %edi
leaq 0x1843f(%rip), %rsi     # 0x1e278
leaq 0x184e1(%rip), %rdx     # 0x1e321
movl $0x6fb, %ecx            # imm = 0x6FB
movb $0x0, %al
callq 0xf610
jmp 0x5ed8
movl $0x0, -0x31dc(%rbp)
movl -0x31dc(%rbp), %eax
cmpl -0x31d4(%rbp), %eax
jge 0x5e9c
movslq -0x31dc(%rbp), %rax
movq -0x21c0(%rbp,%rax,8), %rdx
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x31dc(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x31dc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x31dc(%rbp)
jmp 0x5e5b
movl -0x31d4(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x24(%rax)
movl -0x31d8(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x38(%rax)
movq 0x1e2cb(%rip), %rcx     # 0x24188
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movq -0x28(%rbp), %rax
movq %rax, 0x1e2b9(%rip)     # 0x24188
movl $0x1, -0x4(%rbp)
jmp 0x5f2c
cmpq $0x0, -0x28(%rbp)
je 0x5ee8
movq -0x28(%rbp), %rdi
callq 0x5360
movl $0x0, -0x31dc(%rbp)
movl -0x31dc(%rbp), %eax
cmpl -0x31d4(%rbp), %eax
jge 0x5f25
movslq -0x31dc(%rbp), %rax
movq -0x21c0(%rbp,%rax,8), %rdi
callq 0x1040
movl -0x31dc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x31dc(%rbp)
jmp 0x5ef2
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x31e0, %rsp           # imm = 0x31E0
popq %rbp
retq
nopl (%rax,%rax)