// File: /workspace/github_c_makefile_star2_O1/markdryan[P]subtilis/backends/riscos_common/riscos_arm.c
// Function: subtilis_riscos_generate
subtilis_arm_prog_t *
subtilis_riscos_generate(
	subtilis_arm_op_pool_t *op_pool, subtilis_ir_prog_t *p,
	const subtilis_ir_rule_raw_t *rules_raw,
	size_t rule_count, size_t globals,
	const subtilis_arm_fp_if_t *fp_if,
	int32_t start_address, subtilis_error_t *err)
/* clang-format on */
{
	subtilis_ir_rule_t *parsed;
	subtilis_arm_prog_t *arm_p = NULL;
	subtilis_arm_section_t *arm_s;
	subtilis_ir_section_t *s;
	size_t i;

	parsed = malloc(sizeof(*parsed) * rule_count);
	if (!parsed) {
		subtilis_error_set_oom(err);
		return NULL;
	}

	subtilis_ir_parse_rules(rules_raw, parsed, rule_count, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	arm_p = subtilis_arm_prog_new(p->num_sections + 2, op_pool,
				      p->string_pool, p->constant_pool,
				      p->settings, fp_if, start_address, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	s = p->sections[0];
	arm_s = subtilis_arm_prog_section_new(arm_p, s->type, s->reg_counter,
					      s->freg_counter, s->label_counter,
					      s->locals, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;
	prv_add_preamble(arm_s, globals, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;
	prv_add_section(s, arm_s, parsed, rule_count, err);
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	for (i = 1; i < p->num_sections; i++) {
		s = p->sections[i];
		if (s->section_type == SUBTILIS_IR_SECTION_ASM) {
			arm_s = (subtilis_arm_section_t *)s->asm_code;

			/*
			 * Ownership of inline assembly transfers to the ARM
			 * section.
			 */

			s->asm_code = NULL;
			subtilis_arm_prog_append_section(arm_p, arm_s, err);
			if (err->type != SUBTILIS_ERROR_OK)
				goto cleanup;
			continue;
		}
		arm_s = subtilis_arm_prog_section_new(
		    arm_p, s->type, s->reg_counter, s->freg_counter,
		    s->label_counter, s->locals, err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;
		if (s->section_type == SUBTILIS_IR_SECTION_BACKEND_BUILTIN)
			prv_add_builtin(s, arm_s, err);
		else
			prv_add_section(s, arm_s, parsed, rule_count, err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;
	}

	free(parsed);
	parsed = NULL;

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

	return arm_p;

cleanup:

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

	subtilis_arm_prog_delete(arm_p);
	free(parsed);

	return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r9, %r14
movq %r8, %r15
movq %rcx, %rbx
movq %rdx, %r13
movq %rsi, 0x10(%rsp)
movq %rdi, %r12
movq 0x68(%rsp), %rbp
imulq $0x150, %rcx, %rdi      # imm = 0x150
callq 0x121d0
testq %rax, %rax
je 0x6e280
movq %r13, %rdi
movq %rax, 0x18(%rsp)
movq %rax, %rsi
movq %rbx, %rdx
movq %rbp, %rcx
callq 0x2d48a
cmpl $0x0, (%rbp)
je 0x6e29e
xorl %r14d, %r14d
jmp 0x6e530
leaq 0xaf8d(%rip), %rdx      # 0x79214
movq %rbp, %rdi
movl $0x1, %esi
movl $0x224, %ecx            # imm = 0x224
callq 0x145f4
jmp 0x6e55b
movq %rbp, %r13
movl 0x60(%rsp), %eax
movq 0x10(%rsp), %rbp
movq 0x8(%rbp), %rdi
movq 0x18(%rbp), %rdx
addq $0x2, %rdi
movq 0x28(%rbp), %rcx
movq 0x20(%rbp), %r8
movq %r12, %rsi
movq %r14, %r9
pushq %r13
pushq %rax
callq 0x4f372
addq $0x10, %rsp
movq %rax, %r14
cmpl $0x0, (%r13)
jne 0x6e530
movq (%rbp), %rax
movq (%rax), %r12
movq 0x18(%r12), %rdx
movq 0x20(%r12), %rcx
movq 0x28(%r12), %r8
movq 0x8(%r12), %rsi
movq 0x10(%r12), %r9
subq $0x8, %rsp
movq %r14, %rdi
pushq %r13
callq 0x4f4b4
addq $0x10, %rsp
cmpl $0x0, (%r13)
jne 0x6e530
movq %r12, 0x20(%rsp)
movq %rax, 0x8(%rsp)
callq 0x60bf8
movq 0x8(%rsp), %rdi
addq $0x3, %r15
andq $-0x4, %r15
movl %eax, %eax
movq 0xa8(%rdi), %rcx
leaq (%r15,%rax), %rdx
addq $0x200c, %rdx           # imm = 0x200C
leaq (%r15,%rax), %r12
addq $0x2000, %r12           # imm = 0x2000
cmpb $0x0, (%rcx)
cmovneq %rdx, %r12
movq %r12, %rax
shrq $0x20, %rax
je 0x6e384
leaq 0xaea7(%rip), %rdx      # 0x79214
movq %r13, %rdi
movl $0xf, %esi
movl $0xf5, %ecx
callq 0x145f4
jmp 0x6e508
subq $0x8, %rsp
movl $0xf, %r8d
movl $0xf, %r9d
movl $0xd, %esi
movl $0xe, %edx
xorl %ecx, %ecx
pushq %r13
callq 0x5060d
addq $0x10, %rsp
cmpl $0x0, (%r13)
jne 0x6e508
subq $0x8, %rsp
movl $0xf, %r8d
movl $0xf, %r9d
movq 0x10(%rsp), %rdi
movl $0xd, %esi
movl $0xe, %edx
xorl %ecx, %ecx
pushq %r13
callq 0x5060d
addq $0x10, %rsp
cmpl $0x0, (%r13)
jne 0x6e508
movl $0x10, %edx
movq 0x8(%rsp), %rdi
movl $0xe, %esi
xorl %ecx, %ecx
movl $0x7, %r8d
movq %r13, %r9
callq 0x505b2
cmpl $0x0, (%r13)
jne 0x6e508
movl $0xa, %esi
movl $0xb, %edx
movq 0x8(%rsp), %rdi
movq %r13, %rcx
callq 0x70795
cmpl $0x0, (%r13)
jne 0x6e508
subq $0x8, %rsp
movl $0xc, %r9d
movq 0x10(%rsp), %rdi
movl $0x4, %esi
movl $0x2, %edx
movl $0xe, %ecx
xorl %r8d, %r8d
pushq %r13
pushq %r12
pushq $0xb
callq 0x4fe13
addq $0x20, %rsp
cmpl $0x0, (%r13)
jne 0x6e508
movl $0xc, %ecx
movl $0x1, %r8d
movq 0x8(%rsp), %rdi
movl $0xa, %esi
movl $0xe, %edx
movq %r13, %r9
callq 0x50b58
cmpl $0x0, (%r13)
jne 0x6e508
movq 0x8(%rsp), %rdi
movl $0x16, %esi
movq %r13, %rdx
callq 0x4f73a
cmpl $0x0, (%r13)
jne 0x6e508
movl $0xc, 0x8(%rax)
movb $0x0, 0xc(%rax)
movl $0x0, 0x10(%rax)
movq 0x8(%rsp), %rdi
movq 0xb0(%rdi), %rcx
movq %rcx, 0x18(%rax)
movl $0xc, %r8d
movl $0x1, %r9d
movl $0x2, %esi
movl $0xe, %edx
xorl %ecx, %ecx
pushq %r13
pushq %r15
callq 0x4fc61
addq $0x10, %rsp
cmpl $0x0, (%r13)
je 0x6e637
cmpl $0x0, (%r13)
jne 0x6e530
movq 0x20(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq %rbx, %rcx
movq %r13, %r8
callq 0x6e7ac
cmpl $0x0, (%r13)
je 0x6e570
leaq 0x58bd(%rip), %rdi      # 0x73df4
callq 0x12080
testq %r14, %r14
je 0x6e549
movq %r14, %rdi
callq 0x5ca92
movq %r14, %rdi
callq 0x4f459
movq 0x18(%rsp), %rdi
callq 0x122c0
xorl %r14d, %r14d
movq %r14, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $0x2, 0x8(%rbp)
jb 0x6e628
movl $0x1, %r12d
movq (%rbp), %rax
movq (%rax,%r12,8), %rbp
cmpl $0x2, (%rbp)
jne 0x6e5ae
movq 0xd0(%rbp), %rsi
movq $0x0, 0xd0(%rbp)
movq %r14, %rdi
movq %r13, %rdx
callq 0x4f523
jmp 0x6e60b
movq 0x18(%rbp), %rdx
movq 0x20(%rbp), %rcx
movq 0x28(%rbp), %r8
movq 0x8(%rbp), %rsi
movq 0x10(%rbp), %r9
subq $0x8, %rsp
movq %r14, %rdi
pushq %r13
callq 0x4f4b4
addq $0x10, %rsp
cmpl $0x0, (%r13)
jne 0x6e530
cmpl $0x1, (%rbp)
jne 0x6e5f5
movq %rbp, %rdi
movq %rax, %rsi
movq %r13, %rdx
callq 0x6e8f1
jmp 0x6e60b
movq %rbp, %rdi
movq %rax, %rsi
movq 0x18(%rsp), %rdx
movq %rbx, %rcx
movq %r13, %r8
callq 0x6e7ac
cmpl $0x0, (%r13)
movq 0x10(%rsp), %rbp
jne 0x6e530
incq %r12
cmpq 0x8(%rbp), %r12
jb 0x6e581
movq 0x18(%rsp), %rdi
callq 0x122c0
jmp 0x6e55e
movq %r13, %r12
movq 0x8(%rsp), %rax
movq 0xa8(%rax), %rax
cmpb $0x1, (%rax)
jne 0x6e70e
subq $0x8, %rsp
movl $0xd, %r9d
movq 0x10(%rsp), %rdi
movl $0x2, %esi
movl $0x4, %edx
movl $0xe, %ecx
xorl %r8d, %r8d
movq %r12, %r13
pushq %r12
pushq $0xc
pushq $0xc
callq 0x4fe13
addq $0x20, %rsp
cmpl $0x0, (%r12)
movq 0x10(%rsp), %rbp
jne 0x6e508
movl $0x1, %r9d
movq 0x8(%rsp), %rdi
movl $0xd, %esi
movl $0xf, %edx
movl $0xe, %ecx
xorl %r8d, %r8d
pushq %r13
pushq $0x0
callq 0x50249
addq $0x10, %rsp
cmpl $0x0, (%r13)
jne 0x6e508
movl $0x1, %ecx
movl $0xc, %r8d
movq 0x8(%rsp), %rdi
movl $0x13, %esi
movl $0xe, %edx
movl $0xfffffffc, %r9d       # imm = 0xFFFFFFFC
pushq %r13
pushq $0x0
callq 0x5067b
addq $0x10, %rsp
cmpl $0x0, (%r13)
jne 0x6e508
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x7199a
jmp 0x6e73a
subq $0x8, %rsp
movl $0xd, %r8d
movl $0xc, %r9d
movq 0x10(%rsp), %rdi
movl $0xd, %esi
movl $0xe, %edx
xorl %ecx, %ecx
pushq %r12
callq 0x5060d
addq $0x10, %rsp
movq %r12, %r13
cmpl $0x0, (%r12)
movq 0x10(%rsp), %rbp
jne 0x6e508
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x71bcb
cmpl $0x0, (%r13)
jne 0x6e508
movq 0x8(%rsp), %rax
movq 0xc0(%rax), %rax
testq %rax, %rax
je 0x6e786
movq 0x8(%rsp), %rdi
movq %r12, %r13
movq %r12, %rsi
callq *0x20(%rax)
jmp 0x6e7a2
leaq 0xaa87(%rip), %rdx      # 0x79214
movq %r12, %r13
movq %r12, %rdi
movl $0xf, %esi
movl $0x166, %ecx            # imm = 0x166
callq 0x145f4
movq 0x10(%rsp), %rbp
jmp 0x6e508