// File: /workspace/github_c_makefile_star2_O2/markdryan[P]subtilis/arch/arm32/arm_link.c
// Function: subtilis_arm_link_link
void subtilis_arm_link_link(subtilis_arm_link_t *link, uint8_t *buf,
			    size_t buf_size, const size_t *raw_constants,
			    size_t num_raw_constants, subtilis_error_t *err)
{
	size_t i;
	size_t si;
	size_t index;
	uint32_t *ptr;
	subtilis_arm_link_constant_t *cnst;

	for (i = 0; i < link->num_externals; i++) {
		index = link->externals[i];
		if (index >= buf_size) {
			subtilis_error_set_assertion_failed(err);
			return;
		}
		ptr = prv_get_word_ptr(buf, index, err);
		if (err->type != SUBTILIS_ERROR_OK)
			return;
		si = *ptr & 0xffffff;
		if (si >= link->num_sections) {
			subtilis_error_set_assertion_failed(err);
			return;
		}
		if (link->sections[si] == SIZE_MAX) {
			/* TODO: Need proper error here */
			subtilis_error_set_assertion_failed(err);
			return;
		}
		*ptr &= 0xff000000;
		*ptr |= ((link->sections[si] - (index + 8)) / 4) & 0xffffff;
	}

	for (i = 0; i < link->num_constants; i++) {
		cnst = &link->constants[i];
		if (cnst->constant_offset >= buf_size) {
			subtilis_error_set_assertion_failed(err);
			return;
		}
		if (cnst->index >= num_raw_constants) {
			subtilis_error_set_assertion_failed(err);
			return;
		}
		ptr = prv_get_word_ptr(buf, cnst->constant_offset, err);
		if (err->type != SUBTILIS_ERROR_OK)
			return;
		*ptr = (raw_constants[cnst->index] - cnst->code_index);
		/* Adjust for PC relative addressing. */
		*ptr -= 12;
	}

	for (i = 0; i < link->num_extrefs; i++) {
		cnst = &link->extrefs[i];
		if (cnst->constant_offset >= buf_size) {
			subtilis_error_set_assertion_failed(err);
			return;
		}
		if (cnst->index >= link->num_sections) {
			subtilis_error_set_assertion_failed(err);
			return;
		}
		ptr = prv_get_word_ptr(buf, cnst->constant_offset, err);
		if (err->type != SUBTILIS_ERROR_OK)
			return;
		*ptr = ((link->sections[cnst->index] - cnst->code_index) - 12);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %r9, %rbx
movq %r8, 0x10(%rsp)
movq %rcx, 0x8(%rsp)
movq %rdx, %r14
movq %rsi, (%rsp)
movq %rdi, %r12
xorl %r13d, %r13d
cmpq 0x8(%r12), %r13
jae 0x56576
movq (%r12), %rax
movq (%rax,%r13,8), %rbp
cmpq %r14, %rbp
jae 0x565dc
movq (%rsp), %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x566ea
cmpl $0x0, (%rbx)
jne 0x5667a
movl (%rax), %ecx
movl %ecx, %edx
andl $0xffffff, %edx         # imm = 0xFFFFFF
cmpq %rdx, 0x20(%r12)
jbe 0x565f3
movq 0x18(%r12), %rsi
movq (%rsi,%rdx,8), %rdx
cmpq $-0x1, %rdx
je 0x5660a
andl $0xff000000, %ecx       # imm = 0xFF000000
subl %ebp, %edx
addl $0x3fffff8, %edx        # imm = 0x3FFFFF8
shrl $0x2, %edx
andl $0xffffff, %edx         # imm = 0xFFFFFF
orl %ecx, %edx
movl %edx, (%rax)
incq %r13
jmp 0x564fe
xorl %r13d, %r13d
xorl %ebp, %ebp
cmpq 0x30(%r12), %rbp
jae 0x56621
movq 0x28(%r12), %r15
movq 0x10(%r15,%r13), %rsi
cmpq %r14, %rsi
jae 0x56689
movq 0x10(%rsp), %rax
cmpq %rax, (%r15,%r13)
jae 0x5669d
movq (%rsp), %rdi
movq %rbx, %rdx
callq 0x566ea
cmpl $0x0, (%rbx)
jne 0x5667a
movq (%r15,%r13), %rcx
movq 0x8(%rsp), %rdx
movl (%rdx,%rcx,8), %ecx
subl 0x8(%r15,%r13), %ecx
addl $-0xc, %ecx
movl %ecx, (%rax)
incq %rbp
addq $0x18, %r13
jmp 0x5657b
leaq 0x19825(%rip), %rdx     # 0x6fe08
pushq $0xf
popq %rsi
movq %rbx, %rdi
movl $0x8e, %ecx
jmp 0x566d7
leaq 0x1980e(%rip), %rdx     # 0x6fe08
pushq $0xf
popq %rsi
movq %rbx, %rdi
movl $0x96, %ecx
jmp 0x566d7
leaq 0x197f7(%rip), %rdx     # 0x6fe08
pushq $0xf
popq %rsi
movq %rbx, %rdi
movl $0x9b, %ecx
jmp 0x566d7
xorl %r13d, %r13d
xorl %r15d, %r15d
cmpq 0x48(%r12), %r15
jae 0x5667a
movq 0x40(%r12), %rbp
movq 0x10(%rbp,%r13), %rsi
cmpq %r14, %rsi
jae 0x566b1
movq (%rbp,%r13), %rax
cmpq 0x20(%r12), %rax
jae 0x566c5
movq (%rsp), %rdi
movq %rbx, %rdx
callq 0x566ea
cmpl $0x0, (%rbx)
jne 0x5667a
movq 0x18(%r12), %rcx
movq (%rbp,%r13), %rdx
movl (%rcx,%rdx,8), %ecx
subl 0x8(%rbp,%r13), %ecx
addl $-0xc, %ecx
movl %ecx, (%rax)
incq %r15
addq $0x18, %r13
jmp 0x56627
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x19778(%rip), %rdx     # 0x6fe08
pushq $0xf
popq %rsi
movq %rbx, %rdi
movl $0xa5, %ecx
jmp 0x566d7
leaq 0x19764(%rip), %rdx     # 0x6fe08
pushq $0xf
popq %rsi
movq %rbx, %rdi
movl $0xa9, %ecx
jmp 0x566d7
leaq 0x19750(%rip), %rdx     # 0x6fe08
pushq $0xf
popq %rsi
movq %rbx, %rdi
movl $0xb7, %ecx
jmp 0x566d7
leaq 0x1973c(%rip), %rdx     # 0x6fe08
pushq $0xf
popq %rsi
movq %rbx, %rdi
movl $0xbb, %ecx
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1429e