// File: /workspace/github_c_makefile_star2_O1/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 0x5a424
xorl %r15d, %r15d
leaq 0x1bac5(%rip), %r13     # 0x75e58
movq (%r12), %rax
movq (%rax,%r15,8), %rbp
cmpq %r14, %rbp
jae 0x5a533
testb $0x3, %bpl
je 0x5a3c3
movq %rbx, %rdi
movl $0x5d, %esi
movq %r13, %rdx
movl $0x7a, %ecx
callq 0x145f4
xorl %eax, %eax
jmp 0x5a3ca
movq (%rsp), %rax
addq %rbp, %rax
cmpl $0x0, (%rbx)
jne 0x5a524
movl (%rax), %ecx
movl %ecx, %edx
andl $0xffffff, %edx         # imm = 0xFFFFFF
cmpq %rdx, 0x20(%r12)
jbe 0x5a54c
movq 0x18(%r12), %rsi
movq (%rsi,%rdx,8), %rdx
cmpq $-0x1, %rdx
je 0x5a562
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 0x5a393
cmpq $0x0, 0x30(%r12)
je 0x5a4a7
xorl %r15d, %r15d
xorl %ebp, %ebp
movq 0x28(%r12), %r13
movq 0x10(%r13,%r15), %rax
cmpq %r14, %rax
jae 0x5a578
movq 0x10(%rsp), %rcx
cmpq %rcx, (%r13,%r15)
jae 0x5a58e
testb $0x3, %al
je 0x5a475
movq %rbx, %rdi
movl $0x5d, %esi
leaq 0x1b9f1(%rip), %rdx     # 0x75e58
movl $0x7a, %ecx
callq 0x145f4
xorl %eax, %eax
jmp 0x5a479
addq (%rsp), %rax
cmpl $0x0, (%rbx)
jne 0x5a524
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 0x5a431
cmpq $0x0, 0x48(%r12)
je 0x5a524
xorl %r13d, %r13d
xorl %ebp, %ebp
movq 0x40(%r12), %r15
movq 0x10(%r15,%r13), %rax
cmpq %r14, %rax
jae 0x5a5a4
movq (%r15,%r13), %rcx
cmpq 0x20(%r12), %rcx
jae 0x5a5ba
testb $0x3, %al
je 0x5a4f7
movq %rbx, %rdi
movl $0x5d, %esi
leaq 0x1b96f(%rip), %rdx     # 0x75e58
movl $0x7a, %ecx
callq 0x145f4
xorl %eax, %eax
jmp 0x5a4fb
addq (%rsp), %rax
cmpl $0x0, (%rbx)
jne 0x5a524
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 0x5a4b4
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1b91e(%rip), %rdx     # 0x75e58
movq %rbx, %rdi
movl $0xf, %esi
movl $0x8e, %ecx
jmp 0x5a5ce
leaq 0x1b905(%rip), %rdx     # 0x75e58
movq %rbx, %rdi
movl $0xf, %esi
movl $0x96, %ecx
jmp 0x5a5ce
leaq 0x1b8ef(%rip), %rdx     # 0x75e58
movq %rbx, %rdi
movl $0xf, %esi
movl $0x9b, %ecx
jmp 0x5a5ce
leaq 0x1b8d9(%rip), %rdx     # 0x75e58
movq %rbx, %rdi
movl $0xf, %esi
movl $0xa5, %ecx
jmp 0x5a5ce
leaq 0x1b8c3(%rip), %rdx     # 0x75e58
movq %rbx, %rdi
movl $0xf, %esi
movl $0xa9, %ecx
jmp 0x5a5ce
leaq 0x1b8ad(%rip), %rdx     # 0x75e58
movq %rbx, %rdi
movl $0xf, %esi
movl $0xb7, %ecx
jmp 0x5a5ce
leaq 0x1b897(%rip), %rdx     # 0x75e58
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 0x145f4