// File: chhylp123[P]hifiasm/Overlaps.cpp
// Function: update_unitig_graph(ma_ug_t*, asg_t*, ma_sub_t*, ma_hit_t_alloc*, ma_hit_t_alloc*, R_to_U*, unsigned char*, unsigned char, float, unsigned char, float)
void update_unitig_graph(ma_ug_t* ug, asg_t* read_g, ma_sub_t* coverage_cut, 
ma_hit_t_alloc* sources, ma_hit_t_alloc* reverse_sources, R_to_U* ruIndex, uint8_t* is_r_het,
uint8_t is_final_check, float double_check_rate, uint8_t flag, float drop_rate)
{
    asg_t* nsg = ug->g;
    uint32_t v, n_vtx = nsg->n_seq, k, rId, flag_occ, non_flag_occ, hap_label_occ, n_reduce = 1;
    ma_utg_t *u;
    uint8_t* primary_flag = (uint8_t*)calloc(read_g->n_seq, sizeof(uint8_t));

    drop_semi_circle(ug, nsg, read_g, reverse_sources, ruIndex, is_r_het);

    while (n_reduce)
    {
        n_reduce = 0;
        n_vtx = nsg->n_seq;
        for (v = 0; v < n_vtx; ++v) 
        {
            if (nsg->seq[v].del) continue;
            u = &((ug)->u.a[v]);
            if(u->m == 0) continue;
            if((get_real_length(nsg, v<<1, NULL)!=0) 
                && (get_real_length(nsg, ((v<<1)^1), NULL)!=0)) continue;///check tig
            flag_occ = non_flag_occ = hap_label_occ = 0;
            for (k = 0; k < u->n; k++)
            {
                rId = u->a[k]>>33;
                if(read_g->seq[rId].c == HAP_LABLE) hap_label_occ++;
                if(R_INF.trio_flag[rId] == AMBIGU) continue;
                if(R_INF.trio_flag[rId] == DROP) continue;
                if(R_INF.trio_flag[rId] == flag) flag_occ++;
                if(R_INF.trio_flag[rId] != flag) non_flag_occ++;
            }

            ///if(is_final_check && v == 0) fprintf(stderr, "flag: %u, flag_occ: %u, non_flag_occ: %u\n", flag, flag_occ, non_flag_occ);
            
            if(is_final_check == 0 && hap_label_occ == u->n) continue;
            ///if(is_double_check && non_flag_occ < u->n*DOUBLE_CHECK_THRES) continue;
            ///if(is_double_check && non_flag_occ < u->n*double_check_rate) continue;
 
            if(is_final_check)
            {
                /**if(non_flag_occ < u->n*double_check_rate) continues**/;
            }
            else
            {
                if(u->n >= 100)
                {
                    if(non_flag_occ < u->n*double_check_rate)
                    {
                        continue;
                    }
                }
                else if(u->n >= 50)
                {
                    if(non_flag_occ < u->n*double_check_rate*0.5)
                    {
                        continue;
                    }
                }
                else
                {
                    if(non_flag_occ < u->n*double_check_rate*0.25)
                    {
                        continue;
                    }
                }
            }
            
            if(non_flag_occ > ((non_flag_occ+flag_occ)*drop_rate))
            {
                
                if(if_primary_unitig(u, read_g, coverage_cut, sources, ruIndex, primary_flag))
                {
                    continue;
                }

                if(u->m != 0)
                {
                    u->circ = u->end = u->len = u->m = u->n = u->start = 0;
                    free(u->a);
                    u->a = NULL;
                }
                asg_seq_del(nsg, v);
                n_reduce++;
            }
        }
    }

    drop_semi_circle(ug, nsg, read_g, reverse_sources, ruIndex, is_r_het);
    asg_cleanup(nsg);
    free(primary_flag);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movb 0x20(%rbp), %al
movb 0x18(%rbp), %al
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movss %xmm0, -0x34(%rbp)
movss %xmm1, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, -0x48(%rbp)
movl $0x1, -0x60(%rbp)
movq -0x10(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %edi
movl $0x1, %esi
callq 0x2140
movq %rax, -0x70(%rbp)
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x10(%rbp), %rdx
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %r8
movq 0x10(%rbp), %r9
callq 0x11d700
cmpl $0x0, -0x60(%rbp)
je 0x11ee55
movl $0x0, -0x60(%rbp)
movq -0x40(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, -0x48(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x48(%rbp), %eax
jae 0x11ee50
movq -0x40(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x44(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x11eb5f
jmp 0x11ee42
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x44(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpl $0x0, 0xc(%rax)
jne 0x11eb84
jmp 0x11ee42
movq -0x40(%rbp), %rdi
movl -0x44(%rbp), %esi
shll %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x166b50
cmpl $0x0, %eax
je 0x11ebba
movq -0x40(%rbp), %rdi
movl -0x44(%rbp), %esi
shll %esi
xorl $0x1, %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x166b50
cmpl $0x0, %eax
je 0x11ebba
jmp 0x11ee42
movl $0x0, -0x5c(%rbp)
movl $0x0, -0x58(%rbp)
movl $0x0, -0x54(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movq -0x68(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0x11eca2
movq -0x68(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x4c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movl %eax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x50(%rbp), %ecx
movzbl 0x4(%rax,%rcx,8), %eax
cmpl $0x2, %eax
jne 0x11ec1a
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
leaq 0x2a66af(%rip), %rax    # 0x3c52d0
movq 0x28(%rax), %rax
movl -0x50(%rbp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x11ec33
jmp 0x11ec94
leaq 0x2a6696(%rip), %rax    # 0x3c52d0
movq 0x28(%rax), %rax
movl -0x50(%rbp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x5, %eax
jne 0x11ec4c
jmp 0x11ec94
leaq 0x2a667d(%rip), %rax    # 0x3c52d0
movq 0x28(%rax), %rax
movl -0x50(%rbp), %ecx
movzbl (%rax,%rcx), %eax
movzbl 0x20(%rbp), %ecx
cmpl %ecx, %eax
jne 0x11ec6f
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
leaq 0x2a665a(%rip), %rax    # 0x3c52d0
movq 0x28(%rax), %rax
movl -0x50(%rbp), %ecx
movzbl (%rax,%rcx), %eax
movzbl 0x20(%rbp), %ecx
cmpl %ecx, %eax
je 0x11ec92
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x11ec94
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x11ebd6
movzbl 0x18(%rbp), %eax
cmpl $0x0, %eax
jne 0x11ecbc
movl -0x5c(%rbp), %eax
movq -0x68(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jne 0x11ecbc
jmp 0x11ee42
cmpb $0x0, 0x18(%rbp)
je 0x11ecc7
jmp 0x11ed70
movq -0x68(%rbp), %rax
cmpl $0x64, 0x10(%rax)
jb 0x11ecf6
movl -0x58(%rbp), %eax
cvtsi2ss %rax, %xmm1
movq -0x68(%rbp), %rax
movl 0x10(%rax), %eax
cvtsi2ss %rax, %xmm0
mulss -0x34(%rbp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x11ecf4
jmp 0x11ee42
jmp 0x11ed6e
movq -0x68(%rbp), %rax
cmpl $0x32, 0x10(%rax)
jb 0x11ed36
movl -0x58(%rbp), %eax
cvtsi2sd %rax, %xmm1
movq -0x68(%rbp), %rax
movl 0x10(%rax), %eax
cvtsi2ss %rax, %xmm0
mulss -0x34(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x26354b(%rip), %xmm2   # 0x382270
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x11ed34
jmp 0x11ee42
jmp 0x11ed6c
movl -0x58(%rbp), %eax
cvtsi2sd %rax, %xmm1
movq -0x68(%rbp), %rax
movl 0x10(%rax), %eax
cvtsi2ss %rax, %xmm0
mulss -0x34(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x2634f5(%rip), %xmm2   # 0x382250
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x11ed6a
jmp 0x11ee42
jmp 0x11ed6c
jmp 0x11ed6e
jmp 0x11ed70
movl -0x58(%rbp), %eax
movl %eax, %ecx
movl %ecx, %eax
cvtsi2ss %rcx, %xmm0
movl -0x54(%rbp), %ecx
addl %ecx, %eax
cvtsi2ss %rax, %xmm1
mulss -0x38(%rbp), %xmm1
ucomiss %xmm1, %xmm0
jbe 0x11ee40
movq -0x68(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
movq -0x30(%rbp), %r8
movq -0x70(%rbp), %r9
callq 0x1185a0
cmpb $0x0, %al
je 0x11edba
jmp 0x11ee42
movq -0x68(%rbp), %rax
cmpl $0x0, 0xc(%rax)
je 0x11ee2b
movq -0x68(%rbp), %rax
movl $0x0, 0x4(%rax)
movq -0x68(%rbp), %rax
movl $0x0, 0x10(%rax)
movq -0x68(%rbp), %rax
movl $0x0, 0xc(%rax)
movq -0x68(%rbp), %rax
movl (%rax), %ecx
andl $0x80000000, %ecx       # imm = 0x80000000
orl $0x0, %ecx
movl %ecx, (%rax)
movq -0x68(%rbp), %rax
movl $0x0, 0x8(%rax)
movq -0x68(%rbp), %rax
movl (%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl $0x0, %ecx
movl %ecx, (%rax)
movq -0x68(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x2210
movq -0x68(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x40(%rbp), %rdi
movl -0x44(%rbp), %esi
callq 0xd9510
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x11ee42
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x11eb38
jmp 0x11eb11
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x10(%rbp), %rdx
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %r8
movq 0x10(%rbp), %r9
callq 0x11d700
movq -0x40(%rbp), %rdi
callq 0xcdd10
movq -0x70(%rbp), %rdi
callq 0x2210
addq $0x70, %rsp
popq %rbp
retq
nopw (%rax,%rax)