// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: generate_haplotypes_naive_HiFi(haplotype_evdience_alloc*, overlap_region_alloc*, double, UC_Read*, unsigned long, double, unsigned long)
void generate_haplotypes_naive_HiFi(haplotype_evdience_alloc* hap, overlap_region_alloc* overlap_list, double up, UC_Read* g_read, uint64_t multi_check, double st_rate, uint64_t st_max)
{
    // fprintf(stderr, "[M::%s::] Done\n", __func__);
    if(hap->length == 0) return;
    uint64_t k, l, i, o, *a, ii, m_snp_stat, m_list, m_off;
    int64_t z; 
    SnpStats *s = NULL, *t = NULL;

    for (k = 1, l = 0, i = m_snp_stat = m_list = 0; k <= hap->snp_stat.n; ++k) {///filter snps
        if(k == hap->snp_stat.n || hap->snp_stat.a[k].site != hap->snp_stat.a[l].site) {
            if((l > 0) && (hap->snp_stat.a[l].site == (hap->snp_stat.a[l-1].site + 1))) {
                l = k; continue;
            }
            if((k < hap->snp_stat.n) && ((hap->snp_stat.a[l].site+1) == hap->snp_stat.a[k].site)) {
                l = k; continue;
            }

            for (; i < hap->length && hap->list[i].site != hap->snp_stat.a[l].site; i++);
            assert(i < hap->length && hap->list[i].site == hap->snp_stat.a[l].site);
            m_off = l - m_snp_stat; 
            for (; i < hap->length && hap->list[i].site == hap->snp_stat.a[l].site; i++) {
                assert(hap->list[i].overlapSite>=l && hap->list[i].overlapSite < k);
                // assert(hap->snp_stat.a[hap->list[i].overlapSite].site==hap->list[i].site);
                hap->list[m_list] = hap->list[i]; hap->list[m_list++].overlapSite -= m_off;
            }
        
            for (; l < k; l++) hap->snp_stat.a[m_snp_stat++] = hap->snp_stat.a[l];
        }
    }
    hap->snp_stat.n = m_snp_stat; hap->length = m_list;
    if(hap->snp_stat.n == 0 || hap->length == 0) return;

    // for (k = 0; k < hap->snp_stat.n; k++) {
    //     s = &(hap->snp_stat.a[k]); if(s->site != 1502) continue;
    //     fprintf(stderr, "[M::%s] site::%u, occ_0::%u, occ_1::%u, occ_2::%u, is_homopolymer::%u\n", __func__, s->site, s->occ_0, s->occ_1, s->occ_2, s->is_homopolymer);
    //     prt_sub_read(g_read->seq, g_read->length, s->site, 25);
    //     for (i = 0; i < overlap_list->length; i++) {
    //         if(/**overlap_list->list[i].is_match == 1 &&**/ overlap_list->list[i].x_pos_s <= s->site && s->site <= overlap_list->list[i].x_pos_e) {
    //             fprintf(stderr, "%.*s\tis_match::%u\tid::%u\n", (int)Get_NAME_LENGTH(R_INF, overlap_list->list[i].y_id), Get_NAME(R_INF, overlap_list->list[i].y_id), overlap_list->list[i].is_match, overlap_list->list[i].y_id);
    //         }   
    //     }
    // }
    

    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(is_st_bs((*s), st_rate, st_max)) continue;
                if(s->occ_0 >= asm_opt.s_hap_cov && s->occ_1 >= asm_opt.infor_cov) o++;///allels must be real
            }

            // if(overlap_list->list[hap->list[l].overlapID].y_id == 4378830 || overlap_list->list[hap->list[l].overlapID].y_id == 4378829 || overlap_list->list[hap->list[l].overlapID].y_id == 4378799) {
            //     fprintf(stderr, "[M::%s-id::%u] o->%lu(%c), l::%lu\n", __func__, overlap_list->list[hap->list[l].overlapID].y_id, o, "+-"[overlap_list->list[hap->list[l].overlapID].y_pos_strand], l);
            //     for (i = l; 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(is_st_bs((*s), st_rate, st_max)) continue;
            //         if(s->occ_0 >= asm_opt.s_hap_cov && s->occ_1 >= asm_opt.infor_cov) {
            //             fprintf(stderr, "[M::%s] site::%u, occ_0::%u, occ_1::%u, occ_2::%u, is_homopolymer::%u\n", __func__, s->site, s->occ_0, s->occ_1, s->occ_1, s->is_homopolymer);
            //             prt_sub_read(g_read->seq, g_read->length, s->site, 50);
            //         }
            //     }
            // } 
            // else {
            //     for (i = l, o = 0; i < k; i++) {
            //         if(hh_tp(hap->list[i])!=0) 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) {
            //             if(s->site == 7878 || s->site == 9682) {
                        
            //                 fprintf(stderr, "***[M::%s] site::%u, rid::%u\t%.*s\n", __func__, s->site, overlap_list->list[hap->list[l].overlapID].y_id, (int)Get_NAME_LENGTH(R_INF, overlap_list->list[hap->list[l].overlapID].y_id), 
            //                     Get_NAME(R_INF, overlap_list->list[hap->list[l].overlapID].y_id));
            //                 prt_sub_cigar(&(overlap_list->list[hap->list[l].overlapID]), g_read->length, s->site, 50);
            //             }
            //             // o++;///allels must be real
            //             // fprintf(stderr, "[M::%s] site::%u, occ_0::%u, occ_1::%u, occ_2::%u, is_homopolymer::%u\n", __func__, s->site, s->occ_0, s->occ_1, s->occ_1, s->is_homopolymer);
            //             // prt_sub_read(g_read->seq, g_read->length, s->site, 50);
            //         }
            //     }
            // }

            // if(overlap_list->list[hap->list[l].overlapID].y_id == 20835) {
            //     fprintf(stderr, "***0***[M::%s-id::%u] o->%lu\n", __func__, overlap_list->list[hap->list[l].overlapID].y_id, o);
            // }

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

    // fprintf(stderr, "[M::%s] snp_srt.n->%lu\n", __func__, ((uint64_t)hap->snp_srt.n));
    
    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(is_st_bs((*s), st_rate, st_max)) continue;
                if(s->occ_0 >= asm_opt.s_hap_cov && s->occ_1 >= asm_opt.infor_cov) {
                    o++;
                    // if(overlap_list->list[hap->list[l].overlapID].y_id == 20835) {
                    //     fprintf(stderr, "[M::%s-id::%u] occ_0->%u, occ_1->%u, occ_2->%u, site->%u\n", __func__, overlap_list->list[hap->list[l].overlapID].y_id, s->occ_0, s->occ_1, s->occ_2, s->site);
                    // }
                }
            }
            // if(overlap_list->list[hap->list[l].overlapID].y_id == 4378830 || overlap_list->list[hap->list[l].overlapID].y_id == 4378829) {
            //  fprintf(stderr, "***1***[M::%s-id::%u] o->%lu, l->%lu\n", __func__, overlap_list->list[hap->list[l].overlapID].y_id, o, l);
            // }
            if(o == 0) continue;

            ii = hap->list[l].overlapID;
            if(overlap_list->list[ii].is_match == 1) overlap_list->list[ii].is_match = 2;
            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) && (o>=(overlap_list->list[ii].align_length*up))) {
                else if(hh_tp(hap->list[i])==0) {
                    ///not real allels
                    z = hap->list[i].overlapSite; s = &(hap->snp_stat.a[z]);
                    for (z = hap->list[i].overlapSite; z >= 0; z--) {
                        t = &(hap->snp_stat.a[z]);
                        if(s->site!=t->site) break;
                        // if(t->site == 14217) {
                        //     fprintf(stderr, "[M::%s]\tsite::%u\tn0::%u\tn1::%u\to::%lu\t%.*s\n", __func__, t->site, t->occ_0, t->occ_1, o, (int)Get_NAME_LENGTH(R_INF, overlap_list->list[ii].y_id), Get_NAME(R_INF, overlap_list->list[ii].y_id));
                        // }
                        t->occ_0 -= hap->list[i].cov;
                        assert(t->occ_0 >= 1);
                        if((st_max != ((uint64_t)-1)) && (overlap_list->list[ii].y_pos_strand == 0)) {
                            t->overlap_num -= hap->list[i].cov;
                            assert(t->overlap_num >= 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(is_st_bs((*s), st_rate, st_max)) continue;
                if(s->score == 1) o++;
            }
            ii = hap->list[l].overlapID;
            ///for HiFi, do not flip trans to cis
            // if(overlap_list->list[ii].is_match == 2 && o == 0) {
            //     overlap_list->list[ii].is_match = 1;
            // }
            if(overlap_list->list[ii].is_match == 1 && o > 0) {
                overlap_list->list[ii].is_match = 2;
            }
        }


        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;
            }
        } 
    }

    
    if(multi_check) {
        hap->snp_srt.n = 0;
        for (k = 1, l = 0; k <= hap->length; ++k) {  
            if (k == hap->length || hap->list[k].overlapID != hap->list[l].overlapID) {
                if(overlap_list->list[hap->list[l].overlapID].is_match == 2) {
                    l = k;
                    continue;
                }
                for (i = l, o = 0; i < k; 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(is_st_bs((*s), st_rate, st_max)) continue;
                    if(s->occ_0 >= asm_opt.s_hap_cov && s->occ_1 >= asm_opt.infor_cov) continue;
                    if(s->score == 1) continue;
                    o++; 
                    kv_push(uint64_t, hap->snp_srt, hap->list[i].overlapSite);
                }
                // if(overlap_list->list[hap->list[l].overlapID].y_id == 317 || overlap_list->list[hap->list[l].overlapID].y_id == 287) {
                //     fprintf(stderr, "***2***[M::%s-id::%u] o->%lu\n", __func__, overlap_list->list[hap->list[l].overlapID].y_id, o);
                // }
                hap->snp_srt.n -= o;
                ///there are at least two variants at one read
                if(o>=(overlap_list->list[hap->list[l].overlapID].align_length*up)) {
                    radix_sort_bc64(hap->snp_srt.a + hap->snp_srt.n, hap->snp_srt.a + hap->snp_srt.n + o);
                    a = hap->snp_srt.a + hap->snp_srt.n;
                    for (i = z = 0; i < o; i++) {
                        if(i > 0) s = &(hap->snp_stat.a[a[i-1]]);
                        if(i + 1 < o) t = &(hap->snp_stat.a[a[i+1]]);
                        if(s && s->site + 32 > hap->snp_stat.a[a[i]].site) continue;
                        if(t && hap->snp_stat.a[a[i]].site + 32 > t->site) continue;
                        a[z] = a[i];
                        z++;
                    }
                    if(z >= 2) hap->snp_srt.n += z; 
                }
                l = k;
            }
        }
        if (hap->snp_srt.n > 0) {
            radix_sort_bc64(hap->snp_srt.a, hap->snp_srt.a + hap->snp_srt.n);
            for (k = 1, l = 0; k <= hap->snp_srt.n; ++k) {  
                if(k == hap->snp_srt.n || hap->snp_srt.a[k] != hap->snp_srt.a[l]) {
                    if(k - l >= 2) hap->snp_stat.a[hap->snp_srt.a[l]].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)) && (!(is_st_bs((*s), st_rate, st_max)))) {
                            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;
                                break;
                            }
                        }
                    }

                }
            }
            l = k;
        }
    }    
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rcx, 0x28(%rsp)
movsd %xmm0, 0x30(%rsp)
movq %rsi, (%rsp)
cmpl $0x0, 0x10(%rdi)
je 0x248f9
movq %r8, %rbx
movq %rdi, %r15
pushq $0x1
popq %rcx
xorl %esi, %esi
xorl %edi, %edi
xorl %edx, %edx
xorl %eax, %eax
movq 0x220(%r15), %r8
cmpq %r8, %rcx
ja 0x23f3c
je 0x23e04
movq 0x218(%r15), %r9
imulq $0x28, %rcx, %r10
movl 0x20(%r9,%r10), %r10d
imulq $0x28, %rdi, %r11
cmpl 0x20(%r9,%r11), %r10d
je 0x23e49
testq %rdi, %rdi
je 0x23e23
movq 0x218(%r15), %r9
imulq $0x28, %rdi, %r10
movl -0x8(%r9,%r10), %r11d
incl %r11d
cmpl %r11d, 0x20(%r9,%r10)
je 0x23e46
cmpq %r8, %rcx
jae 0x23e4e
movq 0x218(%r15), %r8
imulq $0x28, %rdi, %r9
movl 0x20(%r8,%r9), %r9d
incl %r9d
imulq $0x28, %rcx, %r10
cmpl 0x20(%r8,%r10), %r9d
jne 0x23e4e
movq %rcx, %rdi
incq %rcx
jmp 0x23dd7
movl 0x10(%r15), %r12d
imulq $0x18, %rsi, %r9
imulq $0x28, %rdi, %r8
cmpq %r12, %rsi
jae 0x24927
movq (%r15), %r10
movl (%r10,%r9), %r11d
movq 0x218(%r15), %r14
cmpl 0x20(%r14,%r8), %r11d
je 0x23e81
incq %rsi
addq $0x18, %r9
jmp 0x23e5a
movl %edx, %r11d
subl %edi, %r11d
imulq $0x18, %rax, %r14
movl %r12d, %r12d
cmpq %r12, %rsi
jae 0x23ef6
movl (%r10,%r9), %ebp
movq 0x218(%r15), %r12
imulq $0x28, %rdi, %r13
cmpl 0x20(%r12,%r13), %ebp
jne 0x23ef6
movl 0x8(%r10,%r9), %r12d
cmpq %r12, %rdi
ja 0x24946
cmpq %r12, %rcx
jbe 0x24946
leaq (%r10,%r9), %r12
movq 0x10(%r12), %r13
movq %r13, 0x10(%r10,%r14)
movupd (%r12), %xmm0
movupd %xmm0, (%r10,%r14)
movq (%r15), %r10
incq %rax
addl %r11d, 0x8(%r10,%r14)
incq %rsi
movl 0x10(%r15), %r12d
addq $0x18, %r9
addq $0x18, %r14
jmp 0x23e8b
imulq $0x28, %rdx, %r9
cmpq %rcx, %rdi
jae 0x23e49
movq 0x218(%r15), %r10
incq %rdx
movq 0x20(%r10,%r8), %r11
movq %r11, 0x20(%r10,%r9)
movupd (%r10,%r8), %xmm0
movups 0x10(%r10,%r8), %xmm2
movups %xmm2, 0x10(%r10,%r9)
movupd %xmm0, (%r10,%r9)
incq %rdi
addq $0x28, %r8
addq $0x28, %r9
jmp 0x23efa
movq %rdx, 0x220(%r15)
movl %eax, 0x10(%r15)
testq %rdx, %rdx
je 0x248f9
testl %eax, %eax
je 0x248f9
movsd %xmm1, 0x8(%rsp)
andq $0x0, 0x238(%r15)
movq (%r15), %rdi
movl %eax, %eax
imulq $0x18, %rax, %rsi
addq %rdi, %rsi
callq 0x19538
pushq $0x1
popq %r14
xorl %r13d, %r13d
leaq 0x22f7d3(%rip), %rsi    # 0x253758
movabsq $-0x100000000, %r11     # imm = 0xFFFFFFFF00000000
xorl %r12d, %r12d
movl 0x10(%r15), %eax
cmpq %rax, %r14
ja 0x24110
je 0x23fba
movq (%r15), %rax
imulq $0x18, %r14, %rcx
movl 0x4(%rax,%rcx), %ecx
imulq $0x18, %r13, %rdx
cmpl 0x4(%rax,%rdx), %ecx
je 0x24108
movq 0x210(%rsi), %rax
movq 0x218(%rsi), %rcx
imulq $0x18, %r13, %rdx
addq $0xc, %rdx
movq %r13, %rsi
xorl %ebp, %ebp
movsd 0x8(%rsp), %xmm3
cmpq %r14, %rsi
jae 0x24084
movq (%r15), %rdi
testb $0x1, (%rdi,%rdx)
je 0x2404c
movq 0x218(%r15), %r12
movl -0x4(%rdi,%rdx), %r8d
imulq $0x28, %r8, %r8
movl 0x20(%r12,%r8), %r9d
cmpl -0xc(%rdi,%rdx), %r9d
jne 0x24965
addq %r8, %r12
movl 0x8(%r12), %r8d
cmpq $0x2, %r8
jb 0x2404c
movl 0xc(%r12), %edi
cmpq $0x2, %rdi
jb 0x2404c
cmpq $-0x1, %rbx
je 0x2403a
movl 0x4(%r12), %r9d
leaq (%r9,%rbx), %r10
cmpq %r8, %r10
jae 0x24055
cmpq %r8, %rcx
jg 0x2404c
xorl %r8d, %r8d
cmpq %rdi, %rax
setle %r8b
addq %r8, %rbp
incq %rsi
addq $0x18, %rdx
jmp 0x23fdb
cvtsi2sd %r8, %xmm0
movl %r9d, %r9d
cvtsi2sd %r9, %xmm1
movapd %xmm0, %xmm2
mulsd %xmm3, %xmm2
addsd %xmm1, %xmm2
ucomisd %xmm0, %xmm2
setae %r9b
cmpq %r8, %rcx
setg %r8b
orb %r9b, %r8b
je 0x2403f
jmp 0x2404c
testq %rbp, %rbp
je 0x240fe
movq 0x238(%r15), %rcx
movq 0x248(%r15), %rax
cmpq 0x240(%r15), %rcx
jne 0x240d9
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x240(%r15)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movabsq $-0x100000000, %r11     # imm = 0xFFFFFFFF00000000
movq %rax, 0x248(%r15)
movq 0x238(%r15), %rcx
leaq 0x22f678(%rip), %rsi    # 0x253758
shlq $0x20, %rbp
subq %rbp, %r13
addq %r11, %r13
leaq 0x1(%rcx), %rdx
movq %rdx, 0x238(%r15)
movq %r13, (%rax,%rcx,8)
movq %r14, %r13
jmp 0x24108
movq %r14, %r13
leaq 0x22f650(%rip), %rsi    # 0x253758
incq %r14
jmp 0x23f92
movq 0x238(%r15), %rcx
testq %rcx, %rcx
je 0x244b0
movq 0x248(%r15), %rdi
leaq (%rdi,%rcx,8), %rsi
callq 0x1905d
movq 0x238(%r15), %rax
movq %rax, 0x10(%rsp)
xorl %ecx, %ecx
xorl %ebp, %ebp
movsd 0x8(%rsp), %xmm3
cmpq 0x10(%rsp), %rcx
je 0x2431f
movq 0x248(%r15), %rax
movq %rcx, 0x20(%rsp)
movl (%rax,%rcx,8), %edx
movl 0x10(%r15), %esi
leaq 0x22f5ed(%rip), %rax    # 0x253758
movq 0x210(%rax), %rcx
movq %rcx, 0x18(%rsp)
movq 0x218(%rax), %r9
imulq $0x18, %rdx, %rdi
leaq 0xc(%rdi), %r10
movq %rdx, %r14
xorl %r11d, %r11d
cmpq %rsi, %r14
jae 0x24234
movq (%r15), %rax
movl -0x8(%rax,%r10), %r8d
cmpl 0x4(%rax,%rdi), %r8d
jne 0x24234
testb $0x1, (%rax,%r10)
je 0x241ff
movq 0x218(%r15), %r8
movl -0x4(%rax,%r10), %eax
imulq $0x28, %rax, %rax
leaq (%r8,%rax), %r12
movl 0x8(%r8,%rax), %eax
cmpq $0x2, %rax
jb 0x241ff
movl 0xc(%r12), %r13d
cmpq $0x2, %r13
jb 0x241ff
cmpq $-0x1, %rbx
je 0x241ed
movl 0x4(%r12), %r8d
leaq (%r8,%rbx), %rcx
cmpq %rax, %rcx
jae 0x24208
cmpq %rax, %r9
jg 0x241ff
xorl %eax, %eax
cmpq %r13, 0x18(%rsp)
setle %al
addq %rax, %r11
incq %r14
addq $0x18, %r10
jmp 0x2418c
cvtsi2sd %rax, %xmm0
movl %r8d, %ecx
cvtsi2sd %rcx, %xmm1
movapd %xmm0, %xmm2
mulsd %xmm3, %xmm2
addsd %xmm1, %xmm2
ucomisd %xmm0, %xmm2
setae %cl
cmpq %rax, %r9
setg %al
orb %cl, %al
je 0x241f2
jmp 0x241ff
testq %r11, %r11
je 0x24312
movq (%r15), %r8
movl 0x4(%r8,%rdi), %edi
movq (%rsp), %rax
movq (%rax), %rax
imulq $0xa8, %rdi, %r9
cmpb $0x1, 0x2c(%rax,%r9)
jne 0x24266
leaq (%rax,%r9), %rcx
addq $0x2c, %rcx
movb $0x2, (%rcx)
addq %rax, %r9
addq $0x1c, %r9
cmpq %rsi, %rdx
jae 0x24312
imulq $0x18, %rdx, %rax
cmpl %edi, 0x4(%r8,%rax)
jne 0x24312
leaq (%r8,%rax), %r10
testb $0x1, 0xc(%r10)
jne 0x242ea
movl 0x8(%r10), %r10d
imulq $0x28, %r10, %r12
addq 0x218(%r15), %r12
leaq (%r8,%rax), %r11
addq $0x10, %r11
movq %r12, %r14
testq %r10, %r10
js 0x2430a
movq %r14, %rax
movl 0x20(%r12), %ecx
cmpl 0x20(%r14), %ecx
jne 0x24307
movl (%r11), %ebp
subl %ebp, 0x8(%rax)
je 0x24908
cmpq $-0x1, %rbx
je 0x242de
cmpl $0x0, (%r9)
jne 0x242de
subl %ebp, 0x4(%rax)
je 0x24984
decq %r10
leaq -0x28(%rax), %r14
movq %rax, %rbp
jmp 0x242aa
movq 0x218(%r15), %rax
movl 0x8(%r10), %ecx
imulq $0x28, %rcx, %rcx
leaq (%rax,%rcx), %r12
movl $0x1, 0x1c(%rax,%rcx)
jmp 0x2430a
movq %rax, %rbp
incq %rdx
jmp 0x2426d
movq 0x20(%rsp), %rcx
incq %rcx
jmp 0x24146
xorl %ecx, %ecx
cmpq 0x10(%rsp), %rcx
je 0x24411
movq 0x248(%r15), %rax
movl (%rax,%rcx,8), %esi
movl 0x10(%r15), %edi
movq (%r15), %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 0x243de
movl (%r10), %eax
cmpl %eax, -0x8(%r11)
jne 0x243e3
testb $0x1, (%r11)
je 0x243d2
movq 0x218(%r15), %rax
movl -0x4(%r11), %r14d
imulq $0x28, %r14, %r14
leaq (%rax,%r14), %r12
movl 0x8(%rax,%r14), %eax
cmpq $0x2, %rax
jb 0x243d2
cmpl $0x2, 0xc(%r12)
jb 0x243d2
cmpq $-0x1, %rbx
je 0x243c4
movl 0x4(%r12), %r14d
leaq (%r14,%rbx), %r13
cmpq %rax, %r13
jb 0x243c4
cvtsi2sd %rax, %xmm0
movl %r14d, %eax
cvtsi2sd %rax, %xmm1
movapd %xmm0, %xmm2
mulsd %xmm3, %xmm2
addsd %xmm1, %xmm2
ucomisd %xmm0, %xmm2
jae 0x243d2
xorl %eax, %eax
cmpl $0x1, 0x1c(%r12)
sete %al
addq %rax, %rdx
incq %rsi
addq $0x18, %r11
jmp 0x24353
movl 0x4(%r8,%r9), %eax
movl %eax, %esi
movq (%rsp), %rax
movq (%rax), %rax
imulq $0xa8, %rsi, %rsi
cmpb $0x1, 0x2c(%rax,%rsi)
jne 0x24409
testq %rdx, %rdx
je 0x24409
addq %rsi, %rax
addq $0x2c, %rax
movb $0x2, (%rax)
incq %rcx
jmp 0x24321
movl 0x10(%r15), %eax
leaq 0x1(%rax), %rcx
pushq $0x1
popq %rdx
xorl %esi, %esi
cmpq %rcx, %rdx
je 0x244b8
movq (%r15), %rdi
cmpq %rax, %rdx
jne 0x2443a
imulq $0x18, %rsi, %r8
movl 0x4(%rdi,%r8), %r8d
jmp 0x24453
imulq $0x18, %rdx, %r9
imulq $0x18, %rsi, %r8
movl 0x4(%rdi,%r8), %r8d
cmpl %r8d, 0x4(%rdi,%r9)
jne 0x24453
movq %rsi, %r8
jmp 0x244a5
movl %r8d, %r8d
movq (%rsp), %r9
movq (%r9), %r9
imulq $0xa8, %r8, %r8
cmpb $0x1, 0x2c(%r9,%r8)
movq %rdx, %r8
jne 0x244a5
imulq $0x18, %rsi, %r8
addq %r8, %rdi
addq $0xc, %rdi
cmpq %rdx, %rsi
jae 0x244a2
testb $0x1, (%rdi)
je 0x24499
movq 0x218(%r15), %r8
movl -0x4(%rdi), %r9d
imulq $0x28, %r9, %r9
orl $-0x1, 0x1c(%r8,%r9)
incq %rsi
addq $0x18, %rdi
jmp 0x2447a
movq %rdx, %r8
incq %rdx
movq %r8, %rsi
jmp 0x2441e
xorl %ebp, %ebp
movsd 0x8(%rsp), %xmm3
cmpq $0x0, 0x28(%rsp)
je 0x247ce
andq $0x0, 0x238(%r15)
pushq $0x1
popq %r10
xorl %r9d, %r9d
movl %eax, %ecx
cmpq %rcx, %r10
ja 0x247a8
movq (%r15), %rcx
jne 0x244ed
imulq $0x18, %r9, %rax
movl 0x4(%rcx,%rax), %eax
jmp 0x24503
imulq $0x18, %r10, %rdx
imulq $0x18, %r9, %rax
movl 0x4(%rcx,%rax), %eax
cmpl %eax, 0x4(%rcx,%rdx)
je 0x2479c
movq (%rsp), %rdx
movq (%rdx), %rdx
movl %eax, %eax
imulq $0xa8, %rax, %rax
cmpb $0x2, 0x2c(%rdx,%rax)
je 0x24799
imulq $0x18, %r9, %r14
movq %r9, %r13
xorl %r8d, %r8d
cmpq %r10, %r13
jae 0x2465d
testb $0x1, 0xc(%rcx,%r14)
je 0x24651
movq 0x218(%r15), %rax
movl 0x8(%rcx,%r14), %edx
imulq $0x28, %rdx, %rdx
leaq (%rax,%rdx), %r12
movl 0x8(%rax,%rdx), %eax
cmpq $0x2, %rax
jb 0x24651
movl 0xc(%r12), %edx
cmpq $0x2, %rdx
jb 0x24651
cmpq $-0x1, %rbx
je 0x245a4
movl 0x4(%r12), %esi
leaq (%rsi,%rbx), %rdi
cmpq %rax, %rdi
jb 0x245a4
cvtsi2sd %rax, %xmm0
movl %esi, %esi
cvtsi2sd %rsi, %xmm1
movapd %xmm0, %xmm2
mulsd %xmm3, %xmm2
addsd %xmm1, %xmm2
ucomisd %xmm0, %xmm2
jae 0x24651
leaq 0x22f1ad(%rip), %rsi    # 0x253758
cmpq %rax, 0x218(%rsi)
jg 0x245c1
cmpq %rdx, 0x210(%rsi)
jle 0x24651
cmpl $0x1, 0x1c(%r12)
je 0x24651
movq 0x238(%r15), %rdx
movq 0x248(%r15), %rax
cmpq 0x240(%r15), %rdx
jne 0x2463a
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x240(%r15)
shlq $0x3, %rsi
movq %rax, %rdi
movq %r10, 0x10(%rsp)
movq %r8, 0x20(%rsp)
movq %r9, 0x18(%rsp)
callq 0x2370
movq 0x18(%rsp), %r9
movq 0x20(%rsp), %r8
movq 0x10(%rsp), %r10
movsd 0x8(%rsp), %xmm3
movq %rax, 0x248(%r15)
movq (%r15), %rcx
movq 0x238(%r15), %rdx
incq %r8
movl 0x8(%rcx,%r14), %esi
leaq 0x1(%rdx), %rdi
movq %rdi, 0x238(%r15)
movq %rsi, (%rax,%rdx,8)
incq %r13
addq $0x18, %r14
jmp 0x24528
movq 0x238(%r15), %rax
movq %rax, %rdx
subq %r8, %rdx
movq %rdx, 0x238(%r15)
movq %r8, %xmm0
punpckldq 0x1bebf2(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x1bebfa(%rip), %xmm0   # 0x1e3280
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
movq (%rsp), %rsi
movq (%rsi), %rsi
imulq $0x18, %r9, %rdi
movl 0x4(%rcx,%rdi), %ecx
imulq $0xa8, %rcx, %rcx
movl 0x28(%rsi,%rcx), %ecx
cvtsi2sd %rcx, %xmm2
addsd %xmm0, %xmm1
mulsd 0x30(%rsp), %xmm2
ucomisd %xmm2, %xmm1
jb 0x24799
movq 0x248(%r15), %rcx
leaq (%rcx,%rdx,8), %rdi
leaq (%rcx,%rax,8), %rsi
movq %r10, %r14
movq %r8, %r13
callq 0x1905d
movq %r13, %r11
movq %r14, %r10
movsd 0x8(%rsp), %xmm3
movq 0x238(%r15), %rax
shlq $0x3, %rax
addq 0x248(%r15), %rax
movq %r13, %rdx
negq %rdx
xorl %esi, %esi
xorl %ecx, %ecx
incq %rsi
leaq (%rdx,%rsi), %rdi
cmpq $0x1, %rdi
je 0x2478c
cmpq $0x1, %rsi
je 0x24723
imulq $0x28, -0x10(%rax,%rsi,8), %r12
addq 0x218(%r15), %r12
cmpq %r11, %rsi
jae 0x24734
imulq $0x28, (%rax,%rsi,8), %rbp
addq 0x218(%r15), %rbp
testq %r12, %r12
je 0x24755
movl 0x20(%r12), %edi
addl $0x20, %edi
movq 0x218(%r15), %r8
imulq $0x28, -0x8(%rax,%rsi,8), %r9
cmpl 0x20(%r8,%r9), %edi
ja 0x24703
testq %rbp, %rbp
je 0x2477b
movq 0x218(%r15), %r8
movq -0x8(%rax,%rsi,8), %rdi
imulq $0x28, %rdi, %r9
movl 0x20(%r8,%r9), %r8d
addl $0x20, %r8d
cmpl 0x20(%rbp), %r8d
ja 0x24703
jmp 0x24780
movq -0x8(%rax,%rsi,8), %rdi
movq %rdi, (%rax,%rcx,8)
incq %rcx
jmp 0x24703
cmpq $0x2, %rcx
jb 0x24799
addq %rcx, 0x238(%r15)
movq %r10, %r9
incq %r10
movl 0x10(%r15), %eax
jmp 0x244d3
movq 0x238(%r15), %rcx
testq %rcx, %rcx
je 0x247ce
movq 0x248(%r15), %rdi
leaq (%rdi,%rcx,8), %rsi
callq 0x1905d
movsd 0x8(%rsp), %xmm3
movl 0x10(%r15), %eax
movl %eax, %eax
leaq 0x1(%rax), %rcx
pushq $0x1
popq %rdx
xorl %esi, %esi
cmpq %rcx, %rdx
je 0x248f9
movq (%r15), %r11
cmpq %rax, %rdx
jne 0x247f5
imulq $0x18, %rsi, %rdi
movl 0x4(%r11,%rdi), %edi
jmp 0x24811
imulq $0x18, %rdx, %r8
imulq $0x18, %rsi, %rdi
movl 0x4(%r11,%rdi), %edi
cmpl %edi, 0x4(%r11,%r8)
jne 0x24811
movq %rsi, %r10
jmp 0x248e6
movl %edi, %edi
movq (%rsp), %r8
movq (%r8), %r8
imulq $0xa8, %rdi, %r9
leaq (%r8,%r9), %rdi
movzbl 0x2c(%r8,%r9), %ebp
cmpl $0x2, %ebp
je 0x248cf
movq %rdx, %r10
cmpl $0x1, %ebp
jne 0x248e6
leaq (%r8,%r9), %r10
addq $0x2e, %r10
imulq $0x18, %rsi, %r14
addq %r14, %r11
addq $0xc, %r11
cmpq %rdx, %rsi
jae 0x248e3
movq 0x218(%r15), %r14
movl -0x4(%r11), %r12d
imulq $0x28, %r12, %r12
cmpl $0x1, 0x1c(%r14,%r12)
jne 0x248c6
addq %r12, %r14
movl 0x8(%r14), %r12d
cmpq $0x2, %r12
jb 0x248c6
cmpl $0x2, 0xc(%r14)
jb 0x248c6
movb (%r11), %bpl
cmpq $-0x1, %rbx
je 0x248bc
movl 0x4(%r14), %r14d
leaq (%r14,%rbx), %r13
cmpq %r12, %r13
jb 0x248bc
cvtsi2sd %r12, %xmm0
movl %r14d, %r14d
cvtsi2sd %r14, %xmm1
movapd %xmm0, %xmm2
mulsd %xmm3, %xmm2
addsd %xmm1, %xmm2
ucomisd %xmm0, %xmm2
jae 0x248c6
movb $0x1, (%r10)
testb $0x1, %bpl
jne 0x248f1
incq %rsi
addq $0x18, %r11
jmp 0x24853
movb $0x1, 0x2e(%rdi)
movl 0x8(%rdi), %esi
subl 0x4(%rdi), %esi
incl %esi
movq (%rsp), %rdi
subq %rsi, 0x18(%rdi)
movq %rdx, %r10
incq %rdx
movq %r10, %rsi
jmp 0x247d9
movb $0x2, 0x2c(%r8,%r9)
jmp 0x248d3
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1bf28b(%rip), %rdi    # 0x1e3b9a
leaq 0x1bed8d(%rip), %rsi    # 0x1e36a3
leaq 0x1bf37b(%rip), %rcx    # 0x1e3c98
movl $0x231d, %edx           # imm = 0x231D
callq 0x2110
leaq 0x1bf32a(%rip), %rdi    # 0x1e3c58
leaq 0x1bed6e(%rip), %rsi    # 0x1e36a3
leaq 0x1bf35c(%rip), %rcx    # 0x1e3c98
movl $0x229f, %edx           # imm = 0x229F
callq 0x2110
leaq 0x1bf3d2(%rip), %rdi    # 0x1e3d1f
leaq 0x1bed4f(%rip), %rsi    # 0x1e36a3
leaq 0x1bf33d(%rip), %rcx    # 0x1e3c98
movl $0x22a2, %edx           # imm = 0x22A2
callq 0x2110
leaq 0x1bf3ef(%rip), %rdi    # 0x1e3d5b
leaq 0x1bed30(%rip), %rsi    # 0x1e36a3
leaq 0x1bf31e(%rip), %rcx    # 0x1e3c98
movl $0x22c0, %edx           # imm = 0x22C0
callq 0x2110
leaq 0x1bf3ed(%rip), %rdi    # 0x1e3d78
leaq 0x1bed11(%rip), %rsi    # 0x1e36a3
leaq 0x1bf2ff(%rip), %rcx    # 0x1e3c98
movl $0x2320, %edx           # imm = 0x2320
callq 0x2110