// File: /workspace/github_c_makefile_star2_O3/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
cmpq $0x0, 0x8(%rdi)
je 0x5a4d9
xorl %r15d, %r15d
leaq 0x1ba50(%rip), %r13     # 0x75e98
movq (%r12), %rax
movq (%rax,%r15,8), %rbp
cmpq %r14, %rbp
jae 0x5a5e8
testb $0x3, %bpl
je 0x5a478
movq %rbx, %rdi
movl $0x5d, %esi
movq %r13, %rdx
movl $0x7a, %ecx
callq 0x145a4
xorl %eax, %eax
jmp 0x5a47f
movq (%rsp), %rax
addq %rbp, %rax
cmpl $0x0, (%rbx)
jne 0x5a5d9
movl (%rax), %ecx
movl %ecx, %edx
andl $0xffffff, %edx         # imm = 0xFFFFFF
cmpq %rdx, 0x20(%r12)
jbe 0x5a601
movq 0x18(%r12), %rsi
movq (%rsi,%rdx,8), %rdx
cmpq $-0x1, %rdx
je 0x5a617
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 %r15
cmpq 0x8(%r12), %r15
jb 0x5a448
cmpq $0x0, 0x30(%r12)
je 0x5a55c
xorl %r15d, %r15d
xorl %ebp, %ebp
movq 0x28(%r12), %r13
movq 0x10(%r13,%r15), %rax
cmpq %r14, %rax
jae 0x5a62d
movq 0x10(%rsp), %rcx
cmpq %rcx, (%r13,%r15)
jae 0x5a643
testb $0x3, %al
je 0x5a52a
movq %rbx, %rdi
movl $0x5d, %esi
leaq 0x1b97c(%rip), %rdx     # 0x75e98
movl $0x7a, %ecx
callq 0x145a4
xorl %eax, %eax
jmp 0x5a52e
addq (%rsp), %rax
cmpl $0x0, (%rbx)
jne 0x5a5d9
movq (%r13,%r15), %rcx
movq 0x8(%rsp), %rdx
movl (%rdx,%rcx,8), %ecx
subl 0x8(%r13,%r15), %ecx
addl $-0xc, %ecx
movl %ecx, (%rax)
incq %rbp
addq $0x18, %r15
cmpq 0x30(%r12), %rbp
jb 0x5a4e6
cmpq $0x0, 0x48(%r12)
je 0x5a5d9
xorl %r13d, %r13d
xorl %ebp, %ebp
movq 0x40(%r12), %r15
movq 0x10(%r15,%r13), %rax
cmpq %r14, %rax
jae 0x5a659
movq (%r15,%r13), %rcx
cmpq 0x20(%r12), %rcx
jae 0x5a66f
testb $0x3, %al
je 0x5a5ac
movq %rbx, %rdi
movl $0x5d, %esi
leaq 0x1b8fa(%rip), %rdx     # 0x75e98
movl $0x7a, %ecx
callq 0x145a4
xorl %eax, %eax
jmp 0x5a5b0
addq (%rsp), %rax
cmpl $0x0, (%rbx)
jne 0x5a5d9
movq 0x18(%r12), %rcx
movq (%r15,%r13), %rdx
movl (%rcx,%rdx,8), %ecx
subl 0x8(%r15,%r13), %ecx
addl $-0xc, %ecx
movl %ecx, (%rax)
incq %rbp
addq $0x18, %r13
cmpq 0x48(%r12), %rbp
jb 0x5a569
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1b8a9(%rip), %rdx     # 0x75e98
movq %rbx, %rdi
movl $0xf, %esi
movl $0x8e, %ecx
jmp 0x5a683
leaq 0x1b890(%rip), %rdx     # 0x75e98
movq %rbx, %rdi
movl $0xf, %esi
movl $0x96, %ecx
jmp 0x5a683
leaq 0x1b87a(%rip), %rdx     # 0x75e98
movq %rbx, %rdi
movl $0xf, %esi
movl $0x9b, %ecx
jmp 0x5a683
leaq 0x1b864(%rip), %rdx     # 0x75e98
movq %rbx, %rdi
movl $0xf, %esi
movl $0xa5, %ecx
jmp 0x5a683
leaq 0x1b84e(%rip), %rdx     # 0x75e98
movq %rbx, %rdi
movl $0xf, %esi
movl $0xa9, %ecx
jmp 0x5a683
leaq 0x1b838(%rip), %rdx     # 0x75e98
movq %rbx, %rdi
movl $0xf, %esi
movl $0xb7, %ecx
jmp 0x5a683
leaq 0x1b822(%rip), %rdx     # 0x75e98
movq %rbx, %rdi
movl $0xf, %esi
movl $0xbb, %ecx
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x145a4