// File: /workspace/github_cpp_makefile_star2_O2/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, 0x30(%rsp)
movq %r8, %rbx
movq %rcx, 0x50(%rsp)
movl %edx, %r14d
movq %rdi, %r12
movq 0x110(%rsp), %r15
movq 0x108(%rsp), %r8
pushq $0x1
popq %r9
movl %r9d, 0x3c(%rsp)
leaq 0x4c(%rsp), %r13
movl %r9d, (%r13)
movq 0x18(%rsi), %rdi
movq %r13, 0x20(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
andl $0x0, 0x8(%rsp)
movl $0x5, (%rsp)
movq %rsi, 0x40(%rsp)
movq %r15, %rcx
callq 0xa58d0
cmpl $0x0, (%r13)
jne 0xa5fd6
movq 0x40(%rsp), %rsi
movq 0x18(%rsi), %rdi
leaq 0x3c(%rsp), %rax
movq %rax, 0x20(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
andl $0x0, 0x8(%rsp)
movl $0x5, (%rsp)
pushq $0x2
popq %r9
movl %r14d, %edx
movq %r15, %rcx
movq 0x108(%rsp), %r8
callq 0xa58d0
movl %r14d, %eax
shrl %eax
movl %eax, 0x2c(%rsp)
xorl %eax, %eax
leaq 0x1ae248(%rip), %rcx    # 0x254230
movq 0x30(%rsp), %r9
movl %eax, %eax
movq 0x108(%rsp), %rdx
cmpq %rax, 0x38(%rdx)
jbe 0xa605c
movq 0x48(%rdx), %rdx
movl (%rdx,%rax,4), %edx
shrl %edx
cmpl 0x2c(%rsp), %edx
je 0xa6020
movq 0x108(%rsp), %rsi
movq 0x18(%rsi), %rsi
movl (%rsi), %esi
shrl %esi
cmpl %esi, %edx
jne 0xa6024
incl %eax
jmp 0xa5fed
imulq $0x28, %rdx, %rdx
movq 0x40(%rsp), %rsi
addq 0x10(%rsi), %rdx
xorl %esi, %esi
movl 0x10(%rdx), %edi
cmpq %rdi, %rsi
jae 0xa6020
movq 0x18(%rdx), %rdi
movq (%rdi,%rsi,8), %rdi
shrq $0x21, %rdi
movq 0x28(%rcx), %r8
cmpb $0x5, (%r8,%rdi)
jne 0xa6057
movb $0x0, (%r8,%rdi)
incq %rsi
jmp 0xa6033
movl 0x3c(%rsp), %ecx
xorl %eax, %eax
orl 0x4c(%rsp), %ecx
je 0xa6497
movq 0x108(%rsp), %rax
movq 0x18(%rax), %rax
movl (%rax), %eax
xorl %ecx, %ecx
testb $0x1, %r14b
sete %cl
movq 0x40(%rsp), %rdx
movq 0x10(%rdx), %rdx
movl 0x2c(%rsp), %esi
imulq $0x28, %rsi, %rsi
addq %rdx, %rsi
movl 0x4(%rsi,%rcx,4), %ebp
xorl $0x1, %ebp
movl %eax, %ecx
shrl %ecx
imulq $0x28, %rcx, %rcx
addq %rdx, %rcx
andl $0x1, %eax
movl 0x4(%rcx,%rax,4), %r14d
movl 0x14(%r12), %edx
addl %edx, %edx
andq $0x0, (%r9)
movq 0xf8(%rsp), %rdi
xorl %esi, %esi
callq 0x20e0
movq 0x30(%rsp), %r8
movq (%r8), %rcx
pushq $0x2
popq %rax
cmpq 0x8(%r8), %rcx
jne 0xa6106
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
cmoveq %rax, %rsi
movq %rsi, 0x8(%r8)
movq 0x10(%r8), %rdi
shlq $0x2, %rsi
callq 0x2370
movq 0x30(%rsp), %r8
movq %rax, 0x10(%r8)
movq (%r8), %rcx
jmp 0xa610a
movq 0x10(%r8), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, (%r8)
movl %ebp, (%rax,%rcx,4)
movq 0x108(%rsp), %rax
testq %rdx, %rdx
je 0xa61df
leaq -0x1(%rdx), %rax
movq %rax, (%r8)
movq 0x10(%r8), %rax
movl -0x4(%rax,%rdx,4), %eax
movq 0x8(%r12), %rcx
movq 0x28(%r12), %rdx
movq (%rdx,%rax,8), %rdx
movl %edx, %esi
shrq $0x20, %rdx
movq 0xf8(%rsp), %rdi
movb $0x1, (%rdi,%rax)
imulq $0x18, %rsi, %r15
imulq $0x18, %rdx, %rax
leaq (%rcx,%rax), %r13
addq $0x8, %r13
xorl %ebp, %ebp
cmpq %rbp, %r15
je 0xa61d7
cmpl $0x0, 0x4(%r13,%rbp)
js 0xa61d1
movl (%r13,%rbp), %ecx
movq 0xf8(%rsp), %rax
cmpb $0x0, (%rax,%rcx)
jne 0xa61d1
cmpl %r14d, %ecx
je 0xa61d1
movq (%r8), %rdx
movq 0x10(%r8), %rax
cmpq 0x8(%r8), %rdx
jne 0xa61c7
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r8)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x30(%rsp), %r8
movq %rax, 0x10(%r8)
movl (%r13,%rbp), %ecx
movq (%r8), %rdx
leaq 0x1(%rdx), %rsi
movq %rsi, (%r8)
movl %ecx, (%rax,%rdx,4)
addq $0x18, %rbp
jmp 0xa6166
movq (%r8), %rdx
jmp 0xa6114
movq 0xf8(%rsp), %rcx
movb $0x1, (%rcx,%r14)
xorl %edx, %edx
movl %edx, %edx
cmpq %rdx, 0x38(%rax)
jbe 0xa6494
movq 0x48(%rax), %rax
movl (%rax,%rdx,4), %eax
shrl %eax
cmpl 0x2c(%rsp), %eax
je 0xa621d
movq 0x108(%rsp), %rcx
movq 0x18(%rcx), %rcx
movl (%rcx), %ecx
shrl %ecx
cmpl %ecx, %eax
jne 0xa6229
incl %edx
movq 0x108(%rsp), %rax
jmp 0xa61ee
imulq $0x28, %rax, %rcx
movq 0x40(%rsp), %rax
addq 0x10(%rax), %rcx
xorl %esi, %esi
movq %rdx, 0x60(%rsp)
movq %rcx, 0x58(%rsp)
movl 0x10(%rcx), %eax
cmpq %rax, %rsi
jae 0xa621d
movq 0x18(%rcx), %rax
movq %rsi, 0x68(%rsp)
movq (%rax,%rsi,8), %rax
movq %rax, %rcx
shrq $0x20, %rcx
andl $-0x2, %ecx
movq %rcx, 0x70(%rsp)
shrq $0x21, %rax
imulq $0x18, %rax, %r13
addq 0x50(%rsp), %r13
xorl %ebp, %ebp
cmpq $0x2, %rbp
je 0xa647d
movq %rbp, 0x30(%rsp)
orq 0x70(%rsp), %rbp
movq 0xf8(%rsp), %rax
cmpb $0x0, (%rax,%rbp)
je 0xa6470
movl %ebp, %eax
xorl $0x1, %eax
movl %eax, 0x48(%rsp)
xorl %r14d, %r14d
xorl %r15d, %r15d
movl 0xc(%r13), %eax
cmpq %rax, %r15
jae 0xa6470
movq (%r13), %rdi
movl 0xc(%rdi,%r14), %edx
imulq $0xc, %rdx, %rcx
movl (%rbx,%rcx), %eax
testl %eax, %eax
js 0xa6464
movq 0x20(%r12), %rsi
cmpl $0x0, (%rsi,%rdx,8)
js 0xa6464
movl 0x4(%rdi,%r14), %edx
addq %r14, %rdi
imulq $0xc, %rdx, %rdx
addq %rbx, %rdx
addq %rbx, %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 0x1ad44c(%rip), %rax    # 0x253758
movss 0x158(%rax), %xmm0
movl 0xe0(%rsp), %ecx
movl 0xe8(%rsp), %r8d
leaq 0x78(%rsp), %r9
callq 0x6a4a7
testl %eax, %eax
js 0xa6464
movl 0x7c(%rsp), %eax
cmpq %rbp, %rax
jne 0xa6464
movq 0xf8(%rsp), %rax
cmpb $0x0, (%rax,%rbp)
je 0xa6464
movl 0x80(%rsp), %r9d
cmpb $0x0, (%rax,%r9)
je 0xa6464
xorl $0x1, %r9d
leaq 0x90(%rsp), %rax
movq %rax, 0x8(%rsp)
movl 0x48(%rsp), %eax
movl %eax, (%rsp)
movq 0x50(%rsp), %rdi
movq %rbx, %rsi
movl 0xe0(%rsp), %ecx
movl 0xe8(%rsp), %r8d
callq 0x79204
testl %eax, %eax
je 0xa6464
movq 0x100(%rsp), %rsi
movq (%rsi), %rcx
movq 0x10(%rsi), %rax
cmpq 0x8(%rsi), %rcx
jne 0xa63e5
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%rsi)
imulq $0x18, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x100(%rsp), %rsi
movq %rax, 0x10(%rsi)
movq (%rsi), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%rsi)
imulq $0x18, %rcx, %rcx
movq 0x88(%rsp), %rdx
movq %rdx, 0x10(%rax,%rcx)
movups 0x78(%rsp), %xmm0
movups %xmm0, (%rax,%rcx)
movq (%rsi), %rcx
movq 0x10(%rsi), %rax
cmpq 0x8(%rsi), %rcx
jne 0xa6440
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%rsi)
imulq $0x18, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x100(%rsp), %rsi
movq %rax, 0x10(%rsi)
movq (%rsi), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%rsi)
imulq $0x18, %rcx, %rcx
movq 0xa0(%rsp), %rdx
movq %rdx, 0x10(%rax,%rcx)
movups 0x90(%rsp), %xmm0
movups %xmm0, (%rax,%rcx)
incq %r15
addq $0x28, %r14
jmp 0xa62aa
movq 0x30(%rsp), %rbp
incq %rbp
jmp 0xa6275
movq 0x68(%rsp), %rsi
incq %rsi
movq 0x60(%rsp), %rdx
movq 0x58(%rsp), %rcx
jmp 0xa6242
pushq $0x1
popq %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq