// File: auton1[P]viterbi/viterbi.cpp
// Function: read_genetic_map_at_positions(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&, std::vector<unsigned int, std::allocator<unsigned int>>&, std::vector<double, std::allocator<double>>&)
void read_genetic_map_at_positions(string &filename, vector<unsigned int> &positions, vector<double> &map_out)
{
	// FILE FORMAT IS CHROM POS(BP) RATE(cM/Mb) MAP(cM)
	ifstream in(filename.c_str());
	if (!in.good())
		LOG.error("Could not open map file: " + filename);
	string line;
	stringstream ss;
	getline(in, line);	// Header
	string CHROM;
	int POS;
	double rate, map_pos;
	vector<int> pos;
	vector<double> map;

	while (!in.eof())
	{
		getline(in, line);
		if (line == "")
			continue;
		ss.clear(); ss.str("");
		ss.str(line);

		ss >> CHROM >> POS >> rate >> map_pos;

		pos.push_back(POS);
		map.push_back(map_pos);
	}
	// Interpolate map at positions
	map_out.resize(positions.size());

	for (unsigned int ui=0; ui<positions.size(); ui++)
	{
		vector<int>::iterator it = upper_bound(pos.begin(), pos.end(), positions[ui]);
		if (it == pos.end())
		{
			map_out[ui] = map[map.size()-1];
			continue;
		}
		else if (it != pos.begin())
			it--;

		int idx = distance(pos.begin(), it);
		int idx2 = idx+1;

		if (pos[idx] == positions[ui])
			map_out[ui] = map[idx];
		else
			map_out[ui] = map[idx] + ((map[idx2] - map[idx]) * (positions[ui] - pos[idx]) / (pos[idx2] - pos[idx]));
	}
}

