// File: /workspace/github_c_makefile_star2_O3/markdryan[P]subtilis/backends/riscos_common/riscos_arm.c
// Function: prv_add_escape_handler
static void prv_add_escape_handler(subtilis_arm_section_t *arm_s,
				   subtilis_error_t *err)
{
	size_t label;
	subtilis_arm_instr_t *instr;
	subtilis_arm_br_instr_t *br;

	subtilis_arm_add_mov_imm(arm_s, SUBTILIS_ARM_CCODE_AL, false, 0, 9,
				 err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;
	subtilis_arm_add_add_imm(arm_s, SUBTILIS_ARM_CCODE_AL, false, 1, 15, 16,
				 err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;
	subtilis_arm_add_mov_reg(arm_s, SUBTILIS_ARM_CCODE_AL, false, 2, 12,
				 err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	/* I'm assuming that this call can't fail. */

	/* read_mask = 0x7 = R0, R1, R2 */
	/* write_mask = 0xe = R1, R2, R3 */
	subtilis_arm_add_swi(arm_s, SUBTILIS_ARM_CCODE_AL, 0x40, 0x7, 0xe, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	subtilis_arm_add_stran_imm(arm_s, SUBTILIS_ARM_INSTR_STR,
				   SUBTILIS_ARM_CCODE_AL, 1, 12,
				   RISCOS_ARM_GLOBAL_ESC_HANDLER, false, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	subtilis_arm_add_stran_imm(arm_s, SUBTILIS_ARM_INSTR_STR,
				   SUBTILIS_ARM_CCODE_AL, 2, 12,
				   RISCOS_ARM_GLOBAL_ESC_R12, false, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	label = arm_s->label_counter++;
	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_AL;
	br->link = false;
	br->link_type = SUBTILIS_ARM_BR_LINK_VOID;
	br->target.label = label;

	/* Code for the handler */

	subtilis_arm_add_cmp_imm(arm_s, SUBTILIS_ARM_INSTR_TST,
				 SUBTILIS_ARM_CCODE_AL, 11, 1 << 6, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	subtilis_arm_add_mov_reg(arm_s, SUBTILIS_ARM_CCODE_EQ, false, 15, 14,
				 err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	/*
	 * We're going to store the value of R11 into the escape variable.  If
	 * we get here we know that R11 is non zero, i.e., it has at least one
	 * bit set.  By using the value in R11 we don't need to corrupt any
	 * registers.
	 */

	subtilis_arm_add_stran_imm(arm_s, SUBTILIS_ARM_INSTR_STR,
				   SUBTILIS_ARM_CCODE_AL, 11, 12,
				   RISCOS_ARM_GLOBAL_ESC_FLAG, false, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

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

	subtilis_arm_section_add_label(arm_s, label, err);
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
movq %rsi, %rbx
movq %rdi, %r14
movl $0xd, %esi
movl $0xf, %edx
movl $0xe, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq %rbx
pushq $0x9
callq 0x50130
addq $0x10, %rsp
cmpl $0x0, (%rbx)
jne 0x717b7
subq $0x8, %rsp
movl $0x1, %r9d
movq %r14, %rdi
movl $0x4, %esi
movl $0x2, %edx
movl $0xe, %ecx
xorl %r8d, %r8d
pushq %rbx
pushq $0x10
pushq $0xf
callq 0x4fd3f
addq $0x20, %rsp
cmpl $0x0, (%rbx)
jne 0x717b7
subq $0x8, %rsp
movl $0x2, %r8d
movl $0xc, %r9d
movq %r14, %rdi
movl $0xd, %esi
movl $0xe, %edx
xorl %ecx, %ecx
pushq %rbx
callq 0x504b3
addq $0x10, %rsp
cmpl $0x0, (%rbx)
jne 0x717b7
movl $0x40, %edx
movq %r14, %rdi
movl $0xe, %esi
movl $0x7, %ecx
movl $0xe, %r8d
movq %rbx, %r9
callq 0x50463
cmpl $0x0, (%rbx)
jne 0x717b7
movl $0x1, %ecx
movl $0xc, %r8d
movq %r14, %rdi
movl $0x13, %esi
movl $0xe, %edx
movl $0xfffffff8, %r9d       # imm = 0xFFFFFFF8
pushq %rbx
pushq $0x0
callq 0x50516
addq $0x10, %rsp
cmpl $0x0, (%rbx)
jne 0x717b7
movl $0x2, %ecx
movl $0xc, %r8d
movq %r14, %rdi
movl $0x13, %esi
movl $0xe, %edx
movl $0xfffffff4, %r9d       # imm = 0xFFFFFFF4
pushq %rbx
pushq $0x0
callq 0x50516
addq $0x10, %rsp
cmpl $0x0, (%rbx)
jne 0x717b7
movq 0x10(%r14), %r15
leaq 0x1(%r15), %rax
movq %rax, 0x10(%r14)
movq %r14, %rdi
movl $0x16, %esi
movq %rbx, %rdx
callq 0x4f6e2
cmpl $0x0, (%rbx)
jne 0x717b7
movl $0xe, 0x8(%rax)
movb $0x0, 0xc(%rax)
movl $0x0, 0x10(%rax)
movq %r15, 0x18(%rax)
movl $0xb, %ecx
movq %r14, %rdi
movl $0x8, %esi
movl $0xe, %edx
movl $0x40, %r8d
movq %rbx, %r9
callq 0x508d6
cmpl $0x0, (%rbx)
jne 0x717b7
subq $0x8, %rsp
movl $0xf, %r8d
movl $0xe, %r9d
movq %r14, %rdi
movl $0xd, %esi
xorl %edx, %edx
xorl %ecx, %ecx
pushq %rbx
callq 0x504b3
addq $0x10, %rsp
cmpl $0x0, (%rbx)
jne 0x717b7
movl $0xb, %ecx
movl $0xc, %r8d
movq %r14, %rdi
movl $0x13, %esi
movl $0xe, %edx
movl $0xfffffffc, %r9d       # imm = 0xFFFFFFFC
pushq %rbx
pushq $0x0
callq 0x50516
addq $0x10, %rsp
cmpl $0x0, (%rbx)
jne 0x717b7
subq $0x8, %rsp
movl $0xf, %r8d
movl $0xe, %r9d
movq %r14, %rdi
movl $0xd, %esi
movl $0xe, %edx
xorl %ecx, %ecx
pushq %rbx
callq 0x504b3
addq $0x10, %rsp
cmpl $0x0, (%rbx)
je 0x717bd
popq %rbx
popq %r14
popq %r15
retq
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
popq %rbx
popq %r14
popq %r15
jmp 0x4f530