// File: /workspace/github_cpp_makefile_star2_O2/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, 0x50(%rsp)
movq %rdx, 0x48(%rsp)
movq %rsi, %r12
movq %rdi, 0x28(%rsp)
movq 0x18(%rdi), %r14
movl 0x14(%r14), %r15d
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andq %rax, %r15
movl 0x14(%rsi), %edi
andq %rax, %rdi
pushq $0x1
popq %rbp
movq %rbp, %rsi
callq 0x2120
movq %rax, %r13
movq %r15, %rdi
movq %rbp, %rsi
callq 0x2120
movq %rax, 0x8(%rsp)
leaq 0x1b9e81(%rip), %rax    # 0x253758
movslq 0x13c(%rax), %rcx
movq %rcx, 0x40(%rsp)
movslq 0x140(%rax), %rax
movq %rax, 0x38(%rsp)
xorl %edx, %edx
movsd 0x149c07(%rip), %xmm0   # 0x1e3500
movsd 0x149c3f(%rip), %xmm1   # 0x1e3540
movq %r14, 0x20(%rsp)
movq %r15, 0x18(%rsp)
cmpq %r15, %rdx
je 0x99abb
movq 0x20(%r14), %rax
cmpl $0x0, (%rax,%rdx,8)
js 0x99ab3
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rsi
imulq $0x28, %rdx, %rax
cmpl $0x0, 0xc(%rsi,%rax)
je 0x99ab3
addq %rax, %rsi
movl 0x10(%rsi), %r10d
xorl %eax, %eax
cmpq %rax, %r10
je 0x9995f
movq 0x18(%rsi), %rcx
movq (%rcx,%rax,8), %rcx
shrq $0x21, %rcx
movb $0x1, (%r13,%rcx)
incq %rax
jmp 0x99943
movq %rdx, 0x30(%rsp)
xorl %ebp, %ebp
movl $0x0, 0x4(%rsp)
movq %rsi, 0x10(%rsp)
cmpq %r10, %rbp
je 0x99a5c
movq 0x18(%rsi), %rax
movq (%rax,%rbp,8), %rax
shrq $0x21, %rax
imulq $0xc, %rax, %rcx
movq 0x48(%rsp), %rsi
movl 0x4(%rsi,%rcx), %edx
movl (%rsi,%rcx), %ecx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl %esi, %ecx
subl %ecx, %edx
imulq $0x18, %rax, %rax
movq 0x50(%rsp), %rcx
leaq (%rcx,%rax), %rdi
movl 0xc(%rcx,%rax), %eax
imulq $0x28, %rax, %rcx
xorl %r8d, %r8d
xorl %eax, %eax
xorl %esi, %esi
cmpq %r8, %rcx
je 0x99a18
movq (%rdi), %r11
cmpb $0x1, 0x20(%r11,%r8)
jne 0x99a12
movl 0xc(%r11,%r8), %r14d
movq 0x20(%r12), %r15
cmpl $0x0, (%r15,%r14,8)
js 0x999f4
movl 0x8(%r11,%r8), %r15d
subl (%r11,%r8), %r15d
cmpb $0x0, (%r13,%r14)
je 0x99a0f
addq %r15, %rax
jmp 0x99a12
cmpq %r14, (%rbx)
jbe 0x99a12
movq 0x8(%rbx), %r9
movslq (%r9,%r14,4), %r14
testq %r14, %r14
js 0x99a12
cmpl $0x0, (%r15,%r14,8)
jns 0x999de
jmp 0x99a12
addq %r15, %rsi
addq $0x28, %r8
jmp 0x999bd
addq %rsi, %rax
cvtsi2sd %rsi, %xmm2
cvtsi2sd %rax, %xmm3
mulsd %xmm1, %xmm3
ucomisd %xmm2, %xmm3
ja 0x99a4f
movl %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 %rbp
jmp 0x99973
movl 0x4(%rsp), %ecx
movl %ecx, %eax
cvtsi2sd %rax, %xmm2
cvtsi2sd %r10, %xmm3
mulsd %xmm0, %xmm3
ucomisd %xmm2, %xmm3
setbe %al
testl %ecx, %ecx
setne %cl
andb %al, %cl
movq 0x8(%rsp), %rax
movq 0x30(%rsp), %rdx
movb %cl, (%rax,%rdx)
xorl %eax, %eax
movq 0x20(%rsp), %r14
movq 0x18(%rsp), %r15
cmpq %rax, %r10
je 0x99ab3
movq 0x18(%rsi), %rcx
movq (%rcx,%rax,8), %rcx
shrq $0x21, %rcx
movb $0x0, (%r13,%rcx)
incq %rax
jmp 0x99a97
incq %rdx
jmp 0x9990b
movq %r13, %rdi
callq 0x2220
movq 0x8(%rsp), %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq