// File: /workspace/github_cpp_makefile_star2_O1/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 $0xa8, %rsp
movq %r9, %r15
movq %r8, %rbx
movq %rcx, 0x48(%rsp)
movq %rsi, %rbp
movq %rdi, 0x38(%rsp)
movq 0x110(%rsp), %r14
movq 0x108(%rsp), %r13
movl $0x1, %eax
movl %eax, 0x34(%rsp)
leaq 0x44(%rsp), %r12
movl %eax, (%r12)
movq 0x18(%rsi), %rdi
movq %r12, 0x20(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
movl $0x0, 0x8(%rsp)
movl $0x5, (%rsp)
movq %rdx, 0x28(%rsp)
movq %r14, %rcx
movq %r13, %r8
movl $0x1, %r9d
callq 0xe1869
cmpl $0x0, (%r12)
jne 0xe2145
movq 0x18(%rbp), %rdi
leaq 0x34(%rsp), %rax
movq %rax, 0x20(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
movl $0x0, 0x8(%rsp)
movl $0x5, (%rsp)
movq %rbp, %rsi
movq 0x28(%rsp), %rdx
movq %r14, %rcx
movq %r13, %r8
movl $0x2, %r9d
callq 0xe1869
cmpq $0x0, 0x38(%r13)
je 0xe21ba
movq 0x28(%rsp), %rax
shrl %eax
xorl %ecx, %ecx
leaq 0x1a90d4(%rip), %rdx    # 0x28b230
movq 0x48(%r13), %rsi
movl (%rsi,%rcx,4), %esi
shrl %esi
cmpl %eax, %esi
je 0xe21b2
movq 0x18(%r13), %rdi
movl (%rdi), %edi
shrl %edi
cmpl %edi, %esi
je 0xe21b2
movq 0x10(%rbp), %rdi
leaq (%rsi,%rsi,4), %rsi
cmpl $0x0, 0x10(%rdi,%rsi,8)
je 0xe21b2
leaq (%rdi,%rsi,8), %rsi
xorl %edi, %edi
movq 0x18(%rsi), %r8
movq (%r8,%rdi,8), %r8
shrq $0x21, %r8
movq 0x28(%rdx), %r9
cmpb $0x5, (%r9,%r8)
jne 0xe21a6
movb $0x0, (%r9,%r8)
incq %rdi
movl 0x10(%rsi), %r8d
cmpq %r8, %rdi
jb 0xe218a
incl %ecx
cmpq %rcx, 0x38(%r13)
ja 0xe215c
movl 0x34(%rsp), %ecx
xorl %eax, %eax
orl 0x44(%rsp), %ecx
je 0xe264d
movq 0xf8(%rsp), %rdi
movq 0x18(%r13), %rax
movl (%rax), %eax
xorl %ecx, %ecx
movq 0x28(%rsp), %r13
testb $0x1, %r13b
sete %cl
movq %rbp, 0x50(%rsp)
movq 0x10(%rbp), %rdx
shrl %r13d
movq %r13, 0x28(%rsp)
leaq (,%r13,4), %rsi
addq %r13, %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 0x38(%rsp), %rax
movl 0x14(%rax), %edx
addl %edx, %edx
movq $0x0, (%r15)
xorl %esi, %esi
callq 0x20e0
movq 0x8(%r15), %rax
cmpq %rax, (%r15)
jne 0xe2267
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0x8(%r15)
movq 0x10(%r15), %rdi
shlq $0x2, %rsi
callq 0x2370
movq %rax, 0x10(%r15)
movq (%r15), %rcx
movq 0x10(%r15), %rdx
movq %rcx, %rax
incq %rax
movq %rax, (%r15)
movl %ebp, (%rdx,%rcx,4)
je 0xe234d
leaq -0x1(%rax), %rcx
movq %rcx, (%r15)
movq 0x10(%r15), %rcx
movl -0x4(%rcx,%rax,4), %edx
movq 0x38(%rsp), %rcx
movq 0x8(%rcx), %rax
movq 0x28(%rcx), %rcx
movq (%rcx,%rdx,8), %rcx
movq 0xf8(%rsp), %rsi
movb $0x1, (%rsi,%rdx)
testl %ecx, %ecx
je 0xe2341
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 0xe2338
movl (%r13,%r12), %eax
movq 0xf8(%rsp), %rcx
cmpb $0x0, (%rcx,%rax)
jne 0xe2338
cmpl %r14d, %eax
je 0xe2338
movq 0x8(%r15), %rax
cmpq %rax, (%r15)
jne 0xe2322
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r15)
movq 0x10(%r15), %rdi
shlq $0x2, %rsi
callq 0x2370
movq %rax, 0x10(%r15)
movl (%r13,%r12), %eax
movq (%r15), %rcx
movq 0x10(%r15), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r15)
movl %eax, (%rdx,%rcx,4)
addq $0x18, %r12
cmpq %r12, %rbp
jne 0xe22d4
movq (%r15), %rax
testq %rax, %rax
jne 0xe2280
movq 0xf8(%rsp), %rax
movb $0x1, (%rax,%r14)
movq 0x108(%rsp), %rax
cmpq $0x0, 0x38(%rax)
je 0xe2648
xorl %ecx, %ecx
movq 0x48(%rax), %rax
movq %rcx, 0x58(%rsp)
movl (%rax,%rcx,4), %eax
shrl %eax
cmpl 0x28(%rsp), %eax
je 0xe262f
movq 0x108(%rsp), %rcx
movq 0x18(%rcx), %rcx
movl (%rcx), %ecx
shrl %ecx
cmpl %ecx, %eax
je 0xe262f
movq 0x50(%rsp), %rcx
movq 0x10(%rcx), %rcx
leaq (%rax,%rax,4), %rax
cmpl $0x0, 0x10(%rcx,%rax,8)
je 0xe262f
leaq (%rcx,%rax,8), %rcx
xorl %edx, %edx
movq %rcx, 0x60(%rsp)
movq 0x18(%rcx), %rax
movq %rdx, 0x68(%rsp)
movq (%rax,%rdx,8), %rax
movq %rax, %rcx
shrq $0x20, %rcx
andl $-0x2, %ecx
movq %rcx, 0x70(%rsp)
shrq $0x21, %rax
leaq (%rax,%rax,2), %rax
movq 0x48(%rsp), %rcx
leaq (%rcx,%rax,8), %r12
xorl %r15d, %r15d
movq %r15, %r14
orq 0x70(%rsp), %r14
movq 0xf8(%rsp), %rax
cmpb $0x0, (%rax,%r14)
je 0xe2606
cmpl $0x0, 0xc(%r12)
je 0xe2606
xorl %ebp, %ebp
xorl %r13d, %r13d
movq (%r12), %rdi
movl 0xc(%rdi,%rbp), %edx
leaq (%rdx,%rdx,2), %rcx
movl (%rbx,%rcx,4), %eax
testl %eax, %eax
js 0xe25f1
movq 0x38(%rsp), %rsi
movq 0x20(%rsi), %rsi
cmpl $0x0, (%rsi,%rdx,8)
js 0xe25f1
movl 0x4(%rdi,%rbp), %edx
addq %rbp, %rdi
leaq (%rdx,%rdx,2), %rdx
leaq (%rbx,%rdx,4), %rdx
leaq (%rbx,%rcx,4), %rcx
movl 0x4(%rdx), %esi
movl (%rdx), %edx
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
andl %r8d, %edx
subl %edx, %esi
movl 0x4(%rcx), %edx
subl %eax, %edx
leaq 0x1a82e1(%rip), %rax    # 0x28a758
movss 0x158(%rax), %xmm0
movl 0xe0(%rsp), %ecx
movl 0xe8(%rsp), %r8d
leaq 0x78(%rsp), %r9
callq 0xa159b
testl %eax, %eax
js 0xe25f1
movq 0x78(%rsp), %rax
shrq $0x20, %rax
cmpq %r14, %rax
jne 0xe25f1
movq 0xf8(%rsp), %rcx
cmpb $0x0, (%rcx,%rax)
je 0xe25f1
movl 0x80(%rsp), %r9d
cmpb $0x0, (%rcx,%r9)
je 0xe25f1
xorl $0x1, %r9d
xorl $0x1, %eax
leaq 0x90(%rsp), %rcx
movq %rcx, 0x8(%rsp)
movl %eax, (%rsp)
movq 0x48(%rsp), %rdi
movq %rbx, %rsi
movl 0xe0(%rsp), %ecx
movl 0xe8(%rsp), %r8d
callq 0xb2ced
testl %eax, %eax
je 0xe25f1
movq 0x100(%rsp), %rsi
movq 0x8(%rsi), %rax
cmpq %rax, (%rsi)
jne 0xe255c
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x8(%rsi)
movq 0x100(%rsp), %rax
movq 0x10(%rax), %rdi
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rsi
callq 0x2370
movq 0x100(%rsp), %rsi
movq %rax, 0x10(%rsi)
movq (%rsi), %rax
movq 0x10(%rsi), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rsi)
leaq (%rax,%rax,2), %rax
movq 0x88(%rsp), %rdx
movq %rdx, 0x10(%rcx,%rax,8)
movups 0x78(%rsp), %xmm0
movups %xmm0, (%rcx,%rax,8)
movq 0x8(%rsi), %rax
cmpq %rax, (%rsi)
jne 0xe25c6
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x8(%rsi)
movq 0x100(%rsp), %rax
movq 0x10(%rax), %rdi
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rsi
callq 0x2370
movq 0x100(%rsp), %rsi
movq %rax, 0x10(%rsi)
movq (%rsi), %rax
movq 0x10(%rsi), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rsi)
leaq (%rax,%rax,2), %rax
movq 0xa0(%rsp), %rdx
movq %rdx, 0x10(%rcx,%rax,8)
movups 0x90(%rsp), %xmm0
movups %xmm0, (%rcx,%rax,8)
incq %r13
movl 0xc(%r12), %eax
addq $0x28, %rbp
cmpq %rax, %r13
jb 0xe241e
leaq 0x1(%r15), %rax
testq %r15, %r15
movq %rax, %r15
je 0xe23f2
movq 0x68(%rsp), %rdx
incq %rdx
movq 0x60(%rsp), %rcx
movl 0x10(%rcx), %eax
cmpq %rax, %rdx
jb 0xe23c2
movq 0x58(%rsp), %rcx
incl %ecx
movq 0x108(%rsp), %rax
cmpq %rcx, 0x38(%rax)
ja 0xe236f
movl $0x1, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq