// File: /workspace/github_cpp_makefile_star2_O1/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 $0x38, %rsp
movq %r8, 0x10(%rsp)
movq %rcx, %r15
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 0x16ec38
movq %rax, %r14
movq (%rbx), %rax
movq %rax, 0x18(%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, (%rsp)
movq %rax, %rdi
movl $0xff, %esi
movq %rbx, %rdx
callq 0x20e0
movq (%r14), %rbp
testq %rbp, %rbp
movq %r14, 0x28(%rsp)
jne 0x1b40c
movq $0x0, 0x20(%rsp)
movq $0x0, 0x8(%rsp)
movq 0x26ec3d(%rip), %r14    # 0x289fe0
movq (%r14), %rdi
leaq 0x215166(%rip), %rsi    # 0x230513
leaq 0x2151a7(%rip), %rdx    # 0x23055b
movq 0x20(%rsp), %rcx
movq 0x8(%rsp), %r8
xorl %eax, %eax
callq 0x22b0
leaq 0x26fe64(%rip), %rax    # 0x28b230
movq 0x28(%rax), %rdi
movq 0x50(%rax), %rdx
xorl %esi, %esi
callq 0x20e0
movq 0x18(%rsp), %rsi
movq 0x18(%rsi), %rax
testl $0x7fffffff, 0x14(%rax) # imm = 0x7FFFFFFF
jne 0x1b507
movq $0x0, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
movq (%rsp), %r10
jmp 0x1b635
movq %r14, %r8
movq 0x10(%r14), %r12
addq $0x10, %r12
movq $0x0, 0x8(%rsp)
movq $0x0, 0x20(%rsp)
movq (%rsp), %rdi
movq %r15, 0x30(%rsp)
movq (%r12), %rcx
movl %ecx, %eax
shrq $0x20, %rcx
cmpq %rax, %rcx
je 0x1b4ee
movb (%r15,%rcx), %r14b
testb %r14b, %r14b
je 0x1b4ee
movb (%r15,%rax), %r15b
testb %r15b, %r15b
je 0x1b500
movq -0x10(%r12), %rax
movq -0x8(%r12), %rbx
addq %rbx, %rbx
movb 0x48(%r8), %dl
xorl %ecx, %ecx
subb %dl, %cl
shrq %cl, %rbx
leaq (%rax,%rax), %rsi
shrq %cl, %rsi
pushq $0x1
popq %rdx
testb %r15b, %r15b
js 0x1b490
movabsq $0x100000000, %rdx      # imm = 0x100000000
movq 0x50(%r8), %r13
andq %r13, %rax
movq %rax, %rcx
movq 0x10(%rsp), %r8
callq 0x15a23c
pushq $0x1
popq %rdx
testb %r14b, %r14b
js 0x1b4b6
movabsq $0x100000000, %rdx      # imm = 0x100000000
andq -0x8(%r12), %r13
movq (%rsp), %rdi
movq %rbx, %rsi
movq %r13, %rcx
movq 0x10(%rsp), %r8
callq 0x15a23c
cmpb %r15b, %r14b
jne 0x1b4db
incq 0x20(%rsp)
jmp 0x1b4e0
incq 0x8(%rsp)
movq 0x28(%rsp), %r8
movq 0x30(%rsp), %r15
movq (%rsp), %rdi
addq $0x20, %r12
decq %rbp
jne 0x1b432
jmp 0x1b39c
movq 0x30(%rsp), %r15
jmp 0x1b4ee
xorl %ebx, %ebx
movq $0x0, 0x10(%rsp)
movq $0x0, 0x8(%rsp)
movq (%rsp), %r10
movq 0x20(%rax), %rax
cmpl $0x0, (%rax,%rbx,8)
js 0x1b61c
movb (%r15,%rbx), %al
testb %al, %al
je 0x1b61c
movq 0x10(%rsi), %rbp
imulq $0x28, %rbx, %rcx
cmpl $0x0, 0xc(%rbp,%rcx)
je 0x1b61c
addq %rcx, %rbp
cmpl $0x0, 0x10(%rbp)
je 0x1b61c
testb %al, %al
setle %r12b
incb %r12b
xorl %r13d, %r13d
movq 0x18(%rbp), %rax
movq (%rax,%r13,8), %rdx
shrq $0x21, %rdx
movq (%r10,%rdx,8), %r9
cmpq $-0x1, %r9
je 0x1b58a
movq %r9, %r8
shrq $0x20, %r8
movl %r9d, %eax
cmpq %rax, %r8
jne 0x1b59a
incq %r13
movl 0x10(%rbp), %eax
cmpq %rax, %r13
jb 0x1b565
jmp 0x1b61c
cmpl %r8d, %eax
movl $0x0, %eax
adcl $-0x1, %eax
orl $0x1, %eax
movsbl (%r15,%rbx), %ecx
cmpl %ecx, %eax
jne 0x1b5f8
leaq 0x26fc78(%rip), %rax    # 0x28b230
movq 0x28(%rax), %rax
movb %r12b, (%rax,%rdx)
movq (%r14), %rdi
movq 0x18(%rbp), %rax
movq (%rax,%r13,8), %rdx
shrq $0x21, %rdx
movsbl (%r15,%rbx), %ecx
movq (%r10,%rdx,8), %r9
movq %r9, %r8
shrq $0x20, %r8
leaq 0x214f7f(%rip), %rsi    # 0x230565
xorl %eax, %eax
callq 0x22b0
movq (%rsp), %r10
incq 0x8(%rsp)
jmp 0x1b612
movq (%r14), %rdi
leaq 0x214f91(%rip), %rsi    # 0x230593
xorl %eax, %eax
callq 0x22b0
movq (%rsp), %r10
incq 0x10(%rsp)
movq 0x18(%rsp), %rsi
jmp 0x1b58a
incq %rbx
movq 0x18(%rsi), %rax
movl 0x14(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq %rcx, %rbx
jb 0x1b51f
movq %r10, %rdi
callq 0x2220
movq 0x28(%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 (%r14), %rdi
leaq 0x214f52(%rip), %rsi    # 0x2305c1
leaq 0x214ee5(%rip), %rdx    # 0x23055b
movq 0x8(%rsp), %rcx
movq 0x10(%rsp), %r8
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x22b0