// File: /workspace/github_cpp_makefile_star2_O3/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), %r12
movl 0x40(%rdi), %esi
decl %esi
movq %r12, %rdi
callq 0x3d110
movslq %eax, %r15
movl 0x40(%r14), %esi
decl %esi
movq %r12, %rdi
callq 0x3d11b
cltq
movl 0x4(%r14), %ecx
movl 0x14(%r14), %r10d
addq %r15, %rax
subq %rcx, %rax
addq %r10, %rax
cmpq %rbx, %rax
jne 0x1a89e6
movq %r15, %rax
jmp 0x1a8ad4
cmpl $0x0, 0x40(%r14)
je 0x1a8ae3
xorl %r15d, %r15d
movq %r12, %rdi
movl %r15d, %esi
callq 0x3d110
movslq %eax, %r13
movq %r12, %rdi
movl %r15d, %esi
callq 0x3d11b
movslq %eax, %rbp
movl 0x4(%r14), %eax
movl 0x14(%r14), %r10d
addq %r13, %rbp
subq %rax, %rbp
addq %r10, %rbp
cmpq %rbx, %rbp
jg 0x1a8a42
incq %r15
movl 0x40(%r14), %eax
cmpq %rax, %r15
jb 0x1a89f4
movq $-0x1, %rbp
movq $-0x1, %r13
jmp 0x1a8a4b
testq %r15, %r15
je 0x1a8ae3
movl 0x40(%r14), %eax
cmpq %rax, %r15
je 0x1a8ae6
decl %r15d
movq %r12, %rdi
movl %r15d, %esi
callq 0x3d110
cltq
movq %rax, (%rsp)
movq %r12, %rdi
movl %r15d, %esi
callq 0x3d11b
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
xorps %xmm1, %xmm1
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
xorl %r15d, %r15d
movq 0xec4f3(%rip), %rax     # 0x294fe0
movq (%rax), %rdi
movl (%r14), %r8d
movl 0x10(%r14), %r9d
movl 0x18(%r14), %r11d
leaq 0x9783c(%rip), %rsi     # 0x24033e
leaq 0x97152(%rip), %rdx     # 0x23fc5b
movl $0xa66, %ecx            # imm = 0xA66
xorl %eax, %eax
pushq %r11
pushq %r10
pushq %r15
pushq %rbx
callq 0x22b0
addq $0x20, %rsp
xorl %edi, %edi
callq 0x2250