// File: /workspace/github_c_makefile_star2_O2/markdryan[P]subtilis/arch/arm32/arm_heap.c
// Function: subtilis_arm_heap_free
void subtilis_arm_heap_free(subtilis_arm_section_t *arm_s,
			    subtilis_arm_reg_t heap_start,
			    subtilis_arm_reg_t block, subtilis_error_t *err)
{
	subtilis_arm_stran_instr_t *stran;
	subtilis_arm_instr_t *instr;
	const subtilis_arm_reg_t block_size = 3;
	const subtilis_arm_reg_t ptr = 4;
	const subtilis_arm_reg_t slot_number = 8;
	const subtilis_arm_reg_t scratch = 9;

	subtilis_arm_add_sub_imm(arm_s, SUBTILIS_ARM_CCODE_AL, false, block,
				 block, 8, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	subtilis_arm_add_stran_imm(arm_s, SUBTILIS_ARM_INSTR_LDR,
				   SUBTILIS_ARM_CCODE_AL, block_size, block, 0,
				   false, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	prv_get_slot(arm_s, block_size, slot_number, scratch, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

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

	subtilis_arm_add_mov_imm(arm_s, SUBTILIS_ARM_CCODE_GT, false,
				 slot_number, subtilis_arm_heap_max_slot, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	instr =
	    subtilis_arm_section_add_instr(arm_s, SUBTILIS_ARM_INSTR_LDR, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;
	stran = &instr->operands.stran;

	stran->ccode = SUBTILIS_ARM_CCODE_AL;
	stran->dest = ptr;
	stran->base = heap_start;
	stran->offset.type = SUBTILIS_ARM_OP2_SHIFTED;
	stran->offset.op.shift.reg = slot_number;
	stran->offset.op.shift.type = SUBTILIS_ARM_SHIFT_LSL;
	stran->offset.op.shift.shift.integer = 2;
	stran->offset.op.shift.shift_reg = false;
	stran->pre_indexed = true;
	stran->write_back = false;
	stran->subtract = false;
	stran->byte = false;

	instr =
	    subtilis_arm_section_add_instr(arm_s, SUBTILIS_ARM_INSTR_STR, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;
	stran = &instr->operands.stran;

	stran->ccode = SUBTILIS_ARM_CCODE_AL;
	stran->dest = block;
	stran->base = heap_start;
	stran->offset.type = SUBTILIS_ARM_OP2_SHIFTED;
	stran->offset.op.shift.reg = slot_number;
	stran->offset.op.shift.type = SUBTILIS_ARM_SHIFT_LSL;
	stran->offset.op.shift.shift.integer = 2;
	stran->offset.op.shift.shift_reg = false;
	stran->pre_indexed = true;
	stran->write_back = false;
	stran->subtract = false;
	stran->byte = false;

	subtilis_arm_add_stran_imm(arm_s, SUBTILIS_ARM_INSTR_STR,
				   SUBTILIS_ARM_CCODE_AL, ptr, block, 4, false,
				   err);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movq %rcx, %rbx
movq %rdx, %r14
movq %rsi, %r12
movq %rdi, %r15
subq $0x8, %rsp
pushq $0x2
popq %rbp
pushq $0x4
popq %rdx
pushq $0xe
popq %rcx
movl %ebp, %esi
xorl %r8d, %r8d
movq %r14, %r9
pushq %rbx
pushq $0x8
pushq %r14
callq 0x4ced7
addq $0x20, %rsp
cmpl $0x0, (%rbx)
jne 0x5e237
subq $0x10, %rsp
movq %rbx, 0x8(%rsp)
andl $0x0, (%rsp)
pushq $0x12
popq %rsi
pushq $0xe
popq %rdx
pushq $0x3
popq %rcx
movq %r15, %rdi
movq %r14, %r8
xorl %r9d, %r9d
callq 0x4d55b
popq %rax
popq %rcx
cmpl $0x0, (%rbx)
jne 0x5e237
pushq $0x3
popq %rsi
pushq $0x8
popq %rdx
pushq $0x9
popq %rcx
movq %r15, %rdi
movq %rbx, %r8
callq 0x5df12
cmpl $0x0, (%rbx)
jne 0x5e237
pushq $0xa
popq %rsi
pushq $0xe
popq %rdx
pushq $0x8
popq %rcx
pushq $0xc
popq %r8
movq %r15, %rdi
movq %rbx, %r9
callq 0x4d87e
cmpl $0x0, (%rbx)
jne 0x5e237
pushq $0xd
popq %rsi
pushq $0xf
popq %rdx
pushq $0xc
popq %rcx
pushq $0x8
popq %r9
movq %r15, %rdi
xorl %r8d, %r8d
pushq %rbx
pushq $0xc
callq 0x4d213
popq %rax
popq %rcx
cmpl $0x0, (%rbx)
jne 0x5e237
pushq $0x12
popq %rsi
movq %r15, %rdi
movq %rbx, %rdx
callq 0x4c92b
cmpl $0x0, (%rbx)
jne 0x5e237
movl $0xe, 0x8(%rax)
movq $0x4, 0x10(%rax)
movq %r12, 0x18(%rax)
movl %ebp, 0x20(%rax)
movq $0x8, 0x28(%rax)
andl $0x0, 0x30(%rax)
movl %ebp, 0x38(%rax)
movb $0x0, 0x40(%rax)
movl $0x1, 0x48(%rax)
pushq $0x13
popq %rsi
movq %r15, %rdi
movq %rbx, %rdx
callq 0x4c92b
cmpl $0x0, (%rbx)
jne 0x5e237
movl $0xe, 0x8(%rax)
movq %r14, 0x10(%rax)
movq %r12, 0x18(%rax)
movl %ebp, 0x20(%rax)
movq $0x8, 0x28(%rax)
andl $0x0, 0x30(%rax)
movl %ebp, 0x38(%rax)
movb $0x0, 0x40(%rax)
movl $0x1, 0x48(%rax)
pushq $0x13
popq %rsi
pushq $0xe
popq %rdx
pushq $0x4
popq %rcx
pushq $0x4
popq %r9
movq %r15, %rdi
movq %r14, %r8
pushq %rbx
pushq $0x0
callq 0x4d55b
popq %rax
popq %rcx
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq