// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: generate_haplotypes_sv(haplotype_evdience_alloc*, overlap_region_alloc*, unsigned long)
void generate_haplotypes_sv(haplotype_evdience_alloc* hap, overlap_region_alloc* overlap_list, uint64_t rid)
{
    uint64_t k, l, i, o, ii;
    int64_t z; SnpStats *s = NULL;
    // fprintf(stderr, "[M::%s] hap->snp_stat.n::%u, hap->length::%u\n", __func__, (uint32_t)hap->snp_stat.n, (uint32_t)hap->length);
    if(hap->snp_stat.n == 0 || hap->length == 0) return;

    hap->snp_srt.n = 0;
    radix_sort_haplotype_evdience_id_srt(hap->list, hap->list + hap->length);
    for (k = 1, l = 0; k <= hap->length; ++k) {  
        if (k == hap->length || hap->list[k].overlapID != hap->list[l].overlapID) {
            for (i = l, o = 0; i < k; i++) {
                if(hh_tp(hap->list[i])!=1) continue;///mismatch
                s = &(hap->snp_stat.a[hap->list[i].overlapSite]); 
                assert(s->site == hap->list[i].site);
                if(s->occ_0 < 2 || s->occ_1 < 2) continue;
                if(s->occ_0 >= asm_opt.s_hap_cov && s->occ_1 >= asm_opt.infor_cov) o++;///allels must be real
            }

            if(o > 0) {
                o = ((uint32_t)-1) - o;
                o <<= 32; o += l; 
                kv_push(uint64_t, hap->snp_srt, o);
            }
            l = k;
        }
    }

    
    if (hap->snp_srt.n > 0) {
        radix_sort_bc64(hap->snp_srt.a, hap->snp_srt.a + hap->snp_srt.n);///sort by how many snps in one overlap
        for (k = 0; k < hap->snp_srt.n; k++) {
            o = 0; l = (uint32_t)hap->snp_srt.a[k];
            for (i = l; i < hap->length && hap->list[i].overlapID == hap->list[l].overlapID; i++) {
                if(hh_tp(hap->list[i])!=1) continue;
                s = &(hap->snp_stat.a[hap->list[i].overlapSite]);
                if(s->occ_0 < 2 || s->occ_1 < 2) continue;
                if(s->occ_0 >= asm_opt.s_hap_cov && s->occ_1 >= asm_opt.infor_cov) {
                    o++;
                }
            }
            if(o == 0) continue;

            ii = hap->list[l].overlapID;
            if(overlap_list->list[ii].is_match == 1) {
                overlap_list->list[ii].is_match = 2;
                // fprintf(stderr, "[M::%s] rid::%u\t%.*s\n", __func__, overlap_list->list[ii].y_id, (int)Get_NAME_LENGTH(R_INF, overlap_list->list[ii].y_id), Get_NAME(R_INF, overlap_list->list[ii].y_id));
            }
            for (i = l; i < hap->length && hap->list[i].overlapID == hap->list[l].overlapID; i++) {
                if(hh_tp(hap->list[i])==1){
                    s = &(hap->snp_stat.a[hap->list[i].overlapSite]);
                    s->score = 1; 
                } else if(hh_tp(hap->list[i])==0) {
                    ///not real allels
                    z = hap->list[i].overlapSite; 
                    s = &(hap->snp_stat.a[z]);
                    s->occ_0--;
                    // if(!(s->occ_0 >= 1)) {
                    //     fprintf(stderr, "[M::%s] rid::%lu, ssite::%u, lsite::%u, i::%lu, idx::%u\n", __func__, rid, s->site, hap->list[i].site, i, hap->list[i].overlapSite);
                    // }
                    assert(s->occ_0 >= 1);
                }
            }
        }

        for (k = 0; k < hap->snp_srt.n; k++) {///sorted by how many allels in each overlap; more -> less
            o = 0; l = (uint32_t)hap->snp_srt.a[k];
            for (i = l; i < hap->length && hap->list[i].overlapID == hap->list[l].overlapID; i++) {
                if(hh_tp(hap->list[i])!=1) continue;
                s = &(hap->snp_stat.a[hap->list[i].overlapSite]);
                if(s->occ_0 < 2 || s->occ_1 < 2) continue;
                if(s->score == 1) o++;
            }
            ii = hap->list[l].overlapID;
            if(overlap_list->list[ii].is_match == 1 && o > 0) {
                overlap_list->list[ii].is_match = 2;
                // fprintf(stderr, "[M::%s] rid::%u\t%.*s\n", __func__, overlap_list->list[ii].y_id, (int)Get_NAME_LENGTH(R_INF, overlap_list->list[ii].y_id), Get_NAME(R_INF, overlap_list->list[ii].y_id));
            }
        }


        for (k = 1, l = 0; k <= hap->length; ++k) {  ///reset snp_stat
            if (k == hap->length || hap->list[k].overlapID != hap->list[l].overlapID) {
                ii = hap->list[l].overlapID;
                if(overlap_list->list[ii].is_match==1) {
                    for (i = l; i < k; i++) {
                        if(hh_tp(hap->list[i])==1) {
                            hap->snp_stat.a[hap->list[i].overlapSite].score = -1;
                        }
                    }
                }
                l = k;
            }
        } 
    }



    for (k = 1, l = 0; k <= hap->length; ++k) {  
        if (k == hap->length || hap->list[k].overlapID != hap->list[l].overlapID) {
            ii = hap->list[l].overlapID;
            if(overlap_list->list[ii].is_match==2) {
                overlap_list->list[ii].strong = 1;
                overlap_list->mapped_overlaps_length -= 
                                            overlap_list->list[ii].x_pos_e + 1 - overlap_list->list[ii].x_pos_s;
            } else if(overlap_list->list[ii].is_match==1) {
                for (i = l; i < k; i++) {
                    if(hh_tp(hap->list[i])==1 || hh_tp(hap->list[i])==0) {
                        s = &(hap->snp_stat.a[hap->list[i].overlapSite]);
                        if((s->score == 1) && (!(s->occ_0 < 2 || s->occ_1 < 2))) {
                            overlap_list->list[ii].strong = 1;
                            if(hh_tp(hap->list[i])==1) {
                                overlap_list->list[ii].is_match = 2;
                                overlap_list->mapped_overlaps_length -= 
                                            overlap_list->list[ii].x_pos_e + 1 - overlap_list->list[ii].x_pos_s;
                                // fprintf(stderr, "[M::%s] rid::%u\t%.*s\n", __func__, overlap_list->list[ii].y_id, (int)Get_NAME_LENGTH(R_INF, overlap_list->list[ii].y_id), Get_NAME(R_INF, overlap_list->list[ii].y_id));
                                break;
                            }
                        }
                    }

                }
            }
            l = k;
        }
    }    
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
cmpq $0x0, 0x220(%rdi)
je 0x24ea8
movq %rdi, %r14
movl 0x10(%rdi), %eax
testq %rax, %rax
je 0x24ea8
movq %rsi, (%rsp)
andq $0x0, 0x238(%r14)
movq (%r14), %rdi
imulq $0x18, %rax, %rsi
addq %rdi, %rsi
callq 0x19538
pushq $0x1
popq %r12
xorl %ebp, %ebp
leaq 0x22ed65(%rip), %r15    # 0x253758
movabsq $-0x100000000, %rbx     # imm = 0xFFFFFFFF00000000
movl 0x10(%r14), %eax
cmpq %rax, %r12
ja 0x24b26
je 0x24a29
movq (%r14), %rax
imulq $0x18, %r12, %rcx
movl 0x4(%rax,%rcx), %ecx
imulq $0x18, %rbp, %rdx
cmpl 0x4(%rax,%rdx), %ecx
jne 0x24a29
movq %rbp, %rax
jmp 0x24b1b
movq 0x210(%r15), %rax
movq 0x218(%r15), %rcx
imulq $0x18, %rbp, %rdx
addq $0xc, %rdx
movq %rbp, %rsi
xorl %r13d, %r13d
cmpq %r12, %rsi
jae 0x24ab1
movq (%r14), %r8
testb $0x1, (%r8,%rdx)
je 0x24aa8
movq 0x218(%r14), %rdi
movl -0x4(%r8,%rdx), %r9d
imulq $0x28, %r9, %r9
movl 0x20(%rdi,%r9), %r10d
cmpl -0xc(%r8,%rdx), %r10d
jne 0x24eb7
addq %r9, %rdi
movl 0x8(%rdi), %r8d
cmpq $0x2, %r8
jb 0x24aa8
movl 0xc(%rdi), %edi
cmpl $0x2, %edi
setae %r9b
cmpq %r8, %rcx
setle %r8b
movl %edi, %edi
cmpq %rdi, %rax
setle %dil
andb %r9b, %r8b
andb %dil, %r8b
movzbl %r8b, %edi
addq %rdi, %r13
incq %rsi
addq $0x18, %rdx
jmp 0x24a45
movq %r12, %rax
testq %r13, %r13
je 0x24b1b
movq 0x238(%r14), %rcx
movq 0x248(%r14), %rax
cmpq 0x240(%r14), %rcx
jne 0x24aff
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x240(%r14)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x248(%r14)
movq 0x238(%r14), %rcx
shlq $0x20, %r13
subq %r13, %rbp
addq %rbx, %rbp
leaq 0x1(%rcx), %rdx
movq %rdx, 0x238(%r14)
movq %rbp, (%rax,%rcx,8)
movq %r12, %rax
incq %r12
movq %rax, %rbp
jmp 0x249fd
movq 0x238(%r14), %rcx
testq %rcx, %rcx
je 0x24dc3
movq 0x248(%r14), %rdi
leaq (%rdi,%rcx,8), %rsi
callq 0x1905d
movq 0x238(%r14), %rax
xorl %ecx, %ecx
movq (%rsp), %r12
cmpq %rax, %rcx
je 0x24c79
movq 0x248(%r14), %rdx
movl (%rdx,%rcx,8), %edx
movl 0x10(%r14), %esi
movq 0x210(%r15), %r8
movq 0x218(%r15), %r9
imulq $0x18, %rdx, %rdi
leaq 0xc(%rdi), %r10
movq %rdx, %r12
xorl %r11d, %r11d
cmpq %rsi, %r12
jae 0x24bef
movq (%r14), %rbp
movl -0x8(%rbp,%r10), %ebx
cmpl 0x4(%rbp,%rdi), %ebx
jne 0x24bef
testb $0x1, (%rbp,%r10)
je 0x24be6
movq 0x218(%r14), %r13
movl -0x4(%rbp,%r10), %ebx
imulq $0x28, %rbx, %rbx
movl 0x8(%r13,%rbx), %ebp
cmpq $0x2, %rbp
jb 0x24be6
addq %rbx, %r13
movl 0xc(%r13), %ebx
cmpl $0x2, %ebx
setae %r13b
cmpq %rbp, %r9
setle %bpl
movl %ebx, %ebx
cmpq %rbx, %r8
setle %bl
andb %r13b, %bpl
andb %bl, %bpl
movzbl %bpl, %ebx
addq %rbx, %r11
incq %r12
addq $0x18, %r10
jmp 0x24b86
testq %r11, %r11
movq (%rsp), %r12
je 0x24c71
movq (%r14), %r9
movl 0x4(%r9,%rdi), %r8d
movq (%r12), %r10
imulq $0xa8, %r8, %r11
cmpb $0x1, 0x2c(%r10,%r11)
jne 0x24c1e
addq %r11, %r10
addq $0x2c, %r10
movb $0x2, (%r10)
addq %r9, %rdi
addq $0xc, %rdi
cmpq %rsi, %rdx
jae 0x24c71
cmpl %r8d, -0x8(%rdi)
jne 0x24c71
testb $0x1, (%rdi)
jne 0x24c50
movl -0x4(%rdi), %r9d
movq 0x218(%r14), %r10
imulq $0x28, %r9, %r9
decl 0x8(%r10,%r9)
jne 0x24c68
jmp 0x24ed6
movq 0x218(%r14), %r9
movl -0x4(%rdi), %r10d
imulq $0x28, %r10, %r10
movl $0x1, 0x1c(%r9,%r10)
incq %rdx
addq $0x18, %rdi
jmp 0x24c25
incq %rcx
jmp 0x24b53
xorl %ecx, %ecx
cmpq %rax, %rcx
je 0x24d27
movq 0x248(%r14), %rdx
movl (%rdx,%rcx,8), %esi
movl 0x10(%r14), %edi
movq (%r14), %r8
imulq $0x18, %rsi, %r9
leaq (%r8,%r9), %r10
addq $0x4, %r10
leaq (%r8,%r9), %r11
addq $0xc, %r11
xorl %edx, %edx
cmpq %rdi, %rsi
jae 0x24cf6
movl (%r10), %ebp
cmpl %ebp, -0x8(%r11)
jne 0x24cfb
testb $0x1, (%r11)
je 0x24ced
movq 0x218(%r14), %r15
movl -0x4(%r11), %ebx
imulq $0x28, %rbx, %rbx
cmpl $0x2, 0x8(%r15,%rbx)
jb 0x24ced
addq %rbx, %r15
cmpl $0x2, 0xc(%r15)
jb 0x24ced
xorl %ebx, %ebx
cmpl $0x1, 0x1c(%r15)
sete %bl
addq %rbx, %rdx
incq %rsi
addq $0x18, %r11
jmp 0x24cab
movl 0x4(%r8,%r9), %ebp
movl %ebp, %edi
movq (%r12), %rsi
imulq $0xa8, %rdi, %rdi
cmpb $0x1, 0x2c(%rsi,%rdi)
jne 0x24d1f
testq %rdx, %rdx
je 0x24d1f
leaq (%rsi,%rdi), %rdx
addq $0x2c, %rdx
movb $0x2, (%rdx)
incq %rcx
jmp 0x24c7b
movl 0x10(%r14), %eax
leaq 0x1(%rax), %rcx
pushq $0x1
popq %rdx
xorl %esi, %esi
cmpq %rcx, %rdx
je 0x24dcb
movq (%r14), %rdi
cmpq %rax, %rdx
jne 0x24d50
imulq $0x18, %rsi, %r8
movl 0x4(%rdi,%r8), %r8d
jmp 0x24d69
imulq $0x18, %rdx, %r9
imulq $0x18, %rsi, %r8
movl 0x4(%rdi,%r8), %r8d
cmpl %r8d, 0x4(%rdi,%r9)
jne 0x24d69
movq %rsi, %r8
jmp 0x24db8
movl %r8d, %r8d
movq (%r12), %r9
imulq $0xa8, %r8, %r8
cmpb $0x1, 0x2c(%r9,%r8)
movq %rdx, %r8
jne 0x24db8
imulq $0x18, %rsi, %r8
addq %r8, %rdi
addq $0xc, %rdi
cmpq %rdx, %rsi
jae 0x24db5
testb $0x1, (%rdi)
je 0x24dac
movq 0x218(%r14), %r8
movl -0x4(%rdi), %r9d
imulq $0x28, %r9, %r9
orl $-0x1, 0x1c(%r8,%r9)
incq %rsi
addq $0x18, %rdi
jmp 0x24d8d
movq %rdx, %r8
incq %rdx
movq %r8, %rsi
jmp 0x24d34
leaq 0x1(%rax), %rcx
movq (%rsp), %r12
pushq $0x1
popq %rdx
xorl %esi, %esi
cmpq %rcx, %rdx
je 0x24ea8
movq (%r14), %r11
cmpq %rax, %rdx
jne 0x24dec
imulq $0x18, %rsi, %rdi
movl 0x4(%r11,%rdi), %edi
jmp 0x24e08
imulq $0x18, %rdx, %r8
imulq $0x18, %rsi, %rdi
movl 0x4(%r11,%rdi), %edi
cmpl %edi, 0x4(%r11,%r8)
jne 0x24e08
movq %rsi, %r10
jmp 0x24e95
movl %edi, %edi
movq (%r12), %r8
imulq $0xa8, %rdi, %r9
leaq (%r8,%r9), %rdi
movzbl 0x2c(%r8,%r9), %ebx
cmpl $0x2, %ebx
je 0x24e81
movq %rdx, %r10
cmpl $0x1, %ebx
jne 0x24e95
leaq (%r8,%r9), %r10
addq $0x2e, %r10
imulq $0x18, %rsi, %rbx
addq %rbx, %r11
addq $0xc, %r11
cmpq %rdx, %rsi
jae 0x24e92
movq 0x218(%r14), %r15
movl -0x4(%r11), %ebx
imulq $0x28, %rbx, %rbx
cmpl $0x1, 0x1c(%r15,%rbx)
jne 0x24e78
addq %rbx, %r15
cmpl $0x2, 0x8(%r15)
jb 0x24e78
cmpl $0x2, 0xc(%r15)
jb 0x24e78
movb (%r11), %bl
movb $0x1, (%r10)
testb $0x1, %bl
jne 0x24ea0
incq %rsi
addq $0x18, %r11
jmp 0x24e3f
movb $0x1, 0x2e(%rdi)
movl 0x8(%rdi), %esi
subl 0x4(%rdi), %esi
incl %esi
subq %rsi, 0x18(%r12)
movq %rdx, %r10
incq %rdx
movq %r10, %rsi
jmp 0x24dd0
movb $0x2, 0x2c(%r8,%r9)
jmp 0x24e85
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1bee9d(%rip), %rdi    # 0x1e3d5b
leaq 0x1be7de(%rip), %rsi    # 0x1e36a3
leaq 0x1beec0(%rip), %rcx    # 0x1e3d8c
movl $0x23a9, %edx           # imm = 0x23A9
callq 0x2110
leaq 0x1bef09(%rip), %rdi    # 0x1e3de6
leaq 0x1be7bf(%rip), %rsi    # 0x1e36a3
leaq 0x1beea1(%rip), %rcx    # 0x1e3d8c
movl $0x23d7, %edx           # imm = 0x23D7
callq 0x2110