// File: /workspace/github_c_makefile_star2_O1/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, %r14
movl %r8d, 0x8(%rsp)
movl %ecx, %r15d
movl %edx, %ebp
movl %esi, %r13d
movq %rdi, %r12
movl 0x58(%rsp), %ebx
movl $0x0, 0xc(%rsp)
movq %rsp, %rsi
movl %ebx, %edi
callq 0x4f8d1
testl %ebx, %ebx
setns %cl
orb %al, %cl
movl %r15d, 0x4(%rsp)
jne 0x4fe85
movl %ebp, %r15d
movq %r12, %rbp
movq %r14, %r12
movl %ebx, %r14d
negl %r14d
movq %rsp, %rsi
movl %r14d, %edi
callq 0x4f8d1
testb %al, %al
cmovnel %r14d, %ebx
movq %r12, %r14
movq %rbp, %r12
movl %r15d, %ebp
cmovnel %r15d, %r13d
movq 0x60(%rsp), %r10
movq 0x50(%rsp), %r9
testb %al, %al
jne 0x4ff01
movl 0x4(%rsp), %edx
cmpl $0xe, %edx
sete %al
movl 0x8(%rsp), %ecx
xorb $0x1, %cl
orb %al, %cl
jne 0x4feb5
movq %r12, %rdi
movl %r13d, %esi
movl $0x1, %ecx
jmp 0x4ff31
movq %rsp, %rsi
leaq 0xc(%rsp), %rdx
movl %ebx, %edi
callq 0x4f953
testl %ebx, %ebx
setns %cl
orb %al, %cl
jne 0x4fef7
movq %r14, 0x10(%rsp)
movl %ebx, %r14d
negl %r14d
movq %rsp, %rsi
leaq 0xc(%rsp), %rdx
movl %r14d, %edi
callq 0x4f953
testb %al, %al
cmovnel %r14d, %ebx
movq 0x10(%rsp), %r14
cmovnel %ebp, %r13d
movq 0x50(%rsp), %r9
movq 0x60(%rsp), %r10
testb %al, %al
je 0x4ff22
movq %r9, %rbp
movq %r12, %rdi
movq %r10, %rsi
movq %r10, %rbx
callq 0x4f5b7
movq %rbx, %rcx
cmpl $0x0, (%rbx)
je 0x4ff42
xorl %ebx, %ebx
jmp 0x4ff53
movzbl 0x8(%rsp), %ecx
movq %r12, %rdi
movl %r13d, %esi
movl 0x4(%rsp), %edx
movq %r14, %r8
pushq %r10
pushq %rbx
callq 0x4fc61
addq $0x28, %rsp
jmp 0x4ff5c
movl $0x0, (%rax)
movq %rax, %rbx
addq $0x8, %rbx
movl %r13d, 0x8(%rax)
cmpl $0x0, (%rcx)
je 0x4ff67
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rcx, %r15
movq %rbp, %rsi
movl 0x8(%rsp), %eax
movb %al, 0xc(%rbx)
movl $0x1, 0x20(%rbx)
movl 0x4(%rsp), %eax
movl %eax, 0x8(%rbx)
movl 0xc(%rsp), %ebp
movq %r14, %rax
testl %ebp, %ebp
je 0x4ffb5
movq (%r12), %rax
leaq 0x1(%rax), %rcx
movq %rcx, (%r12)
leaq -0x1(%rax), %rcx
cmpq $0x3, %rcx
jae 0x4ffb1
leaq 0x25bd6(%rip), %rcx     # 0x75b80
movq -0x8(%rcx,%rax,8), %rax
jmp 0x4ffb5
addq $0xc, %rax
movq %rax, 0x10(%rbx)
movq %rsi, 0x18(%rbx)
movl (%rsp), %eax
movl %eax, 0x28(%rbx)
testl %ebp, %ebp
je 0x4ff58
movq %r12, %rdi
movq %r15, %rsi
callq 0x4f7a0
cmpl $0x0, (%r15)
jne 0x4ff58
movq 0x10(%rbx), %rcx
movq %rcx, 0x18(%rax)
movl %ebp, 0x28(%rax)
movq %r14, 0x10(%rax)
jmp 0x4ff58