// File: /workspace/github_cpp_makefile_star2_O2/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, 0x10(%rsp)
movq %rcx, 0x38(%rsp)
movq %rdx, %rcx
movq %rsi, %rax
movq %rdi, %r15
movq (%rdi), %rdx
movq 0x8(%rdi), %rsi
movq 0x18(%rdi), %r8
movq 0x30(%rdi), %r9
movq %rax, %rdi
callq 0x131c74
movq %rax, %rbx
movq (%r15), %rax
movq %rax, 0x40(%rsp)
movq 0x8(%r15), %rax
movl 0x14(%rax), %r13d
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
pushq $0x8
popq %rsi
movq %r13, %rdi
callq 0x2120
shlq $0x3, %r13
movq %rax, 0x18(%rsp)
movq %rax, %rdi
movl $0xff, %esi
movq %r13, %rdx
callq 0x20e0
movq (%rbx), %r12
pushq $0x10
popq %rdi
movq $0x0, 0x30(%rsp)
movq $0x0, 0x28(%rsp)
movq %rbx, 0x20(%rsp)
subq $0x1, %r12
jb 0x11ad0e
movq 0x10(%rbx), %rcx
movq (%rcx,%rdi), %rdx
movl %edx, %eax
shrq $0x20, %rdx
cmpq %rax, %rdx
je 0x11ad05
movq 0x38(%rsp), %rsi
movb (%rsi,%rdx), %r14b
testb %r14b, %r14b
je 0x11ad05
movb (%rsi,%rax), %r15b
testb %r15b, %r15b
je 0x11ad05
movq -0x10(%rcx,%rdi), %rax
movq %rdi, 0x8(%rsp)
movq -0x8(%rcx,%rdi), %r13
leaq (,%r13,2), %rbp
movb 0x48(%rbx), %dl
xorl %ecx, %ecx
subb %dl, %cl
shrq %cl, %rbp
leaq (%rax,%rax), %rsi
shrq %cl, %rsi
pushq $0x1
popq %rdx
testb %r15b, %r15b
js 0x11aca7
movabsq $0x100000000, %rdx      # imm = 0x100000000
movq 0x50(%rbx), %rbx
andq %rbx, %rax
movq 0x18(%rsp), %rdi
movq %rax, %rcx
movq 0x10(%rsp), %r8
callq 0x11ab2f
pushq $0x1
popq %rdx
testb %r14b, %r14b
js 0x11acd2
movabsq $0x100000000, %rdx      # imm = 0x100000000
andq %rbx, %r13
movq 0x18(%rsp), %rdi
movq %rbp, %rsi
movq %r13, %rcx
movq 0x10(%rsp), %r8
callq 0x11ab2f
cmpb %r15b, %r14b
jne 0x11acf6
incq 0x30(%rsp)
jmp 0x11acfb
incq 0x28(%rsp)
movq 0x20(%rsp), %rbx
movq 0x8(%rsp), %rdi
addq $0x20, %rdi
jmp 0x11ac2d
movq 0x1382cb(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
leaq 0xd38f0(%rip), %rsi     # 0x1ee60f
leaq 0xd3931(%rip), %rdx     # 0x1ee657
movq 0x30(%rsp), %rcx
movq 0x28(%rsp), %r8
xorl %eax, %eax
callq 0x22b0
leaq 0x1394f2(%rip), %rax    # 0x254230
movq 0x28(%rax), %rdi
movq 0x50(%rax), %rdx
xorl %esi, %esi
callq 0x20e0
movq $0x0, 0x10(%rsp)
movq $0x0, 0x8(%rsp)
xorl %ebp, %ebp
movq 0x38(%rsp), %r14
movq 0x40(%rsp), %r15
movq 0x18(%rsp), %rsi
movq 0x18(%r15), %rax
movl 0x14(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq %rcx, %rbp
jae 0x11ae8d
movq 0x20(%rax), %rax
cmpl $0x0, (%rax,%rbp,8)
js 0x11ae85
movb (%r14,%rbp), %al
testb %al, %al
je 0x11ae85
movq 0x10(%r15), %rbx
imulq $0x28, %rbp, %rcx
cmpl $0x0, 0xc(%rbx,%rcx)
je 0x11ae85
addq %rcx, %rbx
testb %al, %al
setle %r12b
incb %r12b
xorl %r13d, %r13d
movl 0x10(%rbx), %eax
cmpq %rax, %r13
jae 0x11ae85
movq 0x18(%rbx), %rax
movq (%rax,%r13,8), %rdx
shrq $0x21, %rdx
movq (%rsi,%rdx,8), %r9
cmpq $-0x1, %r9
je 0x11adf3
movq %r9, %r8
shrq $0x20, %r8
movl %r9d, %eax
cmpq %rax, %r8
jne 0x11adf8
incq %r13
jmp 0x11adc2
cmpl %r8d, %eax
movl $0x0, %eax
adcl $-0x1, %eax
orl $0x1, %eax
movsbl (%r14,%rbp), %ecx
cmpl %ecx, %eax
jne 0x11ae5e
leaq 0x13941a(%rip), %rax    # 0x254230
movq 0x28(%rax), %rax
movb %r12b, (%rax,%rdx)
movq 0x1381bb(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
movq 0x18(%rbx), %rax
movq (%rax,%r13,8), %rdx
shrq $0x21, %rdx
movsbl (%r14,%rbp), %ecx
movq (%rsi,%rdx,8), %r9
movq %r9, %r8
shrq $0x20, %r8
leaq 0xd3816(%rip), %rsi     # 0x1ee661
xorl %eax, %eax
callq 0x22b0
movq 0x18(%rsp), %rsi
incq 0x10(%rsp)
jmp 0x11adf3
movq 0x13817b(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
leaq 0xd3820(%rip), %rsi     # 0x1ee68f
xorl %eax, %eax
callq 0x22b0
movq 0x18(%rsp), %rsi
incq 0x8(%rsp)
jmp 0x11adf3
incq %rbp
jmp 0x11ad70
movq %rsi, %rdi
callq 0x2220
movq 0x20(%rsp), %rbx
movq 0x10(%rbx), %rdi
callq 0x2220
movq 0x28(%rbx), %rdi
callq 0x2220
movq 0x40(%rbx), %rdi
callq 0x2220
movq %rbx, %rdi
callq 0x2220
movq 0x13811c(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
leaq 0xd37ef(%rip), %rsi     # 0x1ee6bd
leaq 0xd3782(%rip), %rdx     # 0x1ee657
movq 0x10(%rsp), %rcx
movq 0x8(%rsp), %r8
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x22b0