// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r8, 0x18(%rsp)
movq %rcx, 0x10(%rsp)
movq %rdx, %rcx
movq %rsi, %rax
movq %rdi, %rbx
movq (%rdi), %rdx
movq 0x8(%rdi), %rsi
movq 0x18(%rdi), %r8
movq 0x30(%rdi), %r9
movq %rax, %rdi
callq 0x175747
movq %rax, %r15
movq (%rbx), %rax
movq %rax, 0x38(%rsp)
movq 0x8(%rbx), %rax
movl 0x14(%rax), %ebx
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
pushq $0x8
popq %rsi
movq %rbx, %rdi
callq 0x2120
shlq $0x3, %rbx
movq %rax, 0x8(%rsp)
movq %rax, %rdi
movl $0xff, %esi
movq %rbx, %rdx
callq 0x20e0
movq (%r15), %rbp
testq %rbp, %rbp
jne 0x1acd0
movq $0x0, 0x28(%rsp)
movq $0x0, 0x30(%rsp)
jmp 0x1adca
movq 0x10(%r15), %r12
addq $0x10, %r12
movq $0x0, 0x30(%rsp)
movq $0x0, 0x28(%rsp)
movq 0x10(%rsp), %rdx
movq 0x8(%rsp), %rdi
movq %r15, 0x40(%rsp)
movq (%r12), %rcx
movl %ecx, %eax
shrq $0x20, %rcx
cmpq %rax, %rcx
je 0x1adbd
movb (%rdx,%rcx), %r14b
testb %r14b, %r14b
je 0x1adbd
movb (%rdx,%rax), %r8b
testb %r8b, %r8b
je 0x1adbd
movq -0x10(%r12), %rax
movq -0x8(%r12), %rbx
leaq (%rbx,%rbx), %r13
movb 0x48(%r15), %dl
xorl %ecx, %ecx
subb %dl, %cl
shrq %cl, %r13
leaq (%rax,%rax), %rsi
shrq %cl, %rsi
pushq $0x1
popq %rdx
movb %r8b, 0x20(%rsp)
testb %r8b, %r8b
js 0x1ad5d
movabsq $0x100000000, %rdx      # imm = 0x100000000
movq 0x50(%r15), %r15
andq %r15, %rax
movq %rax, %rcx
movq 0x18(%rsp), %r8
callq 0x160bc7
pushq $0x1
popq %rdx
testb %r14b, %r14b
js 0x1ad83
movabsq $0x100000000, %rdx      # imm = 0x100000000
andq %r15, %rbx
movq 0x8(%rsp), %rdi
movq %r13, %rsi
movq %rbx, %rcx
movq 0x18(%rsp), %r8
callq 0x160bc7
cmpb 0x20(%rsp), %r14b
jne 0x1ada9
incq 0x28(%rsp)
jmp 0x1adae
incq 0x30(%rsp)
movq 0x40(%rsp), %r15
movq 0x10(%rsp), %rdx
movq 0x8(%rsp), %rdi
addq $0x20, %r12
decq %rbp
jne 0x1acf9
movq 0x27a20f(%rip), %r14    # 0x294fe0
movq (%r14), %rdi
leaq 0x221944(%rip), %rsi    # 0x23c71f
leaq 0x221985(%rip), %rdx    # 0x23c767
movq 0x28(%rsp), %rcx
movq 0x30(%rsp), %r8
xorl %eax, %eax
callq 0x22b0
leaq 0x27b436(%rip), %rax    # 0x296230
movq 0x28(%rax), %rdi
movq 0x50(%rax), %rdx
xorl %esi, %esi
callq 0x20e0
movq 0x38(%rsp), %rdi
movq 0x18(%rdi), %rax
testl $0x7fffffff, 0x14(%rax) # imm = 0x7FFFFFFF
jne 0x1ae37
movq $0x0, 0x20(%rsp)
movq $0x0, 0x18(%rsp)
movq 0x8(%rsp), %r10
jmp 0x1af74
xorl %ebp, %ebp
movq $0x0, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %r10
movq 0x20(%rax), %rcx
cmpl $0x0, (%rcx,%rbp,8)
js 0x1af5f
movb (%rsi,%rbp), %cl
testb %cl, %cl
je 0x1af5f
movq 0x10(%rdi), %rbx
imulq $0x28, %rbp, %rdx
cmpl $0x0, 0xc(%rbx,%rdx)
je 0x1af5f
addq %rdx, %rbx
cmpl $0x0, 0x10(%rbx)
je 0x1af5f
testb %cl, %cl
setle %r12b
incb %r12b
xorl %r13d, %r13d
movq 0x18(%rbx), %rax
movq (%rax,%r13,8), %rdx
shrq $0x21, %rdx
movq (%r10,%rdx,8), %r9
cmpq $-0x1, %r9
je 0x1aebf
movq %r9, %r8
shrq $0x20, %r8
movl %r9d, %eax
cmpq %rax, %r8
jne 0x1aecf
incq %r13
movl 0x10(%rbx), %eax
cmpq %rax, %r13
jb 0x1ae9a
jmp 0x1af56
cmpl %r8d, %eax
movl $0x0, %eax
adcl $-0x1, %eax
orl $0x1, %eax
movsbl (%rsi,%rbp), %ecx
cmpl %ecx, %eax
jne 0x1af31
leaq 0x27b344(%rip), %rax    # 0x296230
movq 0x28(%rax), %rax
movb %r12b, (%rax,%rdx)
movq (%r14), %rdi
movq 0x18(%rbx), %rax
movq (%rax,%r13,8), %rdx
shrq $0x21, %rdx
movsbl (%rsi,%rbp), %ecx
movq (%r10,%rdx,8), %r9
movq %r9, %r8
shrq $0x20, %r8
leaq 0x221858(%rip), %rsi    # 0x23c771
xorl %eax, %eax
callq 0x22b0
movq 0x8(%rsp), %r10
movq 0x10(%rsp), %rsi
incq 0x20(%rsp)
jmp 0x1aebf
movq (%r14), %rdi
leaq 0x221864(%rip), %rsi    # 0x23c79f
xorl %eax, %eax
callq 0x22b0
movq 0x8(%rsp), %r10
movq 0x10(%rsp), %rsi
incq 0x18(%rsp)
jmp 0x1aebf
movq 0x38(%rsp), %rdi
movq 0x18(%rdi), %rax
incq %rbp
movl 0x14(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq %rcx, %rbp
jb 0x1ae55
movq %r10, %rdi
callq 0x2220
movq 0x10(%r15), %rdi
callq 0x2220
movq 0x28(%r15), %rdi
callq 0x2220
movq 0x40(%r15), %rdi
callq 0x2220
movq %r15, %rdi
callq 0x2220
movq (%r14), %rdi
leaq 0x221824(%rip), %rsi    # 0x23c7cd
leaq 0x2217b7(%rip), %rdx    # 0x23c767
movq 0x20(%rsp), %rcx
movq 0x18(%rsp), %r8
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x22b0