// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: get_utg_attributes(ma_ug_t*, asg_t*, ma_sub_t*, ma_hit_t_alloc*, R_to_U*)
uint8_t* get_utg_attributes(ma_ug_t *ug, asg_t* read_g, ma_sub_t* coverage_cut, 
ma_hit_t_alloc* sources, R_to_U* ruIndex)
{
    asg_t* nsg = ug->g;
    uint32_t v, n_vtx = nsg->n_seq, k, j, rId, available_reads = 0, tn, is_Unitig;
    ma_utg_t* u = NULL;
    ma_hit_t *h;
    long long R_bases = 0, C_bases = 0, C_bases_primary = 0, C_bases_alter = 0;
    uint8_t* r_flag = (uint8_t*)calloc(read_g->n_seq, sizeof(uint8_t));
    uint8_t* u_flag = (uint8_t*)calloc(n_vtx, sizeof(uint8_t));
    for (v = 0; v < n_vtx; ++v) 
    {
        if(nsg->seq[v].del) continue;
        u = &(ug->u.a[v]);
        if(u->m == 0) continue;
        available_reads = 0;
        for (k = 0; k < u->n; k++)
        {
            rId = u->a[k]>>33;
            r_flag[rId] = 1;
        }

        for (k = 0; k < u->n; k++)
        {
            rId = u->a[k]>>33;
            C_bases = C_bases_primary = C_bases_alter = 0;
            R_bases = coverage_cut[rId].e - coverage_cut[rId].s;
            for (j = 0; j < (uint64_t)(sources[rId].length); j++)
            {
                h = &(sources[rId].buffer[j]);
                if(h->el != 1) continue;
                tn = Get_tn((*h));
                if(read_g->seq[tn].del == 1)
                {
                    ///get the id of read that contains it 
                    get_R_to_U(ruIndex, tn, &tn, &is_Unitig);
                    if(tn == (uint32_t)-1 || is_Unitig == 1 || read_g->seq[tn].del == 1) continue;
                }
                if(read_g->seq[tn].del == 1) continue;
                if(r_flag[tn])
                {
                    C_bases_primary += Get_qe((*h)) - Get_qs((*h));
                }
                else
                {
                    C_bases_alter += Get_qe((*h)) - Get_qs((*h));
                }
            }

            C_bases = C_bases_primary + C_bases_alter;
            if(C_bases_alter < C_bases * ALTER_COV_THRES) continue;

            C_bases = C_bases/R_bases;
            if(C_bases >= asm_opt.recover_atg_cov_min && C_bases <= asm_opt.recover_atg_cov_max)
            {
                available_reads++;
            }
        }


        if(available_reads < (u->n * 0.8) || available_reads == 0)
        {
            u_flag[v] = 0;
        }
        else
        {
            u_flag[v] = 1;
        }

        for (k = 0; k < u->n; k++)
        {
            rId = u->a[k]>>33;
            r_flag[rId] = 0;
        }
    }

    free(r_flag);
    return u_flag;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r8, %rbx
movq %rcx, 0x10(%rsp)
movq %rdx, 0x48(%rsp)
movq %rsi, %r12
movq %rdi, %r13
movq 0x18(%rdi), %r15
movl 0x14(%r15), %r14d
movl 0x14(%rsi), %edi
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andq %rax, %rdi
movl $0x1, %esi
callq 0x2120
movq %rax, %rbp
movq %r14, %rdi
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andq %rax, %rdi
movl $0x1, %esi
callq 0x2120
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andq %rcx, %r14
je 0xd6b77
movq 0x20(%r15), %r9
leaq 0x1bee06(%rip), %rcx    # 0x295758
movslq 0x13c(%rcx), %rdx
movq %rdx, 0x40(%rsp)
movslq 0x140(%rcx), %rcx
movq %rcx, 0x38(%rsp)
xorl %ecx, %ecx
movsd 0x15ac84(%rip), %xmm0   # 0x2315f8
movsd 0x15ac34(%rip), %xmm1   # 0x2315b0
movq %r13, 0x30(%rsp)
movq %r14, 0x28(%rsp)
movq %rax, 0x20(%rsp)
movq %r9, 0x18(%rsp)
movq %rcx, 0x8(%rsp)
cmpl $0x0, (%r9,%rcx,8)
js 0xd6b66
movq 0x10(%r13), %rdx
movq 0x8(%rsp), %rcx
leaq (%rcx,%rcx,4), %rcx
cmpl $0x0, 0xc(%rdx,%rcx,8)
je 0xd6b66
leaq (%rdx,%rcx,8), %r8
movl 0x10(%r8), %r10d
testq %r10, %r10
movq 0x10(%rsp), %rsi
je 0xd6b5d
movq 0x18(%r8), %rdi
xorl %ecx, %ecx
movq (%rdi,%rcx,8), %rdx
shrq $0x21, %rdx
movb $0x1, (%rbp,%rdx)
incq %rcx
cmpq %rcx, %r10
jne 0xd69d4
testq %r10, %r10
je 0xd6b5d
movq 0x18(%r8), %rax
movq %rax, 0x50(%rsp)
xorl %r9d, %r9d
movl $0x0, 0x4(%rsp)
movq 0x50(%rsp), %rax
movq (%rax,%r9,8), %rax
shrq $0x21, %rax
leaq (%rax,%rax,2), %rdx
movl 0xc(%rsi,%rdx,8), %eax
testq %rax, %rax
je 0xd6a91
leaq (%rsi,%rdx,8), %rsi
movq (%rsi), %rsi
shlq $0x3, %rax
leaq (%rax,%rax,4), %rdi
xorl %r15d, %r15d
xorl %r11d, %r11d
xorl %eax, %eax
cmpb $0x1, 0x20(%rsi,%r15)
jne 0xd6a86
movl 0xc(%rsi,%r15), %r14d
movq 0x20(%r12), %r13
cmpl $0x0, (%r13,%r14,8)
js 0xd6a67
movl 0x8(%rsi,%r15), %r13d
subl (%rsi,%r15), %r13d
cmpb $0x0, (%rbp,%r14)
je 0xd6a83
addq %r13, %rax
jmp 0xd6a86
cmpq %r14, (%rbx)
jbe 0xd6a86
movq 0x8(%rbx), %rcx
movslq (%rcx,%r14,4), %r14
testq %r14, %r14
js 0xd6a86
cmpl $0x0, (%r13,%r14,8)
jns 0xd6a51
jmp 0xd6a86
addq %r13, %r11
addq $0x28, %r15
cmpq %r15, %rdi
jne 0xd6a37
jmp 0xd6a96
xorl %eax, %eax
xorl %r11d, %r11d
addq %r11, %rax
xorps %xmm2, %xmm2
cvtsi2sd %r11, %xmm2
xorps %xmm3, %xmm3
cvtsi2sd %rax, %xmm3
mulsd %xmm0, %xmm3
ucomisd %xmm2, %xmm3
ja 0xd6ae6
movq 0x48(%rsp), %rsi
movl 0x4(%rsi,%rdx,4), %ecx
movl (%rsi,%rdx,4), %edx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl %esi, %edx
subl %edx, %ecx
cqto
idivq %rcx
cmpq 0x40(%rsp), %rax
setge %cl
cmpq 0x38(%rsp), %rax
setle %al
andb %cl, %al
movzbl %al, %eax
addl %eax, 0x4(%rsp)
movq 0x10(%rsp), %rsi
incq %r9
cmpq %r10, %r9
jne 0xd6a06
movl 0x4(%rsp), %ecx
movl %ecx, %eax
xorps %xmm2, %xmm2
cvtsi2sd %rax, %xmm2
xorps %xmm3, %xmm3
cvtsi2sd %r10, %xmm3
mulsd %xmm1, %xmm3
ucomisd %xmm2, %xmm3
setbe %al
testl %ecx, %ecx
setne %cl
andb %al, %cl
movq 0x20(%rsp), %rax
movq 0x8(%rsp), %rdx
movb %cl, (%rax,%rdx)
testq %r10, %r10
movq 0x30(%rsp), %r13
movq 0x28(%rsp), %r14
movq 0x18(%rsp), %r9
je 0xd6b66
movq 0x18(%r8), %rsi
xorl %ecx, %ecx
movq (%rsi,%rcx,8), %rdx
shrq $0x21, %rdx
movb $0x0, (%rbp,%rdx)
incq %rcx
cmpq %rcx, %r10
jne 0xd6b46
jmp 0xd6b66
movq 0x8(%rsp), %rcx
movb $0x0, (%rax,%rcx)
movq 0x8(%rsp), %rcx
incq %rcx
cmpq %r14, %rcx
jne 0xd6990
movq %rbp, %rdi
movq %rax, %rbx
callq 0x2220
movq %rbx, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq