// File: nyorain[P]bintoheader/bintoheader.cpp
// Function: main
int main(int argc, char** argv)
{
	// parse args
	const flags::args args(argc, argv);

	// help
	auto help = args.get<bool>("help", false);
	if(help) {
		std::cout << "Transforms a file into a c/cpp header holding its binary data as array.\n";
		std::cout << "Valid Usages:\n\n";
		std::cout << "\tbintoheader <INPUT> [<OUTPUT>] [<ARGS>]\n";
		std::cout << "\tbintoheader <ARGS>\n\n";
		std::cout << "The following arguments are recognized:\n\n";
		std::cout << "\t-i\tThe input file\n";
		std::cout << "\t-o\tThe output file [defaulted to '<input>.h']\n";
		std::cout << "\t-name\tThe name of the c array\n";
		std::cout << "\t\tDefaulted to '<input>.h' with path stripped all '.' replaced by '.'\n";
		std::cout << "\t-size\tThe size of the array values in bits. Defaulted to 8.\n";
		std::cout << "\t\tThe file size must be a multiple of the size value\n";
		std::cout << "\t-help\tShow this error message\n\n";
		std::cout << "The program is released into the public domain\n";
		std::cout << "A copy of the source can be obtained under\n";
		std::cout << "https://github.com/nyorain/bintoheader\n";
		return 0;
	}

	// input, output
	auto input = args.get<std::string>("i", {});
	auto output = args.get<std::string>("o", {});
	const auto& pos = args.positional();

	auto current = 0u;
	if(input.empty() && pos.size() > current)
		input = pos[current++];

	if(output.empty() && pos.size() > current)
		output = pos[current++];

	if(output.empty())
		output = input + ".h";

	if(input.empty()) {
		std::cout << "Must give a valid input file\n";
		std::cout << "Use -h for help\n";
		return 2;
	}

	if(output.empty()) {
		std::cout << "Invalid output file name\n";
		std::cout << "Use -h for help\n";
		return 3;
	}

	// name, size
	auto name = args.get<std::string>("name", {});
	if(name.empty()) {
		name = input + "_data";
		stripPath(name);
		replace(name, ".", "_");
	}

	if(name.empty()) {
		std::cout << "Empty array name is invalid!\n";
		std::cout << "Use -h for help\n";
		return 4;
	}

	auto size = args.get<unsigned int>("size", 8);
	if(size != 8 && size != 16 && size != 32 && size != 64) {
		std::cout << "Invalid size parameter\n";
		std::cout << "Must be one of {8, 16, 32, 64}\n";
		std::cout << "Use -h for help\n";
		return 5;
	}

	auto data = loadFile(input);
	outputData(data, output, name, size);
	std::cout << "Succesfully created " << name << ": " << data.size () << " bytes\n";
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x310, %rsp            # imm = 0x310
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl -0x8(%rbp), %esi
movq -0x10(%rbp), %rdx
leaq -0x78(%rbp), %rdi
movq %rdi, -0x280(%rbp)
callq 0x5a80
leaq 0x6b77(%rip), %rsi      # 0xb1de
leaq -0x90(%rbp), %rdi
movq %rdi, -0x278(%rbp)
callq 0x5af0
movq -0x280(%rbp), %rdi
movq -0x278(%rbp), %rsi
movb $0x0, -0x91(%rbp)
leaq -0x91(%rbp), %rdx
callq 0x5ab0
movb %al, -0x269(%rbp)
jmp 0x46a3
movb -0x269(%rbp), %al
andb $0x1, %al
movb %al, -0x79(%rbp)
testb $0x1, -0x79(%rbp)
je 0x4820
movq 0xc911(%rip), %rdi      # 0x10fd0
leaq 0x6b1d(%rip), %rsi      # 0xb1e3
callq 0x3270
jmp 0x46cd
movq 0xc8fc(%rip), %rdi      # 0x10fd0
leaq 0x6b51(%rip), %rsi      # 0xb22c
callq 0x3270
jmp 0x46e2
movq 0xc8e7(%rip), %rdi      # 0x10fd0
leaq 0x6b4c(%rip), %rsi      # 0xb23c
callq 0x3270
jmp 0x46f7
movq 0xc8d2(%rip), %rdi      # 0x10fd0
leaq 0x6b61(%rip), %rsi      # 0xb266
callq 0x3270
jmp 0x470c
movq 0xc8bd(%rip), %rdi      # 0x10fd0
leaq 0x6b62(%rip), %rsi      # 0xb27c
callq 0x3270
jmp 0x4721
movq 0xc8a8(%rip), %rdi      # 0x10fd0
leaq 0x6b77(%rip), %rsi      # 0xb2a6
callq 0x3270
jmp 0x4736
movq 0xc893(%rip), %rdi      # 0x10fd0
leaq 0x6b76(%rip), %rsi      # 0xb2ba
callq 0x3270
jmp 0x474b
movq 0xc87e(%rip), %rdi      # 0x10fd0
leaq 0x6b91(%rip), %rsi      # 0xb2ea
callq 0x3270
jmp 0x4760
movq 0xc869(%rip), %rdi      # 0x10fd0
leaq 0x6b9c(%rip), %rsi      # 0xb30a
callq 0x3270
jmp 0x4775
movq 0xc854(%rip), %rdi      # 0x10fd0
leaq 0x6bce(%rip), %rsi      # 0xb351
callq 0x3270
jmp 0x478a
movq 0xc83f(%rip), %rdi      # 0x10fd0
leaq 0x6bf7(%rip), %rsi      # 0xb38f
callq 0x3270
jmp 0x479f
movq 0xc82a(%rip), %rdi      # 0x10fd0
leaq 0x6c18(%rip), %rsi      # 0xb3c5
callq 0x3270
jmp 0x47b4
movq 0xc815(%rip), %rdi      # 0x10fd0
leaq 0x6c24(%rip), %rsi      # 0xb3e6
callq 0x3270
jmp 0x47c9
movq 0xc800(%rip), %rdi      # 0x10fd0
leaq 0x6c3f(%rip), %rsi      # 0xb416
callq 0x3270
jmp 0x47de
movq 0xc7eb(%rip), %rdi      # 0x10fd0
leaq 0x6c56(%rip), %rsi      # 0xb442
callq 0x3270
jmp 0x47f3
movl $0x0, -0x4(%rbp)
movl $0x1, -0xa8(%rbp)
jmp 0x4f3a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
jmp 0x4f91
leaq 0x69b5(%rip), %rsi      # 0xb1dc
leaq -0xd8(%rbp), %rdi
movq %rdi, -0x290(%rbp)
callq 0x5af0
leaq -0xf8(%rbp), %rdi
movq %rdi, -0x288(%rbp)
callq 0x3380
movq -0x290(%rbp), %rdx
movq -0x288(%rbp), %rcx
leaq -0xc8(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x5b40
jmp 0x486d
leaq -0xf8(%rbp), %rdi
callq 0x31b0
leaq 0x6cb8(%rip), %rsi      # 0xb538
leaq -0x128(%rbp), %rdi
movq %rdi, -0x2a0(%rbp)
callq 0x5af0
leaq -0x148(%rbp), %rdi
movq %rdi, -0x298(%rbp)
callq 0x3380
movq -0x2a0(%rbp), %rdx
movq -0x298(%rbp), %rcx
leaq -0x118(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x5b40
jmp 0x48c6
leaq -0x148(%rbp), %rdi
callq 0x31b0
leaq -0x78(%rbp), %rdi
callq 0x5bc0
movq %rax, -0x2a8(%rbp)
jmp 0x48e4
movq -0x2a8(%rbp), %rax
movq %rax, -0x150(%rbp)
movl $0x0, -0x154(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x3430
testb $0x1, %al
jne 0x4911
jmp 0x49ba
movq -0x150(%rbp), %rdi
callq 0x5be0
movl -0x154(%rbp), %ecx
cmpq %rcx, %rax
jbe 0x49ba
movq -0x150(%rbp), %rdi
movl -0x154(%rbp), %eax
movl %eax, %esi
movl %esi, %eax
incl %eax
movl %eax, -0x154(%rbp)
callq 0x5c00
movq %rax, %rsi
leaq -0xc8(%rbp), %rdi
callq 0x5c20
jmp 0x495b
jmp 0x49ba
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0x31b0
jmp 0x4f91
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
leaq -0x148(%rbp), %rdi
callq 0x31b0
jmp 0x4f85
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
jmp 0x4f79
leaq -0x118(%rbp), %rdi
callq 0x3430
testb $0x1, %al
jne 0x49cc
jmp 0x4a14
movq -0x150(%rbp), %rdi
callq 0x5be0
movl -0x154(%rbp), %ecx
cmpq %rcx, %rax
jbe 0x4a14
movq -0x150(%rbp), %rdi
movl -0x154(%rbp), %eax
movl %eax, %esi
movl %esi, %eax
incl %eax
movl %eax, -0x154(%rbp)
callq 0x5c00
movq %rax, %rsi
leaq -0x118(%rbp), %rdi
callq 0x5c20
jmp 0x4a12
jmp 0x4a14
leaq -0x118(%rbp), %rdi
callq 0x3430
testb $0x1, %al
jne 0x4a26
jmp 0x4a61
leaq 0x6a3d(%rip), %rdx      # 0xb46a
leaq -0x178(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x5c50
jmp 0x4a42
leaq -0x118(%rbp), %rdi
leaq -0x178(%rbp), %rsi
callq 0x3310
leaq -0x178(%rbp), %rdi
callq 0x31b0
leaq -0xc8(%rbp), %rdi
callq 0x3430
testb $0x1, %al
jne 0x4a73
jmp 0x4ab3
movq 0xc556(%rip), %rdi      # 0x10fd0
leaq 0x69ec(%rip), %rsi      # 0xb46d
callq 0x3270
jmp 0x4a88
movq 0xc541(%rip), %rdi      # 0x10fd0
leaq 0x69f5(%rip), %rsi      # 0xb48b
callq 0x3270
jmp 0x4a9d
movl $0x2, -0x4(%rbp)
movl $0x1, -0xa8(%rbp)
jmp 0x4f22
leaq -0x118(%rbp), %rdi
callq 0x3430
testb $0x1, %al
jne 0x4ac5
jmp 0x4b05
movq 0xc504(%rip), %rdi      # 0x10fd0
leaq 0x69c9(%rip), %rsi      # 0xb49c
callq 0x3270
jmp 0x4ada
movq 0xc4ef(%rip), %rdi      # 0x10fd0
leaq 0x69a3(%rip), %rsi      # 0xb48b
callq 0x3270
jmp 0x4aef
movl $0x3, -0x4(%rbp)
movl $0x1, -0xa8(%rbp)
jmp 0x4f22
leaq 0x69aa(%rip), %rsi      # 0xb4b6
leaq -0x1a8(%rbp), %rdi
movq %rdi, -0x2b8(%rbp)
callq 0x5af0
leaq -0x1c8(%rbp), %rdi
movq %rdi, -0x2b0(%rbp)
callq 0x3380
movq -0x2b8(%rbp), %rdx
movq -0x2b0(%rbp), %rcx
leaq -0x198(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x5b40
jmp 0x4b52
leaq -0x1c8(%rbp), %rdi
callq 0x31b0
leaq -0x198(%rbp), %rdi
callq 0x3430
testb $0x1, %al
jne 0x4b73
jmp 0x4d22
leaq 0x6941(%rip), %rdx      # 0xb4bb
leaq -0x1e8(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x5c50
jmp 0x4b8f
leaq -0x198(%rbp), %rdi
movq %rdi, -0x2c0(%rbp)
leaq -0x1e8(%rbp), %rsi
movq %rsi, -0x2c8(%rbp)
callq 0x3310
movq -0x2c8(%rbp), %rdi
callq 0x31b0
movq -0x2c0(%rbp), %rdi
callq 0x38c0
jmp 0x4bca
leaq -0x209(%rbp), %rdi
movq %rdi, -0x2d0(%rbp)
callq 0x3480
movq -0x2d0(%rbp), %rdx
leaq 0x68d6(%rip), %rsi      # 0xb4c1
leaq -0x208(%rbp), %rdi
callq 0x59d0
jmp 0x4bf9
leaq -0x231(%rbp), %rdi
movq %rdi, -0x2d8(%rbp)
callq 0x3480
movq -0x2d8(%rbp), %rdx
leaq 0x68a9(%rip), %rsi      # 0xb4c3
leaq -0x230(%rbp), %rdi
callq 0x59d0
jmp 0x4c28
leaq -0x198(%rbp), %rdi
leaq -0x208(%rbp), %rsi
leaq -0x230(%rbp), %rdx
callq 0x3830
jmp 0x4c44
leaq -0x230(%rbp), %rdi
callq 0x31b0
leaq -0x231(%rbp), %rdi
callq 0x32b0
leaq -0x208(%rbp), %rdi
callq 0x31b0
leaq -0x209(%rbp), %rdi
callq 0x32b0
jmp 0x4d22
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
leaq -0x1c8(%rbp), %rdi
callq 0x31b0
jmp 0x4f79
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
jmp 0x4f6d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
jmp 0x4d11
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
jmp 0x4cf9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
leaq -0x230(%rbp), %rdi
callq 0x31b0
leaq -0x231(%rbp), %rdi
callq 0x32b0
leaq -0x208(%rbp), %rdi
callq 0x31b0
leaq -0x209(%rbp), %rdi
callq 0x32b0
jmp 0x4f6d
leaq -0x198(%rbp), %rdi
callq 0x3430
testb $0x1, %al
jne 0x4d34
jmp 0x4d74
movq 0xc295(%rip), %rdi      # 0x10fd0
leaq 0x6783(%rip), %rsi      # 0xb4c5
callq 0x3270
jmp 0x4d49
movq 0xc280(%rip), %rdi      # 0x10fd0
leaq 0x6734(%rip), %rsi      # 0xb48b
callq 0x3270
jmp 0x4d5e
movl $0x4, -0x4(%rbp)
movl $0x1, -0xa8(%rbp)
jmp 0x4f16
leaq 0x6768(%rip), %rsi      # 0xb4e3
leaq -0x248(%rbp), %rdi
movq %rdi, -0x2e8(%rbp)
callq 0x5af0
movq -0x2e8(%rbp), %rsi
movl $0x8, -0x24c(%rbp)
leaq -0x78(%rbp), %rdi
leaq -0x24c(%rbp), %rdx
callq 0x5cd0
movl %eax, -0x2dc(%rbp)
jmp 0x4db7
movl -0x2dc(%rbp), %eax
movl %eax, -0x238(%rbp)
cmpl $0x8, -0x238(%rbp)
je 0x4e3c
cmpl $0x10, -0x238(%rbp)
je 0x4e3c
cmpl $0x20, -0x238(%rbp)
je 0x4e3c
cmpl $0x40, -0x238(%rbp)
je 0x4e3c
movq 0xc1e2(%rip), %rdi      # 0x10fd0
leaq 0x66f3(%rip), %rsi      # 0xb4e8
callq 0x3270
jmp 0x4dfc
movq 0xc1cd(%rip), %rdi      # 0x10fd0
leaq 0x66f6(%rip), %rsi      # 0xb500
callq 0x3270
jmp 0x4e11
movq 0xc1b8(%rip), %rdi      # 0x10fd0
leaq 0x666c(%rip), %rsi      # 0xb48b
callq 0x3270
jmp 0x4e26
movl $0x5, -0x4(%rbp)
movl $0x1, -0xa8(%rbp)
jmp 0x4f16
leaq -0x268(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x3910
jmp 0x4e51
movl -0x238(%rbp), %ecx
leaq -0x268(%rbp), %rdi
leaq -0x118(%rbp), %rsi
leaq -0x198(%rbp), %rdx
callq 0x3cb0
jmp 0x4e73
movq 0xc156(%rip), %rdi      # 0x10fd0
leaq 0x669f(%rip), %rsi      # 0xb520
callq 0x3270
movq %rax, -0x2f0(%rbp)
jmp 0x4e8f
movq -0x2f0(%rbp), %rdi
leaq -0x198(%rbp), %rsi
callq 0x3250
movq %rax, -0x2f8(%rbp)
jmp 0x4eab
movq -0x2f8(%rbp), %rdi
leaq 0x623f(%rip), %rsi      # 0xb0f8
callq 0x3270
movq %rax, -0x300(%rbp)
jmp 0x4ec7
leaq -0x268(%rbp), %rdi
callq 0x5670
movq -0x300(%rbp), %rdi
movq %rax, %rsi
callq 0x3080
movq %rax, -0x308(%rbp)
jmp 0x4eeb
movq -0x308(%rbp), %rdi
leaq 0x61e8(%rip), %rsi      # 0xb0e1
callq 0x3270
jmp 0x4f00
leaq -0x268(%rbp), %rdi
callq 0x5610
movl $0x0, -0xa8(%rbp)
leaq -0x198(%rbp), %rdi
callq 0x31b0
leaq -0x118(%rbp), %rdi
callq 0x31b0
leaq -0xc8(%rbp), %rdi
callq 0x31b0
leaq -0x78(%rbp), %rdi
callq 0x5d10
movl -0x4(%rbp), %eax
addq $0x310, %rsp            # imm = 0x310
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
leaq -0x268(%rbp), %rdi
callq 0x5610
leaq -0x198(%rbp), %rdi
callq 0x31b0
leaq -0x118(%rbp), %rdi
callq 0x31b0
leaq -0xc8(%rbp), %rdi
callq 0x31b0
leaq -0x78(%rbp), %rdi
callq 0x5d10
movq -0xa0(%rbp), %rdi
callq 0x3470
nopw %cs:(%rax,%rax)