// File: chhylp123[P]hifiasm/inter.cpp
// Function: adjust_utg_chain_qoffset(unsigned long*, long*, long*, long)
int64_t adjust_utg_chain_qoffset(uint64_t *r_srt, int64_t *r_pos, int64_t *q_pos, int64_t r_off)
{
	if(r_off < ((int64_t)(r_srt[0]>>32))) {
		// fprintf(stderr, "r_off:%ld, r_srt[0]:%ld\n", r_off, ((int64_t)(r_srt[0]>>32))); 
		return q_pos[(uint32_t)r_srt[0]]; ///have small chance
	}
	if(r_off >= ((int64_t)(r_srt[3]>>32))) {
		// fprintf(stderr, "r_off:%ld, r_srt[3]:%ld\n", r_off, ((int64_t)(r_srt[3]>>32))); 
		return q_pos[(uint32_t)r_srt[3]]; ///have small chance
	}
	int64_t k, rdis, qdis;
	for (k = 0; k < 3; k++) {
		if(r_off >= ((int64_t)(r_srt[k]>>32)) && r_off < ((int64_t)(r_srt[k+1]>>32))) break;
	}
	// if(k >= 3) {
	// 	if(rs >= ((int64_t)(r_srt[2]>>32)) && rs <= ((int64_t)(r_srt[3]>>32))) k = 2;
	// }
	if(k >= 3) {
		for (k = 0; k < 3; k++) {
			if(r_off >= ((int64_t)(r_srt[k]>>32)) && r_off <= ((int64_t)(r_srt[k+1]>>32))) break;
		}
	}
	assert(k < 3);

	qdis = q_pos[(uint32_t)r_srt[k+1]] - q_pos[(uint32_t)r_srt[k]]; 
	rdis = r_pos[(uint32_t)r_srt[k+1]] - r_pos[(uint32_t)r_srt[k]]; 
	if(qdis < 0) return -1;
	return q_pos[(uint32_t)r_srt[k]] + get_offset_adjust(r_off-r_pos[(uint32_t)r_srt[k]], rdis, qdis);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
shrq $0x20, %rcx
cmpq %rcx, %rax
jge 0x29a126
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0x8(%rbp)
jmp 0x29a2f8
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
shrq $0x20, %rcx
cmpq %rcx, %rax
jl 0x29a156
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0x8(%rbp)
jmp 0x29a2f8
movq $0x0, -0x30(%rbp)
cmpq $0x3, -0x30(%rbp)
jge 0x29a1aa
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
movq -0x30(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
cmpq %rcx, %rax
jl 0x29a19a
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
movq -0x30(%rbp), %rdx
movq 0x8(%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
cmpq %rcx, %rax
jge 0x29a19a
jmp 0x29a1aa
jmp 0x29a19c
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x29a15e
cmpq $0x3, -0x30(%rbp)
jl 0x29a207
movq $0x0, -0x30(%rbp)
cmpq $0x3, -0x30(%rbp)
jge 0x29a205
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
movq -0x30(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
cmpq %rcx, %rax
jl 0x29a1f5
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
movq -0x30(%rbp), %rdx
movq 0x8(%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
cmpq %rcx, %rax
jg 0x29a1f5
jmp 0x29a205
jmp 0x29a1f7
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x29a1b9
jmp 0x29a207
cmpq $0x3, -0x30(%rbp)
jge 0x29a210
jmp 0x29a22f
leaq 0xf776a(%rip), %rdi     # 0x391981
leaq 0xf6611(%rip), %rsi     # 0x39082f
movl $0x12a3, %edx           # imm = 0x12A3
leaq 0xf775d(%rip), %rcx     # 0x391987
callq 0x2130
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
movq -0x30(%rbp), %rdx
movq 0x8(%rcx,%rdx,8), %rcx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rdx
movq -0x30(%rbp), %rsi
movq (%rdx,%rsi,8), %rdx
movl %edx, %edx
subq (%rcx,%rdx,8), %rax
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
movq -0x30(%rbp), %rdx
movq 0x8(%rcx,%rdx,8), %rcx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
movq -0x18(%rbp), %rcx
movq -0x10(%rbp), %rdx
movq -0x30(%rbp), %rsi
movq (%rdx,%rsi,8), %rdx
movl %edx, %edx
subq (%rcx,%rdx,8), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x40(%rbp)
jge 0x29a2a2
movq $-0x1, -0x8(%rbp)
jmp 0x29a2f8
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
movq -0x30(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0x48(%rbp)
movq -0x28(%rbp), %rax
movq -0x18(%rbp), %rcx
movq -0x10(%rbp), %rdx
movq -0x30(%rbp), %rsi
movq (%rdx,%rsi,8), %rdx
movl %edx, %edx
subq (%rcx,%rdx,8), %rax
movl %eax, %edi
movq -0x38(%rbp), %rax
movl %eax, %esi
movq -0x40(%rbp), %rax
movl %eax, %edx
callq 0xc14d0
movl %eax, %ecx
movq -0x48(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)