// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2f8, %rsp            # imm = 0x2F8
movl %ecx, %r12d
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, -0x58(%rbp)
leaq -0x120(%rbp), %r14
movq %r14, %rdi
callq 0x2270
leaq -0x320(%rbp), %rdi
pushq $0x10
popq %rdx
movq %r14, %rsi
callq 0x2370
leaq -0x2b0(%rbp), %rdi
callq 0x2060
testb %al, %al
je 0x2c35
pushq $0x8
popq %rcx
movl %r12d, %eax
cltd
idivl %ecx
movq -0x58(%rbp), %rdx
movq 0x8(%rdx), %rcx
subq (%rdx), %rcx
movl %eax, -0x30(%rbp)
movslq %eax, %rsi
movq %rcx, %rax
xorl %edx, %edx
movq %rsi, -0xc0(%rbp)
divq %rsi
testq %rdx, %rdx
jne 0x2ca0
movq %r15, -0xb0(%rbp)
leaq -0xf0(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq (%rcx,%rcx,4), %rsi
leaq -0x100(%rbp), %rdi
callq 0x22c0
movq %r12, -0xa8(%rbp)
leal -0x8(%r12), %eax
roll $0x1d, %eax
pushq $0x4
popq %r14
xorl %ebx, %ebx
movl %eax, -0x5c(%rbp)
movl %eax, %eax
movq %rax, -0xb8(%rbp)
leaq -0xe0(%rbp), %r13
xorl %r12d, %r12d
movl %r12d, %esi
movq -0x58(%rbp), %rax
movq (%rax), %rcx
movq 0x8(%rax), %rax
subq %rcx, %rax
xorl %edx, %edx
divq -0xc0(%rbp)
cmpq %rsi, %rax
jbe 0x2a65
xorl %esi, %esi
cmpl $0x7, -0x5c(%rbp)
ja 0x29e5
movl %ebx, %eax
movq -0xb8(%rbp), %rdx
leaq 0x263d(%rip), %rdi      # 0x5004
movslq (%rdi,%rdx,4), %rdx
addq %rdi, %rdx
jmpq *%rdx
movzbl (%rcx,%rax), %esi
jmp 0x29e5
movl (%rcx,%rax), %esi
jmp 0x29e5
movzwl (%rcx,%rax), %esi
jmp 0x29e5
movq (%rcx,%rax), %rsi
movq %r13, %rdi
callq 0x34a3
movl %r14d, %eax
movq -0xd8(%rbp), %rcx
addq %rcx, %rax
addq $-0x4e, %rax
cmpq $-0x51, %rax
ja 0x2a1d
leaq -0x100(%rbp), %rdi
leaq 0x26f4(%rip), %rsi      # 0x5106
callq 0x2360
pushq $0x4
popq %r14
jmp 0x2a39
testl %r12d, %r12d
je 0x2a39
leaq -0x100(%rbp), %rdi
leaq 0x26dd(%rip), %rsi      # 0x510d
callq 0x2360
addl $0x2, %r14d
movq -0xd8(%rbp), %r15
leaq -0x100(%rbp), %rdi
movq %r13, %rsi
callq 0x2380
addl %r15d, %r14d
movq %r13, %rdi
callq 0x2100
incl %r12d
addl -0x30(%rbp), %ebx
jmp 0x298c
leaq -0x80(%rbp), %rdi
movq -0xa8(%rbp), %rsi
callq 0x353c
leaq 0x2694(%rip), %rsi      # 0x5110
leaq -0xa0(%rbp), %rdi
leaq -0x80(%rbp), %rdx
callq 0x350e
movq -0xb0(%rbp), %r12
leaq 0x267b(%rip), %rdx      # 0x5115
leaq -0xe0(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x34e5
leaq -0xa0(%rbp), %rdi
callq 0x2100
leaq -0x80(%rbp), %rdi
callq 0x2100
leaq -0xa0(%rbp), %rdi
movq %r12, %rsi
callq 0x2270
movq -0xa0(%rbp), %rbx
movq -0x98(%rbp), %r14
xorl %r15d, %r15d
cmpq %r15, %r14
je 0x2afa
movsbl (%rbx,%r15), %edi
callq 0x2350
movb %al, (%rbx,%r15)
incq %r15
jmp 0x2ae2
leaq 0x2618(%rip), %rsi      # 0x5119
leaq -0x80(%rbp), %rdi
leaq -0x50(%rbp), %rdx
callq 0x359c
leaq 0x26c8(%rip), %rsi      # 0x51dd
leaq -0x50(%rbp), %rdi
leaq -0x29(%rbp), %rdx
callq 0x359c
leaq -0x80(%rbp), %rdi
leaq -0x50(%rbp), %rsi
leaq -0xe0(%rbp), %rdx
callq 0x2676
leaq -0x50(%rbp), %rdi
callq 0x2100
leaq 0x269a(%rip), %rsi      # 0x51e0
leaq -0x50(%rbp), %rdi
leaq -0x29(%rbp), %rdx
callq 0x359c
leaq -0x80(%rbp), %rdi
leaq -0x50(%rbp), %rsi
leaq -0x100(%rbp), %rdx
callq 0x2676
leaq -0x50(%rbp), %rdi
callq 0x2100
leaq 0x266c(%rip), %rsi      # 0x51e3
leaq -0x50(%rbp), %rdi
leaq -0x29(%rbp), %rdx
callq 0x359c
leaq -0x80(%rbp), %rdi
leaq -0x50(%rbp), %rsi
movq %r12, %rdx
callq 0x2676
leaq -0x50(%rbp), %rdi
callq 0x2100
leaq 0x2642(%rip), %rsi      # 0x51e6
leaq -0x50(%rbp), %rdi
leaq -0x29(%rbp), %rdx
callq 0x359c
leaq -0x80(%rbp), %rdi
leaq -0x50(%rbp), %rsi
leaq -0xa0(%rbp), %rdx
callq 0x2676
leaq -0x50(%rbp), %rdi
callq 0x2100
leaq -0x320(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x2190
leaq -0x80(%rbp), %rdi
callq 0x2100
leaq -0xa0(%rbp), %rdi
callq 0x2100
leaq -0xe0(%rbp), %rdi
callq 0x2100
leaq -0x100(%rbp), %rdi
callq 0x2100
leaq -0x320(%rbp), %rdi
callq 0x2290
leaq -0x120(%rbp), %rdi
callq 0x2100
addq $0x2f8, %rsp            # imm = 0x2F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x53b4(%rip), %rdi      # 0x7ff0
leaq 0x2433(%rip), %rsi      # 0x5076
callq 0x21b0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x2190
leaq 0x2655(%rip), %rsi      # 0x52af
movq %rax, %rdi
callq 0x21b0
movq 0x5387(%rip), %rdi      # 0x7ff0
leaq 0x23d9(%rip), %rsi      # 0x5049
callq 0x21b0
movq %rax, %rbx
callq 0x2030
movl (%rax), %edi
callq 0x20a0
movq %rbx, %rdi
movq %rax, %rsi
callq 0x21b0
leaq 0x2619(%rip), %rsi      # 0x52af
movq %rax, %rdi
callq 0x21b0
jmp 0x2d15
movq 0x5349(%rip), %rdi      # 0x7ff0
leaq 0x23ee(%rip), %rsi      # 0x509c
callq 0x21b0
movq 0x5336(%rip), %rdi      # 0x7ff0
leaq 0x241e(%rip), %rsi      # 0x50df
callq 0x21b0
movq -0x58(%rbp), %rcx
movq 0x8(%rcx), %rsi
subq (%rcx), %rsi
movq %rax, %rdi
callq 0x2140
leaq 0x240c(%rip), %rsi      # 0x50ec
movq %rax, %rdi
callq 0x21b0
movq 0x5301(%rip), %rdi      # 0x7ff0
leaq 0x23fe(%rip), %rsi      # 0x50f4
callq 0x21b0
movq %rax, %rdi
movl -0x30(%rbp), %esi
callq 0x2300
leaq 0x23df(%rip), %rsi      # 0x50ec
movq %rax, %rdi
callq 0x21b0
pushq $0x1
popq %rdi
callq 0x2200
jmp 0x2d1f
movq %rax, %rbx
jmp 0x2da7
jmp 0x2d43
jmp 0x2d35
jmp 0x2d43
jmp 0x2d35
jmp 0x2d43
jmp 0x2d35
jmp 0x2d43
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
callq 0x2100
jmp 0x2d46
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0x2100
leaq -0xa0(%rbp), %rdi
callq 0x2100
jmp 0x2d8f
movq %rax, %rbx
jmp 0x2d4f
jmp 0x2d8c
movq %rax, %rbx
leaq -0xa0(%rbp), %rdi
callq 0x2100
jmp 0x2d78
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
jmp 0x2d96
jmp 0x2d87
jmp 0x2d87
movq %rax, %rbx
jmp 0x2db3
movq %rax, %rbx
jmp 0x2d9b
movq %rax, %rbx
leaq -0xe0(%rbp), %rdi
callq 0x2100
leaq -0x100(%rbp), %rdi
callq 0x2100
leaq -0x320(%rbp), %rdi
callq 0x2290
leaq -0x120(%rbp), %rdi
callq 0x2100
movq %rbx, %rdi
callq 0x2330