// File: chhylp123[P]hifiasm/Overlaps.cpp
// Function: get_ref_nid(u_trans_t*, ma_ug_t*, ma_ug_t*, scaf_res_t*, scaf_res_t*, unsigned long, unsigned long)
uint64_t get_ref_nid(u_trans_t *z, ma_ug_t *hu1, ma_ug_t *hu2, scaf_res_t *cp1, scaf_res_t *cp2, uint64_t is_beg, uint64_t gap_bd)
{
    scaf_res_t *sc = NULL; uint64_t id, s_end, e_end;
    if(z->tn < hu1->u.n) {
        sc = cp1; id = z->tn;
    } else {
        sc = cp2; id = z->tn - hu1->u.n;
    }

    if(is_beg) {
        s_end = 0; e_end = 1;
    } else {
        s_end = 1; e_end = 0;
    }

    if(!(z->rev)) {
        s_end ^= 1; e_end ^= 1;
    }


    ul_vec_t *idx = &(sc->a[id]); uint64_t k, m, a_n, os, oe, m0; 
    uc_block_t *a, *am = NULL; uint64_t max_len = 0, cgap, clen; 
    
    for (k = 0; k < idx->bb.n; k++) {
        a = idx->bb.a + idx->bb.a[k].ts; a_n = idx->bb.a[k].te - idx->bb.a[k].ts;
        os = MAX(z->ts, idx->bb.a[k].qs); 
        oe = MIN(z->te, idx->bb.a[k].qe);
        if(os >= oe) continue;
        for(m = 0; m < a_n && a[m].qe <= os; z++);
        if(m >= a_n) continue;
        for(m0 = m; m < a_n && a[m].qs < oe; m++);
        if(m <= m0) continue;

        os = MAX(z->ts, a[m0].qs); 
        oe = MIN(z->te, a[m-1].qe);
        if(oe <= os) continue;
        clen = oe - os; cgap = 0;
        if(s_end) {
            cgap = os - z->ts;
        } else if(e_end) {
            cgap = z->te - oe;
        }
        if(cgap > gap_bd) continue;
        if(clen > max_len) {
            max_len = clen; am = ((s_end)?(&(a[m0])):(&(a[m-1])));
        }
    }

    if(!am) return (uint64_t)-1;
    return ((uint64_t)(am->hid<<1))|((uint64_t)(!!(am->rev))); 
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x58, %rsp
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x10(%rbp), %rax
movl 0x14(%rax), %eax
movq -0x18(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x128d81
movq -0x28(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movl 0x14(%rax), %eax
movq %rax, -0x48(%rbp)
jmp 0x128d9b
movq -0x30(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movl 0x14(%rax), %eax
movq -0x18(%rbp), %rcx
subq (%rcx), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x128db4
movq $0x0, -0x50(%rbp)
movq $0x1, -0x58(%rbp)
jmp 0x128dc4
movq $0x1, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x10(%rbp), %rax
movb 0x28(%rax), %al
shrb $0x6, %al
andb $0x1, %al
cmpb $0x0, %al
jne 0x128dec
movq -0x50(%rbp), %rax
xorq $0x1, %rax
movq %rax, -0x50(%rbp)
movq -0x58(%rbp), %rax
xorq $0x1, %rax
movq %rax, -0x58(%rbp)
movq -0x40(%rbp), %rax
movq (%rax), %rax
imulq $0x58, -0x48(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movq $0x0, -0xa0(%rbp)
movq $0x0, -0xa8(%rbp)
movq $0x0, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq -0x60(%rbp), %rcx
cmpq 0x20(%rcx), %rax
jae 0x1291b5
movq -0x60(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x60(%rbp), %rcx
movq 0x30(%rcx), %rcx
imulq $0x24, -0x68(%rbp), %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x98(%rbp)
movq -0x60(%rbp), %rax
movq 0x30(%rax), %rax
imulq $0x24, -0x68(%rbp), %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
movq -0x60(%rbp), %rcx
movq 0x30(%rcx), %rcx
imulq $0x24, -0x68(%rbp), %rdx
addq %rdx, %rcx
subl 0xc(%rcx), %eax
movl %eax, %eax
movq %rax, -0x78(%rbp)
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
movq -0x60(%rbp), %rcx
movq 0x30(%rcx), %rcx
imulq $0x24, -0x68(%rbp), %rdx
addq %rdx, %rcx
cmpl 0x4(%rcx), %eax
jb 0x128eaf
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0xbc(%rbp)
jmp 0x128ec8
movq -0x60(%rbp), %rax
movq 0x30(%rax), %rax
imulq $0x24, -0x68(%rbp), %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, -0xbc(%rbp)
movl -0xbc(%rbp), %eax
movl %eax, %eax
movq %rax, -0x80(%rbp)
movq -0x10(%rbp), %rax
movl 0x10(%rax), %eax
movq -0x60(%rbp), %rcx
movq 0x30(%rcx), %rcx
imulq $0x24, -0x68(%rbp), %rdx
addq %rdx, %rcx
cmpl 0x8(%rcx), %eax
ja 0x128eff
movq -0x10(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0xc0(%rbp)
jmp 0x128f18
movq -0x60(%rbp), %rax
movq 0x30(%rax), %rax
imulq $0x24, -0x68(%rbp), %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, -0xc0(%rbp)
movl -0xc0(%rbp), %eax
movl %eax, %eax
movq %rax, -0x88(%rbp)
movq -0x80(%rbp), %rax
cmpq -0x88(%rbp), %rax
jb 0x128f39
jmp 0x1291a4
movq $0x0, -0x70(%rbp)
movq -0x70(%rbp), %rcx
xorl %eax, %eax
cmpq -0x78(%rbp), %rcx
movb %al, -0xc1(%rbp)
jae 0x128f72
movq -0x98(%rbp), %rax
imulq $0x24, -0x70(%rbp), %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
cmpq -0x80(%rbp), %rax
setbe %al
movb %al, -0xc1(%rbp)
movb -0xc1(%rbp), %al
testb $0x1, %al
jne 0x128f7e
jmp 0x128f8e
jmp 0x128f80
movq -0x10(%rbp), %rax
addq $0x30, %rax
movq %rax, -0x10(%rbp)
jmp 0x128f41
movq -0x70(%rbp), %rax
cmpq -0x78(%rbp), %rax
jb 0x128f9d
jmp 0x1291a4
movq -0x70(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x70(%rbp), %rcx
xorl %eax, %eax
cmpq -0x78(%rbp), %rcx
movb %al, -0xc2(%rbp)
jae 0x128fdc
movq -0x98(%rbp), %rax
imulq $0x24, -0x70(%rbp), %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
cmpq -0x88(%rbp), %rax
setb %al
movb %al, -0xc2(%rbp)
movb -0xc2(%rbp), %al
testb $0x1, %al
jne 0x128fe8
jmp 0x128ff8
jmp 0x128fea
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x128fa8
movq -0x70(%rbp), %rax
cmpq -0x90(%rbp), %rax
ja 0x12900a
jmp 0x1291a4
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
movq -0x98(%rbp), %rcx
imulq $0x24, -0x90(%rbp), %rdx
addq %rdx, %rcx
cmpl 0x4(%rcx), %eax
jb 0x129037
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0xc8(%rbp)
jmp 0x129052
movq -0x98(%rbp), %rax
imulq $0x24, -0x90(%rbp), %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, -0xc8(%rbp)
movl -0xc8(%rbp), %eax
movl %eax, %eax
movq %rax, -0x80(%rbp)
movq -0x10(%rbp), %rax
movl 0x10(%rax), %eax
movq -0x98(%rbp), %rcx
movq -0x70(%rbp), %rdx
subq $0x1, %rdx
imulq $0x24, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x8(%rcx), %eax
ja 0x12908f
movq -0x10(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0xcc(%rbp)
jmp 0x1290ae
movq -0x98(%rbp), %rax
movq -0x70(%rbp), %rcx
subq $0x1, %rcx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, -0xcc(%rbp)
movl -0xcc(%rbp), %eax
movl %eax, %eax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
cmpq -0x80(%rbp), %rax
ja 0x1290cf
jmp 0x1291a4
movq -0x88(%rbp), %rax
subq -0x80(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq $0x0, -0xb0(%rbp)
cmpq $0x0, -0x50(%rbp)
je 0x12910a
movq -0x80(%rbp), %rax
movq -0x10(%rbp), %rcx
movl 0xc(%rcx), %ecx
subq %rcx, %rax
movq %rax, -0xb0(%rbp)
jmp 0x129128
cmpq $0x0, -0x58(%rbp)
je 0x129126
movq -0x10(%rbp), %rax
movl 0x10(%rax), %eax
subq -0x88(%rbp), %rax
movq %rax, -0xb0(%rbp)
jmp 0x129128
movq -0xb0(%rbp), %rax
cmpq 0x10(%rbp), %rax
jbe 0x129137
jmp 0x1291a4
movq -0xb8(%rbp), %rax
cmpq -0xa8(%rbp), %rax
jbe 0x1291a2
movq -0xb8(%rbp), %rax
movq %rax, -0xa8(%rbp)
cmpq $0x0, -0x50(%rbp)
je 0x129177
movq -0x98(%rbp), %rax
imulq $0x24, -0x90(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0xd8(%rbp)
jmp 0x129194
movq -0x98(%rbp), %rax
movq -0x70(%rbp), %rcx
subq $0x1, %rcx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0xa0(%rbp)
jmp 0x1291a4
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x128e1d
cmpq $0x0, -0xa0(%rbp)
jne 0x1291c9
movq $-0x1, -0x8(%rbp)
jmp 0x1291ff
movq -0xa0(%rbp), %rax
movl (%rax), %eax
shll %eax
movl %eax, %eax
movq -0xa0(%rbp), %rcx
movb 0x20(%rcx), %cl
shrb $0x5, %cl
andb $0x1, %cl
cmpb $0x0, %cl
setne %cl
xorb $-0x1, %cl
xorb $-0x1, %cl
andb $0x1, %cl
movzbl %cl, %ecx
orq %rcx, %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x58, %rsp
popq %rbp
retq
nopl (%rax)