// File: /workspace/github_cpp_makefile_star2_O3/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 $0x21c8, %rsp           # imm = 0x21C8
cmpl $0x2, %edi
jne 0x4133
movq 0x15783(%rip), %rax     # 0x18fc0
movq (%rax), %rbx
movq 0x8(%rsi), %rsi
leaq 0x88(%rsp), %r14
leaq 0x1c0(%rsp), %rdx
movq %r14, %rdi
callq 0x4406
leaq 0x18(%rsp), %r15
movb $0x0, (%r15)
movq $0x0, 0x8(%r15)
movq %r15, %rdi
callq 0x6c96
movq %r15, %rdi
callq 0x6c96
movq (%r14), %rdi
leaq 0xcd96(%rip), %rsi      # 0x1061d
callq 0x3280
testq %rax, %rax
je 0x4152
movq %rax, %r15
movq %rax, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x3250
testl %eax, %eax
jne 0x414a
movq %r15, %rdi
callq 0x3030
movq %rax, %r12
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3250
testl %eax, %eax
jne 0x414a
movq %r12, %rdi
callq 0x3040
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
movq %r15, %rcx
callq 0x3340
cmpq $0x1, %rax
jne 0x4142
movq %r15, %rdi
callq 0x3200
movq %r14, %rdx
addq %r12, %rdx
xorps %xmm0, %xmm0
leaq 0x1c0(%rsp), %rcx
movaps %xmm0, (%rcx)
movaps %xmm0, 0x10(%rcx)
leaq 0xa8(%rsp), %rdi
movq %r14, %rsi
movl $0x1, %r8d
callq 0x4726
leaq 0xa8(%rsp), %r15
movq %r15, %rdi
callq 0x6c96
leaq 0x18(%rsp), %rdi
movb (%rdi), %al
movb (%r15), %cl
movb %cl, (%rdi)
movb %al, (%r15)
leaq 0xb0(%rsp), %r12
movq 0x8(%rdi), %rax
movq (%r12), %rcx
movq %rcx, 0x8(%rdi)
movq %rax, (%r12)
callq 0x6c96
movq %r15, %rdi
callq 0x6c96
movzbl -0x8(%r12), %esi
movq %r12, %rdi
callq 0xe97e
movq 0x1d0(%rsp), %rax
testq %rax, %rax
je 0x399d
leaq 0x1c0(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq %r14, %rdi
callq 0x32b0
leaq 0x58(%rsp), %rax
movq %rax, -0x10(%rax)
movq 0x88(%rsp), %rsi
movq 0x90(%rsp), %rdx
addq %rsi, %rdx
leaq 0x48(%rsp), %rdi
callq 0xe852
movq 0x50(%rsp), %rax
testq %rax, %rax
je 0x39f8
leaq 0x48(%rsp), %r14
movq 0x48(%rsp), %rcx
cmpb $0x2f, -0x1(%rcx,%rax)
je 0x3a13
movq %r14, %rdi
callq 0x3290
movq 0x50(%rsp), %rax
testq %rax, %rax
jne 0x39da
leaq 0xcbee(%rip), %rcx      # 0x105ed
leaq 0x48(%rsp), %rdi
movl $0x2, %r8d
xorl %esi, %esi
xorl %edx, %edx
callq 0x3350
movabsq $-0x8000000000000000, %r15 # imm = 0x8000000000000000
leaq 0x1b8(%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 0x1c0(%rsp), %r12
leaq 0x18(%rsp), %r14
movq %r14, (%r12)
movups %xmm0, 0x8(%r12)
movq %r15, 0x18(%r12)
movq %r12, %rdi
callq 0xa88a
leaq 0xa8(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0xed6e
movq %r14, (%r12)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%r12)
movq %r15, 0x18(%r12)
movq %r13, %r12
leaq 0x1c0(%rsp), %r14
movq %r14, %rdi
callq 0xafe8
leaq 0x118(%rsp), %rdi
movq %r14, %rsi
callq 0xed6e
leaq 0xcb8e(%rip), %rbp      # 0x10644
movl $0x0, 0x10(%rsp)
movq %r12, %rdi
leaq 0x118(%rsp), %rsi
callq 0xa8ee
movl %eax, %r14d
testb %al, %al
jne 0x3d24
movb %r14b, 0xf(%rsp)
movq %r12, %rdi
callq 0x4444
movq (%rax), %r13
callq 0x3060
movq %rax, %r14
movl (%rax), %r15d
movl $0x0, (%rax)
movq %r13, %rdi
leaq 0x1c0(%rsp), %rsi
movl $0xa, %edx
callq 0x3300
cmpq %r13, 0x1c0(%rsp)
je 0x422f
leaq -0x80000000(%rax), %rcx
movabsq $-0x100000000, %rdx     # imm = 0xFFFFFFFF00000000
cmpq %rdx, %rcx
jb 0x423b
movl (%r14), %ecx
cmpl $0x22, %ecx
je 0x423b
testl %ecx, %ecx
jne 0x3b4a
movl %r15d, (%r14)
movl %eax, 0x14(%rsp)
movq %r12, %rdi
callq 0xef6c
movq %rax, %rdi
leaq 0xca90(%rip), %rsi      # 0x105f0
callq 0x44b0
leaq 0x28(%rsp), %rdi
movq %rax, %rsi
callq 0xf312
leaq 0x68(%rsp), %rdi
leaq 0x48(%rsp), %rsi
leaq 0x28(%rsp), %rdx
callq 0x467a
movl 0x14(%rsp), %edx
movq %rbx, %rdi
leaq 0xca61(%rip), %rsi      # 0x105f5
xorl %eax, %eax
callq 0x3310
movq 0x68(%rsp), %rdi
leaq 0xca76(%rip), %rsi      # 0x1061d
callq 0x3280
testq %rax, %rax
je 0x3ccb
movq %rax, %r14
movl $0x1, %esi
movl $0x2000, %edx           # imm = 0x2000
leaq 0x1c0(%rsp), %rdi
movq %rax, %rcx
callq 0x3340
testq %rax, %rax
je 0x3c25
movq %rax, %r13
xorl %r15d, %r15d
xorl %r12d, %r12d
movzbl 0x1c0(%rsp,%r12), %edx
movq %rbx, %rdi
movq %rbp, %rsi
xorl %eax, %eax
callq 0x3310
incq %r12
cmpq %r12, %r13
jne 0x3be0
addl %r13d, %r15d
movl $0x1, %esi
movl $0x2000, %edx           # imm = 0x2000
leaq 0x1c0(%rsp), %rdi
movq %r14, %rcx
callq 0x3340
movq %rax, %r13
testq %rax, %rax
jne 0x3bdd
jmp 0x3c28
xorl %r15d, %r15d
movq %r14, %rdi
callq 0x33e0
testl %eax, %eax
jne 0x3ce2
movq %r14, %rdi
callq 0x3200
movl $0x3, %esi
movl $0x1, %edx
leaq 0xca83(%rip), %rdi      # 0x106d4
movq %rbx, %rcx
callq 0x3410
leaq 0x188(%rsp), %rdi
leaq 0x14(%rsp), %rsi
callq 0xf486
leaq 0xa8(%rsp), %r12
movl %r15d, (%rax)
movb $0x1, %r15b
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x3c95
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x3220
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x3cb1
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3220
testb %r15b, %r15b
je 0x3d1f
movq %r12, %rdi
callq 0xa354
incq 0xc8(%rsp)
jmp 0x3abe
leaq 0x68(%rsp), %rdi
callq 0x34b3
movl $0x1, 0x10(%rsp)
xorl %r15d, %r15d
jmp 0x3c79
movq 0x152f7(%rip), %rax     # 0x18fe0
movq (%rax), %rdi
movq 0x68(%rsp), %rdx
xorl %r15d, %r15d
leaq 0xc94d(%rip), %rsi      # 0x10648
xorl %eax, %eax
callq 0x3310
movq %r14, %rdi
callq 0x3200
movl $0x1, 0x10(%rsp)
leaq 0xa8(%rsp), %r12
jmp 0x3c79
movb 0xf(%rsp), %r14b
leaq 0x178(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3d45
movq 0x178(%rsp), %rsi
incq %rsi
callq 0x3220
leaq 0x158(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3d66
movq 0x158(%rsp), %rsi
incq %rsi
callq 0x3220
leaq 0x108(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3d87
movq 0x108(%rsp), %rsi
incq %rsi
callq 0x3220
leaq 0xe8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3da8
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x3220
testb %r14b, %r14b
je 0x41a5
leaq 0xc8b4(%rip), %rdi      # 0x1066c
movl $0x6b, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x3410
leaq 0xc907(%rip), %rdi      # 0x106d8
movl $0x20, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x3410
leaq 0xa8(%rsp), %r12
leaq 0x18(%rsp), %r14
movq %r14, (%r12)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%r12)
movabsq $-0x8000000000000000, %r15 # imm = 0x8000000000000000
movq %r15, 0x18(%r12)
movq %r12, %rdi
callq 0xa88a
leaq 0x1c0(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0xed6e
leaq 0x118(%rsp), %r12
movq %r14, (%r12)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%r12)
movq %r15, 0x18(%r12)
movq %r12, %rdi
callq 0xafe8
leaq 0xa8(%rsp), %rdi
movq %r12, %rsi
callq 0xed6e
movq %r13, %rdi
leaq 0xa8(%rsp), %rsi
callq 0xa8ee
movl %eax, %ebp
testb %al, %al
jne 0x4051
movb %bpl, 0xf(%rsp)
movq %r13, %rdi
callq 0x4444
movq (%rax), %r15
callq 0x3060
movq %rax, %r14
movl (%rax), %ebp
movl $0x0, (%rax)
movq %r15, %rdi
movq %r12, %rsi
movl $0xa, %edx
callq 0x3300
cmpq %r15, 0x118(%rsp)
je 0x4247
movq %rax, %r13
addq $-0x80000000, %rax      # imm = 0x80000000
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
cmpq %rcx, %rax
jb 0x4253
movl (%r14), %eax
cmpl $0x22, %eax
je 0x4253
testl %eax, %eax
leaq 0x1c0(%rsp), %r15
jne 0x3ee8
movl %ebp, (%r14)
movq %r15, %rdi
callq 0xef6c
movq %rax, %rdi
leaq 0xc7ff(%rip), %rsi      # 0x106f9
callq 0x44b0
movq %r12, %rdi
movq %rax, %rsi
callq 0xf312
movq %r15, %rdi
callq 0xef6c
movq %rax, %rdi
leaq 0xc6d4(%rip), %rsi      # 0x105f0
callq 0x44b0
leaq 0x28(%rsp), %rdi
movq %rax, %rsi
callq 0xf312
movl %r13d, %eax
movq 0x188(%rsp), %rsi
movq 0x190(%rsp), %rdi
xorl %edx, %edx
divq %rdi
movq (%rsi,%rdx,8), %rax
testq %rax, %rax
je 0x4223
movq %rdx, %rcx
movq (%rax), %r8
cmpl %r13d, 0x8(%r8)
je 0x3f85
movq (%r8), %r8
testq %r8, %r8
je 0x4223
movl 0x8(%r8), %esi
movq %rsi, %rax
xorl %edx, %edx
divq %rdi
cmpq %rcx, %rdx
jne 0x4223
cmpl %r13d, %esi
jne 0x3f5f
movl 0xc(%r8), %r14d
movq 0x120(%rsp), %r15
cmpq $0x3, %r15
ja 0x3fa6
movq %r12, %rdi
movl $0x20, %esi
callq 0x31f0
jmp 0x3f89
cmpq $0x4, %r15
jne 0x403f
movq 0x118(%rsp), %rcx
movq 0x28(%rsp), %rax
movq %rax, (%rsp)
movq %rbx, %rdi
leaq 0xc755(%rip), %rsi      # 0x10720
movl %r13d, %edx
movl %r13d, %r8d
movl %r14d, %r9d
xorl %eax, %eax
callq 0x3310
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x3ff7
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3220
movq 0x118(%rsp), %rdi
leaq 0x128(%rsp), %rax
cmpq %rax, %rdi
je 0x401c
movq 0x128(%rsp), %rsi
incq %rsi
callq 0x3220
cmpq $0x4, %r15
jne 0x405b
leaq 0x1c0(%rsp), %r13
movq %r13, %rdi
callq 0xa354
incq 0x1e0(%rsp)
jmp 0x3e59
movq %r12, %rdi
callq 0x34d7
movl $0x1, 0x10(%rsp)
jmp 0x3fdb
leaq 0xe8(%rsp), %r14
jmp 0x4068
leaq 0xe8(%rsp), %r14
movb 0xf(%rsp), %bpl
movq 0xf8(%rsp), %rdi
leaq 0x108(%rsp), %rax
cmpq %rax, %rdi
je 0x408d
movq 0x108(%rsp), %rsi
incq %rsi
callq 0x3220
movq 0xd8(%rsp), %rdi
cmpq %r14, %rdi
je 0x40aa
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x3220
leaq 0x220(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x40cb
movq 0x220(%rsp), %rsi
incq %rsi
callq 0x3220
leaq 0x200(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x40ec
movq 0x200(%rsp), %rsi
incq %rsi
callq 0x3220
testb %bpl, %bpl
je 0x41a5
leaq 0xc5d8(%rip), %rdi      # 0x106d4
movl $0x3, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x3410
movzbl 0x18(%rsp), %edx
testl %edx, %edx
je 0x418c
cmpl $0x1, %edx
je 0x417c
cmpl $0x2, %edx
jne 0x4187
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rdx
subq (%rax), %rdx
sarq $0x4, %rdx
jmp 0x418c
callq 0x3497
movl $0x1, %eax
jmp 0x4211
movq %r14, %rdi
callq 0x32b0
movq %r15, %rdi
callq 0x3200
movq 0x14e87(%rip), %rax     # 0x18fe0
movq (%rax), %rcx
leaq 0xc45c(%rip), %rdi      # 0x105bf
movl $0x2d, %esi
movl $0x1, %edx
callq 0x3410
movl $0x1, 0x10(%rsp)
jmp 0x41ce
movq 0x20(%rsp), %rax
movq 0x28(%rax), %rdx
jmp 0x418c
movl $0x1, %edx
leaq 0xc5b0(%rip), %rsi      # 0x10743
movl $0x0, 0x10(%rsp)
movq %rbx, %rdi
xorl %eax, %eax
callq 0x3310
leaq 0x188(%rsp), %rdi
callq 0xe7e2
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x41ce
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x3220
leaq 0x20(%rsp), %rbx
leaq 0x18(%rsp), %r14
movq %r14, %rdi
callq 0x6c96
movzbl (%r14), %esi
movq %rbx, %rdi
callq 0xe97e
leaq 0x98(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x420d
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x3220
movl 0x10(%rsp), %eax
addq $0x21c8, %rsp           # imm = 0x21C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xef53(%rip), %rdi      # 0x1317d
callq 0x3320
leaq 0xecaa(%rip), %rdi      # 0x12ee0
callq 0x3170
leaq 0xec9e(%rip), %rdi      # 0x12ee0
callq 0x3320
leaq 0xec92(%rip), %rdi      # 0x12ee0
callq 0x3170
leaq 0xec86(%rip), %rdi      # 0x12ee0
callq 0x3320
jmp 0x42a0
movq %rax, %rbx
jmp 0x43b6
movq %rax, %rbx
jmp 0x43d2
movq %rax, %rbx
movq 0x1d0(%rsp), %rax
testq %rax, %rax
je 0x4293
leaq 0x1c0(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq %r14, %rdi
callq 0x32b0
jmp 0x43d2
movq %rax, %rdi
callq 0x48ac
movq %rax, %rbx
movq %r15, %rdi
callq 0x3200
jmp 0x43d2
jmp 0x431a
movq %rax, %rbx
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x42f7
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x3220
jmp 0x42f7
movq %rax, %rbx
cmpl $0x0, (%r14)
jne 0x438f
movl %ebp, (%r14)
jmp 0x438f
jmp 0x431a
jmp 0x432f
movq %rax, %rbx
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x4332
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3220
jmp 0x4332
movq %rax, %rbx
jmp 0x436a
movq %rax, %rbx
jmp 0x438f
movq %rax, %rbx
cmpl $0x0, (%r14)
jne 0x4332
movl %r15d, (%r14)
jmp 0x4332
jmp 0x432f
movq %rax, %rbx
leaq 0x118(%rsp), %rdi
callq 0x46d4
leaq 0xa8(%rsp), %rdi
jmp 0x43a4
jmp 0x434b
movq %rax, %rbx
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x436a
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3220
movq 0x118(%rsp), %rdi
leaq 0x128(%rsp), %rax
cmpq %rax, %rdi
je 0x438f
movq 0x128(%rsp), %rsi
incq %rsi
callq 0x3220
leaq 0xa8(%rsp), %rdi
callq 0x46d4
leaq 0x1c0(%rsp), %rdi
callq 0x46d4
leaq 0x188(%rsp), %rdi
callq 0xe7e2
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x43d2
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x3220
leaq 0x18(%rsp), %rdi
callq 0x470c
leaq 0x98(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x43fd
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x3220
movq %rbx, %rdi
callq 0x33b0
nop