// File: 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
movq %rsp, %rbp
pushq %rbx
subq $0x78, %rsp
movq %rdi, -0x18(%rbp)
movq %rsi, -0x20(%rbp)
movq $-0x1, -0x48(%rbp)
movq $-0x1, -0x50(%rbp)
movq -0x18(%rbp), %rax
addq $0x38, %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rdi
movq -0x58(%rbp), %rax
movl 0x8(%rax), %esi
subl $0x1, %esi
callq 0x1e320
cltq
movq %rax, -0x28(%rbp)
movq -0x58(%rbp), %rdi
movq -0x58(%rbp), %rax
movl 0x8(%rax), %esi
subl $0x1, %esi
callq 0x1e340
cltq
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rax
movq -0x18(%rbp), %rcx
movl 0x4(%rcx), %ecx
subq %rcx, %rax
movq -0x18(%rbp), %rcx
movl 0x14(%rcx), %ecx
addq %rcx, %rax
addq -0x38(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x20(%rbp), %rax
jne 0x28da56
movq -0x28(%rbp), %rax
movq %rax, -0x10(%rbp)
jmp 0x28dc33
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq -0x58(%rbp), %rcx
movl 0x8(%rcx), %ecx
cmpq %rcx, %rax
jge 0x28dae7
movq -0x58(%rbp), %rdi
movq -0x40(%rbp), %rax
movl %eax, %esi
callq 0x1e320
cltq
movq %rax, -0x28(%rbp)
movq -0x58(%rbp), %rdi
movq -0x40(%rbp), %rax
movl %eax, %esi
callq 0x1e340
cltq
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rax
movq -0x18(%rbp), %rcx
movl 0x4(%rcx), %ecx
subq %rcx, %rax
movq -0x18(%rbp), %rcx
movl 0x14(%rcx), %ecx
addq %rcx, %rax
addq -0x38(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x20(%rbp), %rax
cmpq -0x30(%rbp), %rax
jge 0x28dad4
movq -0x28(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x50(%rbp)
jmp 0x28dae7
jmp 0x28dad6
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x28da5e
cmpq $0x0, -0x40(%rbp)
je 0x28dafe
movq -0x40(%rbp), %rax
movq -0x58(%rbp), %rcx
movl 0x8(%rcx), %ecx
cmpq %rcx, %rax
jne 0x28db61
movq 0x1364db(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
movq -0x18(%rbp), %rax
movl (%rax), %r8d
movq -0x18(%rbp), %rax
movl 0x10(%rax), %r9d
movq -0x20(%rbp), %rbx
movq -0x40(%rbp), %r11
movq -0x18(%rbp), %rax
movl 0x14(%rax), %r10d
movq -0x18(%rbp), %rax
movl 0x18(%rax), %eax
leaq 0x1033dd(%rip), %rsi    # 0x390f12
leaq 0x102cf3(%rip), %rdx    # 0x39082f
movl $0xa66, %ecx            # imm = 0xA66
movq %rbx, (%rsp)
movq %r11, 0x8(%rsp)
movl %r10d, 0x10(%rsp)
movl %eax, 0x18(%rsp)
movb $0x0, %al
callq 0x22a0
xorl %edi, %edi
callq 0x2240
movq -0x58(%rbp), %rdi
movq -0x40(%rbp), %rax
subq $0x1, %rax
movl %eax, %esi
callq 0x1e320
cltq
movq %rax, -0x28(%rbp)
movq -0x58(%rbp), %rdi
movq -0x40(%rbp), %rax
subq $0x1, %rax
movl %eax, %esi
callq 0x1e340
cltq
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rax
movq -0x18(%rbp), %rcx
movl 0x4(%rcx), %ecx
subq %rcx, %rax
movq -0x18(%rbp), %rcx
movl 0x14(%rcx), %ecx
addq %rcx, %rax
addq -0x38(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x20(%rbp), %rax
subq -0x30(%rbp), %rax
cvtsi2sd %rax, %xmm0
movq -0x50(%rbp), %rax
subq -0x30(%rbp), %rax
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
movq -0x48(%rbp), %rax
subq -0x28(%rbp), %rax
cvtsi2sd %rax, %xmm2
cvtsi2sdq -0x28(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq -0x18(%rbp), %rcx
movl 0x4(%rcx), %ecx
cmpq %rcx, %rax
jge 0x28dc10
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq -0x18(%rbp), %rcx
movl 0x8(%rcx), %ecx
cmpq %rcx, %rax
jle 0x28dc2b
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
addq $0x78, %rsp
popq %rbx
popq %rbp
retq
nop