// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, %r15
movq %rdx, %rbx
movq %rsi, %rbp
movq %rdi, %r14
movq 0x10(%rdi), %r12
leaq 0x3(%r12), %rax
movq %rax, 0x10(%rdi)
andl $0x0, (%rsp)
movq %rcx, 0x8(%rsp)
pushq $0xd
popq %rsi
pushq $0xf
popq %rdx
pushq $0xe
popq %rcx
pushq $0x3
popq %r13
xorl %r8d, %r8d
movq %r13, %r9
callq 0x4d213
cmpl $0x0, (%r15)
jne 0x5e4c8
movq %r15, 0x8(%rsp)
movl $0xc, (%rsp)
pushq $0xd
popq %rsi
pushq $0xf
popq %rdx
pushq $0xe
popq %rcx
pushq $0x1
popq %r9
movq %r14, %rdi
xorl %r8d, %r8d
callq 0x4d213
cmpl $0x0, (%r15)
jne 0x5e4c8
movq %r14, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x4c77f
cmpl $0x0, (%r15)
jne 0x5e4c8
pushq $0x12
popq %rsi
movq %r14, %rdi
movq %r15, %rdx
callq 0x4c92b
cmpl $0x0, (%r15)
jne 0x5e4c8
movl $0xe, 0x8(%rax)
andq $0x0, 0x10(%rax)
movq %rbp, 0x18(%rax)
pushq $0x2
popq %rcx
movl %ecx, 0x20(%rax)
movq $0x1, 0x28(%rax)
andl $0x0, 0x30(%rax)
leaq 0x1(%r12), %rbp
movl %ecx, 0x38(%rax)
movb $0x0, 0x40(%rax)
movl $0x1, 0x48(%rax)
movq %r14, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x4c77f
cmpl $0x0, (%r15)
jne 0x5e4c8
pushq $0xa
popq %rsi
pushq $0xe
popq %rdx
movq %r14, %rdi
xorl %ecx, %ecx
xorl %r8d, %r8d
movq %r15, %r9
callq 0x4d87e
cmpl $0x0, (%r15)
jne 0x5e4c8
pushq $0x16
popq %rsi
movq %r14, %rdi
movq %r15, %rdx
callq 0x4c92b
cmpl $0x0, (%r15)
jne 0x5e4c8
andl $0x0, 0x8(%rax)
leaq 0x2(%r12), %rcx
movb $0x0, 0xc(%rax)
movq %rcx, 0x20(%rsp)
movq %rcx, 0x18(%rax)
andl $0x0, (%rsp)
movq %r15, 0x8(%rsp)
pushq $0x12
popq %rsi
pushq $0xe
popq %rdx
pushq $0x2
popq %rcx
movq %r14, %rdi
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x4d55b
xorl %eax, %eax
cmpl %eax, (%r15)
jne 0x5e4c8
pushq $0x4
popq %rsi
movq %r14, %rdi
movq %r15, %rdx
callq 0x4c92b
cmpl $0x0, (%r15)
jne 0x5e4c8
movl $0xe, 0x8(%rax)
movb $0x0, 0xc(%rax)
movq %r13, 0x10(%rax)
movq %r13, 0x18(%rax)
andl $0x0, 0x20(%rax)
movq $0x2, 0x28(%rax)
andl $0x0, (%rsp)
movq %r15, 0x8(%rsp)
xorl %r13d, %r13d
pushq $0x12
popq %rsi
pushq $0xe
popq %rdx
pushq $0x4
popq %r9
movq %r14, %rdi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x4d55b
cmpl %r13d, (%r15)
jne 0x5e4c8
pushq $0x16
popq %rsi
movq %r14, %rdi
movq %r15, %rdx
callq 0x4c92b
cmpl $0x0, (%r15)
jne 0x5e4c8
movl $0xe, 0x8(%rax)
movb $0x0, 0xc(%rax)
movq %rbp, 0x18(%rax)
movq %r14, %rdi
movq 0x20(%rsp), %rsi
movq %r15, %rdx
callq 0x4c77f
cmpl $0x0, (%r15)
jne 0x5e4c8
movq %r15, 0x10(%rsp)
movl $0x1, 0x8(%rsp)
movq $0x1, (%rsp)
pushq $0x2
popq %rsi
pushq $0x4
popq %rdx
pushq $0xe
popq %rcx
pushq $0x1
popq %r8
pushq $0x1
popq %r9
movq %r14, %rdi
callq 0x4ced7
cmpl $0x0, (%r15)
jne 0x5e4c8
pushq $0x16
popq %rsi
movq %r14, %rdi
movq %r15, %rdx
callq 0x4c92b
cmpl $0x0, (%r15)
jne 0x5e4c8
movl $0x5, 0x8(%rax)
movb $0x0, 0xc(%rax)
movq %r12, 0x18(%rax)
movq %r15, (%rsp)
pushq $0xd
popq %rsi
pushq $0xe
popq %rdx
pushq $0x3
popq %r9
movq %r14, %rdi
xorl %ecx, %ecx
movq %rbx, %r8
callq 0x4d518
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop