// File: /workspace/github_c_makefile_star2_O2/markdryan[P]subtilis/backends/riscos_common/riscos_arm.c
// Function: subtilis_riscos_eof
void subtilis_riscos_eof(subtilis_ir_section_t *s, size_t start,
			 void *user_data, subtilis_error_t *err)
{
	subtilis_arm_reg_t dest;
	subtilis_arm_reg_t handle;
	subtilis_arm_reg_t one;
	subtilis_arm_instr_t *instr;
	subtilis_arm_br_instr_t *br;
	subtilis_arm_section_t *arm_s = user_data;
	size_t label = arm_s->label_counter++;
	subtilis_ir_inst_t *bget = &s->ops[start]->op.instr;

	dest = subtilis_arm_ir_to_arm_reg(bget->operands[0].reg);
	handle = subtilis_arm_ir_to_arm_reg(bget->operands[1].reg);

	subtilis_arm_add_mov_imm(arm_s, SUBTILIS_ARM_CCODE_AL, false, 0, 5,
				 err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	subtilis_arm_add_mov_reg(arm_s, SUBTILIS_ARM_CCODE_AL, false, 1, handle,
				 err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	/* OS_Args 5 */
	/* read_mask = 0x3 = r0, r1 */
	/* write_mask = 0x4 = r2 */
	subtilis_arm_add_swi(arm_s, SUBTILIS_ARM_CCODE_AL, 0x20000 + 0x9, 0x3,
			     0x4, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	one = subtilis_arm_ir_to_arm_reg(arm_s->reg_counter++);
	subtilis_arm_gen_sete_load_reg(arm_s, s, SUBTILIS_ARM_CCODE_VS, one,
				       err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	instr =
	    subtilis_arm_section_add_instr(arm_s, SUBTILIS_ARM_INSTR_B, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;
	br = &instr->operands.br;
	br->ccode = SUBTILIS_ARM_CCODE_VS;
	br->link = false;
	br->link_type = SUBTILIS_ARM_BR_LINK_VOID;
	br->target.label = label;

	subtilis_arm_add_cmp_imm(arm_s, SUBTILIS_ARM_INSTR_CMP,
				 SUBTILIS_ARM_CCODE_AL, 2, 0, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	subtilis_arm_add_mov_imm(arm_s, SUBTILIS_ARM_CCODE_EQ, false, dest, 0,
				 err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	subtilis_arm_add_mvn_imm(arm_s, SUBTILIS_ARM_CCODE_NE, false, dest, 0,
				 err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	subtilis_arm_section_add_label(arm_s, label, err);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rcx, %rbx
movq %rdx, %r14
movq %rdi, %r13
movq 0x10(%rdx), %r15
leaq 0x1(%r15), %rax
movq %rax, 0x10(%rdx)
movq 0x48(%rdi), %rax
movq (%rax,%rsi,8), %rbp
movq 0x10(%rbp), %rdi
callq 0x4c214
movq %rax, %r12
movq 0x18(%rbp), %rdi
callq 0x4c214
movq %rax, %rbp
movq %rbx, 0x8(%rsp)
movl $0x5, (%rsp)
pushq $0xd
popq %rsi
pushq $0xf
popq %rdx
pushq $0xe
popq %rcx
movq %r14, %rdi
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x4d213
cmpl $0x0, (%rbx)
jne 0x6bb00
movq %rbx, (%rsp)
pushq $0xd
popq %rsi
pushq $0xe
popq %rdx
pushq $0x1
popq %r8
movq %r14, %rdi
xorl %ecx, %ecx
movq %rbp, %r9
callq 0x4d518
cmpl $0x0, (%rbx)
jne 0x6bb00
pushq $0xe
popq %rsi
pushq $0x3
popq %rcx
pushq $0x4
popq %r8
movl $0x20009, %edx          # imm = 0x20009
movq %r14, %rdi
movq %rbx, %r9
callq 0x4d4d9
cmpl $0x0, (%rbx)
jne 0x6bb00
movq (%r14), %rdi
leaq 0x1(%rdi), %rax
movq %rax, (%r14)
callq 0x4c214
pushq $0x6
popq %rdx
movq %r14, %rdi
movq %r13, %rsi
movq %rax, %rcx
movq %rbx, %r8
callq 0x4f21a
cmpl $0x0, (%rbx)
jne 0x6bb00
pushq $0x16
popq %rsi
movq %r14, %rdi
movq %rbx, %rdx
callq 0x4c92b
cmpl $0x0, (%rbx)
jne 0x6bb00
movl $0x6, 0x8(%rax)
movb $0x0, 0xc(%rax)
andl $0x0, 0x10(%rax)
movq %r15, 0x18(%rax)
pushq $0xa
popq %rsi
pushq $0xe
popq %rdx
pushq $0x2
popq %rcx
movq %r14, %rdi
xorl %r8d, %r8d
movq %rbx, %r9
callq 0x4d87e
cmpl $0x0, (%rbx)
jne 0x6bb00
movq %rbx, 0x8(%rsp)
andl $0x0, (%rsp)
pushq $0xd
popq %rsi
pushq $0xf
popq %rdx
movq %r14, %rdi
xorl %ecx, %ecx
xorl %r8d, %r8d
movq %r12, %r9
callq 0x4d213
cmpl $0x0, (%rbx)
jne 0x6bb00
movq %rbx, 0x8(%rsp)
andl $0x0, (%rsp)
pushq $0xf
popq %rsi
pushq $0xd
popq %rdx
pushq $0x1
popq %rcx
movq %r14, %rdi
xorl %r8d, %r8d
movq %r12, %r9
callq 0x4d213
cmpl $0x0, (%rbx)
je 0x6bb0f
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4c77f