// File: /workspace/github_c_makefile_star2_O1/markdryan[P]subtilis/subtro.c
// Function: main
int main(int argc, char *argv[])
{
	subtilis_error_t err;
	subtilis_stream_t s;
	subtilis_settings_t settings;
	subtilis_backend_t backend;
	subtilis_arm_fp_if_t fp_if;
	subtilis_lexer_t *l = NULL;
	subtilis_parser_t *p = NULL;
	subtilis_arm_prog_t *arm_p = NULL;
	subtilis_arm_op_pool_t *pool = NULL;

	if (argc != 2) {
		fprintf(stderr, "Usage: subtro file\n");
		return 1;
	}

	setlocale(LC_ALL, "C");

	subtilis_error_init(&err);
	subtilis_stream_from_file(&s, argv[1], &err);
	if (err.type != SUBTILIS_ERROR_OK)
		goto fail;

	l = subtilis_lexer_new(&s, SUBTILIS_CONFIG_LEXER_BUF_SIZE,
			       subtilis_keywords_list, SUBTILIS_KEYWORD_TOKENS,
			       subtilis_arm_keywords_list,
			       SUBTILIS_ARM_KEYWORD_TOKENS, &err);
	if (err.type != SUBTILIS_ERROR_OK)
		goto cleanup;

	settings.handle_escapes = true;
	settings.ignore_graphics_errors = true;
	settings.check_mem_leaks = false;

	pool = subtilis_arm_op_pool_new(&err);
	if (err.type != SUBTILIS_ERROR_OK)
		goto cleanup;

	backend.caps = SUBTILIS_RISCOS_ARM_CAPS;
	backend.sys_trans = subtilis_riscos_arm2_sys_trans;
	backend.sys_check = subtilis_riscos_arm2_sys_check;
	backend.backend_data = pool;
	backend.asm_parse = subtilis_riscos_arm2_asm_parse;
	backend.asm_free = subtilis_riscos_asm_free;

	subtilis_arm_fpa_if_init(&fp_if);

	p = subtilis_parser_new(l, &backend, &settings, &err);
	if (err.type != SUBTILIS_ERROR_OK)
		goto cleanup;

	subtilis_parse(p, &err);
	if (err.type != SUBTILIS_ERROR_OK)
		goto cleanup;

	//	subtilis_ir_prog_dump(p->prog);

	arm_p = subtilis_riscos_generate(
	    pool, p->prog, riscos_arm2_rules, riscos_arm2_rules_count,
	    p->st->max_allocated, &fp_if, SUBTILIS_RISCOS_ARM2_PROGRAM_START,
	    &err);
	if (err.type != SUBTILIS_ERROR_OK)
		goto cleanup;

	//	printf("\n\n");
	//	subtilis_arm_prog_dump(arm_p);

	subtilis_arm_encode(arm_p, "RunImage", prv_set_prog_size, &err);
	if (err.type != SUBTILIS_ERROR_OK)
		goto cleanup;

	subtilis_arm_prog_delete(arm_p);
	subtilis_arm_op_pool_delete(pool);
	subtilis_parser_delete(p);
	subtilis_lexer_delete(l, &err);

	return 0;

cleanup:
	subtilis_arm_prog_delete(arm_p);
	subtilis_arm_op_pool_delete(pool);
	subtilis_parser_delete(p);
	if (l)
		subtilis_lexer_delete(l, &err);
	else
		s.close(s.handle, &err);

fail:
	subtilis_error_fprintf(stderr, &err, true);

	return 1;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf00, %rsp            # imm = 0xF00
cmpl $0x2, %edi
jne 0x12502
movq %rsi, %r14
leaq 0x63e35(%rip), %rsi     # 0x76284
movl $0x6, %edi
callq 0x12220
leaq 0x2f4(%rsp), %rbx
movq %rbx, %rdi
callq 0x14404
movq 0x8(%r14), %rsi
leaq 0xd0(%rsp), %rdi
movq %rbx, %rdx
callq 0x126e4
cmpl $0x0, (%rbx)
jne 0x125df
subq $0x8, %rsp
leaq 0x8905f(%rip), %rdx     # 0x9b4f0
leaq 0x8f958(%rip), %r8      # 0xa1df0
leaq 0xd8(%rsp), %rdi
movl $0x20000, %esi          # imm = 0x20000
movl $0x127, %ecx            # imm = 0x127
movl $0x65, %r9d
pushq %rbx
callq 0x128dc
addq $0x10, %rsp
movq %rax, %rbx
cmpl $0x0, 0x2f4(%rsp)
je 0x124d5
xorl %r15d, %r15d
xorl %r12d, %r12d
xorl %r14d, %r14d
jmp 0x12599
movw $0x101, 0x5(%rsp)       # imm = 0x101
movb $0x0, 0x7(%rsp)
leaq 0x2f4(%rsp), %r15
movq %r15, %rdi
callq 0x4ee9c
movq %rax, %r14
cmpl $0x0, (%r15)
je 0x12511
xorl %r15d, %r15d
jmp 0x12596
callq 0x12320
movl $0x1, %ebx
jmp 0x12600
leaq 0x8(%rsp), %r15
movl $0x64, (%r15)
leaq 0x5babe(%rip), %rax     # 0x6dfe2
movq %rax, 0x8(%r15)
leaq 0x5bb2a(%rip), %rax     # 0x6e059
movq %rax, 0x10(%r15)
movq %r14, 0x38(%r15)
leaq 0x5bb31(%rip), %rax     # 0x6e06f
movq %rax, 0x28(%r15)
leaq 0x5f44c(%rip), %rax     # 0x71995
movq %rax, 0x30(%r15)
leaq 0x48(%rsp), %rdi
callq 0x6caf8
leaq 0x5(%rsp), %rdx
leaq 0x2f4(%rsp), %r12
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rcx
callq 0x14e54
movq %rax, %r15
cmpl $0x0, (%r12)
jne 0x12596
leaq 0x2f4(%rsp), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x1549c
cmpl $0x0, (%r12)
je 0x12613
xorl %r12d, %r12d
movq %r12, %rdi
callq 0x4f459
movq %r14, %rdi
callq 0x4eee5
movq %r15, %rdi
callq 0x15030
testq %rbx, %rbx
je 0x125c8
leaq 0x2f4(%rsp), %rsi
movq %rbx, %rdi
callq 0x129cb
jmp 0x125df
movq 0x2d8(%rsp), %rdi
leaq 0x2f4(%rsp), %rsi
callq *0x2e8(%rsp)
movq 0x93a0a(%rip), %rax     # 0xa5ff0
movq (%rax), %rdi
leaq 0x2f4(%rsp), %rsi
movl $0x1, %ebx
movl $0x1, %edx
callq 0x146a0
movl %ebx, %eax
addq $0xf00, %rsp            # imm = 0xF00
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
leaq 0x66bce(%rip), %rax     # 0x791e8
movq (%rax), %rcx
movq 0x58(%r15), %rsi
movq 0x60(%r15), %rax
movq 0x310(%rax), %r8
leaq 0x92bfd(%rip), %rdx     # 0xa5230
leaq 0x48(%rsp), %r9
movq %r14, %rdi
pushq %r12
pushq $0x8000                 # imm = 0x8000
callq 0x6e224
addq $0x10, %rsp
movq %rax, %r12
cmpl $0x0, 0x2f4(%rsp)
jne 0x12599
leaq 0x5f9b5(%rip), %rsi     # 0x72018
leaq 0x4d(%rip), %rdx        # 0x126b7
leaq 0x2f4(%rsp), %r13
movq %r12, %rdi
movq %r13, %rcx
callq 0x56b04
cmpl $0x0, (%r13)
jne 0x12599
movq %r12, %rdi
callq 0x4f459
movq %r14, %rdi
callq 0x4eee5
movq %r15, %rdi
callq 0x15030
leaq 0x2f4(%rsp), %rsi
movq %rbx, %rdi
callq 0x129cb
xorl %ebx, %ebx
jmp 0x12600