// File: bstrysko[P]IntelHexParser/src/IntelHexFileEntry.cpp
// Function: IntelHexFileEntry::IntelHexFileEntry(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
IntelHexFileEntry::IntelHexFileEntry(string entry)
{
	if(entry[0] != ':')
	{
		stringstream o;
		o << "Invalid hex entry: ";
		o << entry;
		throw ios_base::failure(o.str());
	}

	uint8_t count = 2*asciiHexTo64(entry.substr(1,2));
	uint8_t hAddress = asciiHexTo64(entry.substr(3,2));
	uint8_t lAddress = asciiHexTo64(entry.substr(5,2));
	address = ((hAddress << 8) | lAddress);
	recordType = asciiHexTo64(entry.substr(7,2));	

	uint8_t cChecksum = (count/2) + lAddress + hAddress + recordType;

	if(count != entry.length()-(9+2+1))
	{
		stringstream o;
		o << "length provided != length of data | ";
		o << (size_t)(count);
		o << " != ";
		o << (entry.length()-(9+2+1));

		throw ios_base::failure(o.str());
	}

	for(uint8_t i = 0; i < count; i+=2)
	{
		uint8_t v = asciiHexTo64(entry.substr(9+i,2));
		cChecksum += v;
		data.push_back(v);
	}

	uint8_t checksum = asciiHexTo64(entry.substr(entry.length()-3,2));

	if(((uint8_t)(cChecksum + checksum)) != ((uint8_t)0))
	{
		cout << hex << (size_t)address << endl;
		cout << dec;

		stringstream o;
		o << "(cChecksum + checksum) != 0 | 0x";
		o << hex;
		o << (size_t)(cChecksum + checksum);
		o << " != 0 | cChecksum = 0x";
		o << (size_t)cChecksum;
		o << ", checksum = 0x";
		o << (size_t)checksum;

		throw ios_base::failure(o.str());
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x6c0, %rsp            # imm = 0x6C0
movq %rsi, -0x620(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x630(%rbp)
addq $0x8, %rdi
movq %rdi, -0x628(%rbp)
callq 0xb340
movq -0x620(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xc500
movq %rax, -0x618(%rbp)
jmp 0xf2ae
movq -0x618(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x3a, %eax
je 0xf3d0
leaq -0x1a8(%rbp), %rdi
callq 0xb740
jmp 0xf2cf
leaq -0x198(%rbp), %rdi
leaq 0x3db1(%rip), %rsi      # 0x1308e
callq 0xba50
jmp 0xf2e4
movq -0x620(%rbp), %rsi
leaq -0x198(%rbp), %rdi
callq 0xb940
jmp 0xf2f9
movb $0x1, -0x1c9(%rbp)
movl $0x20, %edi
callq 0xb460
movq %rax, -0x638(%rbp)
leaq -0x1c8(%rbp), %rdi
leaq -0x1a8(%rbp), %rsi
callq 0xba30
jmp 0xf326
movq -0x638(%rbp), %rdi
leaq -0x1c8(%rbp), %rsi
callq 0xc480
jmp 0xf33b
movq -0x638(%rbp), %rdi
movb $0x0, -0x1c9(%rbp)
movq 0x8c58(%rip), %rsi      # 0x17fa8
movq 0x8c69(%rip), %rdx      # 0x17fc0
callq 0xc250
jmp 0xfb07
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0xfaf2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0xf3bf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0xf3a6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
leaq -0x1c8(%rbp), %rdi
callq 0xb690
testb $0x1, -0x1c9(%rbp)
jne 0xf3b1
jmp 0xf3bd
movq -0x638(%rbp), %rdi
callq 0xb780
jmp 0xf3bf
leaq -0x1a8(%rbp), %rdi
callq 0xb800
jmp 0xfaf2
movq -0x620(%rbp), %rsi
leaq -0x1f0(%rbp), %rdi
movl $0x1, %edx
movl $0x2, %ecx
callq 0xbaf0
jmp 0xf3ef
movq -0x630(%rbp), %rdi
leaq -0x1f0(%rbp), %rsi
callq 0xbf30
movq %rax, -0x640(%rbp)
jmp 0xf40b
movq -0x640(%rbp), %rax
addl %eax, %eax
movb %al, -0x641(%rbp)
leaq -0x1f0(%rbp), %rdi
callq 0xb690
movb -0x641(%rbp), %al
movq -0x620(%rbp), %rsi
movb %al, -0x1ca(%rbp)
leaq -0x218(%rbp), %rdi
movl $0x3, %edx
movl $0x2, %ecx
callq 0xbaf0
jmp 0xf451
movq -0x630(%rbp), %rdi
leaq -0x218(%rbp), %rsi
callq 0xbf30
movq %rax, -0x650(%rbp)
jmp 0xf46d
movq -0x650(%rbp), %rax
movb %al, -0x651(%rbp)
leaq -0x218(%rbp), %rdi
callq 0xb690
movb -0x651(%rbp), %al
movq -0x620(%rbp), %rsi
movb %al, -0x1f1(%rbp)
leaq -0x240(%rbp), %rdi
movl $0x5, %edx
movl $0x2, %ecx
callq 0xbaf0
jmp 0xf4b1
movq -0x630(%rbp), %rdi
leaq -0x240(%rbp), %rsi
callq 0xbf30
movq %rax, -0x660(%rbp)
jmp 0xf4cd
movq -0x660(%rbp), %rax
movb %al, -0x661(%rbp)
leaq -0x240(%rbp), %rdi
callq 0xb690
movb -0x661(%rbp), %cl
movq -0x630(%rbp), %rax
movq -0x620(%rbp), %rsi
movb %cl, -0x219(%rbp)
movzbl -0x1f1(%rbp), %ecx
shll $0x8, %ecx
movzbl -0x219(%rbp), %edx
orl %edx, %ecx
movw %cx, (%rax)
leaq -0x260(%rbp), %rdi
movl $0x7, %edx
movl $0x2, %ecx
callq 0xbaf0
jmp 0xf52e
movq -0x630(%rbp), %rdi
leaq -0x260(%rbp), %rsi
callq 0xbf30
movq %rax, -0x670(%rbp)
jmp 0xf54a
movq -0x630(%rbp), %rax
movq -0x670(%rbp), %rcx
movb %cl, 0x2(%rax)
leaq -0x260(%rbp), %rdi
callq 0xb690
movq -0x630(%rbp), %rcx
movq -0x620(%rbp), %rdi
movzbl -0x1ca(%rbp), %eax
movl $0x2, %esi
cltd
idivl %esi
movzbl -0x219(%rbp), %edx
addl %edx, %eax
movzbl -0x1f1(%rbp), %edx
addl %edx, %eax
movzbl 0x2(%rcx), %ecx
addl %ecx, %eax
movb %al, -0x261(%rbp)
movzbl -0x1ca(%rbp), %eax
movq %rax, -0x678(%rbp)
callq 0xbfb0
movq %rax, %rcx
movq -0x678(%rbp), %rax
subq $0xc, %rcx
cmpq %rcx, %rax
je 0xf784
leaq -0x3f0(%rbp), %rdi
callq 0xb740
jmp 0xf5da
leaq -0x3e0(%rbp), %rdi
leaq 0x3aba(%rip), %rsi      # 0x130a2
callq 0xba50
jmp 0xf5ef
leaq -0x3e0(%rbp), %rdi
movzbl -0x1ca(%rbp), %eax
movl %eax, %esi
callq 0xb1d0
jmp 0xf606
leaq -0x3e0(%rbp), %rdi
leaq 0x3ab3(%rip), %rsi      # 0x130c7
callq 0xba50
jmp 0xf61b
movq -0x620(%rbp), %rdi
leaq -0x3e0(%rbp), %rax
movq %rax, -0x680(%rbp)
callq 0xbfb0
movq -0x680(%rbp), %rdi
movq %rax, %rsi
addq $-0xc, %rsi
callq 0xb1d0
jmp 0xf64a
movb $0x1, -0x411(%rbp)
movl $0x20, %edi
callq 0xb460
movq %rax, -0x688(%rbp)
leaq -0x410(%rbp), %rdi
leaq -0x3f0(%rbp), %rsi
callq 0xba30
jmp 0xf677
movq -0x688(%rbp), %rdi
leaq -0x410(%rbp), %rsi
callq 0xc480
jmp 0xf68c
movq -0x688(%rbp), %rdi
movb $0x0, -0x411(%rbp)
movq 0x8907(%rip), %rsi      # 0x17fa8
movq 0x8918(%rip), %rdx      # 0x17fc0
callq 0xc250
jmp 0xfb07
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
leaq -0x1f0(%rbp), %rdi
callq 0xb690
jmp 0xfaf2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
leaq -0x218(%rbp), %rdi
callq 0xb690
jmp 0xfaf2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
leaq -0x240(%rbp), %rdi
callq 0xb690
jmp 0xfaf2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
leaq -0x260(%rbp), %rdi
callq 0xb690
jmp 0xfaf2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0xf773
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0xf75a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
leaq -0x410(%rbp), %rdi
callq 0xb690
testb $0x1, -0x411(%rbp)
jne 0xf765
jmp 0xf771
movq -0x688(%rbp), %rdi
callq 0xb780
jmp 0xf773
leaq -0x3f0(%rbp), %rdi
callq 0xb800
jmp 0xfaf2
movb $0x0, -0x412(%rbp)
movzbl -0x412(%rbp), %eax
movzbl -0x1ca(%rbp), %ecx
cmpl %ecx, %eax
jge 0xf86b
movq -0x620(%rbp), %rsi
movzbl -0x412(%rbp), %eax
addl $0x9, %eax
movl %eax, %edx
leaq -0x438(%rbp), %rdi
movl $0x2, %ecx
callq 0xbaf0
jmp 0xf7c7
movq -0x630(%rbp), %rdi
leaq -0x438(%rbp), %rsi
callq 0xbf30
movq %rax, -0x690(%rbp)
jmp 0xf7e3
movq -0x690(%rbp), %rax
movb %al, -0x691(%rbp)
leaq -0x438(%rbp), %rdi
callq 0xb690
movb -0x691(%rbp), %al
movq -0x630(%rbp), %rdi
movb %al, -0x413(%rbp)
movzbl -0x413(%rbp), %ecx
movzbl -0x261(%rbp), %eax
addl %ecx, %eax
movb %al, -0x261(%rbp)
addq $0x8, %rdi
leaq -0x413(%rbp), %rsi
callq 0xb0b0
jmp 0xf837
jmp 0xf839
movzbl -0x412(%rbp), %eax
addl $0x2, %eax
movb %al, -0x412(%rbp)
jmp 0xf78b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
leaq -0x438(%rbp), %rdi
callq 0xb690
jmp 0xfaf2
movq -0x620(%rbp), %rdi
callq 0xbfb0
movq -0x620(%rbp), %rsi
movq %rax, %rdx
addq $-0x3, %rdx
leaq -0x460(%rbp), %rdi
movl $0x2, %ecx
callq 0xbaf0
jmp 0xf898
movq -0x630(%rbp), %rdi
leaq -0x460(%rbp), %rsi
callq 0xbf30
movq %rax, -0x6a0(%rbp)
jmp 0xf8b4
movq -0x6a0(%rbp), %rax
movb %al, -0x6a1(%rbp)
leaq -0x460(%rbp), %rdi
callq 0xb690
movb -0x6a1(%rbp), %al
movb %al, -0x439(%rbp)
movzbl -0x261(%rbp), %eax
movzbl -0x439(%rbp), %ecx
addl %ecx, %eax
movzbl %al, %eax
cmpl $0x0, %eax
je 0xfae9
movq 0x86d4(%rip), %rdi      # 0x17fd0
movq 0x86ed(%rip), %rsi      # 0x17ff0
callq 0xb2e0
movq %rax, -0x6b0(%rbp)
jmp 0xf911
movq -0x6b0(%rbp), %rdi
movq -0x630(%rbp), %rax
movzwl (%rax), %eax
movl %eax, %esi
callq 0xb1d0
movq %rax, -0x6b8(%rbp)
jmp 0xf932
movq -0x6b8(%rbp), %rdi
movq 0x8678(%rip), %rsi      # 0x17fb8
callq 0xbb10
jmp 0xf947
movq 0x8682(%rip), %rdi      # 0x17fd0
movq 0x8673(%rip), %rsi      # 0x17fc8
callq 0xb2e0
jmp 0xf95c
leaq -0x5e8(%rbp), %rdi
callq 0xb740
jmp 0xf96a
leaq -0x5d8(%rbp), %rdi
leaq 0x3754(%rip), %rsi      # 0x130cc
callq 0xba50
jmp 0xf97f
leaq -0x5d8(%rbp), %rdi
movq 0x8663(%rip), %rsi      # 0x17ff0
callq 0xb2e0
jmp 0xf994
leaq -0x5d8(%rbp), %rdi
movzbl -0x261(%rbp), %eax
movzbl -0x439(%rbp), %ecx
addl %ecx, %eax
movl %eax, %esi
callq 0xb1d0
jmp 0xf9b4
leaq -0x5d8(%rbp), %rdi
leaq 0x372b(%rip), %rsi      # 0x130ed
callq 0xba50
jmp 0xf9c9
leaq -0x5d8(%rbp), %rdi
movzbl -0x261(%rbp), %eax
movl %eax, %esi
callq 0xb1d0
jmp 0xf9e0
leaq -0x5d8(%rbp), %rdi
leaq 0x3716(%rip), %rsi      # 0x13104
callq 0xba50
jmp 0xf9f5
leaq -0x5d8(%rbp), %rdi
movzbl -0x439(%rbp), %eax
movl %eax, %esi
callq 0xb1d0
jmp 0xfa0c
movb $0x1, -0x609(%rbp)
movl $0x20, %edi
callq 0xb460
movq %rax, -0x6c0(%rbp)
leaq -0x608(%rbp), %rdi
leaq -0x5e8(%rbp), %rsi
callq 0xba30
jmp 0xfa39
movq -0x6c0(%rbp), %rdi
leaq -0x608(%rbp), %rsi
callq 0xc480
jmp 0xfa4e
movq -0x6c0(%rbp), %rdi
movb $0x0, -0x609(%rbp)
movq 0x8545(%rip), %rsi      # 0x17fa8
movq 0x8556(%rip), %rdx      # 0x17fc0
callq 0xc250
jmp 0xfb07
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
leaq -0x460(%rbp), %rdi
callq 0xb690
jmp 0xfaf2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0xfadb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0xfac2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
leaq -0x608(%rbp), %rdi
callq 0xb690
testb $0x1, -0x609(%rbp)
jne 0xfacd
jmp 0xfad9
movq -0x6c0(%rbp), %rdi
callq 0xb780
jmp 0xfadb
leaq -0x5e8(%rbp), %rdi
callq 0xb800
jmp 0xfaf2
addq $0x6c0, %rsp            # imm = 0x6C0
popq %rbp
retq
movq -0x628(%rbp), %rdi
callq 0xb290
movq -0x18(%rbp), %rdi
callq 0xc2b0
nopw (%rax,%rax)