// File: /workspace/github_c_makefile_star2_O0/markdryan[P]subtilis/arch/arm32/arm_encode.c
// Function: prv_apply_back_patches
static void prv_apply_back_patches(subtilis_arm_encode_ud_t *ud,
				   subtilis_error_t *err)
{
	subtilis_arm_encode_bp_t *bp;
	size_t i;
	int32_t dist;
	uint32_t *ptr;
	uint32_t encoded;

	for (i = 0; i < ud->back_patch_count; i++) {
		bp = &ud->back_patches[i];
		ptr = prv_get_word_ptr(ud, bp->code_index, err);
		if (err->type != SUBTILIS_ERROR_OK)
			return;
		switch (bp->type) {
		case SUBTILIS_ARM_ENCODE_BP_TYPE_BRANCH:
			dist = prv_compute_dist(ud->label_offsets[bp->label],
						bp->code_index, (1 << 23), err);
			if (err->type != SUBTILIS_ERROR_OK)
				return;
			dist = (dist / 4) - 2;
			dist &= 0xffffff;
			*ptr |= dist;
			break;
		case SUBTILIS_ARM_ENCODE_BP_TYPE_ADR:
			dist =
			    prv_compute_dist(ud->label_offsets[bp->label],
					     bp->code_index, 0xffffffff, err);
			if (err->type != SUBTILIS_ERROR_OK)
				return;

			dist -= 8;
			if (dist < 0) {
				/*
				 * Here we switch the ADD for a SUB.
				 */

				*ptr &= ~(1 << 23);
				*ptr |= 1 << 22;
				dist = -dist;
			}

			if (!subtilis_arm_encode_imm(dist, &encoded)) {
				/* TODO: Need a proper error for this */
				subtilis_error_set_ass_bad_adr(err);
				return;
			}

			*ptr |= encoded;
			break;
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq -0x8(%rbp), %rcx
cmpq 0x70(%rcx), %rax
jae 0x8198d
movq -0x8(%rbp), %rax
movq 0x68(%rax), %rax
imulq $0x18, -0x20(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x10(%rbp), %rdx
callq 0x82640
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, (%rax)
je 0x81855
jmp 0x8198d
movq -0x18(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x38(%rbp)
testl %eax, %eax
je 0x81871
jmp 0x81864
movl -0x38(%rbp), %eax
subl $0x1, %eax
je 0x818d8
jmp 0x8197a
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x18(%rbp), %rcx
movq 0x8(%rcx), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x10(%rbp), %rcx
movl $0x800000, %edx         # imm = 0x800000
callq 0x826a0
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, (%rax)
je 0x818ac
jmp 0x8198d
movl -0x24(%rbp), %eax
movl $0x4, %ecx
cltd
idivl %ecx
subl $0x2, %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
andl $0xffffff, %eax         # imm = 0xFFFFFF
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %ecx
movq -0x30(%rbp), %rax
orl (%rax), %ecx
movl %ecx, (%rax)
jmp 0x8197a
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x18(%rbp), %rcx
movq 0x8(%rcx), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x10(%rbp), %rcx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x826a0
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, (%rax)
je 0x81910
jmp 0x8198d
movl -0x24(%rbp), %eax
subl $0x8, %eax
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jge 0x81943
movq -0x30(%rbp), %rax
movl (%rax), %ecx
andl $0xff7fffff, %ecx       # imm = 0xFF7FFFFF
movl %ecx, (%rax)
movq -0x30(%rbp), %rax
movl (%rax), %ecx
orl $0x400000, %ecx         # imm = 0x400000
movl %ecx, (%rax)
xorl %eax, %eax
subl -0x24(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %edi
leaq -0x34(%rbp), %rsi
callq 0x70e60
testb $0x1, %al
jne 0x8196f
movq -0x10(%rbp), %rdi
movl $0x61, %esi
leaq 0x2b4cd(%rip), %rdx     # 0xace30
movl $0x94e, %ecx            # imm = 0x94E
callq 0x158e0
jmp 0x8198d
movl -0x34(%rbp), %ecx
movq -0x30(%rbp), %rax
orl (%rax), %ecx
movl %ecx, (%rax)
jmp 0x8197c
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x81808
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)