// File: /workspace/github_cpp_makefile_star2_O1/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 $0x68, %rsp
movq %r8, %rbx
movq %rcx, %r15
movq %rdx, 0x58(%rsp)
movq %rsi, %r12
movq %rdi, 0x38(%rsp)
movq 0x18(%rdi), %r14
movl 0x14(%r14), %r13d
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andq %rax, %r13
movl 0x14(%rsi), %edi
andq %rax, %rdi
movl $0x1, %esi
callq 0x2120
movq %rax, %rbp
movl $0x1, %esi
movq %r13, %rdi
callq 0x2120
movq %rax, 0x10(%rsp)
testq %r13, %r13
je 0xd585a
movq 0x20(%r14), %rsi
leaq 0x1b5151(%rip), %rax    # 0x28a758
movslq 0x13c(%rax), %rcx
movq %rcx, 0x50(%rsp)
movslq 0x140(%rax), %rax
movq %rax, 0x48(%rsp)
movl %r13d, %edi
xorl %r8d, %r8d
movsd 0x14fe83(%rip), %xmm0   # 0x2254b0
movsd 0x14fec3(%rip), %xmm1   # 0x2254f8
movq %r15, 0x18(%rsp)
movq %rsi, 0x28(%rsp)
movq %rdi, 0x20(%rsp)
cmpl $0x0, (%rsi,%r8,8)
js 0xd584e
movq 0x38(%rsp), %rax
movq 0x10(%rax), %rax
leaq (%r8,%r8,4), %rcx
cmpl $0x0, 0xc(%rax,%rcx,8)
je 0xd584e
leaq (%rax,%rcx,8), %rsi
movl 0x10(%rsi), %r11d
testq %r11, %r11
movq %r8, 0x40(%rsp)
movq %rsi, 0x30(%rsp)
je 0xd57e1
movq 0x18(%rsi), %rax
xorl %ecx, %ecx
movq (%rax,%rcx,8), %rdx
shrq $0x21, %rdx
movb $0x1, (%rbp,%rdx)
incq %rcx
cmpq %rcx, %r11
jne 0xd5688
movq 0x18(%rsi), %rax
movq %rax, 0x60(%rsp)
xorl %r13d, %r13d
movl $0x0, 0xc(%rsp)
movq 0x60(%rsp), %rax
movq (%rax,%r13,8), %rax
shrq $0x21, %rax
leaq (%rax,%rax,2), %rdx
movl 0xc(%r15,%rdx,8), %eax
testq %rax, %rax
je 0xd5779
leaq (%r15,%rdx,8), %rsi
movq (%rsi), %rsi
shlq $0x3, %rax
leaq (%rax,%rax,4), %rdi
xorl %r15d, %r15d
xorl %r10d, %r10d
xorl %eax, %eax
cmpb $0x1, 0x20(%rsi,%r15)
jne 0xd576a
movl 0xc(%rsi,%r15), %r8d
movq 0x20(%r12), %r9
cmpl $0x0, (%r9,%r8,8)
movl %r8d, %r14d
js 0xd5723
movl %r14d, %r14d
cmpl $0x0, (%r9,%r14,8)
js 0xd576a
movl 0x8(%rsi,%r15), %r8d
subl (%rsi,%r15), %r8d
cmpb $0x0, (%rbp,%r14)
je 0xd5767
addq %r8, %rax
jmp 0xd576a
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
cmpq %r8, (%rbx)
jbe 0xd574c
movq 0x8(%rbx), %rcx
movl (%rcx,%r8,4), %r8d
cmpl $-0x1, %r8d
je 0xd574c
movl %r8d, %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
shrl $0x1f, %r8d
jmp 0xd574f
xorl %r8d, %r8d
cmpl $-0x1, %r14d
sete %cl
orb %r8b, %cl
jne 0xd576a
movl %r14d, %ecx
cmpl $0x0, (%r9,%rcx,8)
jns 0xd5703
jmp 0xd576a
addq %r8, %r10
addq $0x28, %r15
cmpq %r15, %rdi
jne 0xd56e7
jmp 0xd577e
xorl %eax, %eax
xorl %r10d, %r10d
addq %r10, %rax
xorps %xmm2, %xmm2
cvtsi2sd %r10, %xmm2
xorps %xmm3, %xmm3
cvtsi2sd %rax, %xmm3
mulsd %xmm1, %xmm3
ucomisd %xmm2, %xmm3
ja 0xd57ce
movq 0x58(%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 0x50(%rsp), %rax
setge %cl
cmpq 0x48(%rsp), %rax
setle %al
andb %cl, %al
movzbl %al, %eax
addl %eax, 0xc(%rsp)
movq 0x18(%rsp), %r15
incq %r13
cmpq %r11, %r13
jne 0xd56b1
jmp 0xd57e9
movl $0x0, 0xc(%rsp)
movl 0xc(%rsp), %ecx
movl %ecx, %eax
xorps %xmm2, %xmm2
cvtsi2sd %rax, %xmm2
xorps %xmm3, %xmm3
cvtsi2sd %r11, %xmm3
mulsd %xmm0, %xmm3
ucomisd %xmm2, %xmm3
setbe %al
testl %ecx, %ecx
setne %cl
andb %al, %cl
movq 0x10(%rsp), %rax
movq 0x40(%rsp), %r8
movb %cl, (%rax,%r8)
testq %r11, %r11
movq 0x28(%rsp), %rsi
movq 0x20(%rsp), %rdi
je 0xd584e
movq 0x30(%rsp), %rax
movq 0x18(%rax), %rax
xorl %ecx, %ecx
movq (%rax,%rcx,8), %rdx
shrq $0x21, %rdx
movb $0x0, (%rbp,%rdx)
incq %rcx
cmpq %rcx, %r11
jne 0xd5839
incq %r8
cmpq %rdi, %r8
jne 0xd5644
movq %rbp, %rdi
callq 0x2220
movq 0x10(%rsp), %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq