// File: nyorain[P]bintoheader/bintoheader.cpp
// Function: outputData(std::vector<unsigned char, std::allocator<unsigned char>> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, int)
void outputData(const std::vector<std::uint8_t>& data, const std::string& file,
	const std::string& name, int size)
{
	auto fname = file;
	std::ofstream ofs(fname);
	if(!ofs.is_open()) {
		std::cerr << "outputData: couldnt open output file " << file << "\n";
		std::cerr << "error: " << std::strerror(errno) << "\n";
		exit(EXIT_FAILURE);
	}

	auto byteSize = size / 8;
	if(data.size() % byteSize) {
		std::cerr << "outputData: file size is not a multiple of specified integer size\n";
		std::cerr << "\tfile size: " << data.size() << " bytes\n";
		std::cerr << "\tspecified size: " << size / 8 << " bytes\n";
		exit(EXIT_FAILURE);
	}

	std::string dataString;
	dataString.reserve(data.size() * 5); //approx.

	auto currColSize = 4u;
	for(auto i = 0u; i < data.size() / byteSize; ++i) {
		std::uint64_t value = 0;
		auto ptr = &data[i * byteSize];

		if(size == 8) value = *reinterpret_cast<const std::uint8_t*>(ptr);
		else if(size == 16) value = *reinterpret_cast<const std::uint16_t*>(ptr);
		else if(size == 32) value = *reinterpret_cast<const std::uint32_t*>(ptr);
		else if(size == 64) value = *reinterpret_cast<const std::uint64_t*>(ptr);

		auto str = std::to_string(value);
		if(currColSize + str.size() + 2 >= lineLength)
		{
			dataString.append(",\n    ");
			currColSize = 4u;
		}
		else if(i != 0)
		{
			dataString.append(", ");
			currColSize += 2;
		}

		currColSize += str.size();
		dataString.append(str);
	}

	std::string outputTypeName = "uint" + std::to_string(size) + "_t";
	std::string includeGuard = name;
	for(auto& ch : includeGuard) ch = std::toupper(ch);

	std::string txt = fileTemplate;
	replace(txt, "%t", outputTypeName);
	replace(txt, "%d", dataString);
	replace(txt, "%n", name);
	replace(txt, "%i", includeGuard);

	ofs << txt;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x4b0, %rsp            # imm = 0x4B0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x40(%rbp), %rdi
movq %rdi, -0x418(%rbp)
callq 0x3110
movq -0x418(%rbp), %rsi
leaq -0x240(%rbp), %rdi
movl $0x10, %edx
callq 0x34c0
jmp 0x3cf8
leaq -0x240(%rbp), %rdi
callq 0x31f0
movb %al, -0x419(%rbp)
jmp 0x3d0c
movb -0x419(%rbp), %al
testb $0x1, %al
jne 0x3df3
jmp 0x3d1c
movq 0xd2cd(%rip), %rdi      # 0x10ff0
leaq 0x7341(%rip), %rsi      # 0xb06b
callq 0x3270
movq %rax, -0x428(%rbp)
jmp 0x3d38
movq -0x428(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x3250
movq %rax, -0x430(%rbp)
jmp 0x3d51
movq -0x430(%rbp), %rdi
leaq 0x7545(%rip), %rsi      # 0xb2a4
callq 0x3270
jmp 0x3d66
movq 0xd283(%rip), %rdi      # 0x10ff0
leaq 0x72ca(%rip), %rsi      # 0xb03e
callq 0x3270
movq %rax, -0x438(%rbp)
jmp 0x3d82
callq 0x3030
movl (%rax), %edi
callq 0x30c0
movq -0x438(%rbp), %rdi
movq %rax, %rsi
callq 0x3270
movq %rax, -0x440(%rbp)
jmp 0x3da6
movq -0x440(%rbp), %rdi
leaq 0x74f0(%rip), %rsi      # 0xb2a4
callq 0x3270
jmp 0x3dbb
movl $0x1, %edi
callq 0x3300
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x248(%rbp)
movl %eax, -0x24c(%rbp)
jmp 0x4612
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x248(%rbp)
movl %eax, -0x24c(%rbp)
jmp 0x4606
movl -0x1c(%rbp), %eax
movl $0x8, %ecx
cltd
idivl %ecx
movl %eax, -0x250(%rbp)
movq -0x8(%rbp), %rdi
callq 0x5670
movslq -0x250(%rbp), %rcx
xorl %edx, %edx
divq %rcx
cmpq $0x0, %rdx
je 0x3eea
movq 0xd1c6(%rip), %rdi      # 0x10ff0
leaq 0x7260(%rip), %rsi      # 0xb091
callq 0x3270
jmp 0x3e38
movq 0xd1b1(%rip), %rdi      # 0x10ff0
leaq 0x728e(%rip), %rsi      # 0xb0d4
callq 0x3270
movq %rax, -0x448(%rbp)
jmp 0x3e54
movq -0x8(%rbp), %rdi
callq 0x5670
movq -0x448(%rbp), %rdi
movq %rax, %rsi
callq 0x3080
movq %rax, -0x450(%rbp)
jmp 0x3e75
movq -0x450(%rbp), %rdi
leaq 0x725e(%rip), %rsi      # 0xb0e1
callq 0x3270
jmp 0x3e8a
movq 0xd15f(%rip), %rdi      # 0x10ff0
leaq 0x7251(%rip), %rsi      # 0xb0e9
callq 0x3270
movq %rax, -0x458(%rbp)
jmp 0x3ea6
movq -0x458(%rbp), %rdi
movl -0x1c(%rbp), %esi
movl %esi, %eax
addl $0x7, %eax
testl %esi, %esi
cmovsl %eax, %esi
sarl $0x3, %esi
callq 0x3450
movq %rax, -0x460(%rbp)
jmp 0x3ecb
movq -0x460(%rbp), %rdi
leaq 0x7208(%rip), %rsi      # 0xb0e1
callq 0x3270
jmp 0x3ee0
movl $0x1, %edi
callq 0x3300
leaq -0x270(%rbp), %rdi
movq %rdi, -0x468(%rbp)
callq 0x3380
movq -0x8(%rbp), %rdi
callq 0x5670
movq -0x468(%rbp), %rdi
leaq (%rax,%rax,4), %rsi
callq 0x33f0
jmp 0x3f18
movl $0x4, -0x274(%rbp)
movl $0x0, -0x278(%rbp)
movl -0x278(%rbp), %eax
movq %rax, -0x470(%rbp)
movq -0x8(%rbp), %rdi
callq 0x5670
movslq -0x250(%rbp), %rcx
xorl %edx, %edx
divq %rcx
movq %rax, %rcx
movq -0x470(%rbp), %rax
cmpq %rcx, %rax
jae 0x411c
movq $0x0, -0x280(%rbp)
movq -0x8(%rbp), %rdi
movl -0x278(%rbp), %eax
imull -0x250(%rbp), %eax
movl %eax, %eax
movl %eax, %esi
callq 0x5690
movq %rax, -0x288(%rbp)
cmpl $0x8, -0x1c(%rbp)
jne 0x3fbd
movq -0x288(%rbp), %rax
movzbl (%rax), %eax
movq %rax, -0x280(%rbp)
jmp 0x400b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x248(%rbp)
movl %eax, -0x24c(%rbp)
jmp 0x45fa
cmpl $0x10, -0x1c(%rbp)
jne 0x3fd6
movq -0x288(%rbp), %rax
movzwl (%rax), %eax
movq %rax, -0x280(%rbp)
jmp 0x4009
cmpl $0x20, -0x1c(%rbp)
jne 0x3fee
movq -0x288(%rbp), %rax
movl (%rax), %eax
movq %rax, -0x280(%rbp)
jmp 0x4007
cmpl $0x40, -0x1c(%rbp)
jne 0x4005
movq -0x288(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x280(%rbp)
jmp 0x4007
jmp 0x4009
jmp 0x400b
movq -0x280(%rbp), %rsi
leaq -0x2a8(%rbp), %rdi
callq 0x56b0
jmp 0x4020
movl -0x274(%rbp), %eax
movq %rax, -0x478(%rbp)
leaq -0x2a8(%rbp), %rdi
callq 0x31d0
movq %rax, %rcx
movq -0x478(%rbp), %rax
addq %rcx, %rax
addq $0x2, %rax
cmpq $0x50, %rax
jb 0x4094
leaq 0x70a4(%rip), %rsi      # 0xb0fb
leaq -0x270(%rbp), %rdi
callq 0x34b0
jmp 0x4065
movl $0x4, -0x274(%rbp)
jmp 0x40c3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x248(%rbp)
movl %eax, -0x24c(%rbp)
leaq -0x2a8(%rbp), %rdi
callq 0x31b0
jmp 0x45fa
cmpl $0x0, -0x278(%rbp)
je 0x40c1
leaq 0x705e(%rip), %rsi      # 0xb102
leaq -0x270(%rbp), %rdi
callq 0x34b0
jmp 0x40b2
movl -0x274(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x274(%rbp)
jmp 0x40c3
leaq -0x2a8(%rbp), %rdi
movq %rdi, -0x480(%rbp)
callq 0x31d0
movq -0x480(%rbp), %rsi
movq %rax, %rcx
movl -0x274(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x274(%rbp)
leaq -0x270(%rbp), %rdi
callq 0x34d0
jmp 0x40fc
leaq -0x2a8(%rbp), %rdi
callq 0x31b0
movl -0x278(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x278(%rbp)
jmp 0x3f2c
movl -0x1c(%rbp), %esi
leaq -0x308(%rbp), %rdi
callq 0x5830
jmp 0x412d
leaq 0x6fd1(%rip), %rsi      # 0xb105
leaq -0x2e8(%rbp), %rdi
leaq -0x308(%rbp), %rdx
callq 0x57e0
jmp 0x4149
leaq 0x6fba(%rip), %rdx      # 0xb10a
leaq -0x2c8(%rbp), %rdi
leaq -0x2e8(%rbp), %rsi
callq 0x5790
jmp 0x4165
leaq -0x2e8(%rbp), %rdi
callq 0x31b0
leaq -0x308(%rbp), %rdi
callq 0x31b0
movq -0x18(%rbp), %rsi
leaq -0x328(%rbp), %rdi
callq 0x3110
jmp 0x418f
leaq -0x328(%rbp), %rax
movq %rax, -0x330(%rbp)
movq -0x330(%rbp), %rdi
callq 0x3230
movq %rax, -0x338(%rbp)
movq -0x330(%rbp), %rdi
callq 0x3190
movq %rax, -0x340(%rbp)
leaq -0x338(%rbp), %rdi
leaq -0x340(%rbp), %rsi
callq 0x5950
testb $0x1, %al
jne 0x41df
jmp 0x4274
leaq -0x338(%rbp), %rdi
callq 0x5990
movq %rax, -0x348(%rbp)
movq -0x348(%rbp), %rax
movsbl (%rax), %edi
callq 0x34a0
movb %al, %cl
movq -0x348(%rbp), %rax
movb %cl, (%rax)
leaq -0x338(%rbp), %rdi
callq 0x59b0
jmp 0x41c3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x248(%rbp)
movl %eax, -0x24c(%rbp)
jmp 0x424c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x248(%rbp)
movl %eax, -0x24c(%rbp)
leaq -0x2e8(%rbp), %rdi
callq 0x31b0
leaq -0x308(%rbp), %rdi
callq 0x31b0
jmp 0x45fa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x248(%rbp)
movl %eax, -0x24c(%rbp)
jmp 0x45ee
leaq -0x369(%rbp), %rdi
movq %rdi, -0x488(%rbp)
callq 0x3480
movq -0x488(%rbp), %rdx
leaq 0x6e79(%rip), %rsi      # 0xb10e
leaq -0x368(%rbp), %rdi
callq 0x59d0
jmp 0x42a3
leaq -0x369(%rbp), %rdi
callq 0x32b0
leaq -0x391(%rbp), %rdi
movq %rdi, -0x490(%rbp)
callq 0x3480
movq -0x490(%rbp), %rdx
leaq 0x6f02(%rip), %rsi      # 0xb1d2
leaq -0x390(%rbp), %rdi
callq 0x59d0
jmp 0x42de
leaq -0x368(%rbp), %rdi
leaq -0x390(%rbp), %rsi
leaq -0x2c8(%rbp), %rdx
callq 0x3830
jmp 0x42fa
leaq -0x390(%rbp), %rdi
callq 0x31b0
leaq -0x391(%rbp), %rdi
callq 0x32b0
leaq -0x3b9(%rbp), %rdi
movq %rdi, -0x498(%rbp)
callq 0x3480
movq -0x498(%rbp), %rdx
leaq 0x6ea2(%rip), %rsi      # 0xb1d5
leaq -0x3b8(%rbp), %rdi
callq 0x59d0
jmp 0x4341
leaq -0x368(%rbp), %rdi
leaq -0x3b8(%rbp), %rsi
leaq -0x270(%rbp), %rdx
callq 0x3830
jmp 0x435d
leaq -0x3b8(%rbp), %rdi
callq 0x31b0
leaq -0x3b9(%rbp), %rdi
callq 0x32b0
leaq -0x3e1(%rbp), %rdi
movq %rdi, -0x4a0(%rbp)
callq 0x3480
movq -0x4a0(%rbp), %rdx
leaq 0x6e42(%rip), %rsi      # 0xb1d8
leaq -0x3e0(%rbp), %rdi
callq 0x59d0
jmp 0x43a4
movq -0x18(%rbp), %rdx
leaq -0x368(%rbp), %rdi
leaq -0x3e0(%rbp), %rsi
callq 0x3830
jmp 0x43bd
leaq -0x3e0(%rbp), %rdi
callq 0x31b0
leaq -0x3e1(%rbp), %rdi
callq 0x32b0
leaq -0x409(%rbp), %rdi
movq %rdi, -0x4a8(%rbp)
callq 0x3480
movq -0x4a8(%rbp), %rdx
leaq 0x6de5(%rip), %rsi      # 0xb1db
leaq -0x408(%rbp), %rdi
callq 0x59d0
jmp 0x4404
leaq -0x368(%rbp), %rdi
leaq -0x408(%rbp), %rsi
leaq -0x328(%rbp), %rdx
callq 0x3830
jmp 0x4420
leaq -0x408(%rbp), %rdi
callq 0x31b0
leaq -0x409(%rbp), %rdi
callq 0x32b0
leaq -0x240(%rbp), %rdi
leaq -0x368(%rbp), %rsi
callq 0x3250
jmp 0x444d
leaq -0x368(%rbp), %rdi
callq 0x31b0
leaq -0x328(%rbp), %rdi
callq 0x31b0
leaq -0x2c8(%rbp), %rdi
callq 0x31b0
leaq -0x270(%rbp), %rdi
callq 0x31b0
leaq -0x240(%rbp), %rdi
callq 0x33b0
leaq -0x40(%rbp), %rdi
callq 0x31b0
addq $0x4b0, %rsp            # imm = 0x4B0
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x248(%rbp)
movl %eax, -0x24c(%rbp)
leaq -0x369(%rbp), %rdi
callq 0x32b0
jmp 0x45e2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x248(%rbp)
movl %eax, -0x24c(%rbp)
jmp 0x44f0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x248(%rbp)
movl %eax, -0x24c(%rbp)
leaq -0x390(%rbp), %rdi
callq 0x31b0
leaq -0x391(%rbp), %rdi
callq 0x32b0
jmp 0x45d6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x248(%rbp)
movl %eax, -0x24c(%rbp)
jmp 0x4533
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x248(%rbp)
movl %eax, -0x24c(%rbp)
leaq -0x3b8(%rbp), %rdi
callq 0x31b0
leaq -0x3b9(%rbp), %rdi
callq 0x32b0
jmp 0x45d6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x248(%rbp)
movl %eax, -0x24c(%rbp)
jmp 0x4576
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x248(%rbp)
movl %eax, -0x24c(%rbp)
leaq -0x3e0(%rbp), %rdi
callq 0x31b0
leaq -0x3e1(%rbp), %rdi
callq 0x32b0
jmp 0x45d6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x248(%rbp)
movl %eax, -0x24c(%rbp)
jmp 0x45b6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x248(%rbp)
movl %eax, -0x24c(%rbp)
leaq -0x408(%rbp), %rdi
callq 0x31b0
leaq -0x409(%rbp), %rdi
callq 0x32b0
jmp 0x45d6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x248(%rbp)
movl %eax, -0x24c(%rbp)
leaq -0x368(%rbp), %rdi
callq 0x31b0
leaq -0x328(%rbp), %rdi
callq 0x31b0
leaq -0x2c8(%rbp), %rdi
callq 0x31b0
leaq -0x270(%rbp), %rdi
callq 0x31b0
leaq -0x240(%rbp), %rdi
callq 0x33b0
leaq -0x40(%rbp), %rdi
callq 0x31b0
movq -0x248(%rbp), %rdi
callq 0x3470
nopw (%rax,%rax)