// File: /workspace/github_cpp_makefile_star2_O1/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
movq %rdi, %r10
movl 0x14(%rdi), %r11d
movq (%rsi), %rdx
xorl %eax, %eax
cmpq %r11, %rdx
cmovaq %rax, %rdx
cmovbeq %r8, %rcx
subq %rdx, %r11
testq %r9, %r9
setne %dl
testb $0x40, 0x28(%rdi)
sete %sil
movq (%rcx), %rdi
imulq $0x58, %r11, %r8
movq 0x20(%rdi,%r8), %rcx
testq %rcx, %rcx
je 0xdb0ef
xorb %sil, %dl
leaq (%rdi,%r8), %rax
addq $0x20, %rax
movq 0x10(%rax), %r8
leaq 0x4(%r8), %rax
movq %rax, -0x8(%rsp)
movq $0x0, -0x10(%rsp)
xorl %eax, %eax
xorl %r11d, %r11d
leaq (%r11,%r11,8), %rsi
movl 0xc(%r10), %edi
movl 0x10(%r10), %r9d
movl 0x4(%r8,%rsi,4), %r15d
movl 0x8(%r8,%rsi,4), %ebp
cmpl %r15d, %edi
cmoval %edi, %r15d
cmpl %ebp, %r9d
cmovbl %r9d, %ebp
cmpl %ebp, %r15d
jae 0xdb0e3
movl 0xc(%r8,%rsi,4), %r12d
movl 0x10(%r8,%rsi,4), %esi
cmpl %r12d, %esi
je 0xdb0e3
leaq (%r12,%r12,8), %r13
leaq (%r8,%r13,4), %rbx
movl %esi, %r14d
subl %r12d, %r14d
movl 0x8(%rbx), %r9d
movq %r10, %rdi
cmpl %r15d, %r9d
ja 0xdb03c
addq $0x30, %rdi
cmpl %r12d, %esi
jne 0xdb026
movq %rdi, %r10
jmp 0xdb0e3
cmpl %r12d, %esi
je 0xdb0e3
cmpl $0x1, %r14d
adcl $0x0, %r14d
movq -0x8(%rsp), %rsi
leaq (%rsi,%r13,4), %rsi
xorl %r13d, %r13d
cmpl %ebp, (%rsi)
jae 0xdb06c
incq %r13
addq $0x24, %rsi
cmpq %r13, %r14
jne 0xdb059
movq %r14, %r13
testq %r13, %r13
je 0xdb0e3
movl 0xc(%r10), %ebp
movl 0x4(%rbx), %r15d
cmpl %r15d, %ebp
cmoval %ebp, %r15d
movl 0x10(%r10), %r12d
leaq (,%r13,8), %rsi
addq %r13, %rsi
movl -0x1c(%rbx,%rsi,4), %r14d
cmpl %r14d, %r12d
cmovbl %r12d, %r14d
cmpl %r15d, %r14d
jbe 0xdb0e3
testb %dl, %dl
je 0xdb0b0
leaq -0x24(%rbx,%rsi,4), %rbx
je 0xdb0b8
subq %r14, %r12
jmp 0xdb0bb
movq %r15, %r12
subq %rbp, %r12
jmp 0xdb0bb
xorl %r12d, %r12d
subq %r15, %r14
cmpq 0x38(%rsp), %r12
setbe %sil
movq -0x10(%rsp), %r9
cmpq %r9, %r14
seta %dil
testb %dil, %sil
cmovneq %rbx, %rax
cmovneq %r14, %r9
movq %r9, -0x10(%rsp)
incq %r11
cmpq %rcx, %r11
jne 0xdafd1
testq %rax, %rax
je 0xdb104
movl (%rax), %ecx
movl 0x20(%rax), %eax
shrl $0x5, %eax
andl $0x1, %eax
leal (%rax,%rcx,2), %eax
jmp 0xdb10b
movq $-0x1, %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq