// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Overlaps.cpp
// Function: cal_exact_ug_o(dedup_idx_t*, ma_utg_t*, unsigned long)
int64_t cal_exact_ug_o(dedup_idx_t *idx, ma_utg_t *u, uint64_t f)
{
    if(u->n <= 0) return INT32_MIN;
    uint64_t sv, *sa, sn, ev, *ea, en, si, ei, zn, *za, rev, k, nf = (uint64_t)-1, m, fn, nfn; ma_utg_t *z;
    if(f == FATHER) nf = MOTHER; if(f == MOTHER) nf = FATHER;
    sv = u->a[0]>>32; sa = idx->ra + idx->ridx[sv>>1]; sn = idx->ridx[(sv>>1)+1]-idx->ridx[sv>>1];
    ev = u->a[u->n-1]>>32; ea = idx->ra + idx->ridx[ev>>1]; en = idx->ridx[(ev>>1)+1] - idx->ridx[ev>>1];
    for (si = 0; si < sn; si++) {
        z = &(idx->ug->u.a[sa[si]>>32]);
        if((z->n == 0) || (z->m == 0) || (idx->ug->g->seq[sa[si]>>32].del)) continue;
        assert(((z->a[(uint32_t)sa[si]]>>32)>>1) == (sv>>1));
        if((z->a[(uint32_t)sa[si]]>>32) == sv) rev = 0;
        else rev = 1;
        for (ei = 0; ei < en; ei++) {
            if((idx->ug->u.a[ea[ei]>>32].n == 0) || (idx->ug->u.a[ea[ei]>>32].m == 0) || (idx->ug->g->seq[ea[ei]>>32].del)) continue;
            assert(((idx->ug->u.a[ea[ei]>>32].a[(uint32_t)ea[ei]]>>32)>>1) == (ev>>1));
            if((sa[si]>>32) != (ea[ei]>>32)) continue;///not the same uid            
            if(((uint32_t)sa[si]) >= ((uint32_t)ea[ei])) {
                zn = ((uint32_t)sa[si]) - ((uint32_t)ea[ei]) + 1; za = z->a + ((uint32_t)ea[ei]);
            } else {
                zn = ((uint32_t)ea[ei]) - ((uint32_t)sa[si]) + 1; za = z->a + ((uint32_t)sa[si]);
            }
            if(u->n != zn) continue;
            if(!rev) {
                for (k = 0; (k < zn) && ((u->a[k]>>32) == ((za[k]>>32))); k++);
                if(k < zn) continue;
            } else {
                for (k = 0; (k < zn) && ((u->a[k]>>32) == (((uint64_t)(za[zn-k-1]>>32))^1)); k++);
                if(k < zn) continue;
            }
            
            assert(u->n <= z->n);
            if(u->n < z->n) {
                return -1;///delete u
            } else if(u->n == z->n) {
                for (m = fn = nfn = 0; m < u->n; m++) {
                    if(R_INF.trio_flag[u->a[m]>>33] == f) fn++;
                    if(R_INF.trio_flag[u->a[m]>>33] == nf) nfn++;
                }
                if(fn < nfn) return -1;///delete u
                else return sa[si]>>32;///delete z
            }
        }
    }
    return INT32_MIN;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl 0x10(%rsi), %r8d
movq $-0x80000000, %rax      # imm = 0x80000000
testq %r8, %r8
je 0xd90b1
xorl %ecx, %ecx
cmpq $0x1, %rdx
sete %cl
leaq (%rcx,%rcx,2), %rcx
decq %rcx
cmpq $0x2, %rdx
movl $0x1, %r9d
cmovneq %rcx, %r9
movq 0x18(%rsi), %rsi
movq (%rsi), %rcx
movq 0x10(%rdi), %r10
movq %rcx, 0x38(%rsp)
movq %rcx, %r11
shrq $0x21, %r11
movl (%r10,%r11,4), %ecx
movq %r11, 0x48(%rsp)
movl 0x4(%r10,%r11,4), %r11d
subl %ecx, %r11d
je 0xd90b1
movq %r9, 0x28(%rsp)
movq %rdx, 0x30(%rsp)
movq 0x18(%rdi), %rdx
leaq (%rdx,%rcx,8), %rax
movq %rax, 0x70(%rsp)
movq %r8, 0x8(%rsp)
leal -0x1(%r8), %ecx
movq %rsi, 0x10(%rsp)
movq (%rsi,%rcx,8), %rax
shrq $0x21, %rax
movl (%r10,%rax,4), %ecx
leaq (%rdx,%rcx,8), %r12
movq %rax, 0x68(%rsp)
movl 0x4(%r10,%rax,4), %eax
movl %eax, 0x1c(%rsp)
movl %eax, %r13d
movq %rcx, 0x40(%rsp)
subl %ecx, %r13d
movq (%rdi), %rax
movq %rax, 0x58(%rsp)
movq 0x10(%rax), %rax
cmpl $0x1, %r13d
adcl $0x0, %r13d
cmpl $0x1, %r11d
adcl $0x0, %r11d
xorl %r8d, %r8d
movq %r11, %r10
movq 0x70(%rsp), %rcx
movq (%rcx,%r8,8), %rbp
movq %rbp, %rdi
shrq $0x20, %rdi
leaq (%rdi,%rdi,4), %rcx
movl 0x10(%rax,%rcx,8), %edx
testl %edx, %edx
je 0xd909b
leaq (%rax,%rcx,8), %rcx
cmpl $0x0, 0xc(%rcx)
je 0xd909b
movq 0x58(%rsp), %rsi
movq 0x18(%rsi), %rsi
movq 0x20(%rsi), %r15
cmpl $0x0, (%r15,%rdi,8)
js 0xd909b
movq 0x18(%rcx), %rsi
movl %ebp, %ecx
movq %rsi, 0x20(%rsp)
movq (%rsi,%rcx,8), %rsi
movq %rsi, %r9
shrq $0x21, %rsi
cmpl 0x48(%rsp), %esi
jne 0xd914f
movl 0x1c(%rsp), %esi
cmpl 0x40(%rsp), %esi
je 0xd909b
movq 0x20(%rsp), %rsi
leaq (%rsi,%rcx,8), %rcx
movq %rcx, 0x60(%rsp)
xorq 0x38(%rsp), %r9
shrq $0x20, %r9
movq %r9, 0x50(%rsp)
xorl %ecx, %ecx
movq (%r12,%rcx,8), %rbx
movq %rbx, %r14
shrq $0x20, %r14
leaq (%r14,%r14,4), %rsi
cmpl $0x0, 0x10(%rax,%rsi,8)
je 0xd908f
leaq (%rax,%rsi,8), %rsi
cmpl $0x0, 0xc(%rsi)
je 0xd908f
cmpl $0x0, (%r15,%r14,8)
js 0xd908f
movq 0x18(%rsi), %r11
movl %ebx, %esi
movq (%r11,%rsi,8), %r11
shrq $0x21, %r11
cmpl 0x68(%rsp), %r11d
jne 0xd9130
cmpl %r14d, %edi
jne 0xd908f
movq %rbp, %r11
subq %rbx, %r11
movq %rbx, %r14
subq %rbp, %r14
cmpl %ebx, %ebp
movq 0x20(%rsp), %r9
leaq (%r9,%rsi,8), %rbx
cmovaeq %r11, %r14
cmovbq 0x60(%rsp), %rbx
incq %r14
cmpl %r14d, 0x8(%rsp)
jne 0xd908f
movl %r14d, %r14d
cmpq $0x0, 0x50(%rsp)
jne 0xd9055
testq %r14, %r14
je 0xd907a
xorl %esi, %esi
movq (%rbx,%rsi,8), %r11
movq 0x10(%rsp), %r9
xorq (%r9,%rsi,8), %r11
shrq $0x20, %r11
jne 0xd908f
incq %rsi
cmpq %rsi, %r14
jne 0xd9038
jmp 0xd907a
movq 0x10(%rsp), %rsi
testq %r14, %r14
je 0xd907a
movq -0x8(%rbx,%r14,8), %r11
xorq (%rsi), %r11
shrq $0x20, %r11
cmpl $0x1, %r11d
jne 0xd908f
addq $0x8, %rsi
decq %r14
jne 0xd905f
movq 0x8(%rsp), %rsi
cmpl %edx, %esi
ja 0xd916e
cmpl %edx, %esi
jb 0xd90c0
cmpl %edx, %esi
je 0xd90c9
incq %rcx
cmpq %r13, %rcx
jne 0xd8fa6
incq %r8
movq %r10, %r11
cmpq %r10, %r8
jne 0xd8f11
movq $-0x80000000, %rax      # imm = 0x80000000
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq $-0x1, %rax
jmp 0xd90b1
leaq 0x1b2160(%rip), %rax    # 0x28b230
movq 0x28(%rax), %rcx
xorl %eax, %eax
xorl %edx, %edx
xorl %r8d, %r8d
movq 0x30(%rsp), %r10
movq 0x8(%rsp), %r11
movq 0x28(%rsp), %rbx
movq 0x10(%rsp), %r14
movq (%r14,%r8,8), %rsi
shrq $0x21, %rsi
movzbl (%rcx,%rsi), %esi
xorl %r9d, %r9d
cmpq %r10, %rsi
sete %r9b
addq %r9, %rdx
xorl %r9d, %r9d
cmpq %rsi, %rbx
sete %r9b
addq %r9, %rax
incq %r8
cmpq %r8, %r11
jne 0xd90ef
cmpq %rax, %rdx
movq $-0x1, %rax
cmovbq %rax, %rdi
movq %rdi, %rax
jmp 0xd90b1
leaq 0x1524e5(%rip), %rdi    # 0x22b61c
leaq 0x150b63(%rip), %rsi    # 0x229ca1
leaq 0x15249b(%rip), %rcx    # 0x22b5e0
movl $0x53bb, %edx           # imm = 0x53BB
callq 0x2110
leaq 0x15245d(%rip), %rdi    # 0x22b5b3
leaq 0x150b44(%rip), %rsi    # 0x229ca1
leaq 0x15247c(%rip), %rcx    # 0x22b5e0
movl $0x53b6, %edx           # imm = 0x53B6
callq 0x2110
leaq 0x1524ea(%rip), %rdi    # 0x22b65f
leaq 0x150b25(%rip), %rsi    # 0x229ca1
leaq 0x15245d(%rip), %rcx    # 0x22b5e0
movl $0x53cb, %edx           # imm = 0x53CB
callq 0x2110