// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/hic.cpp
// Function: get_unitig_het_fly(ma_ug_t*, unsigned int, asg_t*, long, long, ma_hit_t_alloc*, R_to_U*, unsigned char*, unsigned int, unsigned int, unsigned int, unsigned int)
uint32_t get_unitig_het_fly(ma_ug_t* ug, uint32_t uid, asg_t* sg, /**int64_t het_cov_thres,**/
int64_t m_het_cov_thres, int64_t m_hom_cov_thres,
ma_hit_t_alloc* sources, R_to_U* ruIndex, uint8_t* r_flag, uint32_t m_het_occ, uint32_t m_het_label, 
uint32_t p_het_label, uint32_t n_het_label)
{
    ma_utg_t *u = &(ug->u.a[uid]);
    uint32_t k, i, j, rId, nv, tn, is_Unitig;
    asg_arc_t *av = NULL; ma_hit_t *h;
    int64_t R_bases = 0, C_bases = 0, cov;

    ///set
    u = &(ug->u.a[uid]);
    for (k = 0; k < u->n; k++) {
        rId = u->a[k]>>33;
        r_flag[rId] = 1;
    }
    for (i = 0; i < 2; i++) {
        nv = asg_arc_n(ug->g, (uid<<1)+i);
        av = asg_arc_a(ug->g, (uid<<1)+i);
        for (j = 0; j < nv; j++) {
            u = &(ug->u.a[av[j].v>>1]);
            for (k = 0; k < u->n; k++) {
                rId = u->a[k]>>33;
                r_flag[rId] = 2;
            }
        }
    }

    u = &(ug->u.a[uid]);
    for (k = 0; k < u->n; k++) {
        if(u->a[k] == (uint64_t)-1) continue;
        rId = u->a[k]>>33;
        R_bases += sg->seq[rId].len;
        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(sg->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 || sg->seq[tn].del == 1) continue;
            }
            if(sg->seq[tn].del == 1) continue;
            if(r_flag[tn] == 0) continue;
            if(r_flag[tn] == 1) {
                C_bases += (Get_qe((*h)) - Get_qs((*h)));
            }
            if(r_flag[tn] == 2) {
                C_bases += ((Get_qe((*h)) - Get_qs((*h)))/2);
            }
        }
    }

    ///reset
    u = &(ug->u.a[uid]);
    for (k = 0; k < u->n; k++) {
        rId = u->a[k]>>33;
        r_flag[rId] = 0;
    }
    for (i = 0; i < 2; i++) {
        nv = asg_arc_n(ug->g, (uid<<1)+i);
        av = asg_arc_a(ug->g, (uid<<1)+i);
        for (j = 0; j < nv; j++) {
            u = &(ug->u.a[av[j].v>>1]);
            for (k = 0; k < u->n; k++) {
                rId = u->a[k]>>33;
                r_flag[rId] = 0;
            }
        }
    }

    u = &(ug->u.a[uid]); cov = 0;
    if(R_bases > 0) cov = C_bases/R_bases;
    // if(uid == 9253) {
    //     fprintf(stderr, "[M::%s::uid->%u] u->n::%u, C_bases::%ld, R_bases::%ld, het_cov_thres::%ld, m_het_occ::%u\n", 
    //     __func__, uid, (uint32_t)u->n, C_bases, R_bases, het_cov_thres, m_het_occ);
    // }
    // if((cov <= (het_cov_thres*1.333333)) && (u->n >= m_het_occ)) return m_het_label; ///must het
    // if((cov >= (het_cov_thres*1.6))) return n_het_label; ///hom
    if((cov <= m_het_cov_thres) && (u->n >= m_het_occ)) return m_het_label; ///must het
    if(cov >= m_hom_cov_thres) return n_het_label; ///hom
    return p_het_label; ///potential het
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r9, 0x40(%rsp)
movq %r8, 0x28(%rsp)
movq %rcx, 0x20(%rsp)
movq %rdx, %r11
movq 0xa8(%rsp), %r14
movl %esi, %eax
imulq $0x28, %rax, %rdx
movq 0x10(%rdi), %rax
movq %rdx, 0x8(%rsp)
addq %rdx, %rax
xorl %ecx, %ecx
movl 0x10(%rax), %edx
cmpq %rdx, %rcx
jae 0xf7e30
movq 0x18(%rax), %rdx
movq (%rdx,%rcx,8), %rdx
shrq $0x21, %rdx
movb $0x1, (%r14,%rdx)
incq %rcx
jmp 0xf7e12
addl %esi, %esi
xorl %eax, %eax
cmpq $0x2, %rax
je 0xf7ea2
movq 0x18(%rdi), %r8
movq 0x28(%r8), %rcx
movq %rax, %rdx
orq %rsi, %rdx
movq (%rcx,%rdx,8), %rdx
movl %edx, %ecx
shrq $0x20, %rdx
imulq $0x18, %rdx, %rdx
addq 0x8(%r8), %rdx
xorl %r10d, %r10d
cmpq %rcx, %r10
je 0xf7e9d
imulq $0x18, %r10, %r8
movl 0x8(%rdx,%r8), %r8d
shrl %r8d
imulq $0x28, %r8, %rbx
addq 0x10(%rdi), %rbx
xorl %r8d, %r8d
movl 0x10(%rbx), %r9d
cmpq %r9, %r8
jae 0xf7e98
movq 0x18(%rbx), %r9
movq (%r9,%r8,8), %r9
shrq $0x21, %r9
movb $0x2, (%r14,%r9)
incq %r8
jmp 0xf7e79
incq %r10
jmp 0xf7e5d
incq %rax
jmp 0xf7e34
movq 0x10(%rdi), %rcx
addq 0x8(%rsp), %rcx
xorl %edx, %edx
xorl %ebx, %ebx
xorl %r15d, %r15d
movq %rsi, 0x48(%rsp)
movq %rdi, 0x38(%rsp)
movq %rcx, 0x30(%rsp)
movl 0x10(%rcx), %eax
cmpq %rax, %rdx
jae 0xf7fe9
movq 0x18(%rcx), %rax
movq (%rax,%rdx,8), %rax
cmpq $-0x1, %rax
je 0xf7fe1
movq %rdx, 0x58(%rsp)
movq %rbx, 0x60(%rsp)
shrq $0x21, %rax
movq 0x20(%r11), %rcx
movl (%rcx,%rax,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq %rcx, 0x50(%rsp)
imulq $0x18, %rax, %r13
addq 0x40(%rsp), %r13
xorl %r12d, %r12d
xorl %ebp, %ebp
movl 0xc(%r13), %eax
cmpq %rax, %rbp
jae 0xf7fc3
movq (%r13), %rbx
cmpb $0x1, 0x20(%rbx,%r12)
jne 0xf7fb7
movl 0xc(%rbx,%r12), %esi
movl %esi, 0x4(%rsp)
movq 0x20(%r11), %rax
cmpl $0x0, (%rax,%rsi,8)
js 0xf7f57
movzbl (%r14,%rsi), %eax
cmpl $0x2, %eax
je 0xf7f9f
cmpl $0x1, %eax
jne 0xf7fb7
movl 0x8(%rbx,%r12), %eax
subl (%rbx,%r12), %eax
jmp 0xf7faa
movq %r15, 0x10(%rsp)
movq %r11, %r15
movq 0xa0(%rsp), %rdi
leaq 0x4(%rsp), %rdx
leaq 0x1c(%rsp), %rcx
callq 0x6c2a2
movl 0x4(%rsp), %esi
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, %rsi
je 0xf7faf
cmpl $0x1, 0x1c(%rsp)
je 0xf7faf
movq %r15, %r11
movq 0x20(%r15), %rax
cmpl $0x0, (%rax,%rsi,8)
movq 0x10(%rsp), %r15
jns 0xf7f3d
jmp 0xf7fb7
movl 0x8(%rbx,%r12), %eax
subl (%rbx,%r12), %eax
shrl %eax
addq %rax, %r15
jmp 0xf7fb7
movq %r15, %r11
movq 0x10(%rsp), %r15
incq %rbp
addq $0x28, %r12
jmp 0xf7f0d
movq 0x60(%rsp), %rbx
addq 0x50(%rsp), %rbx
movq 0x48(%rsp), %rsi
movq 0x38(%rsp), %rdi
movq 0x30(%rsp), %rcx
movq 0x58(%rsp), %rdx
incq %rdx
jmp 0xf7ec1
movq 0x10(%rdi), %rax
addq 0x8(%rsp), %rax
xorl %ecx, %ecx
movl 0x10(%rax), %edx
cmpq %rdx, %rcx
jae 0xf8012
movq 0x18(%rax), %rdx
movq (%rdx,%rcx,8), %rdx
shrq $0x21, %rdx
movb $0x0, (%r14,%rdx)
incq %rcx
jmp 0xf7ff4
xorl %eax, %eax
cmpq $0x2, %rax
je 0xf8082
movq 0x18(%rdi), %r8
movq 0x28(%r8), %rcx
movq %rax, %rdx
orq %rsi, %rdx
movq (%rcx,%rdx,8), %rdx
movl %edx, %ecx
shrq $0x20, %rdx
imulq $0x18, %rdx, %rdx
addq 0x8(%r8), %rdx
xorl %r10d, %r10d
cmpq %rcx, %r10
je 0xf807d
imulq $0x18, %r10, %r8
movl 0x8(%rdx,%r8), %r8d
shrl %r8d
imulq $0x28, %r8, %r11
addq 0x10(%rdi), %r11
xorl %r8d, %r8d
movl 0x10(%r11), %r9d
cmpq %r9, %r8
jae 0xf8078
movq 0x18(%r11), %r9
movq (%r9,%r8,8), %r9
shrq $0x21, %r9
movb $0x0, (%r14,%r9)
incq %r8
jmp 0xf8059
incq %r10
jmp 0xf803d
incq %rax
jmp 0xf8014
movq 0x10(%rdi), %rcx
testq %rbx, %rbx
jle 0xf8095
movq %r15, %rax
cqto
idivq %rbx
jmp 0xf8097
xorl %eax, %eax
movl 0xb8(%rsp), %edx
movq 0x20(%rsp), %rsi
cmpq %rsi, %rax
jg 0xf80ba
movl 0xb0(%rsp), %esi
movq 0x8(%rsp), %rdi
cmpl %esi, 0x10(%rcx,%rdi)
jae 0xf80ce
cmpq 0x28(%rsp), %rax
movl 0xc8(%rsp), %edx
cmovll 0xc0(%rsp), %edx
movl %edx, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq