// File: /workspace/github_c_makefile_star2_O0/markdryan[P]subtilis/arch/arm32/arm_heap.c
// Function: subtilis_arm_heap_free_space
void subtilis_arm_heap_free_space(subtilis_arm_section_t *arm_s,
				  subtilis_arm_reg_t heap_start,
				  subtilis_arm_reg_t result,
				  subtilis_error_t *err)
{
	size_t outer_loop_label;
	size_t inner_loop_label;
	size_t skip_label;
	subtilis_arm_stran_instr_t *stran;
	subtilis_arm_br_instr_t *br;
	subtilis_arm_instr_t *instr;
	subtilis_arm_data_instr_t *datai;
	const subtilis_arm_reg_t ptr = 0;
	const subtilis_arm_reg_t slots_counter = 1;
	const subtilis_arm_reg_t scratch = 2;
	const subtilis_arm_reg_t sum = 3;

	outer_loop_label = arm_s->label_counter++;
	inner_loop_label = arm_s->label_counter++;
	skip_label = arm_s->label_counter++;

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

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

	subtilis_arm_section_add_label(arm_s, outer_loop_label, 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 = slots_counter;
	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_section_add_label(arm_s, inner_loop_label, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	subtilis_arm_add_cmp_imm(arm_s, SUBTILIS_ARM_INSTR_CMP,
				 SUBTILIS_ARM_CCODE_AL, ptr, 0, 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_EQ;
	br->link = false;
	br->target.label = skip_label;

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

	instr =
	    subtilis_arm_section_add_instr(arm_s, SUBTILIS_ARM_INSTR_ADD, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;
	datai = &instr->operands.data;
	datai->ccode = SUBTILIS_ARM_CCODE_AL;
	datai->status = false;
	datai->dest = sum;
	datai->op1 = sum;
	datai->op2.type = SUBTILIS_ARM_OP2_REG;
	datai->op2.op.reg = scratch;

	subtilis_arm_add_stran_imm(arm_s, SUBTILIS_ARM_INSTR_LDR,
				   SUBTILIS_ARM_CCODE_AL, ptr, ptr, 4, false,
				   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_AL;
	br->link = false;
	br->target.label = inner_loop_label;

	subtilis_arm_section_add_label(arm_s, skip_label, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	subtilis_arm_add_sub_imm(arm_s, SUBTILIS_ARM_CCODE_AL, true,
				 slots_counter, slots_counter, 1, 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_PL;
	br->link = false;
	br->target.label = outer_loop_label;

	subtilis_arm_add_mov_reg(arm_s, SUBTILIS_ARM_CCODE_AL, false, result,
				 sum, err);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x1, -0x68(%rbp)
movq $0x2, -0x70(%rbp)
movq $0x3, -0x78(%rbp)
movq -0x8(%rbp), %rcx
movq 0x10(%rcx), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x10(%rcx)
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rcx
movq 0x10(%rcx), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x10(%rcx)
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rcx
movq 0x10(%rcx), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x10(%rcx)
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movl $0xd, %esi
movl $0xf, %edx
movl $0xe, %ecx
xorl %r8d, %r8d
movl $0x3, %r9d
movl $0x0, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x71b10
movq -0x20(%rbp), %rax
cmpl $0x0, (%rax)
je 0x90c6f
jmp 0x91022
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movl $0xd, %esi
movl $0xf, %edx
movl $0xe, %ecx
xorl %r8d, %r8d
movl $0x1, %r9d
movl $0xc, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x71b10
movq -0x20(%rbp), %rax
cmpl $0x0, (%rax)
je 0x90cae
jmp 0x91022
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x70850
movq -0x20(%rbp), %rax
cmpl $0x0, (%rax)
je 0x90ccd
jmp 0x91022
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rdx
movl $0x12, %esi
callq 0x70b50
movq %rax, -0x50(%rbp)
movq -0x20(%rbp), %rax
cmpl $0x0, (%rax)
je 0x90cf1
jmp 0x91022
movq -0x50(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movl $0xe, (%rax)
movq -0x40(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x10(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x40(%rbp), %rax
movl $0x2, 0x18(%rax)
movq -0x40(%rbp), %rax
movq $0x1, 0x20(%rax)
movq -0x40(%rbp), %rax
movl $0x0, 0x28(%rax)
movq -0x40(%rbp), %rax
movl $0x2, 0x30(%rax)
movq -0x40(%rbp), %rax
movb $0x0, 0x38(%rax)
movq -0x40(%rbp), %rax
movb $0x1, 0x40(%rax)
movq -0x40(%rbp), %rax
movb $0x0, 0x41(%rax)
movq -0x40(%rbp), %rax
movb $0x0, 0x42(%rax)
movq -0x40(%rbp), %rax
movb $0x0, 0x43(%rax)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x70850
movq -0x20(%rbp), %rax
cmpl $0x0, (%rax)
je 0x90d93
jmp 0x91022
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %r9
movl $0xa, %esi
movl $0xe, %edx
xorl %eax, %eax
movl %eax, %ecx
xorl %r8d, %r8d
callq 0x72620
movq -0x20(%rbp), %rax
cmpl $0x0, (%rax)
je 0x90dbf
jmp 0x91022
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rdx
movl $0x16, %esi
callq 0x70b50
movq %rax, -0x50(%rbp)
movq -0x20(%rbp), %rax
cmpl $0x0, (%rax)
je 0x90de3
jmp 0x91022
movq -0x50(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movl $0x0, (%rax)
movq -0x48(%rbp), %rax
movb $0x0, 0x4(%rax)
movq -0x38(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movl $0x12, %esi
movl $0xe, %edx
movl $0x2, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
movl $0x0, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x72030
movq -0x20(%rbp), %rax
cmpl $0x0, (%rax)
je 0x90e49
jmp 0x91022
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rdx
movl $0x4, %esi
callq 0x70b50
movq %rax, -0x50(%rbp)
movq -0x20(%rbp), %rax
cmpl $0x0, (%rax)
je 0x90e6d
jmp 0x91022
movq -0x50(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movl $0xe, (%rax)
movq -0x58(%rbp), %rax
movb $0x0, 0x4(%rax)
movq -0x58(%rbp), %rax
movq $0x3, 0x8(%rax)
movq -0x58(%rbp), %rax
movq $0x3, 0x10(%rax)
movq -0x58(%rbp), %rax
movl $0x0, 0x18(%rax)
movq -0x58(%rbp), %rax
movq $0x2, 0x20(%rax)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movl $0x12, %esi
movl $0xe, %edx
xorl %ecx, %ecx
movl %ecx, %r8d
movl $0x4, %r9d
xorl %ecx, %ecx
movq %r8, %rcx
movl $0x0, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x72030
movq -0x20(%rbp), %rax
cmpl $0x0, (%rax)
je 0x90efb
jmp 0x91022
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rdx
movl $0x16, %esi
callq 0x70b50
movq %rax, -0x50(%rbp)
movq -0x20(%rbp), %rax
cmpl $0x0, (%rax)
je 0x90f1f
jmp 0x91022
movq -0x50(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movl $0xe, (%rax)
movq -0x48(%rbp), %rax
movb $0x0, 0x4(%rax)
movq -0x30(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x70850
movq -0x20(%rbp), %rax
cmpl $0x0, (%rax)
je 0x90f68
jmp 0x91022
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movl $0x2, %esi
movl $0x4, %edx
movl $0xe, %ecx
movl $0x1, %r8d
movl $0x1, %r9d
movq $0x1, (%rsp)
movl $0x1, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0x71640
movq -0x20(%rbp), %rax
cmpl $0x0, (%rax)
je 0x90fb0
jmp 0x91022
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rdx
movl $0x16, %esi
callq 0x70b50
movq %rax, -0x50(%rbp)
movq -0x20(%rbp), %rax
cmpl $0x0, (%rax)
je 0x90fd1
jmp 0x91022
movq -0x50(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movl $0x5, (%rax)
movq -0x48(%rbp), %rax
movb $0x0, 0x4(%rax)
movq -0x28(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %r8
movq -0x20(%rbp), %rax
movl $0xd, %esi
movl $0xe, %edx
xorl %ecx, %ecx
movl $0x3, %r9d
movq %rax, (%rsp)
callq 0x71fa0
addq $0x90, %rsp
popq %rbp
retq
nop
nopl (%rax)