// File: chhylp123[P]hifiasm/hic.cpp
// Function: tag_reads(ha_ug_index*, kvec_pe_hit*, bubble_type*, signed char*, ma_hit_t_alloc*)
void tag_reads(ha_ug_index* idx, kvec_pe_hit *u_hits, bubble_type* bub, int8_t *s, ma_hit_t_alloc* sources)
{
    kvec_pe_hit *r_hits = get_r_hits_for_trio(u_hits, idx->read_g, idx->ug, bub, idx->uID_bits, idx->pos_mode);
    uint64_t *r_tag = NULL, i, k, srid, erid, /**rid, occ, a_occ, **/flag = AMBIGU, cons = 0, incons = 0/**, max, min**/; 
    ma_utg_t *u = NULL;
    ma_ug_t* ug = idx->ug;
    int8_t ss, es, sr;
    CALLOC(r_tag, idx->read_g->n_seq);

    cons = 0, incons = 0; 
    memset(r_tag, -1, sizeof(uint64_t)*idx->read_g->n_seq);
    for (i = 0; i < r_hits->a.n; i++)
    {
        srid = get_hit_suid(*r_hits, i); 
        erid = get_hit_euid(*r_hits, i); 
        ss = s[r_hits->a.a[i].id>>32];
        es = s[(uint32_t)r_hits->a.a[i].id];
        if(ss == 0 || es == 0) continue;
        if((r_hits->a.a[i].id>>32) == ((uint32_t)r_hits->a.a[i].id)) continue;

        set_tag_pre_read(r_tag, srid, es < 0? 1 : ((uint64_t)1<<32), get_hit_spos(*r_hits, i), sources);
        set_tag_pre_read(r_tag, erid, ss < 0? 1 : ((uint64_t)1<<32), get_hit_epos(*r_hits, i), sources);
    
        if(ss == es) cons++;
        else incons++;
    }       
    
    /**
    for (k = 0; k < ug->u.n; k++)
    {
        if(ug->g->seq[k].del) continue;
        u = &(ug->u.a[k]); occ = a_occ = 0;
        for (i = 0; i < u->n; i++)
        {
            rid = u->a[i]>>33; 
            if(r_tag[rid] == (uint64_t)-1) continue;
            max = MAX((r_tag[rid]>>32), ((uint32_t)r_tag[rid]));
            min = MIN((r_tag[rid]>>32), ((uint32_t)r_tag[rid]));
            ///if(max <= (total*0.7) && max != total)
            if(max - min <= max *0.1)
            {
                fprintf(stderr, "max-%lu, min-%lu\n", max, min);
                a_occ++;
            }
            occ++;
        }
        if(occ == 0) continue;
        if(a_occ > occ*0.85) s[k] = 0, filter++, fprintf(stderr, "k: %lu, a_occ: %lu, occ: %lu\n", k, a_occ, occ);
    }

    fprintf(stderr, "[M::%s::] # consistent Hi-C pairs: %lu, # inconsistent Hi-C pairs: %lu, # filter: %lu\n", 
                                                                            __func__, cons, incons, filter);
    
    memset(r_tag, -1, sizeof(uint64_t)*idx->read_g->n_seq);
    for (i = 0; i < r_hits->a.n; i++)
    {
        srid = get_hit_suid(*r_hits, i); 
        erid = get_hit_euid(*r_hits, i); 
        ss = s[r_hits->a.a[i].id>>32];
        es = s[(uint32_t)r_hits->a.a[i].id];
        if(ss == 0 || es == 0) continue;
        if((r_hits->a.a[i].id>>32) == ((uint32_t)r_hits->a.a[i].id)) continue;

        set_tag_pre_read(r_tag, srid, es > 0? 1 : ((uint64_t)1<<32), get_hit_spos(*r_hits, i), sources);
        set_tag_pre_read(r_tag, erid, ss > 0? 1 : ((uint64_t)1<<32), get_hit_epos(*r_hits, i), sources);
    }
    **/  
    fprintf(stderr, "[M::%s::] # consistent Hi-C pairs: %lu, # inconsistent Hi-C pairs: %lu\n", 
                                                                            __func__, cons, incons);

    cons = 0, incons = 0; 
    memset(R_INF.trio_flag, AMBIGU, R_INF.total_reads * sizeof(uint8_t));
    for (i = 0; i < ug->g->n_seq; i++)
    {
        if(ug->g->seq[i].del) continue;

        flag = 0;
        if(s[i] == 0) continue;
        flag = (s[i] > 0? FATHER:MOTHER);

        u = &ug->u.a[i];
        if(u->m == 0) continue;
        for (k = 0; k < u->n; k++)
        {
            if(r_tag[u->a[k]>>33] == (uint64_t)-1) continue;
            if((r_tag[u->a[k]>>33]>>32) == ((uint32_t)r_tag[u->a[k]>>33])) continue;
            sr = (r_tag[u->a[k]>>33]>>32) > ((uint32_t)r_tag[u->a[k]>>33])?1:-1;
            if(sr == s[i])
            {
                R_INF.trio_flag[u->a[k]>>33] = flag;
                fprintf(stderr, "*rid: %lu, s[i]: %d, 1-occ: %u, (-1)-occ: %u\n", u->a[k]>>33, s[i], (uint32_t)(r_tag[u->a[k]>>33]>>32), ((uint32_t)r_tag[u->a[k]>>33]));
                // cons_occ += (r_tag[u->a[k]>>33]>>32) + ((uint32_t)r_tag[u->a[k]>>33]);
                cons++;
            }
            else
            {
                // R_INF.trio_flag[u->a[k]>>33] = -flag;
                fprintf(stderr, "#rid: %lu, s[i]: %d, 1-occ: %u, (-1)-occ: %u\n", u->a[k]>>33, s[i], (uint32_t)(r_tag[u->a[k]>>33]>>32), ((uint32_t)r_tag[u->a[k]>>33]));
                // incons_occ += (r_tag[u->a[k]>>33]>>32) + ((uint32_t)r_tag[u->a[k]>>33]);
                incons++;
            }
        }
    }

    free(r_tag);
    kv_destroy(r_hits->a);
    kv_destroy(r_hits->idx);
    kv_destroy(r_hits->occ);
    free(r_hits);
    fprintf(stderr, "[M::%s::] # consistent reads: %lu, # inconsistent reads: %lu\n", __func__, cons, incons);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x8(%rbp), %rax
movq (%rax), %rdx
movq -0x18(%rbp), %rcx
movq -0x8(%rbp), %rax
movq 0x18(%rax), %r8
movq -0x8(%rbp), %rax
movq 0x30(%rax), %r9
callq 0x2334b0
movq %rax, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
movq $0x0, -0x78(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x80(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %edi
movl $0x8, %esi
callq 0x2140
movq %rax, -0x38(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
movq -0x38(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %edx
shlq $0x3, %rdx
movl $0xff, %esi
callq 0x2100
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq -0x30(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x207abf
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x40(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rax
shlq %rax
movq -0x30(%rbp), %rdx
movl $0x40, %ecx
subq 0x48(%rdx), %rcx
shrq %cl, %rax
movq %rax, -0x50(%rbp)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x40(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
shlq %rax
movq -0x30(%rbp), %rdx
movl $0x40, %ecx
subq 0x48(%rdx), %rcx
shrq %cl, %rax
movq %rax, -0x58(%rbp)
movq -0x20(%rbp), %rax
movq -0x30(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0x40(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
movq 0x10(%rcx), %rcx
shrq $0x20, %rcx
movb (%rax,%rcx), %al
movb %al, -0x81(%rbp)
movq -0x20(%rbp), %rax
movq -0x30(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0x40(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
movq 0x10(%rcx), %rcx
movl %ecx, %ecx
movb (%rax,%rcx), %al
movb %al, -0x82(%rbp)
movsbl -0x81(%rbp), %eax
cmpl $0x0, %eax
je 0x2079a4
movsbl -0x82(%rbp), %eax
cmpl $0x0, %eax
jne 0x2079a9
jmp 0x207aae
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x40(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
shrq $0x20, %rax
movq -0x30(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0x40(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
movq 0x10(%rcx), %rcx
movl %ecx, %ecx
cmpq %rcx, %rax
jne 0x2079e7
jmp 0x207aae
movq -0x38(%rbp), %rdi
movq -0x50(%rbp), %rsi
movsbl -0x82(%rbp), %ecx
movabsq $0x100000000, %rdx      # imm = 0x100000000
movl $0x1, %eax
cmpl $0x0, %ecx
cmovlq %rax, %rdx
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x40(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq -0x30(%rbp), %rax
andq 0x50(%rax), %rcx
movq -0x28(%rbp), %r8
callq 0x2076d0
movq -0x38(%rbp), %rdi
movq -0x58(%rbp), %rsi
movsbl -0x81(%rbp), %ecx
movabsq $0x100000000, %rdx      # imm = 0x100000000
movl $0x1, %eax
cmpl $0x0, %ecx
cmovlq %rax, %rdx
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x40(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rcx
movq -0x30(%rbp), %rax
andq 0x50(%rax), %rcx
movq -0x28(%rbp), %r8
callq 0x2076d0
movsbl -0x81(%rbp), %eax
movsbl -0x82(%rbp), %ecx
cmpl %ecx, %eax
jne 0x207aa0
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x207aac
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x207aae
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x2078d2
movq 0x1bc51a(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
movq -0x68(%rbp), %rcx
movq -0x70(%rbp), %r8
leaq 0x1858a8(%rip), %rsi    # 0x38d380
leaq 0x1858e9(%rip), %rdx    # 0x38d3c8
movb $0x0, %al
callq 0x22a0
movq $0x0, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
leaq 0x1bd7d3(%rip), %rax    # 0x3c52d0
movq 0x28(%rax), %rdi
leaq 0x1bd7c8(%rip), %rax    # 0x3c52d0
movq 0x50(%rax), %rdx
shlq $0x0, %rdx
xorl %esi, %esi
callq 0x2100
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq -0x80(%rbp), %rcx
movq 0x18(%rcx), %rcx
movl 0x14(%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl %ecx, %ecx
cmpq %rcx, %rax
jae 0x207e1a
movq -0x80(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movq -0x40(%rbp), %rcx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x207b5f
jmp 0x207e09
movq $0x0, -0x60(%rbp)
movq -0x20(%rbp), %rax
movq -0x40(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x207b7d
jmp 0x207e09
movq -0x20(%rbp), %rax
movq -0x40(%rbp), %rcx
movsbl (%rax,%rcx), %edx
movl $0x2, %eax
movl $0x1, %ecx
cmpl $0x0, %edx
cmovgl %ecx, %eax
cltq
movq %rax, -0x60(%rbp)
movq -0x80(%rbp), %rax
movq 0x10(%rax), %rax
imulq $0x28, -0x40(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpl $0x0, 0xc(%rax)
jne 0x207bc2
jmp 0x207e09
movq $0x0, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq -0x78(%rbp), %rcx
movl 0x10(%rcx), %ecx
cmpq %rcx, %rax
jae 0x207e07
movq -0x38(%rbp), %rax
movq -0x78(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq -0x48(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x21, %rcx
cmpq $-0x1, (%rax,%rcx,8)
jne 0x207c02
jmp 0x207df6
movq -0x38(%rbp), %rax
movq -0x78(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq -0x48(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x21, %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movq -0x38(%rbp), %rcx
movq -0x78(%rbp), %rdx
movq 0x18(%rdx), %rdx
movq -0x48(%rbp), %rsi
movq (%rdx,%rsi,8), %rdx
shrq $0x21, %rdx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %ecx
cmpq %rcx, %rax
jne 0x207c4a
jmp 0x207df6
movq -0x38(%rbp), %rax
movq -0x78(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq -0x48(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x21, %rcx
movq (%rax,%rcx,8), %rdx
shrq $0x20, %rdx
movq -0x38(%rbp), %rax
movq -0x78(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq -0x48(%rbp), %rsi
movq (%rcx,%rsi,8), %rcx
shrq $0x21, %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
movl %eax, %esi
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl $0x1, %ecx
cmpq %rsi, %rdx
cmoval %ecx, %eax
movb %al, -0x83(%rbp)
movsbl -0x83(%rbp), %eax
movq -0x20(%rbp), %rcx
movq -0x40(%rbp), %rdx
movsbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
jne 0x207d6e
movq -0x60(%rbp), %rax
movb %al, %dl
leaq 0x1bd608(%rip), %rax    # 0x3c52d0
movq 0x28(%rax), %rax
movq -0x78(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq -0x48(%rbp), %rsi
movq (%rcx,%rsi,8), %rcx
shrq $0x21, %rcx
movb %dl, (%rax,%rcx)
movq 0x1bc2f6(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
movq -0x78(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
shrq $0x21, %rdx
movq -0x20(%rbp), %rax
movq -0x40(%rbp), %rcx
movsbl (%rax,%rcx), %ecx
movq -0x38(%rbp), %rax
movq -0x78(%rbp), %rsi
movq 0x18(%rsi), %rsi
movq -0x48(%rbp), %r8
movq (%rsi,%r8,8), %rsi
shrq $0x21, %rsi
movq (%rax,%rsi,8), %rax
shrq $0x20, %rax
movl %eax, %r8d
movq -0x38(%rbp), %rax
movq -0x78(%rbp), %rsi
movq 0x18(%rsi), %rsi
movq -0x48(%rbp), %r9
movq (%rsi,%r9,8), %rsi
shrq $0x21, %rsi
movq (%rax,%rsi,8), %rax
movl %eax, %r9d
leaq 0x18567c(%rip), %rsi    # 0x38d3d2
movb $0x0, %al
callq 0x22a0
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x207df4
movq 0x1bc26b(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
movq -0x78(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
shrq $0x21, %rdx
movq -0x20(%rbp), %rax
movq -0x40(%rbp), %rcx
movsbl (%rax,%rcx), %ecx
movq -0x38(%rbp), %rax
movq -0x78(%rbp), %rsi
movq 0x18(%rsi), %rsi
movq -0x48(%rbp), %r8
movq (%rsi,%r8,8), %rsi
shrq $0x21, %rsi
movq (%rax,%rsi,8), %rax
shrq $0x20, %rax
movl %eax, %r8d
movq -0x38(%rbp), %rax
movq -0x78(%rbp), %rsi
movq 0x18(%rsi), %rsi
movq -0x48(%rbp), %r9
movq (%rsi,%r9,8), %rsi
shrq $0x21, %rsi
movq (%rax,%rsi,8), %rax
movl %eax, %r9d
leaq 0x18561f(%rip), %rsi    # 0x38d400
movb $0x0, %al
callq 0x22a0
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x207df6
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x207bca
jmp 0x207e09
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x207b1f
movq -0x38(%rbp), %rdi
callq 0x2210
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x2210
movq -0x30(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x2210
movq -0x30(%rbp), %rax
movq 0x40(%rax), %rdi
callq 0x2210
movq -0x30(%rbp), %rdi
callq 0x2210
movq 0x1bc186(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
movq -0x68(%rbp), %rcx
movq -0x70(%rbp), %r8
leaq 0x1855c2(%rip), %rsi    # 0x38d42e
leaq 0x185555(%rip), %rdx    # 0x38d3c8
movb $0x0, %al
callq 0x22a0
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)