// File: /workspace/github_cpp_makefile_star2_O2/linuxmao-org[P]VL1-emulator/utils/sources/rescc.cpp
// Function: main
int main(int argc, char *argv[])
{
	if (argc != 2)
	{
		fprintf(stderr, "Please indicate the path of Res.json.\n");
		return 1;
	}

	FILE *res_out = stdout;

	std::string res_dict_filename = argv[1];
	nlohmann::json res_dict;

	if (!load_json(res_dict_filename, res_dict))
	{
		fprintf(stderr, "Cannot load the resource dictionary as JSON.\n");
		return 1;
	}

	std::string res_dict_dirname = res_dict_filename;
	while (!res_dict_dirname.empty() && !is_path_separator(res_dict_dirname.back()))
		res_dict_dirname.pop_back();
	if (res_dict_dirname.empty())
		res_dict_dirname = "./";

	std::unordered_map<unsigned, unsigned> res_lengths;

	for (const auto &item : res_dict.items())
	{
		unsigned res_id = std::stoi(item.key());
		std::string res_file = item.value()["file"];
		std::string res_path = res_dict_dirname + res_file;

		fprintf(res_out, "static const unsigned char res_%u[] = {", res_id);
		unsigned res_length = 0;
		FILE_u res_in(fopen(res_path.c_str(), "rb"));
		if (!res_in)
		{
			fprintf(stderr, "Cannot open the resource file: %s.\n", res_path.c_str());
			return 1;
		}

		char buf[8192];
		for (size_t count; (count = fread(buf, 1, sizeof(buf), res_in.get())) > 0;)
		{
			res_length += count;
			for (size_t i = 0; i < count; ++i)
				fprintf(res_out, "%d,", (unsigned char)buf[i]);
		}

		if (ferror(res_in.get()))
		{
			fprintf(stderr, "Cannot read the resource file: %s.\n", res_path.c_str());
			return 1;
		}
		res_in.reset();

		fprintf(res_out, "};\n");
		res_lengths[res_id] = res_length;
	}

	fprintf(
		res_out, "struct Resource\n{\n"
		"\t" "unsigned int id;" "\n"
		"\t" "unsigned int type;" "\n"
		"\t" "const unsigned char *data;" "\n"
		"\t" "unsigned int size;" "\n"
		"};\n");

	fprintf(res_out, "extern const Resource RES[] =\n{\n");
	for (const auto &item : res_dict.items())
	{
		unsigned res_id = std::stoi(item.key());
		std::string res_type = item.value()["type"];
		std::string res_file = item.value()["file"];
		unsigned res_length = res_lengths.at(res_id);

		while (res_type.size() < 4)
			res_type += ' ';
		if (res_type.size() != 4)
		{
			fprintf(stderr, "The resource type is invalid: %s\n", res_type.c_str());
			return 1;
		}

		fprintf(
			res_out, "\t" "{%u, '%s', res_%u, %u}, /* %s */\n",
			res_id, res_type.c_str(), res_id, res_length, res_file.c_str());
	}

	fprintf(res_out, "};\n");
	fprintf(res_out, "extern const unsigned int NRES = %lu;\n", res_dict.size());

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x21e8, %rsp           # imm = 0x21E8
cmpl $0x2, %edi
jne 0x3b61
movq 0x13a4b(%rip), %rax     # 0x16fb8
movq (%rax), %rbx
movq 0x8(%rsi), %rsi
leaq 0x150(%rsp), %r14
leaq 0x1e0(%rsp), %rdx
movq %r14, %rdi
callq 0x3cf0
leaq 0x38(%rsp), %rdi
xorl %esi, %esi
callq 0x3d2c
movq (%r14), %rdi
leaq 0xa05f(%rip), %rsi      # 0xd601
callq 0x3280
movq %rax, 0xe0(%rsp)
testq %rax, %rax
je 0x3b8f
movq %rax, %r15
pushq $0x2
popq %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0x3260
testl %eax, %eax
jne 0x3b8f
movq %r15, %rdi
callq 0x3030
movq %rax, %r14
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3260
testl %eax, %eax
jne 0x3b8f
movq %r14, %rdi
callq 0x3040
movq %rax, 0xa8(%rsp)
pushq $0x1
popq %rdx
movq %rax, %rdi
movq %r14, %rsi
movq %r15, %rcx
callq 0x3350
cmpq $0x1, %rax
jne 0x3b82
leaq 0xe0(%rsp), %rdi
xorl %esi, %esi
callq 0xc666
movq 0xa8(%rsp), %rsi
addq %rsi, %r14
xorps %xmm0, %xmm0
leaq 0x1e0(%rsp), %rcx
movaps %xmm0, (%rcx)
movaps %xmm0, 0x10(%rcx)
leaq 0x170(%rsp), %rdi
pushq $0x1
popq %r8
movq %r14, %rdx
callq 0x4016
leaq 0x38(%rsp), %r15
leaq 0x170(%rsp), %r14
movq %r15, %rdi
movq %r14, %rsi
callq 0x4110
movq %r14, %rdi
callq 0x3ffc
leaq 0x1e0(%rsp), %rdi
callq 0x4150
leaq 0xa8(%rsp), %rdi
callq 0x416c
leaq 0xe0(%rsp), %rdi
callq 0x3f8a
leaq 0x48(%rsp), %rdi
leaq 0x150(%rsp), %rsi
callq 0x3300
leaq 0x48(%rsp), %r14
movq 0x50(%rsp), %rax
testq %rax, %rax
je 0x36d8
movq 0x48(%rsp), %rcx
cmpb $0x2f, -0x1(%rcx,%rax)
je 0x36e9
movq %r14, %rdi
callq 0x3290
jmp 0x36b8
leaq 0x9ef2(%rip), %rsi      # 0xd5d1
leaq 0x48(%rsp), %rdi
callq 0x3410
leaq 0xd8(%rsp), %rax
movq %rax, -0x30(%rax)
movq $0x1, -0x28(%rax)
xorps %xmm0, %xmm0
movups %xmm0, -0x20(%rax)
movl $0x3f800000, -0x10(%rax) # imm = 0x3F800000
movups %xmm0, -0x8(%rax)
leaq 0x30(%rsp), %r14
movq %r15, (%r14)
leaq 0x170(%rsp), %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0x3d48
leaq 0xe0(%rsp), %rdi
movq %r14, %rsi
callq 0x3d78
leaq 0x88(%rsp), %rbp
pushq $0x1
popq %r15
leaq 0x9edb(%rip), %r14      # 0xd628
movl $0x0, 0x18(%rsp)
movq %r13, %rdi
leaq 0xe0(%rsp), %rsi
callq 0x892c
movl %eax, %r12d
testb %al, %al
je 0x3925
movq %r13, %rdi
callq 0x3dc4
movq %rax, %rdi
xorl %esi, %esi
pushq $0xa
popq %rdx
callq 0x3da8
movl %eax, 0x2c(%rsp)
movq %r13, %rdi
callq 0xc7c6
movq %rax, %rdi
leaq 0x9e39(%rip), %rsi      # 0xd5d4
callq 0x3e30
movq %rbp, %rdi
movq %rax, %rsi
callq 0xcafe
leaq 0x68(%rsp), %rdi
leaq 0x48(%rsp), %rsi
movq %rbp, %rdx
callq 0x3f51
movb %r12b, 0x1c(%rsp)
movl 0x2c(%rsp), %edx
movq %rbx, %rdi
leaq 0x9e09(%rip), %rsi      # 0xd5d9
xorl %eax, %eax
callq 0x3320
movq 0x68(%rsp), %rdi
leaq 0x9e1e(%rip), %rsi      # 0xd601
callq 0x3280
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0x38ce
movq %rax, %rcx
xorl %ebp, %ebp
movl $0x2000, %edx           # imm = 0x2000
leaq 0x1e0(%rsp), %rdi
movq %r15, %rsi
callq 0x3350
testq %rax, %rax
je 0x3845
movq %rax, %r12
xorl %r13d, %r13d
cmpq %r13, %r12
je 0x383b
movzbl 0x1e0(%rsp,%r13), %edx
movq %rbx, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x3320
incq %r13
jmp 0x381b
addl %r12d, %ebp
movq 0x20(%rsp), %rcx
jmp 0x37fb
movq 0x20(%rsp), %rdi
callq 0x33f0
testl %eax, %eax
sete %r12b
jne 0x38f7
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
callq 0xc666
leaq 0x9e4a(%rip), %rdi      # 0xd6b8
pushq $0x3
popq %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x3430
leaq 0xa8(%rsp), %rdi
leaq 0x2c(%rsp), %rsi
callq 0xcc06
movl %ebp, (%rax)
leaq 0x170(%rsp), %r13
leaq 0x88(%rsp), %rbp
leaq 0x20(%rsp), %rdi
callq 0x3f8a
leaq 0x68(%rsp), %rdi
callq 0x3120
movq %rbp, %rdi
callq 0x3120
testb %r12b, %r12b
je 0x3920
movq %r13, %rdi
callq 0x3fa2
jmp 0x3755
movq 0x13703(%rip), %rax     # 0x16fd8
movq (%rax), %rdi
movq 0x68(%rsp), %rdx
xorl %r12d, %r12d
leaq 0x9d1d(%rip), %rsi      # 0xd604
xorl %eax, %eax
callq 0x3320
pushq $0x1
popq %rax
movl %eax, 0x18(%rsp)
jmp 0x38a0
movq 0x136da(%rip), %rax     # 0x16fd8
movq (%rax), %rdi
movq 0x68(%rsp), %rdx
leaq 0x9d1f(%rip), %rsi      # 0xd62c
xorl %eax, %eax
callq 0x3320
pushq $0x1
popq %rax
movl %eax, 0x18(%rsp)
jmp 0x3890
movb 0x1c(%rsp), %r12b
leaq 0xe0(%rsp), %rdi
callq 0x3fb4
leaq 0x170(%rsp), %rdi
callq 0x3fb4
testb %r12b, %r12b
jne 0x3b44
leaq 0x9d01(%rip), %rdi      # 0xd650
pushq $0x6b
popq %rsi
pushq $0x1
popq %r14
movq %r14, %rdx
movq %rbx, %rcx
callq 0x3430
leaq 0x9d54(%rip), %rdi      # 0xd6bc
pushq $0x20
popq %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x3430
leaq 0x68(%rsp), %r15
leaq 0x38(%rsp), %rax
movq %rax, (%r15)
leaq 0x1e0(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x3d48
leaq 0x170(%rsp), %rdi
movq %r15, %rsi
callq 0x3d78
leaq 0xe0(%rsp), %r13
leaq 0x88(%rsp), %r12
pushq $0x20
popq %rbp
movq %r14, %rdi
leaq 0x170(%rsp), %rsi
callq 0x892c
movb %al, 0x17(%rsp)
testb %al, %al
je 0x3ae8
movq %r14, %rdi
callq 0x3dc4
movq %rax, %rdi
xorl %esi, %esi
pushq $0xa
popq %rdx
callq 0x3da8
movl %eax, 0x30(%rsp)
movq %r14, %rdi
callq 0xc7c6
movq %rax, %rdi
leaq 0x9cdd(%rip), %rsi      # 0xd6dd
callq 0x3e30
movq %r13, %rdi
movq %rax, %rsi
callq 0xcafe
movq %r14, %rdi
callq 0xc7c6
movq %rax, %rdi
leaq 0x9bb2(%rip), %rsi      # 0xd5d4
callq 0x3e30
movq %r12, %rdi
movq %rax, %rsi
callq 0xcafe
movq %r14, %r15
leaq 0xa8(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0xcf6c
movl (%rax), %eax
movl %eax, 0x1c(%rsp)
movq 0xe8(%rsp), %r14
cmpq $0x3, %r14
ja 0x3a67
movq %r13, %rdi
movl %ebp, %esi
callq 0x3200
jmp 0x3a4d
movq 0xe0(%rsp), %rcx
cmpq $0x4, %r14
jne 0x3ac4
movl 0x30(%rsp), %edx
movq 0x88(%rsp), %rax
movq %rax, (%rsp)
movq %rbx, %rdi
leaq 0x9c75(%rip), %rsi      # 0xd704
movl %edx, %r8d
movl 0x1c(%rsp), %r9d
xorl %eax, %eax
callq 0x3320
movq %r12, %rdi
callq 0x3120
movq %r13, %rdi
callq 0x3120
cmpq $0x4, %r14
jne 0x3ae8
movq %r15, %r14
movq %r15, %rdi
callq 0x3fa2
jmp 0x39b9
movq 0x1350d(%rip), %rax     # 0x16fd8
movq (%rax), %rdi
leaq 0x9c0d(%rip), %rsi      # 0xd6e2
movq %rcx, %rdx
xorl %eax, %eax
callq 0x3320
pushq $0x1
popq %rax
movl %eax, 0x18(%rsp)
jmp 0x3a9e
leaq 0x170(%rsp), %rdi
callq 0x3fb4
leaq 0x1e0(%rsp), %rdi
callq 0x3fb4
cmpb $0x0, 0x17(%rsp)
jne 0x3b44
leaq 0x9ba8(%rip), %rdi      # 0xd6b8
pushq $0x3
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x3430
leaq 0x38(%rsp), %rdi
callq 0x3fce
leaq 0x9bf8(%rip), %rsi      # 0xd727
movl $0x0, 0x18(%rsp)
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x3320
leaq 0xa8(%rsp), %rdi
callq 0xc21e
leaq 0x48(%rsp), %rdi
callq 0x3120
movl 0x18(%rsp), %ebx
jmp 0x3bbb
pushq $0x1
popq %rbx
movq 0x1346d(%rip), %rax     # 0x16fd8
movq (%rax), %rcx
leaq 0x9a07(%rip), %rdi      # 0xd57c
pushq $0x26
popq %rsi
pushq $0x1
popq %rdx
callq 0x3430
jmp 0x3bd2
leaq 0xa8(%rsp), %rdi
callq 0x416c
leaq 0xe0(%rsp), %rdi
callq 0x3f8a
movq 0x13435(%rip), %rax     # 0x16fd8
movq (%rax), %rcx
leaq 0x99f6(%rip), %rdi      # 0xd5a3
pushq $0x2d
popq %rsi
pushq $0x1
popq %rdx
callq 0x3430
pushq $0x1
popq %rbx
leaq 0x38(%rsp), %rdi
callq 0x3ffc
leaq 0x150(%rsp), %rdi
callq 0x3120
movl %ebx, %eax
addq $0x21e8, %rsp           # imm = 0x21E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x3cc7
movq %rax, %rbx
jmp 0x3cd1
movq %rax, %rbx
leaq 0x1e0(%rsp), %rdi
callq 0x4150
leaq 0xa8(%rsp), %rdi
callq 0x416c
jmp 0x3c18
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0x3f8a
jmp 0x3cd1
jmp 0x3c83
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x3f8a
leaq 0x68(%rsp), %rdi
callq 0x3120
jmp 0x3c4c
jmp 0x3c60
jmp 0x3c60
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x3120
jmp 0x3c6c
movq %rax, %rbx
jmp 0x3c93
movq %rax, %rbx
jmp 0x3ca0
jmp 0x3c69
jmp 0x3c69
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0x3fb4
leaq 0x170(%rsp), %rdi
jmp 0x3cb5
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x3120
leaq 0xe0(%rsp), %rdi
callq 0x3120
leaq 0x170(%rsp), %rdi
callq 0x3fb4
leaq 0x1e0(%rsp), %rdi
callq 0x3fb4
leaq 0xa8(%rsp), %rdi
callq 0xc21e
leaq 0x48(%rsp), %rdi
callq 0x3120
leaq 0x38(%rsp), %rdi
callq 0x3ffc
leaq 0x150(%rsp), %rdi
callq 0x3120
movq %rbx, %rdi
callq 0x33c0