// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movl 0x14(%rdi), %eax
movq (%rsi), %rsi
xorl %edx, %edx
cmpq %rax, %rsi
cmovaq %rdx, %rsi
cmovbeq %r8, %rcx
subq %rsi, %rax
testq %r9, %r9
setne %sil
testb $0x40, 0x28(%rdi)
sete %r8b
xorb %sil, %r8b
movb %r8b, -0x1(%rsp)
movq (%rcx), %rcx
imulq $0x58, %rax, %rax
leaq (%rcx,%rax), %r8
addq $0x20, %r8
movq (%r8), %r9
xorl %ecx, %ecx
xorl %r10d, %r10d
cmpq %r9, %rdx
je 0x9edf4
movq 0x10(%r8), %r15
imulq $0x24, %rdx, %r11
movl 0xc(%rdi), %eax
movl 0x10(%rdi), %esi
movl 0x4(%r15,%r11), %r12d
movl 0x8(%r15,%r11), %ebp
cmpl %r12d, %eax
cmoval %eax, %r12d
cmpl %ebp, %esi
cmovbl %esi, %ebp
cmpl %ebp, %r12d
jae 0x9ed61
movl 0xc(%r15,%r11), %r13d
imulq $0x24, %r13, %rax
leaq (%r15,%rax), %rbx
movl 0x10(%r15,%r11), %esi
movl %esi, %r14d
subl %r13d, %r14d
movq %rdi, %r11
cmpl %r13d, %esi
je 0x9ed61
cmpl %r12d, 0x8(%rbx)
ja 0x9ed6c
addq $0x30, %r11
jmp 0x9ed50
movq %rdi, %r11
incq %rdx
movq %r11, %rdi
jmp 0x9ed03
addq %r15, %rax
addq $0x4, %rax
xorl %edi, %edi
cmpq %rdi, %r14
je 0x9ed8a
cmpl %ebp, (%rax)
jae 0x9ed87
incq %rdi
addq $0x24, %rax
jmp 0x9ed75
movq %rdi, %r14
testq %r14, %r14
je 0x9ed64
movl 0xc(%r11), %r15d
movl 0x4(%rbx), %ebp
cmpl %ebp, %r15d
cmoval %r15d, %ebp
movl 0x10(%r11), %r12d
imulq $0x24, %r14, %rdi
movl -0x1c(%rbx,%rdi), %eax
cmpl %eax, %r12d
cmovbl %r12d, %eax
cmpl %ebp, %eax
jbe 0x9ed64
addq %rbx, %rdi
movl %eax, %eax
addq $-0x24, %rdi
movl %ebp, %esi
subq %rax, %r12
subq %rsi, %rax
subq %r15, %rsi
cmpb $0x0, -0x1(%rsp)
cmoveq %rbx, %rdi
cmovneq %r12, %rsi
cmpq 0x38(%rsp), %rsi
setbe %sil
cmpq %r10, %rax
seta %bl
testb %bl, %sil
cmovneq %rdi, %rcx
cmovneq %rax, %r10
jmp 0x9ed64
testq %rcx, %rcx
je 0x9ee09
movl (%rcx), %eax
movl 0x20(%rcx), %ecx
shrl $0x5, %ecx
andl $0x1, %ecx
leal (%rcx,%rax,2), %eax
jmp 0x9ee13
movabsq $-0x1, %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq