// File: /workspace/github_cpp_makefile_star2_O3/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 0x5308a
movq %rdi, %r14
movl 0x10(%rdi), %eax
testq %rax, %rax
je 0x5308a
movq %rsi, %rbx
movq $0x0, 0x238(%r14)
movq (%r14), %rdi
leaq (%rax,%rax,2), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x45897
movl 0x10(%r14), %eax
testq %rax, %rax
movq %rbx, (%rsp)
je 0x52cb8
movl $0x1, %r13d
xorl %ebp, %ebp
leaq 0x242bf7(%rip), %r15    # 0x295758
movabsq $-0x100000000, %r10     # imm = 0xFFFFFFFF00000000
movl %eax, %ebx
cmpq %rax, %r13
je 0x52b99
movq (%r14), %rax
leaq (,%r13,2), %rcx
addq %r13, %rcx
movl 0x4(%rax,%rcx,8), %ecx
leaq (,%rbp,2), %rdx
addq %rbp, %rdx
cmpl 0x4(%rax,%rdx,8), %ecx
je 0x52ca4
cmpq %r13, %rbp
jae 0x52ca1
movq (%r14), %rcx
movq 0x210(%r15), %rax
movq 0x218(%r15), %rdx
leaq (,%rbp,2), %rsi
addq %rbp, %rsi
leaq (%rcx,%rsi,8), %rsi
addq $0xc, %rsi
xorl %r12d, %r12d
movq %rbp, %rdi
testb $0x1, (%rsi)
je 0x52c25
movq 0x218(%r14), %rcx
movl -0x4(%rsi), %r8d
leaq (%r8,%r8,4), %r8
movl 0x20(%rcx,%r8,8), %r9d
cmpl -0xc(%rsi), %r9d
jne 0x53099
leaq (%rcx,%r8,8), %r8
movl 0x8(%r8), %ecx
cmpq $0x2, %rcx
jb 0x52c25
movl 0xc(%r8), %r8d
cmpl $0x2, %r8d
setae %r9b
cmpq %rcx, %rdx
setle %cl
movl %r8d, %r8d
cmpq %r8, %rax
setle %r8b
andb %r9b, %cl
andb %r8b, %cl
movzbl %cl, %ecx
addq %rcx, %r12
incq %rdi
addq $0x18, %rsi
cmpq %rdi, %r13
jne 0x52bcc
testq %r12, %r12
je 0x52ca1
movq 0x238(%r14), %rcx
movq 0x248(%r14), %rax
cmpq 0x240(%r14), %rcx
jne 0x52c88
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x240(%r14)
shlq $0x3, %rsi
movq %rax, %rdi
movq %r10, %rbx
callq 0x2370
movq %rbx, %r10
movq %rax, 0x248(%r14)
movq 0x238(%r14), %rcx
movl 0x10(%r14), %ebx
shlq $0x20, %r12
subq %r12, %rbp
addq %r10, %rbp
leaq 0x1(%rcx), %rdx
movq %rdx, 0x238(%r14)
movq %rbp, (%rax,%rcx,8)
movq %r13, %rbp
leaq 0x1(%r13), %rcx
movl %ebx, %eax
cmpq %rax, %r13
movq %rcx, %r13
jb 0x52b6d
jmp 0x52cba
xorl %ebx, %ebx
movq 0x238(%r14), %rax
testq %rax, %rax
movq (%rsp), %r13
je 0x52f98
movq 0x248(%r14), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x45373
movq 0x238(%r14), %rbp
movl 0x10(%r14), %ebx
testq %rbp, %rbp
je 0x52eeb
movq 0x248(%r14), %rdi
xorl %esi, %esi
movq %rdi, 0x8(%rsp)
movl (%rdi,%rsi,8), %r9d
cmpl %r9d, %ebx
jbe 0x52e3a
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 0x242a24(%rip), %rax    # 0x295758
movq 0x210(%rax), %r15
movq 0x218(%rax), %r12
movq %rbx, %rcx
subq %r9, %rcx
movq %rcx, %r8
xorl %r13d, %r13d
cmpl %ebp, -0x8(%r11)
jne 0x52da5
testb $0x1, (%r11)
je 0x52d9c
movq 0x218(%r14), %rdx
movl -0x4(%r11), %eax
leaq (%rax,%rax,4), %rax
movl 0x8(%rdx,%rax,8), %edi
cmpq $0x2, %rdi
jb 0x52d9c
leaq (%rdx,%rax,8), %rax
movl 0xc(%rax), %eax
cmpl $0x2, %eax
setae %dl
cmpq %rdi, %r12
setle %dil
movl %eax, %eax
cmpq %rax, %r15
setle %al
andb %dl, %dil
andb %al, %dil
movzbl %dil, %eax
addq %rax, %r13
addq $0x18, %r11
decq %r8
jne 0x52d4e
testq %r13, %r13
movq (%rsp), %r13
movq %r10, %rbp
movq 0x8(%rsp), %rdi
je 0x52e3a
movq (%r14), %r11
leaq (%r9,%r9,2), %rax
movl 0x4(%r11,%rax,8), %r8d
movq (%r13), %rax
imulq $0xa8, %r8, %rdx
cmpb $0x1, 0x2c(%rax,%rdx)
jne 0x52de2
addq %rdx, %rax
addq $0x2c, %rax
movb $0x2, (%rax)
cmpl %r9d, %ebx
jbe 0x52e3a
movq 0x10(%rsp), %rax
leaq (%r11,%rax), %r9
addq $0xc, %r9
cmpl %r8d, -0x8(%r9)
jne 0x52e3a
testb $0x1, (%r9)
jne 0x52e1a
movl -0x4(%r9), %eax
movq 0x218(%r14), %rdx
leaq (%rax,%rax,4), %rax
decl 0x8(%rdx,%rax,8)
jne 0x52e31
jmp 0x530b8
movq 0x218(%r14), %rax
movl -0x4(%r9), %edx
leaq (%rdx,%rdx,4), %rdx
movl $0x1, 0x1c(%rax,%rdx,8)
addq $0x18, %r9
decq %rcx
jne 0x52df4
incq %rsi
cmpq %rbp, %rsi
jne 0x52d00
movq 0x248(%r14), %rdx
movq (%r14), %rsi
movq (%r13), %rdi
leaq 0xc(%rsi), %r8
xorl %r9d, %r9d
movl (%rdx,%r9,8), %ecx
cmpl %ecx, %ebx
jbe 0x52edf
leaq (,%rcx,8), %rax
leaq (%rax,%rax,2), %r11
movl 0x4(%rsi,%r11), %r10d
movq %rbx, %r12
subq %rcx, %r12
addq %r8, %r11
xorl %r15d, %r15d
cmpl %r10d, -0x8(%r11)
jne 0x52ec1
testb $0x1, (%r11)
je 0x52eb8
movq 0x218(%r14), %rcx
movl -0x4(%r11), %eax
leaq (%rax,%rax,4), %rax
cmpl $0x2, 0x8(%rcx,%rax,8)
jb 0x52eb8
leaq (%rcx,%rax,8), %rax
cmpl $0x2, 0xc(%rax)
jb 0x52eb8
xorl %ecx, %ecx
cmpl $0x1, 0x1c(%rax)
sete %cl
addq %rcx, %r15
addq $0x18, %r11
decq %r12
jne 0x52e80
imulq $0xa8, %r10, %rcx
cmpb $0x1, 0x2c(%rdi,%rcx)
jne 0x52edf
testq %r15, %r15
je 0x52edf
leaq (%rdi,%rcx), %rax
addq $0x2c, %rax
movb $0x2, (%rax)
incq %r9
cmpq %rbp, %r9
jne 0x52e5b
testl %ebx, %ebx
je 0x5308a
movq (%r14), %rax
leaq 0xc(%rax), %rdx
movl $0x1, %esi
xorl %edi, %edi
cmpq %rbx, %rsi
jne 0x52f10
leaq (%rdi,%rdi,2), %rcx
movl 0x4(%rax,%rcx,8), %ecx
jmp 0x52f28
leaq (%rsi,%rsi,2), %r8
leaq (%rdi,%rdi,2), %rcx
movl 0x4(%rax,%rcx,8), %ecx
cmpl %ecx, 0x4(%rax,%r8,8)
jne 0x52f28
movq %rdi, %rcx
jmp 0x52f85
movl %ecx, %ecx
movq (%r13), %r8
imulq $0xa8, %rcx, %rcx
cmpb $0x1, 0x2c(%r8,%rcx)
sete %cl
cmpq %rsi, %rdi
setb %r8b
andb %cl, %r8b
movq %rsi, %rcx
cmpb $0x1, %r8b
jne 0x52f85
leaq (%rdi,%rdi,2), %rcx
leaq (%rdx,%rcx,8), %rcx
testb $0x1, (%rcx)
je 0x52f76
movq 0x218(%r14), %r8
movl -0x4(%rcx), %r9d
leaq (%r9,%r9,4), %r9
movl $0xffffffff, 0x1c(%r8,%r9,8) # imm = 0xFFFFFFFF
incq %rdi
addq $0x18, %rcx
cmpq %rdi, %rsi
jne 0x52f59
movq %rsi, %rcx
leaq 0x1(%rsi), %r8
movq %rcx, %rdi
cmpq %rbx, %rsi
movq %r8, %rsi
jne 0x52f01
testl %ebx, %ebx
je 0x5308a
movl %ebx, %eax
movq (%r14), %rcx
leaq 0xc(%rcx), %rdx
movl $0x1, %esi
xorl %edi, %edi
leaq (%rdi,%rdi,2), %r11
cmpq %rax, %rsi
jne 0x52fc0
movl 0x4(%rcx,%r11,8), %r8d
jmp 0x52fd8
leaq (%rsi,%rsi,2), %r9
movl 0x4(%rcx,%r11,8), %r8d
cmpl %r8d, 0x4(%rcx,%r9,8)
jne 0x52fd8
movq %rdi, %rbx
jmp 0x5306d
movl %r8d, %r8d
movq (%r13), %r9
imulq $0xa8, %r8, %r10
leaq (%r9,%r10), %r8
movzbl 0x2c(%r9,%r10), %ebp
cmpl $0x2, %ebp
je 0x53057
movq %rsi, %rbx
cmpl $0x1, %ebp
jne 0x5306d
movq %rsi, %rbx
cmpq %rsi, %rdi
jae 0x5306d
movq 0x218(%r14), %rbx
leaq (%r9,%r10), %r15
addq $0x2e, %r15
leaq (%rdx,%r11,8), %r11
movl -0x4(%r11), %r12d
leaq (%r12,%r12,4), %r12
cmpl $0x1, 0x1c(%rbx,%r12,8)
jne 0x53049
leaq (%rbx,%r12,8), %r12
cmpl $0x2, 0x8(%r12)
jb 0x53049
cmpl $0x2, 0xc(%r12)
jb 0x53049
movb (%r11), %bpl
movb $0x1, (%r15)
testb $0x1, %bpl
jne 0x53082
incq %rdi
addq $0x18, %r11
cmpq %rdi, %rsi
jne 0x53018
jmp 0x5306a
movb $0x1, 0x2e(%r8)
movl 0x8(%r8), %edi
subl 0x4(%r8), %edi
incl %edi
subq %rdi, 0x18(%r13)
movq %rsi, %rbx
leaq 0x1(%rsi), %r8
movq %rbx, %rdi
cmpq %rax, %rsi
movq %r8, %rsi
jne 0x52fb0
jmp 0x5308a
movb $0x2, 0x2c(%r9,%r10)
jmp 0x5305c
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1ded6b(%rip), %rdi    # 0x231e0b
leaq 0x1de6ac(%rip), %rsi    # 0x231753
leaq 0x1ded8e(%rip), %rcx    # 0x231e3c
movl $0x23a9, %edx           # imm = 0x23A9
callq 0x2110
leaq 0x1dedd7(%rip), %rdi    # 0x231e96
leaq 0x1de68d(%rip), %rsi    # 0x231753
leaq 0x1ded6f(%rip), %rcx    # 0x231e3c
movl $0x23d7, %edx           # imm = 0x23D7
callq 0x2110