// File: /workspace/github_cpp_makefile_star2_O2/tonymugen[P]sampleSNPs/varfiles.cpp
// Function: sampFiles::BedFileI::sampleLD(unsigned long const&)
void BedFileI::sampleLD(const uint64_t &n){
	if (n == 0) {
		cerr << "WARNING: zero SNPs requested. Nothing to be done." << endl;
		return;
	}

    if (_bimFile.is_open()) {
        _bimFile.close();
    }
    try {
        string bimName = _fileStub + ".bim";
        _bimFile.open(bimName.c_str(), ios::in);
		if (!_bimFile.is_open()) {
			cerr << "ERROR: failed to open .bim file " << bimName << " for input" << endl;
			exit(2);
		}

    } catch (system_error &error) {
        cerr << "ERROR: cannot open BIM file for input: " << error.code().message() << flush;
        perror(" ");
        exit(1);
    }
	uint64_t nSamp = 2UL * n; // n is the number of pairs
	uint64_t N           = _numLines(); // also sets _nCols correctly
    const uint16_t Npad  = _nCols % 4UL;
    const uint64_t Nbed  = (_nCols/4UL) + static_cast<uint64_t>(Npad > 0UL);
	const string outName = _fileStub + "_LD.tsv";
	if (nSamp > N) {
		cerr << "ERROR: requested a sample of " << nSamp << " SNPs that is greater than the number of SNPs (" << N << ") in the input file." << endl;
		return;
	} else if (nSamp == N) {
		cerr << "WARNING: the number of sampled SNPs the same as the total number of SNPs in the file. LD will be between neighboring loci." << endl;
		// use neighboring loci
		char *locus1  = new char[Nbed];
		char *locus2  = new char[Nbed];
		uint64_t cumS = 0; // cumulative position in the file
		double rSq;
		double Dprime;
		double cnt1;
		double cnt2;
		string chrID;   // chromosome ID
		uint64_t pos1;  // position of locus1
		uint64_t pos2;  // position of locus2
		string bimLine; // line in the .bim file
		string field;   // single field in the .bim file
		stringstream lineStream;

		ofstream outFile(outName);
		outFile << "ChrID\tpos1\tpos2\tDistance\trSq\tDprime\tn1\tn2"  << endl;

        try {
            _varFile.open(_fileName.c_str(), ios::in | ios::binary);
			if (!_varFile.is_open()) {
				cerr << "ERROR: failed to open .bed file " << _fileName << " for input" << endl;
				exit(2);
			}

            // check the magic bytes and SNP-major status
            char magic[3];
            _varFile.read(magic, 3);
            if ( (magic[0] != static_cast<char>(0x6C)) || (magic[1] != static_cast<char>(0x1B)) ) {
                cerr << "ERROR: binary file " << _fileName << " not recognized as BED" << endl;
                exit(2);
            } else if (magic[2] != static_cast<char>(0x01)){
                cerr << "ERROR: binary file " << _fileName << " not SNP-major. Run a newer version of plink to fix it." << endl;
                exit(3);
            }
        } catch (system_error &error) {
            cerr << "ERROR: cannot open .bed file " << _fileName << " for input: " << error.code().message() << flush;
            perror(" ");
            exit(1);
        }
		while (nSamp) {
			// first locus in the pair
			_varFile.seekg(cumS*Nbed + 3); // seekg() index is base-0
			_varFile.read(locus1, Nbed);
			getline(_bimFile, bimLine);
			cumS++;
			nSamp--;

			lineStream.str(bimLine); // replaces whatever was there from before
			lineStream >> field;
			chrID = field;
			lineStream >> field >> field >> field;
			pos1 = atoi(field.c_str());

			// second locus
			_varFile.seekg(cumS*Nbed + 3);
			_varFile.read(locus2, Nbed);
			getline(_bimFile, bimLine);
			cumS++;
			nSamp--;

			lineStream.str(bimLine);
			lineStream >> field;
			if (chrID != field) { // if the chromosome number changes, we discard this pair and continue
				continue;
			}
			lineStream >> field >> field >> field;
			pos2 = atoi(field.c_str());
			if (pos2 <= pos1) {
				cerr << "WARNING: second SNP position (" << pos2 << ") no larger than the first (" << pos1 << ") on chromosome " << chrID << ". Skipping this pair without re-trying." << endl;
				continue;
			}
			_ld(locus1, locus2, Nbed, Npad, rSq, Dprime, cnt1, cnt2);
			outFile << chrID << "\t" << pos1 << "\t" << pos2 << "\t" << pos2 - pos1 << "\t" << rSq << "\t" << Dprime << "\t" << cnt1 << "\t" << cnt2 << endl;
		}

		delete [] locus1;
		delete [] locus2;
		_varFile.close();
		_bimFile.close();
		outFile.close();

	}
	try {
		_varFile.open(_fileName.c_str(), ios::in | ios::binary);
		if (!_varFile.is_open()) {
			cerr << "ERROR: failed to open .bed file " << _fileName << " for input" << endl;
			exit(2);
		}

		// check the magic bytes and SNP-major status
		char magic[3];
		_varFile.read(magic, 3);
		if ( (magic[0] != static_cast<char>(0x6C)) || (magic[1] != static_cast<char>(0x1B)) ) {
			cerr << "ERROR: binary file " << _fileName << " not recognized as BED" << endl;
			exit(2);
		} else if (magic[2] != static_cast<char>(0x01)){
			cerr << "ERROR: binary file " << _fileName << " not SNP-major. Run a newer version of plink to fix it." << endl;
			exit(3);
		}
	} catch (system_error &error) {
		cerr << "ERROR: cannot open BED file " << _fileName << ": " << error.code().message() << flush;
		perror(" ");
		exit(1);
	}
	// sample loci and calculate LD
	char *locus1  = new char[Nbed];
	char *locus2  = new char[Nbed];
	uint64_t cumS = 0; // cumulative position in the file
	uint64_t S;
	double rSq;
	double Dprime;
	double cnt1;
	double cnt2;
	string chrID;   // chromosome ID
	uint64_t pos1;  // position of locus1
	uint64_t pos2;  // position of locus2
	string bimLine; // line in the .bim file
	string field;   // single field in the .bim file
	stringstream lineStream;

	ofstream outFile(outName);
	outFile << "ChrID\tpos1\tpos2\tDistance\trSq\tDprime\tn1\tn2"  << endl;

	RanDraw *snpSamp;  // so that I can catch RanDraw constructor exceptions
	try {
		snpSamp = new RanDraw();
	} catch (string error) {
		cerr << "ERROR: " << error << endl;
		exit(5);
	}

	while (nSamp) {
		// first locus in the pair
		try {
			S = snpSamp->vitter(nSamp, N); // sample the number of SNPs to skip; keep track of the running total
		} catch (string error) {
			cerr << "ERROR: " << error << endl;
			exit(5);
		}
		cumS += S;
		N    -= S + 1;
		_varFile.seekg(cumS*Nbed + 3); // seekg() index is base-0
		_varFile.read(locus1, Nbed);
		cumS++;                        // step up cumS because seekg() will not change it (unlike the getline() that automatially advances)
		while (S) { // skipping S lines in the bim file
			_bimFile.ignore(numeric_limits<streamsize>::max(), '\n');
			S--;
		}
		getline(_bimFile, bimLine);
		lineStream.str(bimLine); // replaces whatever was there from before
		lineStream >> field;
		chrID = field;
		lineStream >> field >> field >> field;
		pos1 = atoi(field.c_str());
		nSamp--;

		// second locus
		try {
			S = snpSamp->vitter(nSamp, N);
		} catch (string error) {
			cerr << "ERROR: " << error << endl;
			exit(5);
		}
		cumS += S;
		N    -= S + 1;
		_varFile.seekg(cumS*Nbed + 3);
		_varFile.read(locus2, Nbed);
		cumS++;
		while (S) {
			_bimFile.ignore(numeric_limits<streamsize>::max(), '\n');
			S--;
		}
		getline(_bimFile, bimLine);

		lineStream.str(bimLine);
		lineStream >> field;
		if (chrID != field) { // if the chromosome number changes, we discard this pair and start over
			nSamp++;          // back up so that we end up with the correct number of pairs regardless of how many chromosome boundaries we cross
			continue;
		}
		lineStream >> field >> field >> field;
		pos2 = atoi(field.c_str());
		if (pos2 <= pos1) {
			cerr << "WARNING: second SNP position (" << pos2 << ") no larger than the first (" << pos1 << ") on chromosome " << chrID << ". Skipping this pair without re-trying." << endl;
			nSamp--; // will not re-try in this case
			continue;
		}
		_ld(locus1, locus2, Nbed, Npad, rSq, Dprime, cnt1, cnt2);
		outFile << chrID << "\t" << pos1 << "\t" << pos2 << "\t" << pos2 - pos1 << "\t" << rSq << "\t" << Dprime << "\t" << cnt1 << "\t" << cnt2 << endl;
		nSamp--;
	}

	delete [] locus1;
	delete [] locus2;
	_varFile.close();
	_bimFile.close();
	outFile.close();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4c8, %rsp            # imm = 0x4C8
cmpq $0x0, (%rsi)
je 0xaeff
movq %rsi, %r15
leaq 0x458(%rdi), %rbx
movq %rdi, 0xc0(%rsp)
leaq 0x4d8(%rdi), %r12
movq %r12, %rdi
callq 0x40b0
testb %al, %al
je 0xadea
movq %rbx, %rdi
callq 0x4050
movq 0xc0(%rsp), %rax
leaq 0x668(%rax), %r14
leaq 0x9a34(%rip), %rdx      # 0x14834
leaq 0x140(%rsp), %rdi
movq %r14, %rsi
callq 0x5e08
movq 0x140(%rsp), %rsi
pushq $0x8
popq %rdx
movq %rbx, %rdi
callq 0x4280
movq %r12, %rdi
callq 0x40b0
testb %al, %al
je 0xba86
leaq 0x140(%rsp), %rdi
callq 0x4558
movq (%r15), %rax
movq %rax, 0x30(%rsp)
leaq (%rax,%rax), %r12
movq 0xc0(%rsp), %r13
movq %r13, %rdi
callq 0x75fe
movq %rax, %r15
movq 0x238(%r13), %rbp
movl %ebp, %eax
andl $0x3, %eax
movw %ax, 0x16(%rsp)
shrq $0x2, %rbp
cmpq $0x1, %rax
sbbq $-0x1, %rbp
movq %rbp, 0x110(%rsp)
leaq 0x9b7f(%rip), %rdx      # 0x14a0a
leaq 0x120(%rsp), %rdi
movq %r14, %rsi
callq 0x5e08
movq %r15, 0x38(%rsp)
cmpq %r15, %r12
jbe 0xaf2b
movq 0x11140(%rip), %rdi     # 0x1bff0
leaq 0x973f(%rip), %rsi      # 0x145f6
callq 0x42a0
movq %rax, %rdi
movq %r12, %rsi
callq 0x41b0
leaq 0x9a2a(%rip), %rsi      # 0x148f8
movq %rax, %rdi
callq 0x42a0
movq %rax, %rdi
movq 0x38(%rsp), %rsi
callq 0x41b0
leaq 0x975a(%rip), %rsi      # 0x14644
movq %rax, %rdi
callq 0x42a0
movq %rax, %rdi
callq 0x4140
jmp 0xba67
movq 0x110ea(%rip), %rdi     # 0x1bff0
leaq 0x96b7(%rip), %rsi      # 0x145c4
callq 0x42a0
movq %rax, %rdi
addq $0x4c8, %rsp            # imm = 0x4C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4140
movq %rbp, 0x18(%rsp)
jne 0xb3ff
movq 0x110b3(%rip), %rdi     # 0x1bff0
leaq 0x9ace(%rip), %rsi      # 0x14a12
callq 0x42a0
movq %rax, %rdi
callq 0x4140
movq %rbp, %rdi
callq 0x4030
movq %rax, 0x20(%rsp)
movq 0x30(%rsp), %r12
movq %rbp, %rdi
callq 0x4030
movq %rax, 0x28(%rsp)
leaq 0x100(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %ecx, %ecx
leaq 0xe0(%rsp), %rdx
movq %rdx, -0x10(%rdx)
andq $0x0, -0x8(%rdx)
movb %cl, (%rax)
movb %cl, (%rdx)
leaq 0x70(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb %cl, (%rax)
leaq 0x340(%rsp), %rdi
callq 0x41d0
leaq 0x140(%rsp), %rdi
leaq 0x120(%rsp), %rsi
pushq $0x10
popq %rdx
callq 0x4530
leaq 0x9ab9(%rip), %rsi      # 0x14a8d
leaq 0x140(%rsp), %rdi
callq 0x42a0
movq %rax, %rdi
callq 0x4140
movq 0xc0(%rsp), %r14
leaq 0x8(%r14), %rdi
leaq 0x218(%r14), %r15
movq 0x218(%r14), %rsi
pushq $0xc
popq %rdx
movq %rdi, 0x8(%rsp)
callq 0x4280
leaq 0x88(%r14), %rdi
callq 0x40b0
testb %al, %al
je 0xbb73
leaq 0x40(%rsp), %rsi
pushq $0x3
popq %r13
movq 0x8(%rsp), %rdi
movq %r13, %rdx
callq 0x4340
cmpb $0x6c, 0x40(%rsp)
jne 0xbbaa
cmpb $0x1b, 0x41(%rsp)
jne 0xbbaa
cmpb $0x1, 0x42(%rsp)
jne 0xbbe6
movq 0x18(%rsp), %rax
addq %rax, %rax
movq %rax, 0x80(%rsp)
addq %r12, %r12
negq %r12
movq %r12, %rax
leaq 0x60(%rsp), %r12
testq %rax, %rax
je 0xb386
movq %rax, %rbp
movq 0x8(%rsp), %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x4350
movq 0x8(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq 0x18(%rsp), %rdx
callq 0x4340
movq %rbx, %rdi
leaq 0xd0(%rsp), %rsi
callq 0x4060
leaq 0x358(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x4400
leaq 0x340(%rsp), %rdi
movq %r12, %rsi
callq 0x4360
leaq 0xf0(%rsp), %rdi
movq %r12, %rsi
callq 0x4170
leaq 0x340(%rsp), %rdi
movq %r12, %rsi
callq 0x4360
movq %rax, %rdi
movq %r12, %rsi
callq 0x4360
movq %rax, %rdi
movq %r12, %rsi
callq 0x4360
movq 0x60(%rsp), %rdi
callq 0x4420
movl %eax, %r14d
movq 0x18(%rsp), %rax
leaq (%rax,%r13), %rsi
movq 0x8(%rsp), %rdi
xorl %edx, %edx
callq 0x4350
movq 0x8(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq 0x18(%rsp), %rdx
callq 0x4340
movq %rbx, %rdi
leaq 0xd0(%rsp), %rsi
callq 0x4060
leaq 0x358(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x4400
leaq 0x340(%rsp), %rdi
movq %r12, %rsi
callq 0x4360
leaq 0xf0(%rsp), %rdi
movq %r12, %rsi
callq 0x12109
testb %al, %al
jne 0xb372
leaq 0x340(%rsp), %rdi
movq %r12, %rsi
callq 0x4360
movq %rax, %rdi
movq %r12, %rsi
callq 0x4360
movq %rax, %rdi
movq %r12, %rsi
callq 0x4360
movslq %r14d, %r14
movq 0x60(%rsp), %rdi
callq 0x4420
movslq %eax, %r15
cmpl %r14d, %r15d
jbe 0xb304
subq $0x8, %rsp
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
leaq 0x118(%rsp), %rcx
leaq 0x1e(%rsp), %r8
leaq 0xc0(%rsp), %r9
leaq 0xa8(%rsp), %rax
pushq %rax
leaq 0xb8(%rsp), %rax
pushq %rax
leaq 0xc8(%rsp), %rax
pushq %rax
callq 0x7d14
addq $0x20, %rsp
leaq 0x140(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
callq 0x4270
movq %rax, %rdi
leaq 0x98f1(%rip), %rsi      # 0x14b2c
callq 0x42a0
movq %rax, %rdi
movq %r14, %rsi
callq 0x41b0
movq %rax, %rdi
leaq 0x98d7(%rip), %rsi      # 0x14b2c
callq 0x42a0
movq %rax, %rdi
movq %r15, %rsi
callq 0x41b0
movq %rax, %rdi
leaq 0x98bd(%rip), %rsi      # 0x14b2c
callq 0x42a0
subq %r14, %r15
movq %rax, %rdi
movq %r15, %rsi
callq 0x41b0
movq %rax, %rdi
leaq 0x98a0(%rip), %rsi      # 0x14b2c
callq 0x42a0
movsd 0xb8(%rsp), %xmm0
movq %rax, %rdi
callq 0x4450
movq %rax, %rdi
leaq 0x9880(%rip), %rsi      # 0x14b2c
callq 0x42a0
movsd 0xb0(%rsp), %xmm0
movq %rax, %rdi
callq 0x4450
movq %rax, %rdi
leaq 0x9860(%rip), %rsi      # 0x14b2c
callq 0x42a0
movsd 0xa8(%rsp), %xmm0
movq %rax, %rdi
callq 0x4450
movq %rax, %rdi
leaq 0x9840(%rip), %rsi      # 0x14b2c
callq 0x42a0
movsd 0xa0(%rsp), %xmm0
movq %rax, %rdi
callq 0x4450
jmp 0xb36a
movq 0x10ce5(%rip), %rdi     # 0x1bff0
leaq 0x97a5(%rip), %rsi      # 0x14ab7
callq 0x42a0
movq %rax, %rdi
movq %r15, %rsi
callq 0x41b0
movq %rax, %rdi
leaq 0x97aa(%rip), %rsi      # 0x14ad6
callq 0x42a0
movq %rax, %rdi
movq %r14, %rsi
callq 0x41b0
movq %rax, %rdi
leaq 0x97ad(%rip), %rsi      # 0x14af3
callq 0x42a0
movq %rax, %rdi
leaq 0xf0(%rsp), %rsi
callq 0x4270
movq %rax, %rdi
leaq 0x979f(%rip), %rsi      # 0x14b04
callq 0x42a0
movq %rax, %rdi
callq 0x4140
addq 0x80(%rsp), %r13
movq %rbp, %rax
addq $0x2, %rax
jmp 0xb079
movq 0x20(%rsp), %rdi
callq 0x4370
movq 0x28(%rsp), %rdi
callq 0x4370
movq 0x8(%rsp), %rdi
callq 0x4050
movq %rbx, %rdi
callq 0x4050
leaq 0x140(%rsp), %rdi
callq 0x43c0
leaq 0x140(%rsp), %rdi
callq 0x43f0
leaq 0x340(%rsp), %rdi
callq 0x4210
leaq 0x60(%rsp), %rdi
callq 0x4558
leaq 0xd0(%rsp), %rdi
callq 0x4558
leaq 0xf0(%rsp), %rdi
callq 0x4558
xorl %r12d, %r12d
movq 0x18(%rsp), %rbp
movq 0xc0(%rsp), %r14
leaq 0x8(%r14), %rdi
leaq 0x218(%r14), %r15
movq 0x218(%r14), %rsi
pushq $0xc
popq %rdx
movq %rdi, 0x8(%rsp)
callq 0x4280
addq $0x88, %r14
movq %r14, %rdi
callq 0x40b0
testb %al, %al
je 0xbac5
leaq 0x140(%rsp), %rsi
pushq $0x3
popq %rdx
movq 0x8(%rsp), %rdi
callq 0x4340
cmpb $0x6c, 0x140(%rsp)
jne 0xbaff
cmpb $0x1b, 0x141(%rsp)
jne 0xbaff
cmpb $0x1, 0x142(%rsp)
jne 0xbb39
movq %rbp, %rdi
callq 0x4030
movq %rax, 0x30(%rsp)
movq %rbp, %rdi
callq 0x4030
movq %rax, 0x90(%rsp)
leaq 0x100(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %ecx, %ecx
leaq 0xe0(%rsp), %rdx
movq %rdx, -0x10(%rdx)
andq $0x0, -0x8(%rdx)
movb %cl, (%rax)
movb %cl, (%rdx)
leaq 0x70(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb %cl, (%rax)
leaq 0x340(%rsp), %rdi
callq 0x41d0
leaq 0x140(%rsp), %rdi
leaq 0x120(%rsp), %rsi
pushq $0x10
popq %rdx
callq 0x4530
leaq 0x9590(%rip), %rsi      # 0x14a8d
leaq 0x140(%rsp), %rdi
callq 0x42a0
movq %rax, %rdi
callq 0x4140
pushq $0x8
popq %rdi
callq 0x42c0
movq %rax, %r15
movq %r12, %r14
movq %rax, %rdi
callq 0x12bf2
movq $0x0, 0x20(%rsp)
movabsq $0x7fffffffffffffff, %r13 # imm = 0x7FFFFFFFFFFFFFFF
pushq $0xa
popq %r12
movq %r15, 0x28(%rsp)
movq %r14, %rax
subq $0x1, %rax
jb 0xb9f3
movq %r14, 0x118(%rsp)
movq %r14, %xmm0
movq 0x8f3a(%rip), %xmm1     # 0x144a0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x8f3e(%rip), %xmm2     # 0x144b0
subpd %xmm2, %xmm0
movapd %xmm0, %xmm3
unpckhpd %xmm0, %xmm3            # xmm3 = xmm3[1],xmm0[1]
addsd %xmm0, %xmm3
movapd %xmm3, 0x80(%rsp)
movq %rax, %xmm0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
subpd %xmm2, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, 0xc0(%rsp)
movaps 0x80(%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
movsd 0x38(%rsp), %xmm0
unpcklps 0x8ed8(%rip), %xmm0     # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x8ee0(%rip), %xmm0     # 0x144b0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd %xmm1, 0x98(%rsp)
movq %r15, %rdi
leaq 0x40(%rsp), %rsi
leaq 0x98(%rsp), %rdx
callq 0x12edc
movq %rax, %r14
movq 0x20(%rsp), %rax
addq %r14, %rax
movq %rax, %r15
imulq %rax, %rbp
addq $0x3, %rbp
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq 0x4350
movq 0x8(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq 0x18(%rsp), %rdx
callq 0x4340
movq %r14, %rax
notq %rax
addq %rax, 0x38(%rsp)
incq %r15
subq $0x1, %r14
jb 0xb657
movq %rbx, %rdi
movq %r13, %rsi
movl %r12d, %edx
callq 0x41a0
jmp 0xb641
movq %rbx, %rdi
leaq 0xd0(%rsp), %rsi
callq 0x4060
leaq 0x358(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x4400
leaq 0x340(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x4360
leaq 0xf0(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x4170
leaq 0x340(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x4360
movq %rax, %rdi
leaq 0x60(%rsp), %rsi
callq 0x4360
movq %rax, %rdi
leaq 0x60(%rsp), %rsi
callq 0x4360
movq 0x60(%rsp), %rdi
callq 0x4420
movl %eax, %r14d
movaps 0xc0(%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
movsd 0x38(%rsp), %xmm0
unpcklps 0x8dac(%rip), %xmm0     # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x8db4(%rip), %xmm0     # 0x144b0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd %xmm1, 0x98(%rsp)
movq 0x28(%rsp), %rdi
leaq 0x40(%rsp), %rsi
leaq 0x98(%rsp), %rdx
callq 0x12edc
movq %rax, %rbp
addq %rax, %r15
movq %r15, 0x20(%rsp)
imulq 0x18(%rsp), %r15
addq $0x3, %r15
movq 0x8(%rsp), %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x4350
movq 0x8(%rsp), %rdi
movq 0x90(%rsp), %rsi
movq 0x18(%rsp), %rdx
callq 0x4340
movq %rbp, %rax
notq %rax
addq %rax, 0x38(%rsp)
incq 0x20(%rsp)
subq $0x1, %rbp
jb 0xb789
movq %rbx, %rdi
movq %r13, %rsi
movl %r12d, %edx
callq 0x41a0
jmp 0xb773
movq %rbx, %rdi
leaq 0xd0(%rsp), %rsi
callq 0x4060
leaq 0x358(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x4400
leaq 0x340(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x4360
leaq 0xf0(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x12109
testb %al, %al
movq 0x18(%rsp), %rbp
movq 0x28(%rsp), %r15
jne 0xb5ad
leaq 0x340(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x4360
movq %rax, %rdi
leaq 0x60(%rsp), %rsi
callq 0x4360
movq %rax, %rdi
leaq 0x60(%rsp), %rsi
callq 0x4360
movslq %r14d, %r14
movq 0x60(%rsp), %rdi
callq 0x4420
cltq
movq %r14, 0x80(%rsp)
cmpl %r14d, %eax
movq %rax, %r14
jbe 0xb96f
subq $0x8, %rsp
movq 0x38(%rsp), %rsi
movq 0x98(%rsp), %rdx
leaq 0x118(%rsp), %rcx
leaq 0x1e(%rsp), %r8
leaq 0xc0(%rsp), %r9
leaq 0xa8(%rsp), %rax
pushq %rax
leaq 0xb8(%rsp), %rax
pushq %rax
leaq 0xc8(%rsp), %rax
pushq %rax
callq 0x7d14
addq $0x20, %rsp
leaq 0x140(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
callq 0x4270
movq %rax, %rdi
leaq 0x9290(%rip), %rsi      # 0x14b2c
callq 0x42a0
movq %rax, %rdi
movq 0x80(%rsp), %rsi
callq 0x41b0
movq %rax, %rdi
leaq 0x9271(%rip), %rsi      # 0x14b2c
callq 0x42a0
movq %rax, %rdi
movq %r14, %rsi
callq 0x41b0
movq %rax, %rdi
leaq 0x9257(%rip), %rsi      # 0x14b2c
callq 0x42a0
subq 0x80(%rsp), %r14
movq %rax, %rdi
movq %r14, %rsi
callq 0x41b0
movq %rax, %rdi
leaq 0x9235(%rip), %rsi      # 0x14b2c
callq 0x42a0
movsd 0xb8(%rsp), %xmm0
movq %rax, %rdi
callq 0x4450
movq %rax, %rdi
leaq 0x9215(%rip), %rsi      # 0x14b2c
callq 0x42a0
movsd 0xb0(%rsp), %xmm0
movq %rax, %rdi
callq 0x4450
movq %rax, %rdi
leaq 0x91f5(%rip), %rsi      # 0x14b2c
callq 0x42a0
movsd 0xa8(%rsp), %xmm0
movq %rax, %rdi
callq 0x4450
movq %rax, %rdi
leaq 0x91d5(%rip), %rsi      # 0x14b2c
callq 0x42a0
movsd 0xa0(%rsp), %xmm0
movq %rax, %rdi
callq 0x4450
jmp 0xb9da
movq 0x1067a(%rip), %rdi     # 0x1bff0
leaq 0x913a(%rip), %rsi      # 0x14ab7
callq 0x42a0
movq %rax, %rdi
movq %r14, %rsi
callq 0x41b0
movq %rax, %rdi
leaq 0x913f(%rip), %rsi      # 0x14ad6
callq 0x42a0
movq %rax, %rdi
movq 0x80(%rsp), %rsi
callq 0x41b0
movq %rax, %rdi
leaq 0x913d(%rip), %rsi      # 0x14af3
callq 0x42a0
movq %rax, %rdi
leaq 0xf0(%rsp), %rsi
callq 0x4270
movq %rax, %rdi
leaq 0x912f(%rip), %rsi      # 0x14b04
callq 0x42a0
movq %rax, %rdi
callq 0x4140
movq 0x118(%rsp), %r14
addq $-0x2, %r14
jmp 0xb544
movq 0x30(%rsp), %rdi
callq 0x4370
movq 0x90(%rsp), %rdi
callq 0x4370
movq 0x8(%rsp), %rdi
callq 0x4050
movq %rbx, %rdi
callq 0x4050
leaq 0x140(%rsp), %rdi
callq 0x43c0
leaq 0x140(%rsp), %rdi
callq 0x43f0
leaq 0x340(%rsp), %rdi
callq 0x4210
leaq 0x60(%rsp), %rdi
callq 0x4558
leaq 0xd0(%rsp), %rdi
callq 0x4558
leaq 0xf0(%rsp), %rdi
callq 0x4558
leaq 0x120(%rsp), %rdi
callq 0x4558
addq $0x4c8, %rsp            # imm = 0x4C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10563(%rip), %rdi     # 0x1bff0
leaq 0x8e25(%rip), %rsi      # 0x148b9
callq 0x42a0
leaq 0x140(%rsp), %rsi
movq %rax, %rdi
callq 0x4270
leaq 0x8abc(%rip), %rsi      # 0x1456c
movq %rax, %rdi
callq 0x42a0
movq %rax, %rdi
callq 0x4140
jmp 0xbbdf
movq 0x10524(%rip), %rdi     # 0x1bff0
leaq 0x8ea7(%rip), %rsi      # 0x1497a
callq 0x42a0
movq %rax, %rdi
movq %r15, %rsi
callq 0x4270
leaq 0x8a82(%rip), %rsi      # 0x1456c
movq %rax, %rdi
callq 0x42a0
movq %rax, %rdi
callq 0x4140
jmp 0xbbdf
movq 0x104ea(%rip), %rdi     # 0x1bff0
leaq 0x8d49(%rip), %rsi      # 0x14856
callq 0x42a0
movq %rax, %rdi
movq %r15, %rsi
callq 0x4270
leaq 0x8d46(%rip), %rsi      # 0x1486a
movq %rax, %rdi
callq 0x42a0
movq %rax, %rdi
callq 0x4140
jmp 0xbbdf
movq 0x104b0(%rip), %rdi     # 0x1bff0
leaq 0x8d0f(%rip), %rsi      # 0x14856
callq 0x42a0
movq %rax, %rdi
movq %r15, %rsi
callq 0x4270
leaq 0x8d23(%rip), %rsi      # 0x14881
movq %rax, %rdi
callq 0x42a0
movq %rax, %rdi
callq 0x4140
jmp 0xbc1b
movq 0x10476(%rip), %rdi     # 0x1bff0
leaq 0x8df9(%rip), %rsi      # 0x1497a
callq 0x42a0
movq %rax, %rdi
movq %r15, %rsi
callq 0x4270
leaq 0x89d4(%rip), %rsi      # 0x1456c
movq %rax, %rdi
callq 0x42a0
movq %rax, %rdi
callq 0x4140
jmp 0xbbdf
movq 0x1043f(%rip), %rdi     # 0x1bff0
leaq 0x8c9e(%rip), %rsi      # 0x14856
callq 0x42a0
movq %rax, %rdi
movq %r15, %rsi
callq 0x4270
leaq 0x8c9b(%rip), %rsi      # 0x1486a
movq %rax, %rdi
callq 0x42a0
movq %rax, %rdi
callq 0x4140
pushq $0x2
jmp 0xbfbd
movq 0x10403(%rip), %rdi     # 0x1bff0
leaq 0x8c62(%rip), %rsi      # 0x14856
callq 0x42a0
movq %rax, %rdi
movq %r15, %rsi
callq 0x4270
leaq 0x8c76(%rip), %rsi      # 0x14881
movq %rax, %rdi
callq 0x42a0
movq %rax, %rdi
callq 0x4140
pushq $0x3
jmp 0xbfbd
movq %rax, %rbx
jmp 0xbef0
movq %rax, %rbx
jmp 0xc000
jmp 0xbed8
jmp 0xbed8
jmp 0xbcdf
movq %rdx, %r14
movq %rax, %rbx
pushq $0x8
popq %rsi
movq %r15, %rdi
callq 0x42d0
jmp 0xbc5a
movq %rdx, %r14
movq %rax, %rbx
cmpl $0x2, %r14d
jne 0xbfe6
movq %rbx, %rdi
callq 0x4330
leaq 0x40(%rsp), %rdi
movq %rax, %rsi
callq 0x43b0
movq %rbx, %rdi
callq 0x4110
movq 0x10368(%rip), %rdi     # 0x1bff0
leaq 0x8a96(%rip), %rsi      # 0x14725
callq 0x42a0
leaq 0x40(%rsp), %rsi
movq %rax, %rdi
callq 0x4270
movq %rax, %rdi
callq 0x4140
jmp 0xbfbb
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x4558
callq 0x4470
jmp 0xbfe6
movq %rax, %rbx
jmp 0xbff3
movq %rax, %rbx
jmp 0xc000
jmp 0xbed8
jmp 0xbed8
movq %rax, %rbx
cmpl $0x1, %edx
jne 0xbee3
movq %rbx, %rdi
callq 0x4110
movq %rax, %rbx
movq 0x102f3(%rip), %rdi     # 0x1bff0
leaq 0x8c97(%rip), %rsi      # 0x1499b
callq 0x42a0
movq %rax, %rdi
movq %r15, %rsi
callq 0x4270
leaq 0x8ce2(%rip), %rsi      # 0x149fd
movq %rax, %rdi
callq 0x42a0
movq %rax, %r14
movq 0x18(%rbx), %rsi
movl 0x10(%rbx), %edx
movq (%rsi), %rax
leaq 0x40(%rsp), %rdi
callq *0x20(%rax)
leaq 0x40(%rsp), %rsi
movq %r14, %rdi
callq 0x4270
movq %rax, %rdi
callq 0x41c0
leaq 0x40(%rsp), %rdi
jmp 0xbe9f
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x4558
jmp 0xbd6b
jmp 0xbd68
movq %rax, %rbx
callq 0x4470
jmp 0xbee3
jmp 0xbd91
jmp 0xbfe3
movq %rdx, %r14
movq %rax, %rbx
jmp 0xbe3e
jmp 0xbee0
jmp 0xbfe3
movq %rax, %rbx
cmpl $0x1, %edx
jne 0xc024
movq %rbx, %rdi
callq 0x4110
movq %rax, %rbx
movq 0x10241(%rip), %rdi     # 0x1bff0
leaq 0x8a83(%rip), %rsi      # 0x14839
callq 0x42a0
movq %rax, %rdi
movq %r15, %rsi
callq 0x4270
leaq 0x8a44(%rip), %rsi      # 0x14811
movq %rax, %rdi
callq 0x42a0
movq %rax, %r14
movq 0x18(%rbx), %rsi
movl 0x10(%rbx), %edx
movq (%rsi), %rax
leaq 0x140(%rsp), %rdi
callq *0x20(%rax)
leaq 0x140(%rsp), %rsi
movq %r14, %rdi
callq 0x4270
movq %rax, %rdi
callq 0x41c0
jmp 0xbe97
movq %rax, %rbx
leaq 0x140(%rsp), %rdi
callq 0x4558
jmp 0xbe21
jmp 0xbe1e
movq %rax, %rbx
callq 0x4470
jmp 0xc024
movq %rdx, %r14
movq %rax, %rbx
leaq 0x140(%rsp), %rdi
callq 0x4558
cmpl $0x1, %r14d
jne 0xc031
movq %rbx, %rdi
callq 0x4110
movq %rax, %r14
movq 0x10196(%rip), %rdi     # 0x1bff0
leaq 0x8b81(%rip), %rsi      # 0x149e2
callq 0x42a0
movq %rax, %rbx
movq 0x18(%r14), %rsi
movl 0x10(%r14), %edx
movq (%rsi), %rax
leaq 0x140(%rsp), %rdi
callq *0x20(%rax)
leaq 0x140(%rsp), %rsi
movq %rbx, %rdi
callq 0x4270
movq %rax, %rdi
callq 0x41c0
leaq 0x140(%rsp), %rdi
callq 0x4558
leaq 0x8967(%rip), %rdi      # 0x14812
callq 0x4250
pushq $0x1
jmp 0xbfbd
movq %rax, %rbx
leaq 0x140(%rsp), %rdi
callq 0x4558
jmp 0xbece
jmp 0xbecb
movq %rax, %rbx
callq 0x4470
jmp 0xc031
movq %rax, %rbx
jmp 0xc024
movq %rax, %rbx
leaq 0x140(%rsp), %rdi
callq 0x43f0
leaq 0x340(%rsp), %rdi
callq 0x4210
jmp 0xc000
jmp 0xbfe3
movq %rax, %rbx
cmpl $0x2, %edx
jne 0xbfe6
movq %rbx, %rdi
callq 0x4330
leaq 0x40(%rsp), %rdi
movq %rax, %rsi
callq 0x43b0
movq %rbx, %rdi
callq 0x4110
movq 0x100b9(%rip), %rdi     # 0x1bff0
leaq 0x87e7(%rip), %rsi      # 0x14725
callq 0x42a0
leaq 0x40(%rsp), %rsi
movq %rax, %rdi
callq 0x4270
movq %rax, %rdi
callq 0x4140
jmp 0xbfbb
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x4558
callq 0x4470
jmp 0xbfe6
movq %rax, %rbx
cmpl $0x2, %edx
jne 0xbfe6
movq %rbx, %rdi
callq 0x4330
leaq 0x40(%rsp), %rdi
movq %rax, %rsi
callq 0x43b0
movq %rbx, %rdi
callq 0x4110
movq 0x10056(%rip), %rdi     # 0x1bff0
leaq 0x8784(%rip), %rsi      # 0x14725
callq 0x42a0
leaq 0x40(%rsp), %rsi
movq %rax, %rdi
callq 0x4270
movq %rax, %rdi
callq 0x4140
pushq $0x5
popq %rdi
callq 0x4310
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x4558
callq 0x4470
jmp 0xbfe6
movq %rax, %rdi
callq 0x6194
jmp 0xbfe3
jmp 0xbfe3
movq %rax, %rbx
leaq 0x140(%rsp), %rdi
callq 0x43f0
leaq 0x340(%rsp), %rdi
callq 0x4210
leaq 0x60(%rsp), %rdi
callq 0x4558
leaq 0xd0(%rsp), %rdi
callq 0x4558
leaq 0xf0(%rsp), %rdi
callq 0x4558
leaq 0x120(%rsp), %rdi
callq 0x4558
movq %rbx, %rdi
callq 0x44d0
nop