// File: /workspace/github_c_makefile_star2_O2/markdryan[P]subtilis/arch/arm32/arm_core.c
// Function: subtilis_arm_add_addsub_imm
void subtilis_arm_add_addsub_imm(subtilis_arm_section_t *s,
				 subtilis_arm_instr_type_t itype,
				 subtilis_arm_instr_type_t alt_type,
				 subtilis_arm_ccode_type_t ccode, bool status,
				 subtilis_arm_reg_t dest,
				 subtilis_arm_reg_t op1, int32_t op2,
				 subtilis_error_t *err)
{
	subtilis_arm_instr_t *instr;
	subtilis_arm_data_instr_t *datai;
	subtilis_arm_data_instr_t *datai2;
	bool can_encode;
	uint32_t encoded;
	uint32_t encoded2 = 0;

	can_encode = subtilis_arm_encode_imm(op2, &encoded);
	/* TODO why does it matter if the op2 is less than 0? */
	if ((!can_encode) && (op2 < 0)) {
		can_encode = subtilis_arm_encode_imm(-op2, &encoded);
		if (can_encode) {
			op2 = -op2;
			itype = alt_type;
		}
	}

	if (!can_encode) {
		if (status && (ccode != SUBTILIS_ARM_CCODE_AL)) {
			(void)subtilis_add_data_imm_ldr_datai(
			    s, itype, ccode, status, dest, op1, op2, err);
			return;
		}

		can_encode =
		    subtilis_arm_encode_lvl2_imm(op2, &encoded, &encoded2);

		if ((!can_encode) && (op2 < 0)) {
			can_encode = subtilis_arm_encode_lvl2_imm(
			    -op2, &encoded, &encoded2);
			if (can_encode) {
				op2 = -op2;
				itype = alt_type;
			}
		}
	}

	if (!can_encode) {
		(void)subtilis_add_data_imm_ldr_datai(s, itype, ccode, status,
						      dest, op1, op2, err);
		return;
	}

	instr = subtilis_arm_section_add_instr(s, itype, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	datai = &instr->operands.data;
	datai->status = status;
	datai->op2.type = SUBTILIS_ARM_OP2_I32;
	datai->ccode = ccode;
	datai->dest = encoded2 == 0 ? dest : subtilis_arm_acquire_new_reg(s);
	datai->op1 = op1;
	datai->op2.op.integer = encoded;

	if (encoded2 == 0)
		return;

	instr = subtilis_arm_section_dup_instr(s, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;
	datai2 = &instr->operands.data;
	datai2->op1 = datai->dest;
	datai2->op2.op.integer = encoded2;
	datai2->dest = dest;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %r9, %r13
movl %r8d, 0x8(%rsp)
movl %ecx, %ebp
movl %edx, %r14d
movl %esi, %ebx
movq %rdi, %r15
movl 0x58(%rsp), %r12d
andl $0x0, 0xc(%rsp)
leaq 0x4(%rsp), %rsi
movl %r12d, %edi
callq 0x4caab
testl %r12d, %r12d
setns %cl
orb %al, %cl
je 0x4cf21
testb %al, %al
je 0x4cf56
movl %ebx, %r14d
jmp 0x4cf34
movl %r12d, %edi
negl %edi
leaq 0x4(%rsp), %rsi
callq 0x4caab
testb %al, %al
je 0x4cf56
movq %r15, %rdi
movl %r14d, %esi
movq 0x60(%rsp), %r14
movq %r14, %rdx
callq 0x4c92b
cmpl $0x0, (%r14)
je 0x4cfa0
addq $0x18, %rsp
jmp 0x4d050
cmpl $0xe, %ebp
sete %al
movl 0x8(%rsp), %ecx
xorb $0x1, %cl
orb %al, %cl
jne 0x4cf6f
pushq $0x1
popq %rcx
jmp 0x4d032
movq %r13, 0x10(%rsp)
testl %r12d, %r12d
setns %r13b
leaq 0x4(%rsp), %rsi
leaq 0xc(%rsp), %rdx
movl %r12d, %edi
callq 0x4cb21
orb %al, %r13b
je 0x4d00c
testb %al, %al
movq 0x10(%rsp), %r13
jne 0x4cf1c
jmp 0x4d02d
movq %rax, %rbx
movl 0x8(%rsp), %eax
movb %al, 0xc(%rbx)
movl $0x1, 0x20(%rbx)
movl %ebp, 0x8(%rbx)
movl 0xc(%rsp), %ebp
movq %r13, %rax
testl %ebp, %ebp
je 0x4cfc7
movq %r15, %rdi
callq 0x4c202
movq %rax, 0x10(%rbx)
movq 0x50(%rsp), %rax
movq %rax, 0x18(%rbx)
movl 0x4(%rsp), %eax
movl %eax, 0x28(%rbx)
testl %ebp, %ebp
je 0x4cf4d
movq %r15, %rdi
movq %r14, %rsi
callq 0x4c98b
cmpl $0x0, (%r14)
jne 0x4cf4d
movq 0x10(%rbx), %rcx
movq %rcx, 0x18(%rax)
movl %ebp, 0x28(%rax)
movq %r13, 0x10(%rax)
jmp 0x4cf4d
movl %r12d, %edi
negl %edi
leaq 0x4(%rsp), %rsi
leaq 0xc(%rsp), %rdx
callq 0x4cb21
testb %al, %al
movq 0x10(%rsp), %r13
jne 0x4cf34
movzbl 0x8(%rsp), %ecx
movq %r15, %rdi
movl %ebx, %esi
movl %ebp, %edx
movq %r13, %r8
movq 0x50(%rsp), %r9
pushq 0x60(%rsp)
pushq %r12
callq 0x4cdab
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq