// File: 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
movq %rsp, %rbp
subq $0x90, %rsp
movl 0x38(%rbp), %eax
movl 0x30(%rbp), %eax
movl 0x28(%rbp), %eax
movl 0x20(%rbp), %eax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x14(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x0, -0x78(%rbp)
movq $0x0, -0x80(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x14(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq -0x40(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0x1c7049
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x44(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movl %eax, -0x50(%rbp)
movq 0x18(%rbp), %rax
movl -0x50(%rbp), %ecx
movb $0x1, (%rax,%rcx)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x1c7011
movl $0x0, -0x48(%rbp)
cmpl $0x2, -0x48(%rbp)
jae 0x1c713f
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x28(%rax), %rax
movl -0x14(%rbp), %ecx
shll %ecx
addl -0x48(%rbp), %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x54(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x8(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq 0x28(%rcx), %rcx
movl -0x14(%rbp), %edx
shll %edx
addl -0x48(%rbp), %edx
movl %edx, %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x54(%rbp), %eax
jae 0x1c712f
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x68(%rbp), %rcx
movl -0x4c(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl 0x8(%rcx), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq -0x40(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0x1c7122
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x44(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movl %eax, -0x50(%rbp)
movq 0x18(%rbp), %rax
movl -0x50(%rbp), %ecx
movb $0x2, (%rax,%rcx)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x1c70ea
jmp 0x1c7124
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x1c70b3
jmp 0x1c7131
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x1c7050
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x14(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq -0x40(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0x1c7308
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x44(%rbp), %ecx
cmpq $-0x1, (%rax,%rcx,8)
jne 0x1c7183
jmp 0x1c72fa
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x44(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movl %eax, -0x50(%rbp)
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x50(%rbp), %ecx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
addq -0x78(%rbp), %rax
movq %rax, -0x78(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movq -0x38(%rbp), %rcx
movl -0x50(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
cmpq %rcx, %rax
jae 0x1c72f8
movq -0x38(%rbp), %rax
movl -0x50(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movl -0x4c(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movzbl 0x20(%rax), %eax
cmpl $0x1, %eax
je 0x1c720b
jmp 0x1c72ea
movq -0x70(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x58(%rbp)
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x58(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x1, %eax
jne 0x1c7268
movq 0x10(%rbp), %rdi
movl -0x58(%rbp), %esi
leaq -0x58(%rbp), %rdx
leaq -0x5c(%rbp), %rcx
callq 0xd3d50
cmpl $-0x1, -0x58(%rbp)
je 0x1c7261
cmpl $0x1, -0x5c(%rbp)
je 0x1c7261
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x58(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x1, %eax
jne 0x1c7266
jmp 0x1c72ea
jmp 0x1c7268
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x58(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x1, %eax
jne 0x1c7280
jmp 0x1c72ea
movq 0x18(%rbp), %rax
movl -0x58(%rbp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x1c7292
jmp 0x1c72ea
movq 0x18(%rbp), %rax
movl -0x58(%rbp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x1, %eax
jne 0x1c72bc
movq -0x70(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x70(%rbp), %rcx
movq (%rcx), %rcx
subl %ecx, %eax
movl %eax, %eax
addq -0x80(%rbp), %rax
movq %rax, -0x80(%rbp)
movq 0x18(%rbp), %rax
movl -0x58(%rbp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x2, %eax
jne 0x1c72e8
movq -0x70(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x70(%rbp), %rcx
movq (%rcx), %rcx
subl %ecx, %eax
shrl %eax
movl %eax, %eax
addq -0x80(%rbp), %rax
movq %rax, -0x80(%rbp)
jmp 0x1c72ea
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x1c71bd
jmp 0x1c72fa
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x1c715c
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x14(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq -0x40(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0x1c735d
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x44(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movl %eax, -0x50(%rbp)
movq 0x18(%rbp), %rax
movl -0x50(%rbp), %ecx
movb $0x0, (%rax,%rcx)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x1c7325
movl $0x0, -0x48(%rbp)
cmpl $0x2, -0x48(%rbp)
jae 0x1c7453
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x28(%rax), %rax
movl -0x14(%rbp), %ecx
shll %ecx
addl -0x48(%rbp), %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x54(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x8(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq 0x28(%rcx), %rcx
movl -0x14(%rbp), %edx
shll %edx
addl -0x48(%rbp), %edx
movl %edx, %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x54(%rbp), %eax
jae 0x1c7443
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x68(%rbp), %rcx
movl -0x4c(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl 0x8(%rcx), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq -0x40(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0x1c7436
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x44(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movl %eax, -0x50(%rbp)
movq 0x18(%rbp), %rax
movl -0x50(%rbp), %ecx
movb $0x0, (%rax,%rcx)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x1c73fe
jmp 0x1c7438
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x1c73c7
jmp 0x1c7445
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x1c7364
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x14(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq $0x0, -0x88(%rbp)
cmpq $0x0, -0x78(%rbp)
jle 0x1c748c
movq -0x80(%rbp), %rax
cqto
idivq -0x78(%rbp)
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
cmpq -0x28(%rbp), %rax
jg 0x1c74ad
movq -0x40(%rbp), %rax
movl 0x10(%rax), %eax
cmpl 0x20(%rbp), %eax
jb 0x1c74ad
movl 0x28(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x1c74c8
movq -0x88(%rbp), %rax
cmpq -0x30(%rbp), %rax
jl 0x1c74c2
movl 0x38(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x1c74c8
movl 0x30(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)