// File: /workspace/github_cpp_makefile_star2_O3/medvedevgroup[P]varmatch/src/wholegenome.cpp
// Function: WholeGenome::ReadWholeGenomeVariant(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, bool)
int WholeGenome::ReadWholeGenomeVariant(string filename, bool flag){
    int total_num = 0;
    int long_num = 0;
    double QUAL_LOWER_BOUND = 0.1;

	ifstream vcf_file;
	vcf_file.open(filename.c_str());
	if (!vcf_file.good()) {
		cout << "[VarMatch] Error: can not open vcf file" << endl;
		return -1;
	}

    vector<float> quality_list;

	int genotype_index = -1;
	char genotype_separator = '/';
	//int genome_sequence_length = genome_sequence.length();
	while (!vcf_file.eof()) { // alternative way is vcf_file != NULL
		string line;
		getline(vcf_file, line, '\n');
		// check ineligible lines
		//dout << line << endl;
		if ((int)line.length() <= 1) continue;
		//if (line.find_first_not_of(' ') == std::string::npos) continue;

		if (line[0] == '#') {
			continue;
		}
		auto columns = split(line, '\t');
		if (columns.size() < 10) {
			if(match_mode_indicator != 1){
                cout << "[VarMatch] Warning: not enough information in VCF file for genotype matching." << endl;
                cout << "[VarMatch] \tAutomatically turn off genotype matching module " << filename << endl;
                match_mode_indicator = 1;
                //continue;
            }
            if(columns.size() < 6){
                cout << "[VarMatch] Warning: not enough information in VCF file for variant matching." << endl;
                cout << "[VarMatch] skip current variant: " << line << endl;
                continue;
            }
		}
		string chr_name = columns[0];
		auto pos = atoi(columns[1].c_str()) - 1; // 0-based coordinate

		auto ref = columns[3];
		auto alt_line = columns[4];
		//double quality = stod(columns[5]);

		double quality = 1.0;
		if(columns[5] != ".") quality = stod(columns[5]);

        if(flag){
            quality_list.push_back(quality);
        }

		ToUpper(ref);
		ToUpper(alt_line);

		bool is_heterozygous_variant = false;
		bool is_multi_alternatives = false;
        bool is_zero_one_var = false;
        
        vector<string> genotype_columns;
		
        if (match_mode_indicator != 1) { // match mode indicator is -1 or 0
			if (genotype_index < 0) {
                // change genotype index
                auto formats = split(columns[8], ':');
                for (int i = 0; i < formats.size(); i++) {
                    if (formats[i] == "GT") {
                        genotype_index = i;
                        break;
                    }
                }
                // if GT not found
                if(genotype_index < 0){
                    if(match_mode_indicator != 1 && match_mode_indicator != 1){
                        cout << "[VarMatch] Warning: VCF entry does not contain genotype information." << endl;
                        cout << "[VarMatch] \tAutomatically turn off genotype matching mode. " << endl;
                        match_mode_indicator = 1;
                    }
                }
			}

            
            if(match_mode_indicator != 1){

    			auto additionals = split(columns[9], ':');
                genotype_columns = split(additionals[genotype_index], genotype_separator);

                if(genotype_columns.size() != 2){
                    
                    if(genotype_separator == '/'){
                        genotype_separator = '|';
                    }else{
                        genotype_separator = '/';
                    }
                    genotype_columns = split(additionals[genotype_index], genotype_separator);
                }

    			// normalize format of genotype: sorted, separated by |
    			if (genotype_columns.size() != 2) {
    				cout << "[VarMatch] Warning: Unrecognized Genotype: " << additionals[genotype_index] << endl;
                    cout << "[VarMatch] \tAutomatically turn off genotype matching mode." << endl;
                    match_mode_indicator = 1;
    			}
    			else {
    				if (genotype_columns[0] != genotype_columns[1]) {
    					is_heterozygous_variant = true;
    				}
                    if (genotype_columns[1] == "0" && genotype_columns[0] == "0") {
                        //cout << "Skip Variants when both genotype is refernce allele: " << line << endl;   
                        continue;
                    }
                    if(genotype_columns[0] == "0" || genotype_columns[1] == "0"){
                        is_zero_one_var = true;
                    }
    			}
            }
		}

		vector<string> alt_list;
		if (alt_line.find(",") != std::string::npos) {
			alt_list = split(alt_line, ',');
			is_multi_alternatives = true;
		}
		else {
			alt_list.push_back(alt_line);
		}

        if(alt_list.size() > 2){
            if(match_mode_indicator != 1){
            vector<string> temp_alt_list = alt_list;
            alt_list.clear();
            for(int i = 0; i < 2; i++){
                int alt_indicator = stoi(genotype_columns[i]);
                if(alt_indicator == 0) continue;
                alt_list.push_back(temp_alt_list[alt_indicator-1]);
            }
            }else{
                vector<string> temp_alt_list = alt_list;
                alt_list.clear();
                alt_list.push_back(temp_alt_list[0]);
                alt_list.push_back(temp_alt_list[1]);
            }
        }

        int snp_ins = max(0, (int)alt_list[0].length() - (int)ref.length());
        int snp_del = max(0, (int)ref.length() - (int)alt_list[0].length());
        if(is_multi_alternatives){
            snp_ins = max(snp_ins, (int)alt_list[1].length() - (int)ref.length());
            snp_del = max(snp_del, (int)ref.length() - (int)alt_list[1].length());
        }

        if(snp_ins > VAR_LEN || snp_del > VAR_LEN){
            //dout << "[VarMatch] skip large INDEL with length > " << VAR_LEN << "| "<< line <<endl;
            long_num ++;
            continue;
        }

		DiploidVariant dv(pos, ref, alt_list, is_heterozygous_variant, is_multi_alternatives, snp_del, snp_ins, flag, quality, is_zero_one_var);
		//if (normalization) {
			//NormalizeDiploidVariant(dv);
		//}
        if(chrid_by_chrname.find(chr_name) != chrid_by_chrname.end()){
            int chr_id = chrid_by_chrname[chr_name];
            if(flag == false){
                ref_variant_by_chrid[chr_id]->push_back(dv);
                //baseline_variant_strings.push_back(line);
            }else{
                que_variant_by_chrid[chr_id]->push_back(dv);
                query_variant_strings.push_back(line);
            }
        }else{
            //cout << "[VarMatch] skip current variant as no corresponding reference genome sequence found." << endl;
            cout << "[VarMatch] Warning: variant skipped, no reference genome sequence found for chromosome name: " << chr_name << "." << endl;
            continue;
            int chr_id = chrname_dict[chr_name];
            if(flag == false){
                ref_variant_by_chrid[chr_id]->push_back(dv);
                //baseline_variant_strings.push_back(line);
            }else{
                que_variant_by_chrid[chr_id]->push_back(dv);
                query_variant_strings.push_back(line);
            }
        }

        total_num++;
	}
	vcf_file.close();

    if(flag){
        sort(quality_list.begin(), quality_list.end());
        auto qual_lower_it = lower_bound(quality_list.begin(), quality_list.end(), QUAL_LOWER_BOUND);
        int qual_lower_index = qual_lower_it - quality_list.begin();
        int rest_size = quality_list.size() - qual_lower_index;

        vector<float> temp_percentage_list;
        temp_percentage_list.push_back(0.0);
        threshold_list.push_back(0.0);
        
        for(int i = 1; i < per_list.size(); i++){
            int additional_index = (int)(rest_size * per_list[i]);
            int real_index = qual_lower_index + additional_index;
            if(real_index >= quality_list.size()) real_index = quality_list.size() - 1;
            double quality = quality_list[real_index];
            threshold_list.push_back(quality);

            auto quality_lowit = lower_bound(quality_list.begin(), quality_list.end(), quality);
            int quality_low_index = quality_lowit - quality_list.begin();
            // following program will retain variants >= quality threshold
            
            int quality_size = quality_low_index + 1; // counting number, +/- 1 does not matter
            if(quality_size > quality_list.size()) quality_size = quality_list.size();
            double percentage = (double)quality_size/ quality_list.size();
            temp_percentage_list.push_back(percentage);
        }
        threshold_num = threshold_list.size();
        // revice percentage
        per_list = temp_percentage_list;
    }
    //cout << flag << "," << total_num << "," << long_num << endl;
	return total_num;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3f8, %rsp            # imm = 0x3F8
movl %edx, 0x8(%rsp)
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x1f0(%rsp), %r14
movq %r14, %rdi
callq 0x5390
movq (%r15), %rsi
movq %r14, %rdi
movl $0x8, %edx
callq 0x5210
movq 0x1f0(%rsp), %rax
movq -0x18(%rax), %rcx
cmpl $0x0, 0x210(%rsp,%rcx)
je 0x12669
movq 0x2a9b0(%rip), %rbx     # 0x3cfd0
leaq 0x1ed5d(%rip), %rsi     # 0x31384
movl $0x27, %edx
movq %rbx, %rdi
callq 0x53a0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x5310
movsbl %al, %esi
movq 0x2a97e(%rip), %rdi     # 0x3cfd0
callq 0x5040
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq %rax, %rdi
callq 0x5240
jmp 0x1381a
movq %r15, 0x1d0(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0xa0(%rsp)
movq $0x0, 0xb0(%rsp)
movq -0x18(%rax), %rax
movl $0x0, 0x3c(%rsp)
testb $0x2, 0x210(%rsp,%rax)
jne 0x134e6
leaq 0x50(%rsp), %r12
leaq 0x88(%rbx), %rax
movq %rax, 0x198(%rsp)
leaq 0x90(%rbx), %rax
movq %rax, 0x1c8(%rsp)
leaq 0x68(%rbx), %rax
movq %rax, 0x1c0(%rsp)
movb $0x2f, %al
movl %eax, 0x34(%rsp)
movl $0xffffffff, 0x38(%rsp) # imm = 0xFFFFFFFF
leaq 0x1f0(%rsp), %r15
leaq 0x40(%rsp), %rbp
movl $0x0, 0x3c(%rsp)
movq %r12, 0x40(%rsp)
movq $0x0, 0x48(%rsp)
movb $0x0, 0x50(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
movl $0xa, %edx
callq 0x5600
cmpl $0x2, 0x48(%rsp)
movq 0x40(%rsp), %rdi
jl 0x134a1
cmpb $0x23, (%rdi)
je 0x134a1
leaq 0xb8(%rsp), %rdi
movq %rbp, %rsi
movl $0x9, %edx
callq 0x2f36e
movq 0xb8(%rsp), %rax
movq 0xc0(%rsp), %rcx
subq %rax, %rcx
cmpq $0x13f, %rcx            # imm = 0x13F
ja 0x128ea
cmpl $0x1, 0x1e4(%rbx)
je 0x12836
movl $0x4d, %edx
movq 0x2a852(%rip), %rdi     # 0x3cfd0
leaq 0x1ec27(%rip), %rsi     # 0x313ac
callq 0x53a0
movq 0x2a83f(%rip), %rcx     # 0x3cfd0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x5310
movsbl %al, %esi
movq 0x2a821(%rip), %rdi     # 0x3cfd0
callq 0x5040
movq %rax, %rdi
callq 0x5240
movl $0x3c, %edx
movq 0x2a808(%rip), %rdi     # 0x3cfd0
leaq 0x1ec2b(%rip), %rsi     # 0x313fa
callq 0x53a0
movq 0x1d0(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq 0x2a7e6(%rip), %rdi     # 0x3cfd0
callq 0x53a0
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x5310
movsbl %al, %esi
movq %r14, %rdi
callq 0x5040
movq %rax, %rdi
callq 0x5240
movl $0x1, 0x1e4(%rbx)
movq 0xb8(%rsp), %rax
movq 0xc0(%rsp), %rcx
subq %rax, %rcx
cmpq $0xbf, %rcx
ja 0x128ea
movl $0x4c, %edx
movq 0x2a781(%rip), %rdi     # 0x3cfd0
leaq 0x1ebe1(%rip), %rsi     # 0x31437
callq 0x53a0
movq 0x2a76e(%rip), %rcx     # 0x3cfd0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x5310
movsbl %al, %esi
movq 0x2a750(%rip), %rdi     # 0x3cfd0
callq 0x5040
movq %rax, %rdi
callq 0x5240
movl $0x21, %edx
movq 0x2a737(%rip), %rdi     # 0x3cfd0
leaq 0x1ebe4(%rip), %rsi     # 0x31484
callq 0x53a0
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
movq 0x2a71a(%rip), %rdi     # 0x3cfd0
callq 0x53a0
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x5310
movsbl %al, %esi
movq %r14, %rdi
callq 0x5040
movq %rax, %rdi
callq 0x5240
jmp 0x1348f
leaq 0x168(%rsp), %rcx
movq %rcx, 0x158(%rsp)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x158(%rsp), %rdi
callq 0x1123c
movq 0xb8(%rsp), %r14
movq 0x20(%r14), %rdi
callq 0x54c0
movl %eax, %r12d
leaq 0x188(%rsp), %rax
movq %rax, 0x178(%rsp)
movq 0x60(%r14), %rsi
movq 0x68(%r14), %rdx
addq %rsi, %rdx
leaq 0x178(%rsp), %rdi
callq 0x1123c
movq 0xb8(%rsp), %rax
leaq 0x140(%rsp), %rcx
movq %rcx, 0x130(%rsp)
movq 0x80(%rax), %rsi
movq 0x88(%rax), %rdx
addq %rsi, %rdx
leaq 0x130(%rsp), %rdi
callq 0x1123c
movq 0xb8(%rsp), %rdi
movl $0xa0, %eax
addq %rax, %rdi
leaq 0x1f0ec(%rip), %rsi     # 0x31a86
callq 0x50e0
movsd 0x1e909(%rip), %xmm0    # 0x312b0
movsd %xmm0, 0x128(%rsp)
testl %eax, %eax
je 0x12a19
movq 0xb8(%rsp), %rax
movq 0xa0(%rax), %rbp
callq 0x5060
movq %rax, %r15
movl (%rax), %r14d
movl $0x0, (%rax)
movq %rbp, %rdi
leaq 0xd0(%rsp), %rsi
callq 0x5290
movsd %xmm0, 0x128(%rsp)
cmpq %rbp, 0xd0(%rsp)
je 0x13853
movl (%r15), %eax
testl %eax, %eax
je 0x12a11
cmpl $0x22, %eax
leaq 0x40(%rsp), %rbp
jne 0x12a19
jmp 0x1385f
movl %r14d, (%r15)
leaq 0x40(%rsp), %rbp
cmpb $0x0, 0x8(%rsp)
je 0x12a6e
xorps %xmm0, %xmm0
cvtsd2ss 0x128(%rsp), %xmm0
movss %xmm0, 0xd0(%rsp)
movq 0xa8(%rsp), %rsi
cmpq 0xb0(%rsp), %rsi
je 0x12a59
movss %xmm0, (%rsi)
addq $0x4, %rsi
movq %rsi, 0xa8(%rsp)
jmp 0x12a6e
leaq 0xa0(%rsp), %rdi
leaq 0xd0(%rsp), %rdx
callq 0x2b46c
movl %r12d, 0x154(%rsp)
movq 0x180(%rsp), %r14
testq %r14, %r14
je 0x12aa4
movq 0x178(%rsp), %r15
xorl %r12d, %r12d
movsbl (%r15,%r12), %edi
callq 0x5610
movb %al, (%r15,%r12)
incq %r12
cmpq %r12, %r14
jne 0x12a8e
movq 0x138(%rsp), %r14
testq %r14, %r14
je 0x12ad2
movq 0x130(%rsp), %r15
xorl %r12d, %r12d
movsbl (%r15,%r12), %edi
callq 0x5610
movb %al, (%r15,%r12)
incq %r12
cmpq %r12, %r14
jne 0x12abc
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movq $0x0, 0x70(%rsp)
cmpl $0x1, 0x1e4(%rbx)
jne 0x12b13
movl $0x0, 0xc(%rsp)
movl $0x0, 0x4(%rsp)
leaq 0x50(%rsp), %r12
leaq 0x130(%rsp), %r14
leaq 0x10(%rsp), %rbp
jmp 0x12f44
cmpl $0x0, 0x38(%rsp)
js 0x12b2c
leaq 0x50(%rsp), %r12
leaq 0x80(%rsp), %r15
jmp 0x12c7b
movq 0xb8(%rsp), %rsi
movl $0x100, %eax            # imm = 0x100
addq %rax, %rsi
leaq 0xd0(%rsp), %rdi
movl $0x3a, %edx
leaq 0x50(%rsp), %r12
callq 0x2f36e
movq 0xd0(%rsp), %rdi
cmpq %rdi, 0xd8(%rsp)
je 0x12ba5
xorl %r15d, %r15d
xorl %r14d, %r14d
addq %r15, %rdi
leaq 0x1e931(%rip), %rsi     # 0x314a6
callq 0x50e0
testl %eax, %eax
je 0x12c43
incq %r14
movq 0xd0(%rsp), %rdi
movq 0xd8(%rsp), %rax
subq %rdi, %rax
sarq $0x5, %rax
addq $0x20, %r15
cmpq %r14, %rax
ja 0x12b6b
cmpl $0x1, 0x1e4(%rbx)
je 0x12c48
movl $0x44, %edx
movq 0x2a412(%rip), %r14     # 0x3cfd0
movq %r14, %rdi
leaq 0x1e8e1(%rip), %rsi     # 0x314a9
callq 0x53a0
leaq 0x80(%rsp), %r15
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x5310
movsbl %al, %esi
movq %r14, %rdi
callq 0x5040
movq %rax, %rdi
callq 0x5240
movl $0x3b, %edx
movq %r14, %rdi
leaq 0x1e8e3(%rip), %rsi     # 0x314ee
callq 0x53a0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x5310
movsbl %al, %esi
movq %r14, %rdi
callq 0x5040
movq %rax, %rdi
callq 0x5240
movl $0x1, 0x1e4(%rbx)
jmp 0x12c50
movl %r14d, 0x38(%rsp)
leaq 0x80(%rsp), %r15
leaq 0xd0(%rsp), %rdi
callq 0xe71c
cmpl $0x1, 0x1e4(%rbx)
jne 0x12c7b
movl $0x0, 0xc(%rsp)
movl $0x0, 0x4(%rsp)
jmp 0x12b01
movq 0xb8(%rsp), %rsi
movl $0x120, %eax            # imm = 0x120
addq %rax, %rsi
leaq 0x10(%rsp), %rdi
movl $0x3a, %edx
callq 0x2f36e
movslq 0x38(%rsp), %r13
shlq $0x5, %r13
movq 0x10(%rsp), %rsi
addq %r13, %rsi
movsbl 0x34(%rsp), %edx
movq %r15, %rdi
callq 0x2f36e
movq 0x70(%rsp), %rax
movaps 0x80(%rsp), %xmm0
movq 0x90(%rsp), %rcx
movq %rcx, 0x70(%rsp)
movaps 0x60(%rsp), %xmm1
movaps %xmm0, 0x60(%rsp)
movaps %xmm1, 0xd0(%rsp)
movq %rax, 0xe0(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
movq $0x0, 0x90(%rsp)
leaq 0xd0(%rsp), %rdi
callq 0xe71c
movq %r15, %rdi
callq 0xe71c
movq %r15, %r14
movq 0x60(%rsp), %r15
movq 0x68(%rsp), %rax
movq %rax, %rcx
subq %r15, %rcx
cmpq $0x40, %rcx
je 0x12dc5
cmpb $0x2f, 0x34(%rsp)
movl $0x2f, %edx
movl $0x7c, %eax
cmovel %eax, %edx
movq 0x10(%rsp), %rsi
addq %r13, %rsi
movq %r14, %rdi
movl %edx, 0x34(%rsp)
callq 0x2f36e
movq 0x70(%rsp), %rax
movaps 0x80(%rsp), %xmm0
movq 0x90(%rsp), %rcx
movq %rcx, 0x70(%rsp)
movaps 0x60(%rsp), %xmm1
movaps %xmm0, 0x60(%rsp)
movaps %xmm1, 0xd0(%rsp)
movq %rax, 0xe0(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
movq $0x0, 0x90(%rsp)
leaq 0xd0(%rsp), %rdi
callq 0xe71c
movq %r14, %rdi
callq 0xe71c
movq 0x60(%rsp), %r15
movq 0x68(%rsp), %rax
subq %r15, %rax
cmpq $0x40, %rax
jne 0x12e00
movq 0x8(%r15), %rdx
cmpq 0x28(%r15), %rdx
jne 0x12df5
testq %rdx, %rdx
je 0x12ec2
movq (%r15), %rdi
movq 0x20(%r15), %rsi
callq 0x5350
testl %eax, %eax
je 0x12ec2
movb $0x1, %al
movl %eax, 0x4(%rsp)
jmp 0x12eca
movl $0x2b, %edx
movq 0x2a1c4(%rip), %rdi     # 0x3cfd0
leaq 0x1e717(%rip), %rsi     # 0x3152a
callq 0x53a0
movq 0x10(%rsp), %rax
movq (%rax,%r13), %rsi
movq 0x8(%rax,%r13), %rdx
movq 0x2a1a3(%rip), %rdi     # 0x3cfd0
callq 0x53a0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x5310
movsbl %al, %esi
movq %r15, %rdi
callq 0x5040
movq %rax, %rdi
callq 0x5240
movl $0x3a, %edx
movq 0x2a168(%rip), %rdi     # 0x3cfd0
leaq 0x1e6e7(%rip), %rsi     # 0x31556
callq 0x53a0
movq 0x2a155(%rip), %rcx     # 0x3cfd0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x5310
movsbl %al, %esi
movq 0x2a137(%rip), %rdi     # 0x3cfd0
callq 0x5040
movq %rax, %rdi
callq 0x5240
movl $0x1, 0x1e4(%rbx)
movl $0x0, 0xc(%rsp)
movl $0x0, 0x4(%rsp)
jmp 0x12f2f
movl $0x0, 0x4(%rsp)
addq $0x20, %r15
movq %r15, %rdi
leaq 0x1e9a9(%rip), %r14     # 0x31881
movq %r14, %rsi
callq 0x50e0
testl %eax, %eax
jne 0x12ef9
movq 0x60(%rsp), %rdi
movq %r14, %rsi
callq 0x50e0
testl %eax, %eax
je 0x134cf
movq 0x60(%rsp), %rdi
movq %r14, %rsi
callq 0x50e0
testl %eax, %eax
je 0x12f29
movq 0x60(%rsp), %rdi
addq $0x20, %rdi
movq %r14, %rsi
callq 0x50e0
testl %eax, %eax
je 0x12f29
movl $0x0, 0xc(%rsp)
jmp 0x12f2f
movb $0x1, %al
movl %eax, 0xc(%rsp)
leaq 0x130(%rsp), %r14
leaq 0x10(%rsp), %rbp
movq %rbp, %rdi
callq 0xe71c
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movl $0x1, %ecx
movq %r14, %rdi
leaq 0x1e62d(%rip), %rsi     # 0x31591
xorl %edx, %edx
callq 0x55b0
movq %rax, %r15
cmpq $-0x1, %rax
je 0x12fee
leaq 0x80(%rsp), %r13
movq %r13, %rdi
movq %r14, %rsi
movl $0x2c, %edx
callq 0x2f36e
movq 0x20(%rsp), %rax
movaps 0x80(%rsp), %xmm0
movq 0x90(%rsp), %rcx
movq %rcx, 0x20(%rsp)
movaps 0x10(%rsp), %xmm1
movaps %xmm0, 0x10(%rsp)
movaps %xmm1, 0xd0(%rsp)
movq %rax, 0xe0(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
movq $0x0, 0x90(%rsp)
leaq 0xd0(%rsp), %rdi
callq 0xe71c
movq %r13, %rdi
callq 0xe71c
jmp 0x12ff9
movq %rbp, %rdi
movq %r14, %rsi
callq 0x10c62
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
cmpq $0x41, %rax
jb 0x131a4
cmpl $0x1, 0x1e4(%rbx)
jne 0x1308c
leaq 0xd0(%rsp), %rdi
movq %rbp, %rsi
callq 0x28582
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %r12
cmpq %r14, %r12
je 0x13063
leaq 0x10(%r14), %r13
movq -0x10(%r13), %rdi
cmpq %rdi, %r13
je 0x1304e
movq (%r13), %rsi
incq %rsi
callq 0x5340
leaq 0x20(%r13), %rax
addq $0x10, %r13
cmpq %r12, %r13
movq %rax, %r13
jne 0x13039
movq %r14, 0x18(%rsp)
movq 0xd0(%rsp), %rsi
movq %rbp, %rdi
callq 0x10c62
movq 0xd0(%rsp), %rsi
addq $0x20, %rsi
movq %rbp, %rdi
callq 0x10c62
jmp 0x13192
leaq 0xd0(%rsp), %rdi
movq %rbp, %rsi
callq 0x28582
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %r12
cmpq %r14, %r12
je 0x130d9
leaq 0x10(%r14), %r13
movq -0x10(%r13), %rdi
cmpq %rdi, %r13
je 0x130c4
movq (%r13), %rsi
incq %rsi
callq 0x5340
leaq 0x20(%r13), %rax
addq $0x10, %r13
cmpq %r12, %r13
movq %rax, %r13
jne 0x130af
movq %r14, 0x18(%rsp)
movb $0x1, %r12b
callq 0x5060
movq %rax, %rbp
xorl %eax, %eax
movq 0x60(%rsp), %rcx
shlq $0x5, %rax
movq (%rcx,%rax), %r13
movl (%rbp), %r14d
movl $0x0, (%rbp)
movq %r13, %rdi
leaq 0x80(%rsp), %rsi
movl $0xa, %edx
callq 0x5430
cmpq %r13, 0x80(%rsp)
je 0x13847
leaq -0x80000000(%rax), %rcx
movabsq $-0x100000000, %rdx     # imm = 0xFFFFFFFF00000000
cmpq %rdx, %rcx
jb 0x1383b
movl (%rbp), %ecx
cmpl $0x22, %ecx
je 0x1383b
testl %ecx, %ecx
jne 0x1314f
movl %r14d, (%rbp)
testl %eax, %eax
je 0x1317d
shlq $0x20, %rax
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
addq %rcx, %rax
sarq $0x1b, %rax
addq 0xd0(%rsp), %rax
leaq 0x10(%rsp), %rdi
movq %rax, %rsi
callq 0x10c62
movl $0x1, %eax
testb $0x1, %r12b
movl $0x0, %r12d
jne 0x130e6
leaq 0xd0(%rsp), %rdi
callq 0xe71c
leaq 0x50(%rsp), %r12
movq 0x10(%rsp), %rax
movl 0x8(%rax), %ebp
movq 0x180(%rsp), %rdx
movl %edx, %r13d
subl %ebp, %r13d
subl %edx, %ebp
testl %ebp, %ebp
movl $0x0, %ecx
cmovlel %ecx, %ebp
testl %r13d, %r13d
cmovlel %ecx, %r13d
cmpq $-0x1, %r15
je 0x131ef
movl 0x28(%rax), %eax
movl %eax, %ecx
subl %edx, %ecx
cmpl %ecx, %ebp
cmovgl %ebp, %ecx
movl %edx, %esi
subl %eax, %esi
cmpl %esi, %r13d
cmovgl %r13d, %esi
movl %esi, %r13d
movl %ecx, %ebp
cmpl $0x64, %ebp
ja 0x13338
cmpl $0x64, %r13d
ja 0x13338
leaq 0x1b0(%rsp), %rax
movq %rax, 0x1a0(%rsp)
movq 0x178(%rsp), %rsi
addq %rsi, %rdx
leaq 0x1a0(%rsp), %rdi
callq 0x1123c
leaq 0x1d8(%rsp), %r14
movq %r14, %rdi
leaq 0x10(%rsp), %rsi
callq 0x28582
movl 0x154(%rsp), %esi
decl %esi
xorl %r9d, %r9d
cmpq $-0x1, %r15
setne %r9b
movzbl 0xc(%rsp), %eax
movzbl 0x8(%rsp), %r10d
movzbl 0x4(%rsp), %r8d
leaq 0xd0(%rsp), %rdi
leaq 0x1a0(%rsp), %rdx
movq %r14, %rcx
movsd 0x128(%rsp), %xmm0
pushq %rax
pushq %r10
pushq %rbp
pushq %r13
callq 0x2860c
addq $0x20, %rsp
movq %r14, %rdi
callq 0xe71c
movq 0x1a0(%rsp), %rdi
leaq 0x1b0(%rsp), %rax
cmpq %rax, %rdi
leaq 0x1f0(%rsp), %r15
leaq 0x40(%rsp), %rbp
je 0x132c9
movq 0x1b0(%rsp), %rsi
incq %rsi
callq 0x5340
movq 0x198(%rsp), %rdi
leaq 0x158(%rsp), %rsi
callq 0x2b218
cmpq 0x1c8(%rsp), %rax
je 0x1334a
movq 0x198(%rsp), %rdi
leaq 0x158(%rsp), %rsi
callq 0x283a0
movslq (%rax), %rax
cmpb $0x0, 0x8(%rsp)
je 0x133be
movq 0x150(%rbx), %rcx
movq (%rcx,%rax,8), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x286b0
movq 0x1c0(%rsp), %rdi
movq %rbp, %rsi
callq 0x10c62
jmp 0x133d6
leaq 0x1f0(%rsp), %r15
leaq 0x40(%rsp), %rbp
jmp 0x1340c
movl $0x5d, %edx
movq 0x29c7a(%rip), %rdi     # 0x3cfd0
leaq 0x1e236(%rip), %rsi     # 0x31593
callq 0x53a0
movq 0x158(%rsp), %rsi
movq 0x160(%rsp), %rdx
movq 0x29c57(%rip), %rdi     # 0x3cfd0
callq 0x53a0
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
leaq 0x1e6f6(%rip), %rsi     # 0x31a86
callq 0x53a0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x5310
movsbl %al, %esi
movq %r14, %rdi
callq 0x5040
movq %rax, %rdi
callq 0x5240
jmp 0x133da
movq 0x148(%rbx), %rcx
movq (%rcx,%rax,8), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x286b0
incl 0x3c(%rsp)
leaq 0xf8(%rsp), %rdi
callq 0xe71c
movq 0xd8(%rsp), %rdi
leaq 0xe8(%rsp), %rax
cmpq %rax, %rdi
je 0x1340c
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x5340
leaq 0x10(%rsp), %rdi
callq 0xe71c
leaq 0x60(%rsp), %rdi
callq 0xe71c
movq 0x130(%rsp), %rdi
leaq 0x140(%rsp), %rax
cmpq %rax, %rdi
je 0x13445
movq 0x140(%rsp), %rsi
incq %rsi
callq 0x5340
movq 0x178(%rsp), %rdi
leaq 0x188(%rsp), %rax
cmpq %rax, %rdi
je 0x1346a
movq 0x188(%rsp), %rsi
incq %rsi
callq 0x5340
movq 0x158(%rsp), %rdi
leaq 0x168(%rsp), %rax
cmpq %rax, %rdi
je 0x1348f
movq 0x168(%rsp), %rsi
incq %rsi
callq 0x5340
leaq 0xb8(%rsp), %rdi
callq 0xe71c
movq 0x40(%rsp), %rdi
cmpq %r12, %rdi
je 0x134b3
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x5340
movq 0x1f0(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x2, 0x210(%rsp,%rax)
je 0x126f4
jmp 0x134e6
leaq 0x10(%rsp), %rdi
callq 0xe71c
leaq 0x1f0(%rsp), %r15
jmp 0x13416
leaq 0x1f0(%rsp), %rdi
callq 0x50a0
cmpb $0x0, 0x8(%rsp)
je 0x137f9
movq 0xa0(%rsp), %r15
movq 0xa8(%rsp), %r14
cmpq %r14, %r15
je 0x13552
movq %r14, %rax
subq %r15, %rax
sarq $0x2, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x2b884
movq %r15, %rdi
movq %r14, %rsi
callq 0x2b973
movq 0xa0(%rsp), %r12
movq 0xa8(%rsp), %r15
jmp 0x13555
movq %r15, %r12
subq %r12, %r15
sarq $0x2, %r15
testq %r15, %r15
jle 0x13599
movsd 0x1dd4f(%rip), %xmm0    # 0x312b8
movq %r15, %rcx
movq %r12, %rbp
movq %r15, %rax
shrq %rax
xorps %xmm1, %xmm1
cvtss2sd (%rbp,%rax,4), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x1358f
leaq 0x4(%rbp,%rax,4), %rbp
notq %rax
addq %rcx, %rax
movq %rax, %rcx
testq %rax, %rax
jg 0x13572
jmp 0x1359c
movq %r12, %rbp
xorps %xmm0, %xmm0
leaq 0xd0(%rsp), %rdi
movaps %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
leaq 0x40(%rsp), %rdx
movl $0x0, (%rdx)
xorl %esi, %esi
callq 0x2b46c
leaq 0x250(%rbx), %r14
movq $0x0, 0x40(%rsp)
movq 0x258(%rbx), %rsi
cmpq 0x260(%rbx), %rsi
je 0x135f8
movq $0x0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x258(%rbx)
jmp 0x13605
leaq 0x40(%rsp), %rdx
movq %r14, %rdi
callq 0x2bc22
movq 0x270(%rbx), %rax
movq 0x278(%rbx), %rcx
subq %rax, %rcx
cmpq $0x5, %rcx
jb 0x137b0
subq %r12, %rbp
shrq $0x2, %rbp
subl %ebp, %r15d
xorps %xmm0, %xmm0
cvtsi2ss %r15d, %xmm0
movss %xmm0, 0x8(%rsp)
movl $0x1, %r15d
leaq 0xd0(%rsp), %r12
leaq 0x158(%rsp), %r13
movss (%rax,%r15,4), %xmm0
mulss 0x8(%rsp), %xmm0
cvttss2si %xmm0, %eax
addl %ebp, %eax
cltq
movq 0xa0(%rsp), %rcx
movq 0xa8(%rsp), %rdx
subq %rcx, %rdx
sarq $0x2, %rdx
leal -0x1(%rdx), %esi
cmpq %rax, %rdx
cmovbel %esi, %eax
cltq
xorps %xmm0, %xmm0
cvtss2sd (%rcx,%rax,4), %xmm0
movsd %xmm0, 0x40(%rsp)
movq 0x258(%rbx), %rsi
cmpq 0x260(%rbx), %rsi
je 0x136b3
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x258(%rbx)
jmp 0x136c0
movq %r14, %rdi
leaq 0x40(%rsp), %rdx
callq 0x2bd62
movq 0xa0(%rsp), %rcx
movq 0xa8(%rsp), %rax
subq %rcx, %rax
sarq $0x2, %rax
movq %rcx, %rdx
testq %rax, %rax
jle 0x13715
movsd 0x40(%rsp), %xmm0
movq %rax, %rdi
movq %rcx, %rdx
movq %rax, %rsi
shrq %rsi
xorps %xmm1, %xmm1
cvtss2sd (%rdx,%rsi,4), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x1370d
leaq (%rdx,%rsi,4), %rdx
addq $0x4, %rdx
notq %rsi
addq %rdi, %rsi
movq %rsi, %rdi
testq %rsi, %rsi
jg 0x136ee
subq %rcx, %rdx
shrq $0x2, %rdx
incl %edx
movslq %edx, %rcx
cmpq %rcx, %rax
cmovbl %eax, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
movq %rax, %xmm1
punpckldq 0x1db25(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x1db2d(%rip), %xmm1    # 0x31270
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
divsd %xmm2, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x158(%rsp)
movq 0xd8(%rsp), %rsi
cmpq 0xe0(%rsp), %rsi
je 0x13784
movss %xmm0, (%rsi)
addq $0x4, %rsi
movq %rsi, 0xd8(%rsp)
jmp 0x1378f
movq %r12, %rdi
movq %r13, %rdx
callq 0x2b46c
incq %r15
movq 0x270(%rbx), %rax
movq 0x278(%rbx), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
cmpq %r15, %rcx
ja 0x1364e
leaq 0x270(%rbx), %rdi
movq 0x258(%rbx), %rax
subq 0x250(%rbx), %rax
shrq $0x3, %rax
movl %eax, 0x268(%rbx)
leaq 0xd0(%rsp), %rsi
callq 0x28702
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
je 0x137f9
movq 0xe0(%rsp), %rsi
subq %rdi, %rsi
callq 0x5340
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0x13816
movq 0xb0(%rsp), %rsi
subq %rdi, %rsi
callq 0x5340
movl 0x3c(%rsp), %ebx
leaq 0x1f0(%rsp), %rdi
callq 0x50c0
movl %ebx, %eax
addq $0x3f8, %rsp            # imm = 0x3F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1e27b(%rip), %rdi     # 0x31abd
callq 0x5460
leaq 0x1e26f(%rip), %rdi     # 0x31abd
callq 0x51f0
leaq 0x1e25e(%rip), %rdi     # 0x31ab8
callq 0x51f0
leaq 0x1e252(%rip), %rdi     # 0x31ab8
callq 0x5460
jmp 0x13939
jmp 0x13939
jmp 0x13939
jmp 0x13894
movq %rax, %rbx
jmp 0x139c1
jmp 0x13971
jmp 0x13906
jmp 0x13906
jmp 0x13906
jmp 0x13971
movq %rax, %rbx
jmp 0x139b7
jmp 0x1395d
movq %rax, %rbx
leaq 0x1d8(%rsp), %rdi
callq 0xe71c
jmp 0x138b6
movq %rax, %rbx
movq 0x1a0(%rsp), %rdi
leaq 0x1b0(%rsp), %rax
cmpq %rax, %rdi
je 0x139ad
movq 0x1b0(%rsp), %rsi
incq %rsi
callq 0x5340
jmp 0x139ad
jmp 0x13906
movq %rax, %rbx
jmp 0x139a0
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
jmp 0x139b2
movq %rax, %rbx
jmp 0x13a59
movq %rax, %rbx
jmp 0x139ad
jmp 0x1395d
movq %rax, %rbx
cmpl $0x0, (%r15)
jne 0x139c1
movl %r14d, (%r15)
jmp 0x139c1
movq %rax, %rbx
jmp 0x139e6
movq %rax, %rbx
jmp 0x13a0b
jmp 0x1398b
jmp 0x13939
movq %rax, %rbx
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
je 0x13a59
movq 0xe0(%rsp), %rsi
subq %rdi, %rsi
jmp 0x13a54
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
callq 0x286da
jmp 0x139ad
jmp 0x13983
movq %rax, %rbx
jmp 0x139ad
movq %rax, %rbx
jmp 0x13a76
movq %rax, %rbx
jmp 0x139a0
movq %rax, %rbx
jmp 0x13a3d
movq %rax, %rbx
jmp 0x13a30
movq %rax, %rbx
cmpl $0x0, (%rbp)
jne 0x139a0
movl %r14d, (%rbp)
leaq 0xd0(%rsp), %rdi
callq 0xe71c
leaq 0x10(%rsp), %rdi
callq 0xe71c
leaq 0x60(%rsp), %rdi
callq 0xe71c
movq 0x130(%rsp), %rdi
leaq 0x140(%rsp), %rax
cmpq %rax, %rdi
je 0x139e6
movq 0x140(%rsp), %rsi
incq %rsi
callq 0x5340
movq 0x178(%rsp), %rdi
leaq 0x188(%rsp), %rax
cmpq %rax, %rdi
je 0x13a0b
movq 0x188(%rsp), %rsi
incq %rsi
callq 0x5340
movq 0x158(%rsp), %rdi
leaq 0x168(%rsp), %rax
cmpq %rax, %rdi
je 0x13a30
movq 0x168(%rsp), %rsi
incq %rsi
callq 0x5340
leaq 0xb8(%rsp), %rdi
callq 0xe71c
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x13a59
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x5340
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0x13a76
movq 0xb0(%rsp), %rsi
subq %rdi, %rsi
callq 0x5340
leaq 0x1f0(%rsp), %rdi
callq 0x50c0
movq %rbx, %rdi
callq 0x55c0
nop