// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: bub_complex_hamming(asg_t*, ma_ug_t*, unsigned int, ma_hit_t_alloc*, ma_sub_t*, kvec_t_u32_warp*, int, int, unsigned char*, unsigned char*, kv_asg_arc_t*, buf_t*, unsigned long)
int bub_complex_hamming(asg_t *sg, ma_ug_t *ug, uint32_t beg_utg, ma_hit_t_alloc* sources, ma_sub_t *coverage_cut, kvec_t_u32_warp* stack, 
int max_hang, int min_ovlp, uint8_t* trio_flag, uint8_t* vis_flag, kv_asg_arc_t* e, buf_t *b, uint64_t tLen)
{
    uint32_t k_i, k_j, k_v, rID;
    int is_switch_0, is_switch_1;
    ma_utg_t* nsu = NULL;
    
    is_switch_0 = is_switch_1 = 1;
    asg_bub_pop1_primary_trio_switch_check(ug->g, ug, beg_utg, tLen, b, FATHER, DROP, 0, NULL, NULL, &is_switch_0);

    if(is_switch_0 == 0)
    {
        asg_bub_pop1_primary_trio_switch_check(ug->g, ug, beg_utg, tLen, b, MOTHER, DROP, 0, NULL, NULL, &is_switch_1);
    } 
    

    for (k_i = 0; k_i < b->b.n; k_i++)
    {
        if((b->b.a[k_i]>>1)==(beg_utg>>1) || (b->b.a[k_i]>>1)==(b->S.a[0]>>1)) continue;
        nsu = &(ug->u.a[b->b.a[k_i]>>1]);
        for (k_j = 0; k_j < nsu->n; k_j++)
        {
            rID = nsu->a[k_j]>>33;
            if(R_INF.trio_flag[rID] == DROP) R_INF.trio_flag[rID] = AMBIGU;
        }
    }
    
    if(is_switch_0 == 0 && is_switch_1 == 0) return 0;



    uint32_t i, sink_utg, begRid, sinkRid;
    sink_utg = b->S.a[0]^1;

    if(beg_utg&1)
    {
        begRid = ug->u.a[beg_utg>>1].start^1;
    }
    else
    {
        begRid = ug->u.a[beg_utg>>1].end^1;
    }

    if(sink_utg&1)
    {
        sinkRid = ug->u.a[sink_utg>>1].start;
    }
    else
    {
        sinkRid = ug->u.a[sink_utg>>1].end;
    }




    asg_arc_t *acur = NULL;
    uint32_t cur, ncur, v, n_vx = sg->n_seq<<1;
    stack->a.n = 0; 
    memset(vis_flag, 0, n_vx);

    kv_push(uint32_t, stack->a, begRid);
    while (stack->a.n > 0)
    {
        stack->a.n--;
        cur = stack->a.a[stack->a.n];
        ncur = asg_arc_n(sg, cur);
        acur = asg_arc_a(sg, cur);
        vis_flag[cur] = 1;
        for (i = 0; i < ncur; i++)
        {
            if(acur[i].del) continue;
            if(vis_flag[acur[i].v]) continue;
            if(acur[i].v == sinkRid) continue;
            kv_push(uint32_t, stack->a, acur[i].v);
        }
    }
    vis_flag[sinkRid] = 1;


    ma_hit_t_alloc* x = NULL;
    ma_hit_t *h = NULL;
    ma_sub_t *sq = NULL;
    ma_sub_t *st = NULL;
    int32_t r;
    asg_arc_t t0, t1;
    

    for (k_i = 0; k_i < b->b.n; k_i++)
    {
        if((b->b.a[k_i]>>1)==(beg_utg>>1) || (b->b.a[k_i]>>1)==(b->S.a[0]>>1)) continue;
        // nsu = &(ug->u.a[a[k_i]>>1]);
        nsu = &(ug->u.a[b->b.a[k_i]>>1]);
        for (k_j = 0; k_j < nsu->n; k_j++)
        {
            rID = nsu->a[k_j]>>33;
            for (k_v = 0; k_v < 2; k_v++)
            {
                v = (rID<<1) + k_v;
                if(vis_flag[v] == 0) continue;
                x = &(sources[v>>1]);
                for (i = 0; i < x->length; i++)
                {
                    h = &(x->buffer[i]);
                    sq = &(coverage_cut[Get_qn(*h)]);
                    st = &(coverage_cut[Get_tn(*h)]);
                    if(st->del || sg->seq[Get_tn(*h)].del) continue;
                    r = ma_hit2arc(h, sq->e - sq->s, st->e - st->s, max_hang, 
                                        asm_opt.max_hang_rate, min_ovlp, &t0);
                
                    ///if it is a contained read, skip
                    if(r < 0) continue;
                    if((t0.ul>>32) != v) continue;
                    if(vis_flag[t0.ul>>32] == 0 || vis_flag[t0.v] == 0) continue;
                    if(get_edge_from_source(sources, coverage_cut, NULL, max_hang, min_ovlp, (t0.v^1), ((t0.ul>>32)^1), &t1)) {
                        kv_push(asg_arc_t, *e, t0); kv_push(asg_arc_t, *e, t1);
                    }
                }
            }
        }
    }

    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %r9, %r15
movq %r8, 0x38(%rsp)
movq %rcx, 0x70(%rsp)
movq %rdi, 0x58(%rsp)
movq 0x130(%rsp), %r12
movq 0x128(%rsp), %r13
movl $0x1, %eax
movl %eax, 0x44(%rsp)
leaq 0x6c(%rsp), %rbx
movl %eax, (%rbx)
movq 0x18(%rsi), %rdi
movq %rbx, 0x20(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
movl $0x0, 0x8(%rsp)
movl $0x5, (%rsp)
movq %rsi, 0x48(%rsp)
movq %rdx, %r14
movq %r12, %rcx
movq %r13, %r8
movl $0x1, %r9d
callq 0xe2bf7
cmpl $0x0, (%rbx)
jne 0xe3466
movq 0x48(%rsp), %rsi
movq 0x18(%rsi), %rdi
leaq 0x44(%rsp), %rax
movq %rax, 0x20(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
movl $0x0, 0x8(%rsp)
movl $0x5, (%rsp)
movl %r14d, %edx
movq %r12, %rcx
movq %r13, %r8
movl $0x2, %r9d
callq 0xe2bf7
movq 0x38(%r13), %rsi
testq %rsi, %rsi
movq 0x38(%rsp), %rbx
je 0xe350c
movl %r14d, %eax
shrl %eax
xorl %ecx, %ecx
leaq 0x1b2daa(%rip), %rdx    # 0x296230
movq 0x128(%rsp), %r8
movq 0x48(%r8), %rdi
movl (%rdi,%rcx,4), %edi
shrl %edi
cmpl %eax, %edi
je 0xe3501
movq 0x18(%r8), %r8
movl (%r8), %r8d
shrl %r8d
cmpl %r8d, %edi
je 0xe3501
movq 0x48(%rsp), %r8
movq 0x10(%r8), %r8
leaq (%rdi,%rdi,4), %r9
movl 0x10(%r8,%r9,8), %edi
testl %edi, %edi
je 0xe3501
leaq (%r8,%r9,8), %rsi
movq 0x28(%rdx), %r8
xorl %r9d, %r9d
movq 0x18(%rsi), %r10
movq (%r10,%r9,8), %r10
shrq $0x21, %r10
cmpb $0x5, (%r8,%r10)
jne 0xe34ea
movb $0x0, (%r8,%r10)
movq 0x28(%rdx), %r8
movl 0x10(%rsi), %edi
incq %r9
movl %edi, %r10d
cmpq %r10, %r9
jb 0xe34cb
movq 0x128(%rsp), %rsi
movq 0x38(%rsi), %rsi
incl %ecx
cmpq %rcx, %rsi
ja 0xe3486
movl 0x44(%rsp), %ecx
xorl %eax, %eax
orl 0x6c(%rsp), %ecx
je 0xe3b68
movq 0x118(%rsp), %r12
movq 0x128(%rsp), %rax
movq 0x18(%rax), %rax
movl (%rax), %eax
xorl %ecx, %ecx
testb $0x1, %r14b
sete %cl
movq 0x48(%rsp), %rdx
movq 0x10(%rdx), %rdx
shrl %r14d
movq %r14, 0x78(%rsp)
leaq (%r14,%r14,4), %rsi
leaq (%rdx,%rsi,8), %rsi
movl 0x4(%rsi,%rcx,4), %ebp
xorl $0x1, %ebp
movl %eax, %ecx
shrl %ecx
leaq (%rcx,%rcx,4), %rcx
leaq (%rdx,%rcx,8), %rcx
andl $0x1, %eax
movl 0x4(%rcx,%rax,4), %r14d
movq 0x58(%rsp), %rax
movl 0x14(%rax), %edx
addl %edx, %edx
movq $0x0, (%r15)
movq %r12, %rdi
xorl %esi, %esi
callq 0x20e0
movq (%r15), %rcx
cmpq 0x8(%r15), %rcx
jne 0xe35bd
leaq (%rcx,%rcx), %rax
testq %rcx, %rcx
movl $0x2, %esi
cmovneq %rax, %rsi
movq %rsi, 0x8(%r15)
movq 0x10(%r15), %rdi
shlq $0x2, %rsi
callq 0x2370
movq %rax, 0x10(%r15)
movq (%r15), %rcx
jmp 0xe35c1
movq 0x10(%r15), %rax
movq %rcx, %rdx
incq %rdx
movq %rdx, (%r15)
movl %ebp, (%rax,%rcx,4)
je 0xe36a0
leaq -0x1(%rdx), %rax
movq %rax, (%r15)
movq 0x10(%r15), %rax
movl -0x4(%rax,%rdx,4), %edx
movq 0x58(%rsp), %rcx
movq 0x8(%rcx), %rax
movq 0x28(%rcx), %rcx
movq (%rcx,%rdx,8), %rcx
movb $0x1, (%r12,%rdx)
testl %ecx, %ecx
je 0xe368c
movq %rcx, %rdx
shrq $0x20, %rdx
movl %ecx, %ecx
leaq (%rdx,%rdx,2), %rdx
leaq (%rax,%rdx,8), %r13
addq $0x8, %r13
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rbp
xorl %r12d, %r12d
cmpl $0x0, 0x4(%r13,%r12)
js 0xe3683
movl (%r13,%r12), %ecx
movq 0x118(%rsp), %rax
cmpb $0x0, (%rax,%rcx)
jne 0xe3683
cmpl %r14d, %ecx
je 0xe3683
movq (%r15), %rdx
movq 0x10(%r15), %rax
cmpq 0x8(%r15), %rdx
jne 0xe3679
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r15)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r15)
movl (%r13,%r12), %ecx
movq (%r15), %rdx
leaq 0x1(%rdx), %rsi
movq %rsi, (%r15)
movl %ecx, (%rax,%rdx,4)
addq $0x18, %r12
cmpq %r12, %rbp
jne 0xe3620
movq (%r15), %rdx
testq %rdx, %rdx
movq 0x118(%rsp), %r12
jne 0xe35d3
movb $0x1, (%r12,%r14)
movq 0x128(%rsp), %rcx
movq 0x38(%rcx), %rax
testq %rax, %rax
je 0xe3b63
xorl %esi, %esi
movl %edx, 0x40(%rsp)
movq %rcx, %rdx
movq 0x48(%rcx), %rcx
movl (%rcx,%rsi,4), %ecx
shrl %ecx
cmpl 0x78(%rsp), %ecx
jne 0xe36da
movq %rdx, %rcx
jmp 0xe3b58
movq 0x18(%rdx), %rdx
movl (%rdx), %edx
shrl %edx
cmpl %edx, %ecx
je 0xe3b50
movq 0x48(%rsp), %rdx
movq 0x10(%rdx), %rdx
leaq (%rcx,%rcx,4), %rcx
cmpl $0x0, 0x10(%rdx,%rcx,8)
je 0xe3b50
leaq (%rdx,%rcx,8), %rcx
xorl %eax, %eax
movq %rsi, 0xa0(%rsp)
movq %rcx, 0x80(%rsp)
movq %rax, %rdx
movq 0x18(%rcx), %rax
movq %rdx, 0x90(%rsp)
movq (%rax,%rdx,8), %rax
movq %rax, %rcx
shrq $0x20, %rcx
andl $-0x2, %ecx
movq %rcx, 0xa8(%rsp)
shrq $0x21, %rax
leaq (%rax,%rax,2), %rax
movq 0x70(%rsp), %rcx
leaq (%rcx,%rax,8), %r8
movb $0x1, %al
xorl %ecx, %ecx
movq %r8, 0x28(%rsp)
orq 0xa8(%rsp), %rcx
cmpb $0x0, (%r12,%rcx)
movl %eax, 0x64(%rsp)
je 0xe3afb
cmpl $0x0, 0xc(%r8)
je 0xe3afb
movq %rcx, 0x98(%rsp)
movl %ecx, %eax
xorl $0x1, %eax
movl %eax, 0x60(%rsp)
movl $0x21, %ebp
xorl %r13d, %r13d
movq (%r8), %rax
movl -0x15(%rax,%rbp), %ecx
leaq (%rcx,%rcx,2), %rsi
movl (%rbx,%rsi,4), %edx
testl %edx, %edx
js 0xe38b1
movq 0x58(%rsp), %rdi
movq 0x20(%rdi), %rdi
cmpl $0x0, (%rdi,%rcx,8)
js 0xe38b1
leaq (%rbx,%rsi,4), %rsi
movl 0x4(%rsi), %esi
subl %edx, %esi
movq -0x9(%rax,%rbp), %rdx
movq %rdx, 0x50(%rsp)
testl %edx, %edx
js 0xe37e6
movl -0x11(%rax,%rbp), %r10d
movl -0xd(%rax,%rbp), %r8d
subl %r8d, %esi
movl %esi, %edi
movl %r10d, %esi
jmp 0xe37f6
movl -0x11(%rax,%rbp), %r10d
movl -0xd(%rax,%rbp), %r8d
subl %r8d, %esi
movl %r10d, %edi
movq -0x21(%rax,%rbp), %r14
movq %r14, %rdx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
movq %rbx, %r15
leaq (%rbx,%rdx,4), %rdx
movl 0x4(%rdx), %r12d
movl (%rdx), %edx
movl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
andl %r9d, %edx
subl %edx, %r12d
cmpl %r14d, %esi
movl %r14d, %edx
cmovll %esi, %edx
movl -0x19(%rax,%rbp), %r9d
movl %r12d, 0x68(%rsp)
subl %r9d, %r12d
cmpl %edi, %r12d
movl %edi, %r11d
cmovll %r12d, %r11d
movl 0x100(%rsp), %ebx
cmpl %ebx, %edx
jg 0xe38a9
cmpl %ebx, %r11d
jg 0xe38a9
movl %r9d, %ebx
subl %r14d, %ebx
cvtsi2ss %rbx, %xmm1
addl %edx, %ebx
addl %r11d, %ebx
xorps %xmm2, %xmm2
cvtsi2ss %rbx, %xmm2
leaq 0x1b1eea(%rip), %r15    # 0x295758
movss 0x158(%r15), %xmm0
mulss %xmm0, %xmm2
ucomiss %xmm1, %xmm2
ja 0xe38a2
subl %r10d, %r8d
xorps %xmm1, %xmm1
cvtsi2ss %r8, %xmm1
addl %edx, %r11d
addl %r8d, %r11d
xorps %xmm2, %xmm2
cvtsi2ss %r11, %xmm2
mulss %xmm2, %xmm0
ucomiss %xmm1, %xmm0
jbe 0xe38ca
movq 0x38(%rsp), %rbx
jmp 0xe38ac
movq %r15, %rbx
movq 0x28(%rsp), %r8
incq %r13
movl 0xc(%r8), %eax
addq $0x28, %rbp
cmpq %rax, %r13
jb 0xe3792
jmp 0xe3afb
cmpl %edi, %r12d
setl %dl
setg %r10b
cmpl %esi, %r14d
setg %r12b
setl %r8b
orb %r10b, %r12b
cmpb $0x1, %r12b
jne 0xe38a2
orb %dl, %r8b
je 0xe38a2
movl 0x108(%rsp), %edx
cmpl %edx, %ebx
jl 0xe38a2
cmpl %edx, %r11d
movq 0x38(%rsp), %rbx
jl 0xe38ac
movq 0x50(%rsp), %r11
testl %r11d, %r11d
setns %r8b
addl %r9d, %edi
movl 0x68(%rsp), %r15d
movl %r15d, %r9d
subl %edi, %r9d
xorl %edx, %edx
movl %r14d, %r12d
subl %esi, %r12d
setle %dil
setg %sil
cmovlel %r9d, %r12d
shrl $0x10, %r11d
andl $0x4000, %r11d          # imm = 0x4000
movl 0x40(%rsp), %r10d
andl $0x3fff, %r10d          # imm = 0x3FFF
orl %r11d, %r10d
movzbl (%rax,%rbp), %r9d
shll $0xf, %r9d
orl %r10d, %r9d
movl %r9d, 0x40(%rsp)
testl %r12d, %r12d
js 0xe38ac
movb %dil, %dl
shrq $0x1f, %r14
andl $-0x2, %r14d
orq %rdx, %r14
movq 0x98(%rsp), %rdi
cmpq %rdi, %r14
jne 0xe38ac
movq 0x118(%rsp), %rdx
cmpb $0x0, (%rdx,%rdi)
je 0xe38ac
xorb %sil, %r8b
addl %ecx, %ecx
movzbl %r8b, %esi
orq %rcx, %rsi
cmpb $0x0, (%rdx,%rsi)
je 0xe38ac
movb -0x1(%rax,%rbp), %al
movb %al, 0x37(%rsp)
movq %rsi, 0x50(%rsp)
movl %esi, %r9d
xorl $0x1, %r9d
leaq 0xb0(%rsp), %rax
movq %rax, 0x8(%rsp)
movl 0x60(%rsp), %eax
movl %eax, (%rsp)
movq 0x70(%rsp), %rdi
movq %rbx, %rsi
movl 0x100(%rsp), %ecx
movl 0x108(%rsp), %r8d
callq 0xb3df3
testl %eax, %eax
je 0xe38ac
movq %r12, 0x88(%rsp)
movq 0x120(%rsp), %r12
movq (%r12), %rcx
movq 0x10(%r12), %rax
cmpq 0x8(%r12), %rcx
jne 0xe3a45
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%r12)
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r12)
movq (%r12), %rcx
shlq $0x20, %r14
movq 0x88(%rsp), %rsi
leaq (%rsi,%r14), %rdx
subl %esi, %r15d
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
leaq 0x1(%rcx), %rsi
movq %rsi, (%r12)
leaq (%rcx,%rcx,2), %rcx
movq %rdx, (%rax,%rcx,8)
movq 0x50(%rsp), %rdx
movl %edx, 0x8(%rax,%rcx,8)
movl %r15d, 0xc(%rax,%rcx,8)
movl 0x40(%rsp), %edx
movw %dx, 0x10(%rax,%rcx,8)
movb 0x37(%rsp), %dl
movb %dl, 0x12(%rax,%rcx,8)
movq (%r12), %rcx
movq 0x10(%r12), %rax
cmpq 0x8(%r12), %rcx
jne 0xe3acc
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%r12)
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r12)
movq (%r12), %rcx
movq 0x28(%rsp), %r8
leaq 0x1(%rcx), %rdx
movq %rdx, (%r12)
leaq (%rcx,%rcx,2), %rcx
movq 0xc0(%rsp), %rdx
movq %rdx, 0x10(%rax,%rcx,8)
movups 0xb0(%rsp), %xmm0
movups %xmm0, (%rax,%rcx,8)
jmp 0xe38b1
movl $0x1, %ecx
testb $0x1, 0x64(%rsp)
movl $0x0, %eax
movq 0x118(%rsp), %r12
movq 0xa0(%rsp), %rsi
jne 0xe3757
movq 0x90(%rsp), %rdx
incq %rdx
movq 0x80(%rsp), %rcx
movl 0x10(%rcx), %eax
cmpq %rax, %rdx
movq %rdx, %rax
jb 0xe3718
movq 0x128(%rsp), %rcx
movq 0x38(%rcx), %rax
jmp 0xe3b58
movq 0x128(%rsp), %rcx
incl %esi
cmpq %rsi, %rax
ja 0xe36c0
movl $0x1, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq