// File: /workspace/github_c_makefile_star2_O1/markdryan[P]subtilis/arch/arm32/arm_heap.c
// Function: subtilis_arm_heap_alloc
void subtilis_arm_heap_alloc(subtilis_arm_section_t *arm_s, size_t good_label,
			     size_t bad_label, subtilis_error_t *err)
{
	subtilis_arm_br_instr_t *br;
	subtilis_arm_instr_t *instr;
	subtilis_arm_data_instr_t *datai;
	subtilis_arm_stran_instr_t *stran;
	const subtilis_arm_reg_t heap_start = 0;
	const subtilis_arm_reg_t ret_val = 0;
	const subtilis_arm_reg_t requested_size = 1;
	const subtilis_arm_reg_t slot_number = 2;
	const subtilis_arm_reg_t scratch1 = 3;
	const subtilis_arm_reg_t first_entry = 3;
	const subtilis_arm_reg_t next_ptr = 4;
	const subtilis_arm_reg_t block_size = 5;
	const subtilis_arm_reg_t scratch2 = 6;
	const subtilis_arm_reg_t scratch = 10;

	size_t big_alloc_label = arm_s->label_counter++;
	size_t search_slots_label = arm_s->label_counter++;
	size_t slot12_alloc_label = arm_s->label_counter++;
	size_t exact_slot_size_label = arm_s->label_counter++;
	size_t split_slot12_block_label = arm_s->label_counter++;
	size_t move_block_to_start_label = arm_s->label_counter++;

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

	subtilis_arm_add_cmp_imm(arm_s, SUBTILIS_ARM_INSTR_TST,
				 SUBTILIS_ARM_CCODE_AL, requested_size,
				 subtilis_arm_heap_min_slot_size - 1, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	subtilis_arm_add_data_imm(arm_s, SUBTILIS_ARM_INSTR_BIC,
				  SUBTILIS_ARM_CCODE_NE, false, requested_size,
				  requested_size,
				  subtilis_arm_heap_min_slot_size - 1, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	subtilis_arm_add_add_imm(arm_s, SUBTILIS_ARM_CCODE_NE, false,
				 requested_size, requested_size,
				 subtilis_arm_heap_min_slot_size, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	subtilis_arm_add_mov_reg(arm_s, SUBTILIS_ARM_CCODE_AL, false, scratch,
				 requested_size, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	prv_get_slot(arm_s, scratch, slot_number, scratch1, 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;

	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_GE;
	br->link = false;
	br->target.label = big_alloc_label;

	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 = first_entry;
	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_cmp_imm(arm_s, SUBTILIS_ARM_INSTR_CMP,
				 SUBTILIS_ARM_CCODE_AL, first_entry, 0, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	subtilis_arm_add_stran_imm(arm_s, SUBTILIS_ARM_INSTR_LDR,
				   SUBTILIS_ARM_CCODE_NE, next_ptr, first_entry,
				   4, false, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	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_NE;
	stran->dest = next_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;

	subtilis_arm_add_add_imm(arm_s, SUBTILIS_ARM_CCODE_NE, false, ret_val,
				 first_entry, 8, 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_NE;
	br->link = false;
	br->target.label = good_label;

	subtilis_arm_add_stran_imm(
	    arm_s, SUBTILIS_ARM_INSTR_LDR, SUBTILIS_ARM_CCODE_AL, first_entry,
	    heap_start, subtilis_arm_heap_max_slot * sizeof(int32_t), false,
	    err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	subtilis_arm_add_cmp_imm(arm_s, SUBTILIS_ARM_INSTR_CMP,
				 SUBTILIS_ARM_CCODE_AL, first_entry, 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 = search_slots_label;

	subtilis_arm_add_add_imm(arm_s, SUBTILIS_ARM_CCODE_AL, false,
				 block_size, slot_number,
				 subtilis_arm_heap_min_slot_shift, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

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

	instr =
	    subtilis_arm_section_add_instr(arm_s, SUBTILIS_ARM_INSTR_MOV, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;
	datai = &instr->operands.data;
	datai->ccode = SUBTILIS_ARM_CCODE_AL;
	datai->status = false;
	datai->dest = block_size;
	datai->op2.type = SUBTILIS_ARM_OP2_SHIFTED;
	datai->op2.op.shift.shift_reg = true;
	datai->op2.op.shift.reg = scratch2;
	datai->op2.op.shift.type = SUBTILIS_ARM_SHIFT_LSL;
	datai->op2.op.shift.shift.reg = block_size;

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

	prv_slot12_alloc(arm_s, good_label, bad_label, exact_slot_size_label,
			 err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

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

	prv_split_slot12_block(arm_s, split_slot12_block_label,
			       exact_slot_size_label, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

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

	prv_exact_slot_size(arm_s, good_label, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

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

	prv_big_alloc(arm_s, good_label, bad_label, slot12_alloc_label,
		      move_block_to_start_label, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

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

	prv_move_block_to_start(arm_s, good_label, bad_label,
				slot12_alloc_label, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

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

	prv_search_slots(arm_s, good_label, bad_label, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	prv_split_small_block(arm_s, good_label, err);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rcx, %rbx
movq %rdx, %r15
movq %rsi, %r13
movq %rdi, %r14
movq 0x10(%rdi), %r12
leaq 0x6(%r12), %rax
movq %rax, 0x10(%rdi)
subq $0x8, %rsp
movl $0x1, %r9d
movl $0x4, %esi
movl $0x2, %edx
movl $0xe, %ecx
xorl %r8d, %r8d
pushq %rbx
pushq $0x8
pushq $0x1
callq 0x4fe13
addq $0x20, %rsp
cmpl $0x0, (%rbx)
jne 0x6140a
movl $0x1, %ecx
movq %r14, %rdi
movl $0x8, %esi
movl $0xe, %edx
movl $0x1f, %r8d
movq %rbx, %r9
callq 0x50a87
cmpl $0x0, (%rbx)
jne 0x6140a
movl $0x1, %r8d
movl $0x1, %r9d
movq %r14, %rdi
movl $0xe, %esi
movl $0x1, %edx
xorl %ecx, %ecx
pushq %rbx
pushq $0x1f
callq 0x503c6
addq $0x10, %rsp
cmpl $0x0, (%rbx)
jne 0x6140a
subq $0x8, %rsp
movl $0x1, %r9d
movq %r14, %rdi
movl $0x4, %esi
movl $0x2, %edx
movl $0x1, %ecx
xorl %r8d, %r8d
pushq %rbx
pushq $0x20
pushq $0x1
callq 0x4fe13
addq $0x20, %rsp
cmpl $0x0, (%rbx)
jne 0x6140a
subq $0x8, %rsp
movl $0xa, %r8d
movl $0x1, %r9d
movq %r14, %rdi
movl $0xd, %esi
movl $0xe, %edx
xorl %ecx, %ecx
pushq %rbx
callq 0x5060d
addq $0x10, %rsp
cmpl $0x0, (%rbx)
jne 0x6140a
movl $0xa, %esi
movl $0x2, %edx
movl $0x3, %ecx
movq %r14, %rdi
movq %rbx, %r8
callq 0x61432
cmpl $0x0, (%rbx)
jne 0x6140a
movl $0x2, %ecx
movq %r14, %rdi
movl $0xa, %esi
movl $0xe, %edx
movl $0xc, %r8d
movq %rbx, %r9
callq 0x50a87
cmpl $0x0, (%rbx)
jne 0x6140a
movq %r14, %rdi
movl $0x16, %esi
movq %rbx, %rdx
callq 0x4f73a
cmpl $0x0, (%rbx)
jne 0x6140a
movl $0xa, 0x8(%rax)
movb $0x0, 0xc(%rax)
movq %r12, 0x18(%rax)
movq %r14, %rdi
movl $0x12, %esi
movq %rbx, %rdx
callq 0x4f73a
cmpl $0x0, (%rbx)
jne 0x6140a
movl $0xe, 0x8(%rax)
movq $0x3, 0x10(%rax)
movq $0x0, 0x18(%rax)
movl $0x2, %ecx
movl %ecx, 0x20(%rax)
movq $0x2, 0x28(%rax)
movl $0x0, 0x30(%rax)
movl %ecx, 0x38(%rax)
movb $0x0, 0x40(%rax)
movl $0x1, 0x48(%rax)
movl $0x3, %ecx
movq %r14, %rdi
movl $0xa, %esi
movl $0xe, %edx
xorl %r8d, %r8d
movq %rbx, %r9
callq 0x50a87
cmpl $0x0, (%rbx)
jne 0x6140a
movl $0x4, %ecx
movl $0x3, %r8d
movq %r14, %rdi
movl $0x12, %esi
movl $0x1, %edx
movl $0x4, %r9d
pushq %rbx
pushq $0x0
callq 0x5067b
addq $0x10, %rsp
cmpl $0x0, (%rbx)
jne 0x6140a
movq %r14, %rdi
movl $0x13, %esi
movq %rbx, %rdx
callq 0x4f73a
cmpl $0x0, (%rbx)
jne 0x6140a
movl $0x1, %ecx
movl %ecx, 0x8(%rax)
movq $0x4, 0x10(%rax)
movq $0x0, 0x18(%rax)
movl $0x2, %edx
movl %edx, 0x20(%rax)
movq $0x2, 0x28(%rax)
movl $0x0, 0x30(%rax)
movl %edx, 0x38(%rax)
movb $0x0, 0x40(%rax)
movl %ecx, 0x48(%rax)
subq $0x8, %rsp
movq %r14, %rdi
movl $0x4, %esi
movl $0x2, %edx
movl $0x1, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq %rbx
pushq $0x8
pushq $0x3
callq 0x4fe13
addq $0x20, %rsp
cmpl $0x0, (%rbx)
jne 0x6140a
movq %r14, %rdi
movl $0x16, %esi
movq %rbx, %rdx
callq 0x4f73a
cmpl $0x0, (%rbx)
jne 0x6140a
movl $0x1, 0x8(%rax)
movb $0x0, 0xc(%rax)
movq %r13, 0x18(%rax)
movl $0x3, %ecx
movq %r14, %rdi
movl $0x12, %esi
movl $0xe, %edx
xorl %r8d, %r8d
movl $0x30, %r9d
pushq %rbx
pushq $0x0
callq 0x5067b
addq $0x10, %rsp
cmpl $0x0, (%rbx)
jne 0x6140a
movl $0x3, %ecx
movq %r14, %rdi
movl $0xa, %esi
movl $0xe, %edx
xorl %r8d, %r8d
movq %rbx, %r9
callq 0x50a87
cmpl $0x0, (%rbx)
jne 0x6140a
movq %r14, %rdi
movl $0x16, %esi
movq %rbx, %rdx
callq 0x4f73a
cmpl $0x0, (%rbx)
jne 0x6140a
leaq 0x1(%r12), %rbp
movl $0x0, 0x8(%rax)
movb $0x0, 0xc(%rax)
movq %rbp, 0x18(%rax)
subq $0x8, %rsp
movl $0x5, %r9d
movq %r14, %rdi
movl $0x4, %esi
movl $0x2, %edx
movl $0xe, %ecx
xorl %r8d, %r8d
pushq %rbx
pushq $0x5
pushq $0x2
callq 0x4fe13
addq $0x20, %rsp
cmpl $0x0, (%rbx)
jne 0x6140a
movl $0x6, %r9d
movq %r14, %rdi
movl $0xd, %esi
movl $0xf, %edx
movl $0xe, %ecx
xorl %r8d, %r8d
pushq %rbx
pushq $0x1
callq 0x50249
addq $0x10, %rsp
cmpl $0x0, (%rbx)
jne 0x6140a
movq %r14, %rdi
movl $0xd, %esi
movq %rbx, %rdx
callq 0x4f73a
cmpl $0x0, (%rbx)
jne 0x6140a
leaq 0x2(%r12), %rsi
movl $0xe, 0x8(%rax)
movb $0x0, 0xc(%rax)
movl $0x5, %ecx
movq %rcx, 0x10(%rax)
movl $0x2, 0x20(%rax)
movb $0x1, 0x40(%rax)
movq $0x6, 0x28(%rax)
movl $0x0, 0x30(%rax)
movq %rcx, 0x38(%rax)
movq %r14, %rdi
movq %rsi, 0x8(%rsp)
movq %rbx, %rdx
callq 0x4f587
cmpl $0x0, (%rbx)
jne 0x6140a
leaq 0x3(%r12), %rdx
movq %r14, %rdi
movq %r13, %rsi
movq %rdx, (%rsp)
movq %rbx, %rcx
callq 0x61632
cmpl $0x0, (%rbx)
jne 0x6140a
leaq 0x4(%r12), %rsi
movq %r14, %rdi
movq %rsi, 0x10(%rsp)
movq %rbx, %rdx
callq 0x4f587
cmpl $0x0, (%rbx)
jne 0x6140a
movq %r14, %rdi
movq 0x10(%rsp), %rsi
movq (%rsp), %rdx
movq %rbx, %rcx
callq 0x619ff
cmpl $0x0, (%rbx)
jne 0x6140a
movq %r14, %rdi
movq (%rsp), %rsi
movq %rbx, %rdx
callq 0x4f587
cmpl $0x0, (%rbx)
jne 0x6140a
movq %r14, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x61ced
cmpl $0x0, (%rbx)
jne 0x6140a
movq %r14, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x4f587
cmpl $0x0, (%rbx)
jne 0x6140a
addq $0x5, %r12
movq %r14, %rdi
movq %r15, %rsi
movq 0x8(%rsp), %rdx
movq %r12, %rcx
movq %rbx, %r8
callq 0x61ea1
cmpl $0x0, (%rbx)
jne 0x6140a
movq %r14, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x4f587
cmpl $0x0, (%rbx)
jne 0x6140a
movq %r14, %rdi
movq 0x8(%rsp), %rsi
movq %rbx, %rdx
callq 0x621d1
cmpl $0x0, (%rbx)
jne 0x6140a
movq %r14, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x4f587
cmpl $0x0, (%rbx)
jne 0x6140a
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x623ba
cmpl $0x0, (%rbx)
je 0x61419
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
movq %rbx, %rsi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x625d3