// File: /workspace/github_cpp_makefile_star2_O3/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), %rdx
xorl %esi, %esi
cmpq %rax, %rdx
cmovbeq %rdx, %rsi
cmovbeq %r8, %rcx
subq %rsi, %rax
testq %r9, %r9
setne %dl
testb $0x40, 0x28(%rdi)
sete %sil
movq (%rcx), %r8
imulq $0x58, %rax, %r9
movq 0x20(%r8,%r9), %rcx
movq $-0x1, %rax
testq %rcx, %rcx
je 0xdc4f9
movq 0x38(%rsp), %rax
xorb %sil, %dl
leaq (%r8,%r9), %rsi
addq $0x20, %rsi
movq 0x10(%rsi), %r8
movl 0xc(%rdi), %r9d
movl 0x10(%rdi), %edi
leaq 0x4(%r8), %r11
xorl %ebx, %ebx
xorl %r10d, %r10d
xorl %r14d, %r14d
leaq (%r14,%r14,8), %rsi
movl 0x4(%r8,%rsi,4), %r15d
movl 0x8(%r8,%rsi,4), %ebp
cmpl %r15d, %r9d
cmoval %r9d, %r15d
cmpl %ebp, %edi
cmovbl %edi, %ebp
cmpl %ebp, %r15d
jae 0xdc4d1
movl 0xc(%r8,%rsi,4), %r13d
movl 0x10(%r8,%rsi,4), %r12d
cmpl %r13d, %r12d
je 0xdc4d1
leaq (,%r13,8), %rsi
addq %r13, %rsi
leaq (%r8,%rsi,4), %r15
subl %r13d, %r12d
cmpl $0x1, %r12d
adcl $0x0, %r12d
leaq (%r11,%rsi,4), %rsi
xorl %r13d, %r13d
cmpl %ebp, (%rsi)
jae 0xdc46c
incq %r13
addq $0x24, %rsi
cmpq %r13, %r12
jne 0xdc457
movq %r12, %r13
jmp 0xdc471
testq %r13, %r13
je 0xdc4d1
movl 0x4(%r15), %esi
cmpl %esi, %r9d
cmoval %r9d, %esi
leaq (,%r13,8), %r12
addq %r13, %r12
movl -0x1c(%r15,%r12,4), %ebp
cmpl %ebp, %edi
cmovbl %edi, %ebp
cmpl %esi, %ebp
jbe 0xdc4d1
leaq (%r15,%r12,4), %r12
movl %ebp, %r13d
addq $-0x24, %r12
movl %esi, %esi
movq %rdi, %rbp
subq %r13, %rbp
subq %rsi, %r13
subq %r9, %rsi
testb %dl, %dl
cmoveq %r15, %r12
cmovneq %rbp, %rsi
cmpq %rax, %rsi
setbe %sil
cmpq %rbx, %r13
seta %bpl
testb %bpl, %sil
cmovneq %r12, %r10
cmovneq %r13, %rbx
incq %r14
cmpq %rcx, %r14
jne 0xdc400
testq %r10, %r10
movq $-0x1, %rax
je 0xdc4f9
movl (%r10), %eax
movl 0x20(%r10), %ecx
shrl $0x5, %ecx
andl $0x1, %ecx
leal (%rcx,%rax,2), %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq