// File: /workspace/github_cpp_makefile_star2_O1/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 $0x48, %rsp
movq %r8, 0x8(%rsp)
movsd %xmm1, 0x20(%rsp)
movq %rcx, 0x30(%rsp)
movsd %xmm0, 0x38(%rsp)
movq %rsi, 0x10(%rsp)
cmpl $0x0, 0x10(%rdi)
je 0x538df
movq %rdi, %r15
movq 0x220(%rdi), %r9
testq %r9, %r9
je 0x52e42
movl $0x1, %edx
movq $-0x1, %rsi
xorl %eax, %eax
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %edi, %edi
cmpq %r9, %rdx
je 0x52c70
movq 0x218(%r15), %r10
leaq (%rdx,%rdx,4), %r11
movl 0x20(%r10,%r11,8), %r11d
leaq (%r8,%r8,4), %rbx
cmpl 0x20(%r10,%rbx,8), %r11d
je 0x52e2a
testq %r8, %r8
je 0x52c8e
movq 0x218(%r15), %r10
leaq (%r8,%r8,4), %r11
movl -0x8(%r10,%r11,8), %ebx
incl %ebx
cmpl %ebx, 0x20(%r10,%r11,8)
je 0x52cb1
cmpq %r9, %rdx
jae 0x52cb9
movq 0x218(%r15), %r9
leaq (%r8,%r8,4), %r10
movl 0x20(%r9,%r10,8), %r10d
incl %r10d
leaq (%rdx,%rdx,4), %r11
cmpl 0x20(%r9,%r11,8), %r10d
jne 0x52cb9
movq %rdx, %r8
jmp 0x52e2a
movl 0x10(%r15), %r10d
cmpq %r10, %rdi
setb %bl
jae 0x538ee
movq (%r15), %r14
movq 0x218(%r15), %r11
leaq (%r8,%r8,4), %r9
movl 0x20(%r11,%r9,8), %r11d
leaq (%rdi,%rdi,2), %r12
cmpl %r11d, (%r14,%r12,8)
je 0x52d10
leaq (%r14,%r12,8), %r14
addq $0x18, %r14
leaq -0x1(%r10), %rbx
cmpq %rdi, %rbx
je 0x538ee
leaq 0x18(%r14), %r12
incq %rdi
cmpl %r11d, (%r14)
movq %r12, %r14
jne 0x52cf2
cmpq %r10, %rdi
setb %bl
testb %bl, %bl
je 0x538ee
movq (%r15), %r10
leaq (%rdi,%rdi,2), %r11
movl (%r10,%r11,8), %r10d
movq 0x218(%r15), %r11
cmpl 0x20(%r11,%r9,8), %r10d
jne 0x538ee
movl 0x10(%r15), %r10d
cmpq %r10, %rdi
jae 0x52dc5
movl %ecx, %r10d
subl %r8d, %r10d
leaq (,%rax,8), %r11
leaq (%r11,%r11,2), %r11
leaq (,%rdi,8), %rbx
leaq (%rbx,%rbx,2), %rbx
movq (%r15), %r14
movl (%r14,%rbx), %ebp
movq 0x218(%r15), %r12
cmpl 0x20(%r12,%r9,8), %ebp
jne 0x52dc5
movl 0x8(%r14,%rbx), %r12d
cmpq %r12, %r8
ja 0x5392c
cmpq %r12, %rdx
jbe 0x5392c
leaq (%r14,%rbx), %r12
movq 0x10(%r12), %r13
movq %r13, 0x10(%r14,%r11)
movupd (%r12), %xmm0
movupd %xmm0, (%r14,%r11)
movq (%r15), %r14
incq %rax
addl %r10d, 0x8(%r14,%r11)
incq %rdi
movl 0x10(%r15), %r14d
addq $0x18, %r11
addq $0x18, %rbx
cmpq %r14, %rdi
jb 0x52d60
cmpq %rdx, %r8
jae 0x52e2a
leaq (%r8,%rsi), %r9
leaq (,%rcx,8), %r10
leaq (%r10,%r10,4), %r10
leaq (,%r8,8), %r11
leaq (%r11,%r11,4), %rbx
xorl %r11d, %r11d
movq 0x218(%r15), %r14
movq 0x20(%r14,%rbx), %r12
movq %r12, 0x20(%r14,%r10)
movupd (%r14,%rbx), %xmm0
movupd 0x10(%r14,%rbx), %xmm1
movupd %xmm1, 0x10(%r14,%r10)
movupd %xmm0, (%r14,%r10)
decq %r11
addq $0x28, %r10
addq $0x28, %rbx
cmpq %r11, %r9
jne 0x52de9
subq %r11, %rcx
subq %r11, %r8
incq %rdx
movq 0x220(%r15), %r9
decq %rsi
cmpq %r9, %rdx
jbe 0x52c4c
jmp 0x52e46
xorl %ecx, %ecx
xorl %eax, %eax
movq %rcx, 0x220(%r15)
movl %eax, 0x10(%r15)
testq %rcx, %rcx
je 0x538df
testl %eax, %eax
je 0x538df
movq $0x0, 0x238(%r15)
movq (%r15), %rdi
movl %eax, %eax
leaq (%rax,%rax,2), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x46275
movl 0x10(%r15), %eax
testq %rax, %rax
je 0x5301f
movl $0x1, %ebx
xorl %r13d, %r13d
movabsq $-0x100000000, %r14     # imm = 0xFFFFFFFF00000000
xorl %r12d, %r12d
cmpq %rax, %rbx
je 0x52ebf
movq (%r15), %rax
leaq (%rbx,%rbx,2), %rcx
movl 0x4(%rax,%rcx,8), %ecx
leaq (%r12,%r12,2), %rdx
cmpl 0x4(%rax,%rdx,8), %ecx
je 0x53009
cmpq %rbx, %r12
jae 0x52f9a
movq (%r15), %rdx
leaq 0x237886(%rip), %rcx    # 0x28a758
movq 0x210(%rcx), %rax
movq 0x218(%rcx), %rcx
leaq (%r12,%r12,2), %rsi
leaq (%rdx,%rsi,8), %rdx
addq $0xc, %rdx
xorl %ebp, %ebp
movq %r12, %rsi
testb $0x1, (%rdx)
je 0x52f55
movq 0x218(%r15), %rdi
movl -0x4(%rdx), %r8d
leaq (%r8,%r8,4), %r8
movl 0x20(%rdi,%r8,8), %r9d
cmpl -0xc(%rdx), %r9d
jne 0x5394b
leaq (%rdi,%r8,8), %r13
movl 0x8(%r13), %r8d
cmpq $0x2, %r8
jb 0x52f55
movl 0xc(%r13), %edi
cmpq $0x2, %rdi
jb 0x52f55
movq 0x8(%rsp), %r10
cmpq $-0x1, %r10
je 0x52f43
movl 0x4(%r13), %r9d
addq %r9, %r10
cmpq %r8, %r10
jae 0x52f63
cmpq %r8, %rcx
jg 0x52f55
xorl %r8d, %r8d
cmpq %rdi, %rax
setle %r8b
addq %r8, %rbp
incq %rsi
addq $0x18, %rdx
cmpq %rsi, %rbx
jne 0x52ef1
jmp 0x52f9c
xorps %xmm0, %xmm0
cvtsi2sd %r8, %xmm0
movl %r9d, %r9d
xorps %xmm1, %xmm1
cvtsi2sd %r9, %xmm1
movapd %xmm0, %xmm2
mulsd 0x20(%rsp), %xmm2
addsd %xmm1, %xmm2
ucomisd %xmm0, %xmm2
setae %r9b
cmpq %r8, %rcx
setg %r8b
orb %r9b, %r8b
je 0x52f48
jmp 0x52f55
xorl %ebp, %ebp
testq %rbp, %rbp
je 0x53006
movq 0x240(%r15), %rax
cmpq %rax, 0x238(%r15)
jne 0x52fdf
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x240(%r15)
movq 0x248(%r15), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x248(%r15)
shlq $0x20, %rbp
subq %rbp, %r12
addq %r14, %r12
movq 0x238(%r15), %rax
movq 0x248(%r15), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x238(%r15)
movq %r12, (%rcx,%rax,8)
movq %rbx, %r12
leaq 0x1(%rbx), %rcx
movl 0x10(%r15), %eax
cmpq %rax, %rbx
movq %rcx, %rbx
jb 0x52ea1
jmp 0x53022
xorl %r13d, %r13d
movq 0x238(%r15), %rax
testq %rax, %rax
je 0x5325a
movq 0x248(%r15), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x45d47
movq 0x238(%r15), %rax
movq %rax, 0x28(%rsp)
testq %rax, %rax
je 0x53261
movq 0x248(%r15), %rax
movq %rax, 0x18(%rsp)
movl 0x10(%r15), %edx
xorl %ebp, %ebp
xorl %edi, %edi
movq 0x18(%rsp), %rax
movl (%rax,%rdi,8), %r8d
movb $0x1, %cl
cmpl %r8d, %edx
jbe 0x53158
movq (%r15), %rax
leaq (%r8,%r8,2), %rcx
leaq (%rax,%rcx,8), %r9
addq $0xc, %r9
movl -0x8(%r9), %r10d
leaq 0x2376bf(%rip), %rax    # 0x28a758
movq 0x210(%rax), %rcx
movq %rcx, 0x40(%rsp)
movq 0x218(%rax), %r14
movq %rdx, %r12
subq %r8, %r12
xorl %ebx, %ebx
cmpl %r10d, -0x8(%r9)
jne 0x53152
testb $0x1, (%r9)
je 0x53114
movq 0x218(%r15), %rax
movl -0x4(%r9), %ecx
leaq (%rcx,%rcx,4), %rcx
leaq (%rax,%rcx,8), %r13
movl 0x8(%r13), %ecx
cmpq $0x2, %rcx
jb 0x53114
movl 0xc(%r13), %esi
cmpq $0x2, %rsi
movq 0x8(%rsp), %r11
jb 0x53114
cmpq $-0x1, %r11
je 0x53102
movl 0x4(%r13), %eax
addq %rax, %r11
cmpq %rcx, %r11
jae 0x5311f
cmpq %rcx, %r14
jg 0x53114
xorl %eax, %eax
cmpq %rsi, 0x40(%rsp)
setle %al
addq %rax, %rbx
addq $0x18, %r9
decq %r12
jne 0x530b4
jmp 0x53152
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
movl %eax, %eax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
movapd %xmm0, %xmm2
mulsd 0x20(%rsp), %xmm2
addsd %xmm1, %xmm2
ucomisd %xmm0, %xmm2
setae %al
cmpq %rcx, %r14
setg %cl
orb %al, %cl
je 0x53107
jmp 0x53114
testq %rbx, %rbx
sete %cl
testb %cl, %cl
jne 0x5324a
movq (%r15), %r9
leaq (%r8,%r8,2), %rcx
movl 0x4(%r9,%rcx,8), %r10d
movq 0x10(%rsp), %rax
movq (%rax), %rsi
imulq $0xa8, %r10, %r11
cmpb $0x1, 0x2c(%rsi,%r11)
jne 0x5318e
leaq (%rsi,%r11), %rax
addq $0x2c, %rax
movb $0x2, (%rax)
cmpl %r8d, %edx
jbe 0x5324a
cmpl %r10d, 0x4(%r9,%rcx,8)
jne 0x5324a
addq %rsi, %r11
addq $0x1c, %r11
leaq (%r9,%rcx,8), %rcx
testb $0x1, 0xc(%rcx)
jne 0x53216
movl 0x8(%rcx), %r14d
movq 0x218(%r15), %rbx
leaq (%r14,%r14,4), %rax
leaq (%rbx,%rax,8), %r13
movl 0x20(%r13), %esi
leaq (%r8,%r8,2), %rax
leaq (%r9,%rax,8), %r12
addq $0x10, %r12
incq %r14
movq %r13, %rbp
movq 0x8(%rsp), %rax
cmpl 0x20(%rbp), %esi
jne 0x53230
movl (%r12), %ecx
subl %ecx, 0x8(%rbp)
je 0x5390d
cmpq $-0x1, %rax
je 0x53208
cmpl $0x0, (%r11)
jne 0x53208
subl %ecx, 0x4(%rbp)
je 0x5396a
addq $-0x28, %rbp
decq %r14
jg 0x531e1
movq %rbx, %rbp
jmp 0x53230
movq 0x218(%r15), %rax
movl 0x8(%rcx), %ecx
leaq (%rcx,%rcx,4), %rcx
leaq (%rax,%rcx,8), %r13
movl $0x1, 0x1c(%r13)
incq %r8
cmpq %rdx, %r8
je 0x5324a
leaq (%r8,%r8,2), %rax
leaq (%r9,%rax,8), %rcx
cmpl %r10d, 0x4(%rcx)
je 0x531ad
incq %rdi
cmpq 0x28(%rsp), %rdi
jne 0x5306b
jmp 0x53263
xorl %ebp, %ebp
jmp 0x5343a
xorl %ebp, %ebp
cmpq $0x0, 0x28(%rsp)
je 0x53387
movq 0x248(%r15), %rsi
movl 0x10(%r15), %edx
movq (%r15), %rcx
movq 0x10(%rsp), %rax
movq (%rax), %rdi
xorl %r8d, %r8d
movq %rcx, 0x18(%rsp)
movl (%rsi,%r8,8), %r11d
leaq (%r11,%r11,2), %r9
cmpl %r11d, %edx
jbe 0x53354
movq (%r15), %rax
leaq (%rax,%r9,8), %r10
addq $0xc, %r10
movl -0x8(%r10), %ebx
movq %rdx, %r14
subq %r11, %r14
xorl %r11d, %r11d
cmpl %ebx, -0x8(%r10)
jne 0x53346
testb $0x1, (%r10)
je 0x53339
movq 0x218(%r15), %rax
movl -0x4(%r10), %r12d
leaq (%r12,%r12,4), %r12
leaq (%rax,%r12,8), %r13
movl 0x8(%r13), %r12d
cmpq $0x2, %r12
jb 0x53339
cmpl $0x2, 0xc(%r13)
movq 0x8(%rsp), %rax
jb 0x53339
cmpq $-0x1, %rax
je 0x5332c
movl 0x4(%r13), %eax
movq 0x8(%rsp), %rcx
addq %rax, %rcx
cmpq %r12, %rcx
jb 0x5332c
xorps %xmm0, %xmm0
cvtsi2sd %r12, %xmm0
movl %eax, %eax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
movapd %xmm0, %xmm2
mulsd 0x20(%rsp), %xmm2
addsd %xmm1, %xmm2
ucomisd %xmm0, %xmm2
jae 0x53339
xorl %eax, %eax
cmpl $0x1, 0x1c(%r13)
sete %al
addq %rax, %r11
addq $0x18, %r10
decq %r14
jne 0x532b6
testq %r11, %r11
setne %r10b
movq 0x18(%rsp), %rcx
jmp 0x53357
xorl %r10d, %r10d
movl 0x4(%rcx,%r9,8), %eax
imulq $0xa8, %rax, %rax
cmpb $0x1, 0x2c(%rdi,%rax)
jne 0x53379
testb %r10b, %r10b
je 0x53379
addq %rdi, %rax
addq $0x2c, %rax
movb $0x2, (%rax)
incq %r8
cmpq 0x28(%rsp), %r8
jne 0x5328d
movl 0x10(%r15), %eax
testq %rax, %rax
je 0x5343a
movl $0x1, %ecx
xorl %edx, %edx
cmpq %rax, %rcx
je 0x533bb
movq (%r15), %rsi
leaq (%rcx,%rcx,2), %rdi
movl 0x4(%rsi,%rdi,8), %edi
leaq (%rdx,%rdx,2), %r8
cmpl 0x4(%rsi,%r8,8), %edi
jne 0x533bb
movq %rdx, %r8
jmp 0x53427
movq (%r15), %rsi
leaq (%rdx,%rdx,2), %rdi
movl 0x4(%rsi,%rdi,8), %r8d
movq 0x10(%rsp), %r9
movq (%r9), %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 0x53427
leaq (%rsi,%rdi,8), %rsi
addq $0xc, %rsi
testb $0x1, (%rsi)
je 0x53418
movq 0x218(%r15), %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 0x533fb
movq %rcx, %r8
leaq 0x1(%rcx), %rsi
movq %r8, %rdx
cmpq %rax, %rcx
movq %rsi, %rcx
jne 0x5339b
cmpq $0x0, 0x30(%rsp)
je 0x53792
movq $0x0, 0x238(%r15)
movl 0x10(%r15), %eax
testq %rax, %rax
je 0x53776
movl $0x1, %ebx
xorl %r12d, %r12d
leaq 0x2372eb(%rip), %rdi    # 0x28a758
cmpq %rax, %rbx
je 0x5348b
movq (%r15), %rax
leaq (%rbx,%rbx,2), %rcx
movl 0x4(%rax,%rcx,8), %ecx
leaq (%r12,%r12,2), %rdx
cmpl 0x4(%rax,%rdx,8), %ecx
je 0x53762
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq (%r15), %rcx
leaq (%r12,%r12,2), %r9
movl 0x4(%rcx,%r9,8), %ecx
imulq $0xa8, %rcx, %rcx
cmpb $0x2, 0x2c(%rax,%rcx)
je 0x53666
cmpq %rbx, %r12
jae 0x535ff
leaq (,%r12,8), %rax
leaq (%rax,%rax,2), %r14
xorl %r8d, %r8d
movq (%r15), %rax
testb $0x1, 0xc(%rax,%r14)
je 0x535ed
movq 0x218(%r15), %rcx
movl 0x8(%rax,%r14), %eax
leaq (%rax,%rax,4), %rax
leaq (%rcx,%rax,8), %r13
movl 0x8(%r13), %eax
cmpq $0x2, %rax
jb 0x535ed
movl 0xc(%r13), %ecx
cmpq $0x2, %rcx
jb 0x535ed
cmpq $-0x1, 0x8(%rsp)
je 0x5354b
movl 0x4(%r13), %edx
movq 0x8(%rsp), %rsi
addq %rdx, %rsi
cmpq %rax, %rsi
jb 0x5354b
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movl %edx, %edx
xorps %xmm1, %xmm1
cvtsi2sd %rdx, %xmm1
movapd %xmm0, %xmm2
mulsd 0x20(%rsp), %xmm2
addsd %xmm1, %xmm2
ucomisd %xmm0, %xmm2
jae 0x535ed
cmpq %rax, 0x218(%rdi)
jg 0x53561
cmpq %rcx, 0x210(%rdi)
jle 0x535ed
cmpl $0x1, 0x1c(%r13)
je 0x535ed
movq 0x240(%r15), %rax
cmpq %rax, 0x238(%r15)
jne 0x535c5
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x240(%r15)
movq 0x248(%r15), %rdi
shlq $0x3, %rsi
movq %r8, 0x28(%rsp)
movq %r9, 0x18(%rsp)
callq 0x2370
movq 0x18(%rsp), %r9
movq 0x28(%rsp), %r8
leaq 0x23719a(%rip), %rdi    # 0x28a758
movq %rax, 0x248(%r15)
incq %r8
movq (%r15), %rax
movl 0x8(%rax,%r14), %eax
movq 0x248(%r15), %rcx
movq 0x238(%r15), %rdx
leaq 0x1(%rdx), %rsi
movq %rsi, 0x238(%r15)
movq %rax, (%rcx,%rdx,8)
incq %r12
addq $0x18, %r14
cmpq %r12, %rbx
jne 0x534c9
jmp 0x53602
xorl %r8d, %r8d
movq 0x238(%r15), %rax
movq %rax, %rcx
subq %r8, %rcx
movq %rcx, 0x238(%r15)
movq %r8, %xmm0
punpckldq 0x1d1c1d(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x1d1c25(%rip), %xmm0   # 0x225250
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
movq 0x10(%rsp), %rdx
movq (%rdx), %rdx
movq (%r15), %rsi
movl 0x4(%rsi,%r9,8), %esi
imulq $0xa8, %rsi, %rsi
movl 0x28(%rdx,%rsi), %edx
xorps %xmm2, %xmm2
cvtsi2sd %rdx, %xmm2
addsd %xmm0, %xmm1
mulsd 0x38(%rsp), %xmm2
ucomisd %xmm2, %xmm1
jae 0x5366e
movq %rbx, %r12
jmp 0x53762
movq 0x248(%r15), %rdx
leaq (%rdx,%rcx,8), %rdi
leaq (%rdx,%rax,8), %rsi
movq %r8, %r14
callq 0x45d47
testq %r14, %r14
je 0x53749
movq %r14, %r10
movq 0x238(%r15), %rax
shlq $0x3, %rax
addq 0x248(%r15), %rax
movq %r14, %rdx
negq %rdx
movl $0x1, %esi
xorl %ecx, %ecx
cmpq $0x1, %rsi
je 0x536ca
movq -0x10(%rax,%rsi,8), %rdi
leaq (%rdi,%rdi,4), %r13
shlq $0x3, %r13
addq 0x218(%r15), %r13
cmpq %r10, %rsi
jae 0x536e2
movq (%rax,%rsi,8), %rdi
leaq (%rdi,%rdi,4), %rbp
shlq $0x3, %rbp
addq 0x218(%r15), %rbp
testq %r13, %r13
je 0x53705
movl 0x20(%r13), %edi
addl $0x20, %edi
movq 0x218(%r15), %r8
movq -0x8(%rax,%rsi,8), %r9
leaq (%r9,%r9,4), %r9
cmpl 0x20(%r8,%r9,8), %edi
ja 0x53733
testq %rbp, %rbp
je 0x53727
movq 0x218(%r15), %rdi
movq -0x8(%rax,%rsi,8), %r8
leaq (%r8,%r8,4), %r8
movl 0x20(%rdi,%r8,8), %edi
addl $0x20, %edi
cmpl 0x20(%rbp), %edi
ja 0x53733
movq -0x8(%rax,%rsi,8), %rdi
movq %rdi, (%rax,%rcx,8)
incq %rcx
leaq (%rdx,%rsi), %rdi
incq %rdi
incq %rsi
cmpq $0x1, %rdi
jne 0x536b0
jmp 0x5374b
xorl %ecx, %ecx
cmpq $0x2, %rcx
jl 0x53758
addq %rcx, 0x238(%r15)
movq %rbx, %r12
leaq 0x236ff6(%rip), %rdi    # 0x28a758
leaq 0x1(%rbx), %rcx
movl 0x10(%r15), %eax
cmpq %rax, %rbx
movq %rcx, %rbx
jb 0x5346d
movq 0x238(%r15), %rax
testq %rax, %rax
je 0x53792
movq 0x248(%r15), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x45d47
movl 0x10(%r15), %eax
testq %rax, %rax
je 0x538df
movl $0x1, %ecx
xorl %edx, %edx
cmpq %rax, %rcx
je 0x537c9
movq (%r15), %rsi
leaq (%rcx,%rcx,2), %rdi
movl 0x4(%rsi,%rdi,8), %edi
leaq (%rdx,%rdx,2), %r8
cmpl 0x4(%rsi,%r8,8), %edi
jne 0x537c9
movq %rdx, %r9
jmp 0x538c2
movq (%r15), %r11
leaq (%rdx,%rdx,2), %rbx
movl 0x4(%r11,%rbx,8), %esi
movq 0x10(%rsp), %rdi
movq (%rdi), %rdi
imulq $0xa8, %rsi, %r8
leaq (%rdi,%r8), %rsi
movzbl 0x2c(%rdi,%r8), %r10d
cmpl $0x2, %r10d
je 0x538aa
movq %rcx, %r9
cmpl $0x1, %r10d
jne 0x538c2
movq %rcx, %r9
cmpq %rcx, %rdx
jae 0x538c2
movq 0x218(%r15), %r9
leaq (%rdi,%r8), %r10
addq $0x2e, %r10
leaq (%r11,%rbx,8), %r11
addq $0xc, %r11
movl -0x4(%r11), %ebx
leaq (%rbx,%rbx,4), %rbx
cmpl $0x1, 0x1c(%r9,%rbx,8)
jne 0x5389c
leaq (%r9,%rbx,8), %r12
movl 0x8(%r12), %r14d
cmpq $0x2, %r14
jb 0x5389c
cmpl $0x2, 0xc(%r12)
jb 0x5389c
movb (%r11), %bl
cmpq $-0x1, 0x8(%rsp)
je 0x53893
movl 0x4(%r12), %r12d
movq 0x8(%rsp), %r13
addq %r12, %r13
cmpq %r14, %r13
jb 0x53893
xorps %xmm0, %xmm0
cvtsi2sd %r14, %xmm0
movl %r12d, %r14d
xorps %xmm1, %xmm1
cvtsi2sd %r14, %xmm1
movapd %xmm0, %xmm2
mulsd 0x20(%rsp), %xmm2
addsd %xmm1, %xmm2
ucomisd %xmm0, %xmm2
jae 0x5389c
movb $0x1, (%r10)
testb $0x1, %bl
jne 0x538d7
incq %rdx
addq $0x18, %r11
cmpq %rdx, %rcx
jne 0x53828
jmp 0x538bf
movb $0x1, 0x2e(%rsi)
movl 0x8(%rsi), %edx
subl 0x4(%rsi), %edx
incl %edx
movq 0x10(%rsp), %rsi
subq %rdx, 0x18(%rsi)
movq %rcx, %r9
leaq 0x1(%rcx), %rsi
movq %r9, %rdx
cmpq %rax, %rcx
movq %rsi, %rcx
jne 0x537a6
jmp 0x538df
movb $0x2, 0x2c(%rdi,%r8)
jmp 0x538ae
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1d2303(%rip), %rdi    # 0x225bf8
leaq 0x1d1d47(%rip), %rsi    # 0x225643
leaq 0x1d2335(%rip), %rcx    # 0x225c38
movl $0x229f, %edx           # imm = 0x229F
callq 0x2110
leaq 0x1d2226(%rip), %rdi    # 0x225b3a
leaq 0x1d1d28(%rip), %rsi    # 0x225643
leaq 0x1d2316(%rip), %rcx    # 0x225c38
movl $0x231d, %edx           # imm = 0x231D
callq 0x2110
leaq 0x1d238c(%rip), %rdi    # 0x225cbf
leaq 0x1d1d09(%rip), %rsi    # 0x225643
leaq 0x1d22f7(%rip), %rcx    # 0x225c38
movl $0x22a2, %edx           # imm = 0x22A2
callq 0x2110
leaq 0x1d23a9(%rip), %rdi    # 0x225cfb
leaq 0x1d1cea(%rip), %rsi    # 0x225643
leaq 0x1d22d8(%rip), %rcx    # 0x225c38
movl $0x22c0, %edx           # imm = 0x22C0
callq 0x2110
leaq 0x1d23a7(%rip), %rdi    # 0x225d18
leaq 0x1d1ccb(%rip), %rsi    # 0x225643
leaq 0x1d22b9(%rip), %rcx    # 0x225c38
movl $0x2320, %edx           # imm = 0x2320
callq 0x2110