// Assembly:
subq $0x578, %rsp            # imm = 0x578
movq %rdi, 0x570(%rsp)
movq %rsi, 0x568(%rsp)
movq %rdx, 0x560(%rsp)
movq 0x570(%rsp), %rdi
callq 0x61a0
movq %rax, %rsi
leaq 0x358(%rsp), %rdi
movl $0x8, %edx
callq 0x6a60
movq 0x358(%rsp), %rax
movq -0x18(%rax), %rax
leaq 0x358(%rsp,%rax), %rdi
callq 0x6960
movb %al, 0xa3(%rsp)
jmp 0x7b73
movb 0xa3(%rsp), %al
testb $0x1, %al
jne 0x7c0b
jmp 0x7b84
movq 0x570(%rsp), %rdx
leaq 0x6c4c2(%rip), %rsi     # 0x74055
leaq 0x328(%rsp), %rdi
callq 0xe8d0
jmp 0x7ba2
leaq 0x8ba57(%rip), %rdi     # 0x93600
leaq 0x328(%rsp), %rsi
movl $0x1, %edx
callq 0x712f0
jmp 0x7bbd
leaq 0x328(%rsp), %rdi
callq 0x6b50
jmp 0x7c0b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x350(%rsp)
movl %eax, 0x34c(%rsp)
jmp 0x82fc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x350(%rsp)
movl %eax, 0x34c(%rsp)
leaq 0x328(%rsp), %rdi
callq 0x6b50
jmp 0x82fc
leaq 0x308(%rsp), %rdi
callq 0x67e0
leaq 0x180(%rsp), %rdi
callq 0x6360
jmp 0x7c27
leaq 0x358(%rsp), %rdi
leaq 0x308(%rsp), %rsi
callq 0x60c0
jmp 0x7c3e
leaq 0x160(%rsp), %rdi
callq 0x67e0
leaq 0x130(%rsp), %rdi
callq 0xea00
leaq 0x118(%rsp), %rdi
callq 0xea10
movq 0x358(%rsp), %rax
movq -0x18(%rax), %rax
leaq 0x358(%rsp,%rax), %rdi
callq 0x6a70
movb %al, 0xa2(%rsp)
jmp 0x7c87
movb 0xa2(%rsp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x7c99
jmp 0x7eaf
leaq 0x358(%rsp), %rdi
leaq 0x308(%rsp), %rsi
callq 0x60c0
jmp 0x7cb0
leaq 0x6e072(%rip), %rsi     # 0x75d29
leaq 0x308(%rsp), %rdi
callq 0xea20
movb %al, 0xa1(%rsp)
jmp 0x7ccd
movb 0xa1(%rsp), %al
testb $0x1, %al
jne 0x7cda
jmp 0x7d27
jmp 0x7c65
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x350(%rsp)
movl %eax, 0x34c(%rsp)
jmp 0x82ef
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x350(%rsp)
movl %eax, 0x34c(%rsp)
jmp 0x82e2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x350(%rsp)
movl %eax, 0x34c(%rsp)
jmp 0x82bb
movq 0x180(%rsp), %rax
movq -0x18(%rax), %rax
leaq 0x180(%rsp,%rax), %rdi
xorl %esi, %esi
callq 0x6950
jmp 0x7d44
leaq 0xf7(%rsp), %rdi
movq %rdi, 0x98(%rsp)
callq 0x69f0
movq 0x98(%rsp), %rdx
leaq 0x6dfc1(%rip), %rsi     # 0x75d29
leaq 0xf8(%rsp), %rdi
callq 0x67c0
jmp 0x7d77
leaq 0x180(%rsp), %rdi
leaq 0xf8(%rsp), %rsi
callq 0x6650
jmp 0x7d8e
leaq 0xf8(%rsp), %rdi
callq 0x6b50
leaq 0xf7(%rsp), %rdi
callq 0x65f0
leaq 0x180(%rsp), %rdi
leaq 0x308(%rsp), %rsi
callq 0x6650
jmp 0x7dbf
leaq 0x180(%rsp), %rdi
leaq 0x160(%rsp), %rsi
callq 0x6720
movq %rax, 0x90(%rsp)
jmp 0x7dde
movq 0x90(%rsp), %rdi
leaq 0x15c(%rsp), %rsi
callq 0x62b0
movq %rax, 0x88(%rsp)
jmp 0x7dfd
movq 0x88(%rsp), %rdi
leaq 0x150(%rsp), %rsi
callq 0x6420
movq %rax, 0x80(%rsp)
jmp 0x7e1c
movq 0x80(%rsp), %rdi
leaq 0x148(%rsp), %rsi
callq 0x6420
jmp 0x7e33
leaq 0x130(%rsp), %rdi
leaq 0x15c(%rsp), %rsi
callq 0xea50
jmp 0x7e4a
leaq 0x118(%rsp), %rdi
leaq 0x148(%rsp), %rsi
callq 0xeac0
jmp 0x7e61
jmp 0x7c65
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x350(%rsp)
movl %eax, 0x34c(%rsp)
jmp 0x7e9d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x350(%rsp)
movl %eax, 0x34c(%rsp)
leaq 0xf8(%rsp), %rdi
callq 0x6b50
leaq 0xf7(%rsp), %rdi
callq 0x65f0
jmp 0x82bb
movq 0x560(%rsp), %rax
movq %rax, 0x78(%rsp)
movq 0x568(%rsp), %rdi
callq 0xe850
movq 0x78(%rsp), %rdi
movq %rax, %rsi
callq 0xe7b0
jmp 0x7ed8
movl $0x0, 0xf0(%rsp)
movl 0xf0(%rsp), %eax
movq %rax, 0x70(%rsp)
movq 0x568(%rsp), %rdi
callq 0xe850
movq %rax, %rcx
movq 0x70(%rsp), %rax
cmpq %rcx, %rax
jae 0x8265
leaq 0x130(%rsp), %rdi
movq %rdi, 0x60(%rsp)
callq 0xeb90
movq 0x60(%rsp), %rdi
movq %rax, 0xe0(%rsp)
callq 0xebc0
movq %rax, 0xd8(%rsp)
movq 0x568(%rsp), %rdi
movl 0xf0(%rsp), %eax
movl %eax, %esi
callq 0xe8b0
movq %rax, %rdx
movq 0xe0(%rsp), %rdi
movq 0xd8(%rsp), %rsi
callq 0xeb30
movq %rax, 0x68(%rsp)
jmp 0x7f6e
movq 0x68(%rsp), %rax
movq %rax, 0xe8(%rsp)
leaq 0x130(%rsp), %rdi
callq 0xebc0
movq %rax, 0xd0(%rsp)
leaq 0xe8(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0xebf0
testb $0x1, %al
jne 0x7fab
jmp 0x7ffb
leaq 0x118(%rsp), %rdi
callq 0xe890
movq %rax, %rsi
subq $0x1, %rsi
leaq 0x118(%rsp), %rdi
callq 0xe870
movsd (%rax), %xmm0
movsd %xmm0, 0x58(%rsp)
movq 0x560(%rsp), %rdi
movl 0xf0(%rsp), %eax
movl %eax, %esi
callq 0xe870
movsd 0x58(%rsp), %xmm0
movsd %xmm0, (%rax)
jmp 0x824f
leaq 0x130(%rsp), %rdi
callq 0xeb90
movq %rax, 0xc8(%rsp)
leaq 0xe8(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0xec30
testb $0x1, %al
jne 0x802b
jmp 0x8042
leaq 0xe8(%rsp), %rdi
xorl %esi, %esi
callq 0xec70
movq %rax, 0xc0(%rsp)
jmp 0x8044
leaq 0x130(%rsp), %rdi
callq 0xeb90
movq %rax, 0xb0(%rsp)
movq 0xe8(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq 0xb0(%rsp), %rdi
movq 0xa8(%rsp), %rsi
callq 0xecb0
movq %rax, 0x50(%rsp)
jmp 0x8085
movq 0x50(%rsp), %rax
movl %eax, 0xbc(%rsp)
movl 0xbc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
movslq 0xbc(%rsp), %rsi
leaq 0x130(%rsp), %rdi
callq 0xecf0
movl (%rax), %eax
movl %eax, 0x4c(%rsp)
movq 0x568(%rsp), %rdi
movl 0xf0(%rsp), %eax
movl %eax, %esi
callq 0xe8b0
movq %rax, %rcx
movl 0x4c(%rsp), %eax
cmpl (%rcx), %eax
jne 0x8122
movslq 0xbc(%rsp), %rsi
leaq 0x118(%rsp), %rdi
callq 0xe870
movsd (%rax), %xmm0
movsd %xmm0, 0x40(%rsp)
movq 0x560(%rsp), %rdi
movl 0xf0(%rsp), %eax
movl %eax, %esi
callq 0xe870
movsd 0x40(%rsp), %xmm0
movsd %xmm0, (%rax)
jmp 0x824d
movslq 0xbc(%rsp), %rsi
leaq 0x118(%rsp), %rdi
movq %rdi, (%rsp)
callq 0xe870
movq (%rsp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, 0x28(%rsp)
movslq 0xa4(%rsp), %rsi
callq 0xe870
movq (%rsp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
movslq 0xbc(%rsp), %rsi
callq 0xe870
movsd 0x8(%rsp), %xmm0
movsd (%rax), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0x568(%rsp), %rdi
movl 0xf0(%rsp), %eax
movl %eax, %esi
callq 0xe8b0
movl (%rax), %eax
movl %eax, 0x1c(%rsp)
movslq 0xbc(%rsp), %rsi
leaq 0x130(%rsp), %rdi
callq 0xecf0
movsd 0x10(%rsp), %xmm0
movq %rax, %rcx
movl 0x1c(%rsp), %eax
movl (%rcx), %ecx
subl %ecx, %eax
cvtsi2sd %rax, %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0x20(%rsp)
movslq 0xa4(%rsp), %rsi
leaq 0x130(%rsp), %rdi
callq 0xecf0
movl (%rax), %eax
movl %eax, 0x34(%rsp)
movslq 0xbc(%rsp), %rsi
leaq 0x130(%rsp), %rdi
callq 0xecf0
movsd 0x20(%rsp), %xmm1
movsd 0x28(%rsp), %xmm0
movq %rax, %rcx
movl 0x34(%rsp), %eax
subl (%rcx), %eax
cvtsi2sd %eax, %xmm2
divsd %xmm2, %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, 0x38(%rsp)
movq 0x560(%rsp), %rdi
movl 0xf0(%rsp), %eax
movl %eax, %esi
callq 0xe870
movsd 0x38(%rsp), %xmm0
movsd %xmm0, (%rax)
jmp 0x824f
movl 0xf0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xf0(%rsp)
jmp 0x7ee3
leaq 0x118(%rsp), %rdi
callq 0xed10
leaq 0x130(%rsp), %rdi
callq 0xed70
leaq 0x160(%rsp), %rdi
callq 0x6b50
leaq 0x180(%rsp), %rdi
callq 0x63c0
leaq 0x308(%rsp), %rdi
callq 0x6b50
leaq 0x358(%rsp), %rdi
callq 0x6b38
addq $0x578, %rsp            # imm = 0x578
retq
leaq 0x118(%rsp), %rdi
callq 0xed10
leaq 0x130(%rsp), %rdi
callq 0xed70
leaq 0x160(%rsp), %rdi
callq 0x6b50
leaq 0x180(%rsp), %rdi
callq 0x63c0
leaq 0x308(%rsp), %rdi
callq 0x6b50
leaq 0x358(%rsp), %rdi
callq 0x6b38
movq 0x350(%rsp), %rdi
callq 0x69e0
nopw %cs:(%rax,%rax)