// File: /workspace/github_c_makefile_star2_O3/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, %r13
movq %r8, %r14
movq %rcx, %rbx
movq %rdx, %r12
movq %rsi, 0x18(%rsp)
movq %rdi, %r15
movq 0x68(%rsp), %rbp
imulq $0x150, %rcx, %rdi      # imm = 0x150
callq 0x121d0
testq %rax, %rax
je 0x6de74
movq %r12, %rdi
movq %rax, 0x10(%rsp)
movq %rax, %rsi
movq %rbx, %rdx
movq %rbp, %rcx
callq 0x2d119
cmpl $0x0, (%rbp)
je 0x6de92
leaq 0x5fad(%rip), %rdi      # 0x73e14
callq 0x12080
xorl %r13d, %r13d
jmp 0x6df24
leaq 0xb3f9(%rip), %rdx      # 0x79274
movq %rbp, %rdi
movl $0x1, %esi
movl $0x224, %ecx            # imm = 0x224
callq 0x145a4
jmp 0x6df36
movq %rbp, %r12
movq %rbx, 0x20(%rsp)
movl 0x60(%rsp), %eax
movq 0x18(%rsp), %rbp
movq 0x8(%rbp), %rdi
movq 0x18(%rbp), %rdx
addq $0x2, %rdi
movq 0x28(%rbp), %rcx
movq 0x20(%rbp), %r8
movq %r15, %rsi
movq %r13, %r9
pushq %r12
pushq %rax
callq 0x4f319
addq $0x10, %rsp
movq %rax, %r13
cmpl $0x0, (%r12)
jne 0x6df07
movq (%rbp), %rax
movq (%rax), %rbp
movq 0x18(%rbp), %rdx
movq 0x20(%rbp), %rcx
movq 0x28(%rbp), %r8
movq 0x8(%rbp), %rsi
movq 0x10(%rbp), %r9
subq $0x8, %rsp
movq %r13, %rdi
pushq %r12
callq 0x4f45d
addq $0x10, %rsp
cmpl $0x0, (%r12)
je 0x6df4b
leaq 0x5f06(%rip), %rdi      # 0x73e14
callq 0x12080
testq %r13, %r13
je 0x6de6c
movq %r13, %rdi
callq 0x5cb30
movq %r13, %rdi
callq 0x4f400
movq 0x10(%rsp), %rdi
callq 0x122c0
xorl %r13d, %r13d
movq %r13, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, 0x8(%rsp)
callq 0x60c10
movq 0x8(%rsp), %rdi
addq $0x3, %r14
andq $-0x4, %r14
movl %eax, %eax
movq 0xa8(%rdi), %rcx
leaq (%r14,%rax), %rdx
addq $0x200c, %rdx           # imm = 0x200C
leaq (%r14,%rax), %r15
addq $0x2000, %r15           # imm = 0x2000
cmpb $0x0, (%rcx)
cmovneq %rdx, %r15
movq %r15, %rax
shrq $0x20, %rax
je 0x6e0ae
leaq 0xb2d8(%rip), %rdx      # 0x79274
movq %r12, %rdi
movl $0xf, %esi
movl $0xf5, %ecx
callq 0x145a4
cmpl $0x0, (%r12)
movq 0x18(%rsp), %r15
movq 0x8(%rsp), %rsi
jne 0x6df07
movq %rbp, %rdi
movq 0x10(%rsp), %rdx
movq 0x20(%rsp), %rcx
movq %r12, %r8
callq 0x6e3a4
cmpl $0x0, (%r12)
jne 0x6df07
cmpq $0x2, 0x8(%r15)
jb 0x6e09f
movq %r15, %rax
movl $0x1, %r15d
movq (%rax), %rax
movq (%rax,%r15,8), %rbp
cmpl $0x2, (%rbp)
jne 0x6e023
movq 0xd0(%rbp), %rsi
movq $0x0, 0xd0(%rbp)
movq %r13, %rdi
movq %r12, %rdx
callq 0x4f4cc
jmp 0x6e082
movq 0x18(%rbp), %rdx
movq 0x20(%rbp), %rcx
movq 0x28(%rbp), %r8
movq 0x8(%rbp), %rsi
movq 0x10(%rbp), %r9
subq $0x8, %rsp
movq %r13, %rdi
pushq %r12
callq 0x4f45d
addq $0x10, %rsp
cmpl $0x0, (%r12)
jne 0x6df07
cmpl $0x1, (%rbp)
jne 0x6e06a
movq %rbp, %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0x6e4e9
jmp 0x6e082
movq %rbp, %rdi
movq %rax, %rsi
movq 0x10(%rsp), %rdx
movq 0x20(%rsp), %rcx
movq %r12, %r8
callq 0x6e3a4
cmpl $0x0, (%r12)
movq 0x18(%rsp), %rax
jne 0x6df07
incq %r15
cmpq 0x8(%rax), %r15
jb 0x6dff7
movq 0x10(%rsp), %rdi
callq 0x122c0
jmp 0x6df39
subq $0x8, %rsp
movl $0xf, %r8d
movl $0xf, %r9d
movl $0xd, %esi
movl $0xe, %edx
xorl %ecx, %ecx
pushq %r12
callq 0x504b3
movq 0x18(%rsp), %rdi
addq $0x10, %rsp
cmpl $0x0, (%r12)
jne 0x6df07
subq $0x8, %rsp
movl $0xf, %r8d
movl $0xf, %r9d
movl $0xd, %esi
movl $0xe, %edx
xorl %ecx, %ecx
pushq %r12
callq 0x504b3
movq 0x18(%rsp), %rdi
addq $0x10, %rsp
cmpl $0x0, (%r12)
jne 0x6df07
movl $0x10, %edx
movl $0xe, %esi
xorl %ecx, %ecx
movl $0x7, %r8d
movq %r12, %r9
callq 0x50463
movq 0x8(%rsp), %rdi
cmpl $0x0, (%r12)
jne 0x6df07
movl $0xa, %esi
movl $0xb, %edx
movq %r12, %rcx
callq 0x703a3
movq 0x8(%rsp), %rdi
cmpl $0x0, (%r12)
jne 0x6df07
subq $0x8, %rsp
movl $0xc, %r9d
movl $0x4, %esi
movl $0x2, %edx
movl $0xe, %ecx
xorl %r8d, %r8d
pushq %r12
pushq %r15
pushq $0xb
callq 0x4fd3f
movq 0x28(%rsp), %rdi
addq $0x20, %rsp
cmpl $0x0, (%r12)
jne 0x6df07
movl $0xc, %ecx
movl $0x1, %r8d
movl $0xa, %esi
movl $0xe, %edx
movq %r12, %r9
callq 0x5099c
movq 0x8(%rsp), %rdi
cmpl $0x0, (%r12)
jne 0x6df07
movl $0x16, %esi
movq %r12, %rdx
callq 0x4f6e2
movq 0x8(%rsp), %rdi
cmpl $0x0, (%r12)
jne 0x6df07
movl $0xc, 0x8(%rax)
movb $0x0, 0xc(%rax)
movl $0x0, 0x10(%rax)
movq 0xb0(%rdi), %rcx
movq %rcx, 0x18(%rax)
movl $0xc, %r8d
movl $0x1, %r9d
movl $0x2, %esi
movl $0xe, %edx
xorl %ecx, %ecx
pushq %r12
pushq %r14
callq 0x4fbd3
movq 0x18(%rsp), %rax
addq $0x10, %rsp
cmpl $0x0, (%r12)
jne 0x6df07
movq 0xa8(%rax), %rax
cmpb $0x1, (%rax)
jne 0x6e30c
subq $0x8, %rsp
movl $0xd, %r9d
movq 0x10(%rsp), %r15
movq %r15, %rdi
movl $0x2, %esi
movl $0x4, %edx
movl $0xe, %ecx
xorl %r8d, %r8d
movq 0x70(%rsp), %r12
pushq %r12
pushq $0xc
pushq $0xc
callq 0x4fd3f
addq $0x20, %rsp
cmpl $0x0, (%r12)
jne 0x6df07
movl $0x1, %r9d
movq %r15, %rdi
movl $0xd, %esi
movl $0xf, %edx
movl $0xe, %ecx
xorl %r8d, %r8d
pushq %r12
pushq $0x0
callq 0x50130
movq 0x18(%rsp), %rdi
addq $0x10, %rsp
cmpl $0x0, (%r12)
jne 0x6df07
movl $0x1, %ecx
movl $0xc, %r8d
movl $0x13, %esi
movl $0xe, %edx
movl $0xfffffffc, %r9d       # imm = 0xFFFFFFFC
pushq %r12
pushq $0x0
callq 0x50516
movq 0x18(%rsp), %rdi
addq $0x10, %rsp
cmpl $0x0, (%r12)
jne 0x6df07
movq %r12, %rsi
callq 0x7159f
jmp 0x6e33a
subq $0x8, %rsp
movl $0xd, %r8d
movl $0xc, %r9d
movq 0x10(%rsp), %rdi
movl $0xd, %esi
movl $0xe, %edx
xorl %ecx, %ecx
pushq 0x70(%rsp)
callq 0x504b3
addq $0x10, %rsp
movq 0x68(%rsp), %r15
cmpl $0x0, (%r15)
movq 0x8(%rsp), %rdi
jne 0x6df07
movq %r15, %rsi
callq 0x717d0
movq 0x8(%rsp), %rax
cmpl $0x0, (%r15)
jne 0x6df07
movq 0xc0(%rax), %rax
testq %rax, %rax
je 0x6e386
movq 0x8(%rsp), %rdi
movq 0x68(%rsp), %r12
movq %r12, %rsi
callq *0x20(%rax)
jmp 0x6dfae
leaq 0xaee7(%rip), %rdx      # 0x79274
movq 0x68(%rsp), %r12
movq %r12, %rdi
movl $0xf, %esi
movl $0x166, %ecx            # imm = 0x166
jmp 0x6dfa9