// File: /workspace/github_c_makefile_star2_O3/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 0x63e75(%rip), %rsi     # 0x762c4
movl $0x6, %edi
callq 0x12220
leaq 0x2f4(%rsp), %rbx
movq %rbx, %rdi
callq 0x143b4
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 0x4ee58
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 0x5b6a6(%rip), %rax     # 0x6dbca
movq %rax, 0x8(%r15)
leaq 0x5b712(%rip), %rax     # 0x6dc41
movq %rax, 0x10(%r15)
movq %r14, 0x38(%r15)
leaq 0x5b719(%rip), %rax     # 0x6dc57
movq %rax, 0x28(%r15)
leaq 0x5f051(%rip), %rax     # 0x7159a
movq %rax, 0x30(%r15)
leaq 0x48(%rsp), %rdi
callq 0x6c6fe
leaq 0x5(%rsp), %rdx
leaq 0x2f4(%rsp), %r12
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rcx
callq 0x14e08
movq %rax, %r15
cmpl $0x0, (%r12)
jne 0x12596
leaq 0x2f4(%rsp), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x15416
cmpl $0x0, (%r12)
je 0x12613
xorl %r12d, %r12d
movq %r12, %rdi
callq 0x4f400
movq %r14, %rdi
callq 0x4eea3
movq %r15, %rdi
callq 0x14fe4
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 0x14650
movl %ebx, %eax
addq $0xf00, %rsp            # imm = 0xF00
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
leaq 0x66c2e(%rip), %rax     # 0x79248
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 0x6de0c
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 0x56c78
cmpl $0x0, (%r13)
jne 0x12599
movq %r12, %rdi
callq 0x4f400
movq %r14, %rdi
callq 0x4eea3
movq %r15, %rdi
callq 0x14fe4
leaq 0x2f4(%rsp), %rsi
movq %rbx, %rdi
callq 0x129cb
xorl %ebx, %ebx
jmp 0x12600