// File: /workspace/github_c_makefile_star2_O2/markdryan[P]subtilis/arch/arm32/arm_heap.c
// Function: subtilis_arm_heap_init
void subtilis_arm_heap_init(subtilis_arm_section_t *arm_s,
			    subtilis_error_t *err)
{
	size_t loop_label;
	subtilis_arm_stran_instr_t *stran;
	subtilis_arm_br_instr_t *br;
	subtilis_arm_instr_t *instr;
	const subtilis_arm_reg_t heap_start = 1;
	const subtilis_arm_reg_t slots_counter = 2;
	const subtilis_arm_reg_t heap_size = 3;
	const subtilis_arm_reg_t zero = 4;

	loop_label = arm_s->label_counter++;

	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_add_mov_imm(arm_s, SUBTILIS_ARM_CCODE_AL, false, zero, 0,
				 err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	/*
	 * Loop to zero out the first subtilis_arm_heap_max_slot slots.
	 * When the heap is first initialised there's only one block in
	 * the final slot that holds all the data.
	 */

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

	subtilis_arm_add_sub_imm(arm_s, SUBTILIS_ARM_CCODE_AL, false,
				 slots_counter, slots_counter, 1, 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_AL;
	stran->dest = zero;
	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_add_cmp_imm(arm_s, SUBTILIS_ARM_INSTR_CMP,
				 SUBTILIS_ARM_CCODE_AL, slots_counter, 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_GT;
	br->link = false;
	br->target.label = loop_label;

	subtilis_arm_add_add_imm(arm_s, SUBTILIS_ARM_CCODE_AL, false,
				 heap_start, heap_start,
				 subtilis_arm_heap_max_slots * 4, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	subtilis_arm_add_sub_imm(arm_s, SUBTILIS_ARM_CCODE_AL, false, heap_size,
				 heap_size, subtilis_arm_heap_max_slots * 4,
				 err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	/*
	 * The size of the first and only slot must be divisible by the
	 * size of the smallest slot, currently 32 bytes.
	 */

	subtilis_arm_add_data_imm(
	    arm_s, SUBTILIS_ARM_INSTR_BIC, SUBTILIS_ARM_CCODE_AL, false,
	    heap_size, heap_size, (1 << subtilis_arm_heap_min_slot_shift) - 1,
	    err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	/*
	 * Initialise heap with on entry in the final slot.
	 */

	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 = heap_start;
	stran->base = heap_start;
	stran->offset.type = SUBTILIS_ARM_OP2_I32;
	stran->offset.op.integer = 4;
	stran->pre_indexed = true;
	stran->write_back = false;
	stran->subtract = true;
	stran->byte = false;

	/*
	 * Set up one and only block.  Start by storing its size
	 */

	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 = heap_size;
	stran->base = heap_start;
	stran->offset.type = SUBTILIS_ARM_OP2_I32;
	stran->offset.op.integer = 0;
	stran->pre_indexed = true;
	stran->write_back = false;
	stran->subtract = false;
	stran->byte = false;

	/*
	 * Zero out the next pointer.
	 */

	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 = zero;
	stran->base = heap_start;
	stran->offset.type = SUBTILIS_ARM_OP2_I32;
	stran->offset.op.integer = 4;
	stran->pre_indexed = true;
	stran->write_back = false;
	stran->subtract = false;
	stran->byte = false;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movq %rsi, %rbx
movq %rdi, %r14
movq 0x10(%rdi), %r15
leaq 0x1(%r15), %rax
movq %rax, 0x10(%rdi)
pushq $0xd
popq %rsi
pushq $0xf
popq %rdx
pushq $0xe
popq %rcx
pushq $0x2
popq %r9
xorl %r8d, %r8d
pushq %rbx
pushq $0xc
callq 0x4d213
popq %rax
popq %rcx
cmpl $0x0, (%rbx)
jne 0x5ca2e
subq $0x10, %rsp
andl $0x0, (%rsp)
movq %rbx, 0x8(%rsp)
pushq $0xd
popq %rsi
pushq $0xf
popq %rdx
pushq $0xe
popq %rcx
pushq $0x4
popq %r9
movq %r14, %rdi
xorl %r8d, %r8d
callq 0x4d213
popq %rax
popq %rcx
cmpl $0x0, (%rbx)
jne 0x5ca2e
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x4c77f
cmpl $0x0, (%rbx)
jne 0x5ca2e
subq $0x8, %rsp
pushq $0x2
popq %rbp
pushq $0x4
popq %rdx
pushq $0xe
popq %rcx
pushq $0x2
popq %r9
movq %r14, %rdi
movl %ebp, %esi
xorl %r8d, %r8d
pushq %rbx
pushq $0x1
pushq $0x2
callq 0x4ced7
addq $0x20, %rsp
cmpl $0x0, (%rbx)
jne 0x5ca2e
pushq $0x13
popq %rsi
movq %r14, %rdi
movq %rbx, %rdx
callq 0x4c92b
cmpl $0x0, (%rbx)
jne 0x5ca2e
movl $0xe, 0x8(%rax)
movq $0x4, 0x10(%rax)
movq $0x1, 0x18(%rax)
movl %ebp, 0x20(%rax)
movq $0x2, 0x28(%rax)
andl $0x0, 0x30(%rax)
movl %ebp, 0x38(%rax)
movb $0x0, 0x40(%rax)
movl $0x1, 0x48(%rax)
pushq $0xa
popq %rsi
pushq $0xe
popq %rdx
pushq $0x2
popq %rcx
movq %r14, %rdi
xorl %r8d, %r8d
movq %rbx, %r9
callq 0x4d87e
cmpl $0x0, (%rbx)
jne 0x5ca2e
pushq $0x16
popq %rsi
movq %r14, %rdi
movq %rbx, %rdx
callq 0x4c92b
cmpl $0x0, (%rbx)
jne 0x5ca2e
movl $0xc, 0x8(%rax)
movb $0x0, 0xc(%rax)
movq %r15, 0x18(%rax)
subq $0x8, %rsp
pushq $0x4
popq %rsi
pushq $0x2
popq %rdx
pushq $0xe
popq %rcx
pushq $0x1
popq %r15
movq %r14, %rdi
xorl %r8d, %r8d
movq %r15, %r9
pushq %rbx
pushq $0x34
pushq $0x1
callq 0x4ced7
addq $0x20, %rsp
cmpl $0x0, (%rbx)
jne 0x5ca2e
subq $0x8, %rsp
pushq $0x2
popq %rsi
pushq $0x4
popq %rdx
pushq $0xe
popq %rcx
pushq $0x3
popq %r9
movq %r14, %rdi
xorl %r8d, %r8d
pushq %rbx
pushq $0x34
pushq $0x3
callq 0x4ced7
addq $0x20, %rsp
cmpl $0x0, (%rbx)
jne 0x5ca2e
pushq $0xe
popq %rdx
pushq $0x3
popq %r8
movq %r14, %rdi
movl %edx, %esi
xorl %ecx, %ecx
movq %r8, %r9
pushq %rbx
pushq $0x1f
callq 0x4d357
popq %rax
popq %rcx
cmpl $0x0, (%rbx)
jne 0x5ca2e
pushq $0x13
popq %rsi
movq %r14, %rdi
movq %rbx, %rdx
callq 0x4c92b
cmpl $0x0, (%rbx)
jne 0x5ca2e
movl $0xe, 0x8(%rax)
movq %r15, 0x10(%rax)
movq %r15, 0x18(%rax)
movl $0x1, 0x20(%rax)
movl $0x4, 0x28(%rax)
movl $0x10001, 0x48(%rax)    # imm = 0x10001
pushq $0x13
popq %rsi
movq %r14, %rdi
movq %rbx, %rdx
callq 0x4c92b
cmpl $0x0, (%rbx)
jne 0x5ca2e
movl $0xe, 0x8(%rax)
movq $0x3, 0x10(%rax)
movq $0x1, 0x18(%rax)
pushq $0x1
popq %rbp
movl %ebp, 0x20(%rax)
andl $0x0, 0x28(%rax)
movl %ebp, 0x48(%rax)
pushq $0x13
popq %rsi
movq %r14, %rdi
movq %rbx, %rdx
callq 0x4c92b
cmpl $0x0, (%rbx)
jne 0x5ca2e
movl $0xe, 0x8(%rax)
movq $0x4, 0x10(%rax)
movq $0x1, 0x18(%rax)
movl %ebp, 0x20(%rax)
movl $0x4, 0x28(%rax)
movl %ebp, 0x48(%rax)
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq