// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/inter.cpp
// Function: get_chain_x(overlap_region*, long)
int64_t get_chain_x(overlap_region* ot, int64_t q)
{
	int64_t x, y, off, i, lx = -1, ly = -1; Fake_Cigar* o = &(ot->f_cigar);
	x = get_fake_gap_pos(o, o->length - 1); 
	off = get_fake_gap_shift(o, o->length - 1);
	y = x - ot->x_pos_s + ot->y_pos_s + off;
	// if(ot->x_id == 98 && (q == 6681 || q == 6990)) fprintf(stderr, "o->length->%u, q->%ld, y->%ld, x->%ld\n", o->length, q, y, x);
	if(y == q) return x;

	for (i = 0; i < (int64_t)o->length; i++){
		x = get_fake_gap_pos(o, i); off = get_fake_gap_shift(o, i);
		y = x - ot->x_pos_s + ot->y_pos_s + off;
		// if(ot->x_id == 98 && (q == 6681 || q == 6990)) fprintf(stderr, "+i->%ld, q->%ld, y->%ld, x->%ld\n", i, q, y, x);
		if(q < y) {
			lx = x; ly = y;
			break;
		}
	}

	if(i == 0 || i == (int64_t)o->length) {
        fprintf(stderr, "ERROR at %s:%d, x_id->%u, y_id->%u, q->%ld, i->%ld, yi_s->%u, yi_e->%u\n", 
		__FILE__, __LINE__, ot->x_id, ot->y_id, q, i, ot->y_pos_s, ot->y_pos_e);
        exit(0);
    }

	x = get_fake_gap_pos(o, i-1); off = get_fake_gap_shift(o, i-1);
	y = x - ot->x_pos_s + ot->y_pos_s + off;
	y = (((double)(q - y))/((double)(ly - y)))*((double)(lx -x)) + x;
	// y = q - y + x;
	if(y < ot->x_pos_s) y = ot->x_pos_s;
	if(y > ot->x_pos_e) y = ot->x_pos_e;
	return y;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x38(%rdi), %r15
movl 0x40(%rdi), %esi
decl %esi
movq %r15, %rdi
callq 0x11a00
movslq %eax, %r12
movl 0x40(%r14), %esi
decl %esi
movq %r15, %rdi
callq 0x11a0b
cltq
movl 0x4(%r14), %ecx
movl 0x14(%r14), %r10d
addq %r12, %rax
subq %rcx, %rax
addq %r10, %rax
cmpq %rbx, %rax
jne 0x160c6e
movq %r12, %rax
jmp 0x160d46
xorl %r12d, %r12d
movl 0x40(%r14), %eax
cmpq %rax, %r12
jae 0x160cb1
movq %r15, %rdi
movl %r12d, %esi
callq 0x11a00
movslq %eax, %r13
movq %r15, %rdi
movl %r12d, %esi
callq 0x11a0b
movslq %eax, %rbp
movl 0x4(%r14), %eax
movl 0x14(%r14), %r10d
addq %r13, %rbp
subq %rax, %rbp
addq %r10, %rbp
cmpq %rbx, %rbp
jg 0x160cb7
incq %r12
jmp 0x160c71
pushq $-0x1
popq %rbp
movq %rbp, %r13
testq %r12, %r12
je 0x160d55
movl 0x40(%r14), %eax
cmpq %r12, %rax
je 0x160d55
decl %r12d
movq %r15, %rdi
movl %r12d, %esi
callq 0x11a00
cltq
movq %rax, (%rsp)
movq %r15, %rdi
movl %r12d, %esi
callq 0x11a0b
movslq %eax, %rcx
movl 0x4(%r14), %eax
movl 0x14(%r14), %edx
movq (%rsp), %rsi
addq %rsi, %rcx
subq %rax, %rcx
addq %rdx, %rcx
subq %rcx, %rbx
cvtsi2sd %rbx, %xmm0
subq %rcx, %rbp
cvtsi2sd %rbp, %xmm1
divsd %xmm1, %xmm0
movq %rsi, %rcx
subq %rsi, %r13
cvtsi2sd %r13, %xmm1
cvtsi2sd %ecx, %xmm2
mulsd %xmm0, %xmm1
addsd %xmm1, %xmm2
cvttsd2si %xmm2, %rcx
movl 0x8(%r14), %edx
cmpq %rax, %rcx
cmovgq %rcx, %rax
cmpq %rdx, %rax
cmovgeq %rdx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xf2284(%rip), %rax     # 0x252fe0
movq (%rax), %rdi
movl (%r14), %r8d
movl 0x10(%r14), %r9d
movl 0x18(%r14), %r11d
leaq 0x91495(%rip), %rsi     # 0x1f2206
leaq 0x90dab(%rip), %rdx     # 0x1f1b23
movl $0xa66, %ecx            # imm = 0xA66
xorl %eax, %eax
pushq %r11
pushq %r10
pushq %r12
pushq %rbx
callq 0x22b0
addq $0x20, %rsp
xorl %edi, %edi
callq 0x2250