// File: /workspace/github_cpp_makefile_star2_O1/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
subq $0x18, %rsp
cmpq $0x0, 0x220(%rdi)
je 0x53f28
movq %rdi, %r14
movl 0x10(%rdi), %eax
testq %rax, %rax
je 0x53f28
movq %rsi, (%rsp)
movq $0x0, 0x238(%r14)
movq (%r14), %rdi
leaq (%rax,%rax,2), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x46275
movl 0x10(%r14), %eax
testq %rax, %rax
je 0x53b4a
movl $0x1, %r15d
xorl %ebp, %ebp
leaq 0x236d69(%rip), %r12    # 0x28a758
movabsq $-0x100000000, %rbx     # imm = 0xFFFFFFFF00000000
cmpq %rax, %r15
je 0x53a22
movq (%r14), %rax
leaq (%r15,%r15,2), %rcx
movl 0x4(%rax,%rcx,8), %ecx
leaq (,%rbp,2), %rdx
addq %rbp, %rdx
cmpl 0x4(%rax,%rdx,8), %ecx
jne 0x53a22
movq %rbp, %rcx
jmp 0x53b33
cmpq %r15, %rbp
jae 0x53ac0
movq (%r14), %rdx
movq 0x210(%r12), %rax
movq 0x218(%r12), %rcx
leaq (,%rbp,2), %rsi
addq %rbp, %rsi
leaq (%rdx,%rsi,8), %rdx
addq $0xc, %rdx
xorl %r13d, %r13d
movq %rbp, %rsi
testb $0x1, (%rdx)
je 0x53ab2
movq 0x218(%r14), %rdi
movl -0x4(%rdx), %r8d
leaq (%r8,%r8,4), %r8
movl 0x20(%rdi,%r8,8), %r9d
cmpl -0xc(%rdx), %r9d
jne 0x53f37
leaq (%rdi,%r8,8), %r8
movl 0x8(%r8), %edi
cmpq $0x2, %rdi
jb 0x53ab2
movl 0xc(%r8), %r8d
cmpl $0x2, %r8d
setae %r9b
cmpq %rdi, %rcx
setle %dil
movl %r8d, %r8d
cmpq %r8, %rax
setle %r8b
andb %r9b, %dil
andb %r8b, %dil
movzbl %dil, %edi
addq %rdi, %r13
incq %rsi
addq $0x18, %rdx
cmpq %rsi, %r15
jne 0x53a57
jmp 0x53ac3
xorl %r13d, %r13d
movq %r15, %rcx
testq %r13, %r13
je 0x53b33
movq 0x240(%r14), %rax
cmpq %rax, 0x238(%r14)
jne 0x53b09
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x240(%r14)
movq 0x248(%r14), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x248(%r14)
shlq $0x20, %r13
subq %r13, %rbp
addq %rbx, %rbp
movq 0x238(%r14), %rax
movq 0x248(%r14), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x238(%r14)
movq %rbp, (%rcx,%rax,8)
movq %r15, %rcx
leaq 0x1(%r15), %rdx
movl 0x10(%r14), %eax
movq %rcx, %rbp
cmpq %rax, %r15
movq %rdx, %r15
jb 0x539f9
movq 0x238(%r14), %rax
testq %rax, %rax
movq (%rsp), %r13
je 0x53e38
movq 0x248(%r14), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x45d47
movq 0x238(%r14), %rbp
testq %rbp, %rbp
je 0x53d89
movq 0x248(%r14), %r10
movl 0x10(%r14), %edi
xorl %esi, %esi
movq %r10, 0x8(%rsp)
movl (%r10,%rsi,8), %r9d
cmpl %r9d, %edi
jbe 0x53cc6
movq %rbp, %r10
movq (%r14), %rax
leaq (,%r9,8), %rcx
leaq (%rcx,%rcx,2), %rcx
movq %rcx, 0x10(%rsp)
leaq 0xc(%rax,%rcx), %r11
movl -0x8(%r11), %ebp
leaq 0x236b94(%rip), %rax    # 0x28a758
movq 0x210(%rax), %r15
movq 0x218(%rax), %r12
movq %rdi, %rdx
subq %r9, %rdx
movq %rdx, %r8
xorl %r13d, %r13d
cmpl %ebp, -0x8(%r11)
jne 0x53c31
testb $0x1, (%r11)
je 0x53c28
movq 0x218(%r14), %rcx
movl -0x4(%r11), %eax
leaq (%rax,%rax,4), %rax
movl 0x8(%rcx,%rax,8), %ebx
cmpq $0x2, %rbx
jb 0x53c28
leaq (%rcx,%rax,8), %rax
movl 0xc(%rax), %eax
cmpl $0x2, %eax
setae %cl
cmpq %rbx, %r12
setle %bl
movl %eax, %eax
cmpq %rax, %r15
setle %al
andb %cl, %bl
andb %al, %bl
movzbl %bl, %eax
addq %rax, %r13
addq $0x18, %r11
decq %r8
jne 0x53bde
testq %r13, %r13
movq (%rsp), %r13
movq %r10, %rbp
movq 0x8(%rsp), %r10
je 0x53cc6
movq (%r14), %r11
leaq (%r9,%r9,2), %rax
movl 0x4(%r11,%rax,8), %r8d
movq (%r13), %rax
imulq $0xa8, %r8, %rcx
cmpb $0x1, 0x2c(%rax,%rcx)
jne 0x53c6e
addq %rcx, %rax
addq $0x2c, %rax
movb $0x2, (%rax)
cmpl %r9d, %edi
jbe 0x53cc6
movq 0x10(%rsp), %rax
leaq (%r11,%rax), %r9
addq $0xc, %r9
cmpl %r8d, -0x8(%r9)
jne 0x53cc6
testb $0x1, (%r9)
jne 0x53ca6
movl -0x4(%r9), %eax
movq 0x218(%r14), %rcx
leaq (%rax,%rax,4), %rax
decl 0x8(%rcx,%rax,8)
jne 0x53cbd
jmp 0x53f56
movq 0x218(%r14), %rax
movl -0x4(%r9), %ecx
leaq (%rcx,%rcx,4), %rcx
movl $0x1, 0x1c(%rax,%rcx,8)
addq $0x18, %r9
decq %rdx
jne 0x53c80
incq %rsi
cmpq %rbp, %rsi
jne 0x53b90
movq 0x248(%r14), %rcx
movl 0x10(%r14), %edx
movq (%r14), %rsi
movq (%r13), %rdi
xorl %r8d, %r8d
movl (%rcx,%r8,8), %r15d
leaq (%r15,%r15,2), %r9
cmpl %r15d, %edx
jbe 0x53d56
movq (%r14), %rax
leaq (%rax,%r9,8), %r10
addq $0xc, %r10
movl -0x8(%r10), %r11d
movq %rdx, %r12
subq %r15, %r12
xorl %r15d, %r15d
cmpl %r11d, -0x8(%r10)
jne 0x53d4d
testb $0x1, (%r10)
je 0x53d44
movq 0x218(%r14), %rbx
movl -0x4(%r10), %eax
leaq (%rax,%rax,4), %rax
cmpl $0x2, 0x8(%rbx,%rax,8)
jb 0x53d44
leaq (%rbx,%rax,8), %rax
cmpl $0x2, 0xc(%rax)
jb 0x53d44
xorl %ebx, %ebx
cmpl $0x1, 0x1c(%rax)
sete %bl
addq %rbx, %r15
addq $0x18, %r10
decq %r12
jne 0x53d0c
testq %r15, %r15
setne %r10b
jmp 0x53d59
xorl %r10d, %r10d
movl 0x4(%rsi,%r9,8), %eax
imulq $0xa8, %rax, %r9
cmpb $0x1, 0x2c(%rdi,%r9)
jne 0x53d7d
testb %r10b, %r10b
je 0x53d7d
leaq (%rdi,%r9), %rax
addq $0x2c, %rax
movb $0x2, (%rax)
incq %r8
cmpq %rbp, %r8
jne 0x53ce7
movl 0x10(%r14), %eax
testq %rax, %rax
je 0x53e38
movl $0x1, %ecx
xorl %edx, %edx
cmpq %rax, %rcx
je 0x53dbd
movq (%r14), %rsi
leaq (%rcx,%rcx,2), %rdi
movl 0x4(%rsi,%rdi,8), %edi
leaq (%rdx,%rdx,2), %r8
cmpl 0x4(%rsi,%r8,8), %edi
jne 0x53dbd
movq %rdx, %r8
jmp 0x53e25
movq (%r14), %rsi
leaq (%rdx,%rdx,2), %rdi
movl 0x4(%rsi,%rdi,8), %r8d
movq (%r13), %r9
imulq $0xa8, %r8, %r8
cmpb $0x1, 0x2c(%r9,%r8)
sete %r8b
cmpq %rcx, %rdx
setb %r9b
andb %r8b, %r9b
movq %rcx, %r8
cmpb $0x1, %r9b
jne 0x53e25
leaq (%rsi,%rdi,8), %rsi
addq $0xc, %rsi
testb $0x1, (%rsi)
je 0x53e16
movq 0x218(%r14), %rdi
movl -0x4(%rsi), %r8d
leaq (%r8,%r8,4), %r8
movl $0xffffffff, 0x1c(%rdi,%r8,8) # imm = 0xFFFFFFFF
incq %rdx
addq $0x18, %rsi
cmpq %rdx, %rcx
jne 0x53df9
movq %rcx, %r8
leaq 0x1(%rcx), %rsi
movq %r8, %rdx
cmpq %rax, %rcx
movq %rsi, %rcx
jne 0x53d9d
movl 0x10(%r14), %eax
testq %rax, %rax
je 0x53f28
movl $0x1, %ecx
xorl %edx, %edx
cmpq %rax, %rcx
je 0x53e6f
movq (%r14), %rsi
leaq (%rcx,%rcx,2), %rdi
movl 0x4(%rsi,%rdi,8), %edi
leaq (%rdx,%rdx,2), %r8
cmpl 0x4(%rsi,%r8,8), %edi
jne 0x53e6f
movq %rdx, %r9
jmp 0x53f0b
movq (%r14), %r11
leaq (%rdx,%rdx,2), %r15
movl 0x4(%r11,%r15,8), %esi
movq (%r13), %rdi
imulq $0xa8, %rsi, %r8
leaq (%rdi,%r8), %rsi
movzbl 0x2c(%rdi,%r8), %r10d
cmpl $0x2, %r10d
je 0x53ef8
movq %rcx, %r9
cmpl $0x1, %r10d
jne 0x53f0b
movq %rcx, %r9
cmpq %rcx, %rdx
jae 0x53f0b
movq 0x218(%r14), %r9
leaq (%rdi,%r8), %r10
addq $0x2e, %r10
leaq (%r11,%r15,8), %r11
addq $0xc, %r11
movl -0x4(%r11), %ebx
leaq (%rbx,%rbx,4), %rbx
cmpl $0x1, 0x1c(%r9,%rbx,8)
jne 0x53eea
leaq (%r9,%rbx,8), %rbx
cmpl $0x2, 0x8(%rbx)
jb 0x53eea
cmpl $0x2, 0xc(%rbx)
jb 0x53eea
movb (%r11), %bl
movb $0x1, (%r10)
testb $0x1, %bl
jne 0x53f20
incq %rdx
addq $0x18, %r11
cmpq %rdx, %rcx
jne 0x53ebe
jmp 0x53f08
movb $0x1, 0x2e(%rsi)
movl 0x8(%rsi), %edx
subl 0x4(%rsi), %edx
incl %edx
subq %rdx, 0x18(%r13)
movq %rcx, %r9
leaq 0x1(%rcx), %rsi
movq %r9, %rdx
cmpq %rax, %rcx
movq %rsi, %rcx
jne 0x53e4c
jmp 0x53f28
movb $0x2, 0x2c(%rdi,%r8)
jmp 0x53efc
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1d1dbd(%rip), %rdi    # 0x225cfb
leaq 0x1d16fe(%rip), %rsi    # 0x225643
leaq 0x1d1de0(%rip), %rcx    # 0x225d2c
movl $0x23a9, %edx           # imm = 0x23A9
callq 0x2110
leaq 0x1d1e29(%rip), %rdi    # 0x225d86
leaq 0x1d16df(%rip), %rsi    # 0x225643
leaq 0x1d1dc1(%rip), %rcx    # 0x225d2c
movl $0x23d7, %edx           # imm = 0x23D7
callq 0x2110