// File: /workspace/github_cpp_makefile_star2_O1/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 $0x21d8, %rsp           # imm = 0x21D8
cmpl $0x2, %edi
jne 0x404b
movq 0x15833(%rip), %rax     # 0x18fc0
movq (%rax), %rbx
movq 0x8(%rsi), %rsi
leaq 0x98(%rsp), %r14
leaq 0x1d0(%rsp), %rdx
movq %r14, %rdi
callq 0x42fc
leaq 0x18(%rsp), %r15
movb $0x0, (%r15)
movq $0x0, 0x8(%r15)
movq %r15, %rdi
callq 0x6d4c
movq %r15, %rdi
callq 0x6d4c
movq (%r14), %rdi
leaq 0xce3a(%rip), %rsi      # 0x10611
callq 0x3280
movq %rax, %r14
xorl %r13d, %r13d
testq %rax, %rax
je 0x3902
movq %r14, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x3250
testl %eax, %eax
jne 0x3902
movq %r14, %rdi
callq 0x3030
movq %rax, %r12
xorl %r13d, %r13d
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3250
testl %eax, %eax
jne 0x3902
movq %r12, %rdi
callq 0x3040
movq %rax, %r15
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
movq %r14, %rcx
callq 0x3340
cmpq $0x1, %rax
sete %r13b
jne 0x38fa
movq %r14, %rdi
callq 0x3200
movq %r15, %rdx
addq %r12, %rdx
xorps %xmm0, %xmm0
leaq 0x1d0(%rsp), %rcx
movaps %xmm0, (%rcx)
movaps %xmm0, 0x10(%rcx)
leaq 0xb8(%rsp), %rdi
movq %r15, %rsi
movl $0x1, %r8d
callq 0x4610
leaq 0xb8(%rsp), %r14
movq %r14, %rdi
callq 0x6d4c
leaq 0x18(%rsp), %rdi
movb (%rdi), %al
movb (%r14), %cl
movb %cl, (%rdi)
movb %al, (%r14)
leaq 0xc0(%rsp), %r12
movq 0x8(%rdi), %rax
movq (%r12), %rcx
movq %rcx, 0x8(%rdi)
movq %rax, (%r12)
callq 0x6d4c
movq %r14, %rdi
callq 0x6d4c
movzbl -0x8(%r12), %esi
movq %r12, %rdi
callq 0xebf0
movq 0x1e0(%rsp), %rax
testq %rax, %rax
je 0x38f7
leaq 0x1d0(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
xorl %r14d, %r14d
movq %r15, %rdi
callq 0x32b0
testq %r14, %r14
je 0x390f
movq %r14, %rdi
callq 0x3200
testb %r13b, %r13b
je 0x405a
leaq 0x68(%rsp), %rax
movq %rax, -0x10(%rax)
movq 0x98(%rsp), %rsi
movq 0xa0(%rsp), %rdx
addq %rsi, %rdx
leaq 0x58(%rsp), %rdi
callq 0xead0
movq 0x60(%rsp), %rdx
testq %rdx, %rdx
sete %al
je 0x3981
movq 0x58(%rsp), %rcx
cmpb $0x2f, -0x1(%rcx,%rdx)
je 0x397d
leaq 0x58(%rsp), %r14
movq %r14, %rdi
callq 0x3290
movq 0x60(%rsp), %rdx
testq %rdx, %rdx
sete %al
je 0x3981
movq 0x58(%rsp), %rcx
cmpb $0x2f, -0x1(%rcx,%rdx)
jne 0x395c
testb %al, %al
je 0x399a
leaq 0xcc59(%rip), %rcx      # 0x105e1
leaq 0x58(%rsp), %rdi
movl $0x2, %r8d
xorl %esi, %esi
callq 0x3350
leaq 0x1c8(%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 0x1d0(%rsp), %r15
leaq 0x18(%rsp), %r14
movq %r15, %rdi
movq %r14, %rsi
callq 0xa06a
leaq 0xb8(%rsp), %rdi
movq %r15, %rsi
movq %rdi, %r15
callq 0xefd4
leaq 0x1d0(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0xa0bc
leaq 0x128(%rsp), %rdi
movq %r12, %rsi
callq 0xefd4
leaq 0xcc23(%rip), %r13      # 0x10638
movl $0x0, 0x10(%rsp)
movq %rbx, 0x30(%rsp)
movq %r15, %rdi
leaq 0x128(%rsp), %rsi
callq 0xaa80
movl %eax, %ebp
testb %al, %al
jne 0x3ca2
movb %bpl, 0xf(%rsp)
movq %r15, %rdi
callq 0x433a
movq (%rax), %r12
callq 0x3060
movq %rax, %rbp
movl (%rax), %r14d
movl $0x0, (%rax)
movq %r12, %rdi
leaq 0x1d0(%rsp), %rsi
movl $0xa, %edx
callq 0x3300
cmpq %r12, 0x1d0(%rsp)
je 0x4110
leaq -0x80000000(%rax), %rcx
movabsq $-0x100000000, %rdx     # imm = 0xFFFFFFFF00000000
cmpq %rdx, %rcx
jb 0x411c
movl (%rbp), %ecx
cmpl $0x22, %ecx
je 0x411c
testl %ecx, %ecx
jne 0x3aae
movl %r14d, (%rbp)
movl %eax, 0x14(%rsp)
movq %r15, %rdi
callq 0xf1d6
movq %rax, %rdi
leaq 0xcb20(%rip), %rsi      # 0x105e4
callq 0x43a6
leaq 0x38(%rsp), %rdi
movq %rax, %rsi
callq 0xf580
leaq 0x78(%rsp), %rdi
leaq 0x58(%rsp), %rsi
leaq 0x38(%rsp), %rdx
callq 0x4563
movl 0x14(%rsp), %edx
movq %rbx, %rdi
leaq 0xcaf1(%rip), %rsi      # 0x105e9
xorl %eax, %eax
callq 0x3310
movq 0x78(%rsp), %rdi
leaq 0xcb06(%rip), %rsi      # 0x10611
callq 0x3280
movq %rax, %rbp
testq %rax, %rax
je 0x3c62
movl $0x1, %esi
movl $0x2000, %edx           # imm = 0x2000
leaq 0x1d0(%rsp), %rdi
movq %rbp, %rcx
callq 0x3340
testq %rax, %rax
je 0x3ba1
movq %rax, %r15
xorl %r14d, %r14d
movq %rbp, 0x28(%rsp)
cmpq $0x1, %r15
movq %r15, %r12
adcq $0x0, %r12
xorl %ebp, %ebp
movq 0x30(%rsp), %rbx
movzbl 0x1d0(%rsp,%rbp), %edx
movq %rbx, %rdi
movq %r13, %rsi
xorl %eax, %eax
callq 0x3310
incq %rbp
cmpq %rbp, %r12
jne 0x3b58
addl %r15d, %r14d
movl $0x1, %esi
movl $0x2000, %edx           # imm = 0x2000
leaq 0x1d0(%rsp), %rdi
movq 0x28(%rsp), %rbp
movq %rbp, %rcx
callq 0x3340
movq %rax, %r15
testq %rax, %rax
jne 0x3b46
jmp 0x3ba4
xorl %r14d, %r14d
movq %rbp, %rdi
callq 0x33e0
testl %eax, %eax
sete %r12b
jne 0x3c79
movq %rbp, %rdi
callq 0x3200
movl $0x3, %esi
movl $0x1, %edx
leaq 0xcaf7(%rip), %rdi      # 0x106c8
movq 0x30(%rsp), %rbx
movq %rbx, %rcx
callq 0x3410
leaq 0x198(%rsp), %rdi
leaq 0x14(%rsp), %rsi
callq 0xf6e2
leaq 0xb8(%rsp), %r15
movl %r14d, (%rax)
xorl %ebp, %ebp
testq %rbp, %rbp
je 0x3c0a
movq %rbp, %rdi
callq 0x3200
movq 0x78(%rsp), %rdi
leaq 0x88(%rsp), %rax
cmpq %rax, %rdi
je 0x3c2c
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x3220
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x3c48
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x3220
testb %r12b, %r12b
je 0x3c9d
movq %r15, %rdi
callq 0xa4e6
incq 0xd8(%rsp)
jmp 0x3a22
leaq 0x78(%rsp), %rdi
callq 0x34ea
movl $0x1, 0x10(%rsp)
xorl %r12d, %r12d
jmp 0x3bfd
leaq 0x78(%rsp), %rdi
callq 0x34c6
movl $0x1, 0x10(%rsp)
leaq 0xb8(%rsp), %r15
movq 0x30(%rsp), %rbx
jmp 0x3bfd
movb 0xf(%rsp), %bpl
leaq 0x188(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3cc3
movq 0x188(%rsp), %rsi
incq %rsi
callq 0x3220
leaq 0x168(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3ce4
movq 0x168(%rsp), %rsi
incq %rsi
callq 0x3220
leaq 0x118(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3d05
movq 0x118(%rsp), %rsi
incq %rsi
callq 0x3220
leaq 0xf8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3d26
movq 0xf8(%rsp), %rsi
incq %rsi
callq 0x3220
testb %bpl, %bpl
je 0x4092
leaq 0xc92a(%rip), %rdi      # 0x10660
movl $0x6b, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x3410
leaq 0xc97d(%rip), %rdi      # 0x106cc
movl $0x20, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x3410
leaq 0xb8(%rsp), %r14
leaq 0x18(%rsp), %r13
movq %r14, %rdi
movq %r13, %rsi
callq 0xa06a
leaq 0x1d0(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rsi
callq 0xefd4
leaq 0x128(%rsp), %r12
movq %r12, %rdi
movq %r13, %rsi
callq 0xa0bc
leaq 0xb8(%rsp), %rdi
movq %r12, %rsi
callq 0xefd4
movq %rbp, %rdi
leaq 0xb8(%rsp), %rsi
callq 0xaa80
movl %eax, %r14d
testb %al, %al
jne 0x3f78
movb %r14b, 0x28(%rsp)
movq %rbp, %r15
movq %rbp, %rdi
callq 0x433a
movq (%rax), %r14
callq 0x3060
movq %rax, %r13
movl (%rax), %ebp
movl $0x0, (%rax)
movq %r14, %rdi
movq %r12, %rsi
movl $0xa, %edx
callq 0x3300
cmpq %r14, 0x128(%rsp)
je 0x4128
leaq -0x80000000(%rax), %rcx
movabsq $-0x100000000, %rdx     # imm = 0xFFFFFFFF00000000
cmpq %rdx, %rcx
jb 0x4134
movl (%r13), %ecx
cmpl $0x22, %ecx
je 0x4134
testl %ecx, %ecx
jne 0x3e3a
movl %ebp, (%r13)
movl %eax, 0x78(%rsp)
movq %r15, %rbp
movq %r15, %rdi
callq 0xf1d6
movq %rax, %rdi
leaq 0xc89a(%rip), %rsi      # 0x106ed
callq 0x43a6
movq %r12, %rdi
movq %rax, %rsi
callq 0xf580
movq %rbp, %rdi
callq 0xf1d6
movq %rax, %rdi
leaq 0xc76f(%rip), %rsi      # 0x105e4
callq 0x43a6
leaq 0x38(%rsp), %rdi
movq %rax, %rsi
callq 0xf580
leaq 0x198(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0xf9c4
movl (%rax), %r13d
movq 0x130(%rsp), %r14
cmpq $0x3, %r14
ja 0x3eb9
movq %r12, %rdi
movl $0x20, %esi
callq 0x31f0
jmp 0x3e9c
movq 0x128(%rsp), %rcx
cmpq $0x4, %r14
jne 0x3f4b
movl 0x78(%rsp), %edx
movq 0x38(%rsp), %rax
movq %rax, (%rsp)
movq %rbx, %rdi
leaq 0xc832(%rip), %rsi      # 0x10714
movl %edx, %r8d
movl %r13d, %r9d
xorl %eax, %eax
callq 0x3310
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x3f0b
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x3220
movq 0x128(%rsp), %rdi
leaq 0x138(%rsp), %rax
cmpq %rax, %rdi
je 0x3f30
movq 0x138(%rsp), %rsi
incq %rsi
callq 0x3220
cmpq $0x4, %r14
jne 0x3f73
movq %rbp, %rdi
callq 0xa4e6
incq 0x1f0(%rsp)
jmp 0x3daf
movq 0x1508e(%rip), %rax     # 0x18fe0
movq (%rax), %rdi
leaq 0xc796(%rip), %rsi      # 0x106f2
movq %rcx, %rdx
xorl %eax, %eax
callq 0x3310
movl $0x1, 0x10(%rsp)
jmp 0x3eef
movb 0x28(%rsp), %r14b
movq 0x108(%rsp), %rdi
leaq 0x118(%rsp), %rax
cmpq %rax, %rdi
je 0x3f9d
movq 0x118(%rsp), %rsi
incq %rsi
callq 0x3220
movq 0xe8(%rsp), %rdi
leaq 0xf8(%rsp), %rax
cmpq %rax, %rdi
je 0x3fc2
movq 0xf8(%rsp), %rsi
incq %rsi
callq 0x3220
leaq 0x230(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3fe3
movq 0x230(%rsp), %rsi
incq %rsi
callq 0x3220
leaq 0x210(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4004
movq 0x210(%rsp), %rsi
incq %rsi
callq 0x3220
testb %r14b, %r14b
je 0x4092
leaq 0xc6b4(%rip), %rdi      # 0x106c8
movl $0x3, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x3410
movzbl 0x18(%rsp), %edx
testl %edx, %edx
je 0x4079
cmpl $0x1, %edx
je 0x4069
cmpl $0x2, %edx
jne 0x4074
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rdx
subq (%rax), %rdx
sarq $0x4, %rdx
jmp 0x4079
callq 0x3488
movl $0x1, %eax
jmp 0x40fe
callq 0x34a4
movl $0x1, 0x10(%rsp)
jmp 0x40bb
movq 0x20(%rsp), %rax
movq 0x28(%rax), %rdx
jmp 0x4079
movl $0x1, %edx
leaq 0xc6b7(%rip), %rsi      # 0x10737
movl $0x0, 0x10(%rsp)
movq %rbx, %rdi
xorl %eax, %eax
callq 0x3310
leaq 0x198(%rsp), %rdi
callq 0xea60
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x40bb
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x3220
leaq 0x20(%rsp), %rbx
leaq 0x18(%rsp), %r14
movq %r14, %rdi
callq 0x6d4c
movzbl (%r14), %esi
movq %rbx, %rdi
callq 0xebf0
leaq 0xa8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x40fa
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x3220
movl 0x10(%rsp), %eax
addq $0x21d8, %rsp           # imm = 0x21D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xedbd(%rip), %rdi      # 0x12ed4
callq 0x3170
leaq 0xedb1(%rip), %rdi      # 0x12ed4
callq 0x3320
leaq 0xeda5(%rip), %rdi      # 0x12ed4
callq 0x3170
leaq 0xed99(%rip), %rdi      # 0x12ed4
callq 0x3320
jmp 0x4171
movq %rax, %rbx
movq 0x1e0(%rsp), %rax
testq %rax, %rax
je 0x4164
leaq 0x1d0(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq %r15, %rdi
callq 0x32b0
xorl %r14d, %r14d
jmp 0x417c
movq %rax, %rdi
callq 0x4796
movq %rax, %rbx
testq %r14, %r14
je 0x42c8
movq %r14, %rdi
callq 0x3200
jmp 0x42c8
movq %rax, %rbx
jmp 0x42ac
movq %rax, %rbx
jmp 0x42c8
jmp 0x4241
jmp 0x4211
movq %rax, %rbx
movq 0x78(%rsp), %rdi
leaq 0x88(%rsp), %rax
cmpq %rax, %rdi
je 0x41ee
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x3220
jmp 0x41ee
movq %rax, %rbx
cmpl $0x0, (%r13)
jne 0x4285
movl %ebp, (%r13)
jmp 0x4285
jmp 0x4211
jmp 0x4227
movq %rax, %rbx
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x422a
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x3220
jmp 0x422a
movq %rax, %rbx
jmp 0x4260
movq %rax, %rbx
jmp 0x4285
movq %rax, %rbx
cmpl $0x0, (%rbp)
jne 0x422a
movl %r14d, (%rbp)
jmp 0x422a
jmp 0x4227
movq %rax, %rbx
leaq 0x128(%rsp), %rdi
callq 0x45be
leaq 0xb8(%rsp), %rdi
jmp 0x429a
movq %rax, %rbx
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x4260
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x3220
movq 0x128(%rsp), %rdi
leaq 0x138(%rsp), %rax
cmpq %rax, %rdi
je 0x4285
movq 0x138(%rsp), %rsi
incq %rsi
callq 0x3220
leaq 0xb8(%rsp), %rdi
callq 0x45be
leaq 0x1d0(%rsp), %rdi
callq 0x45be
leaq 0x198(%rsp), %rdi
callq 0xea60
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x42c8
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x3220
leaq 0x18(%rsp), %rdi
callq 0x45f6
leaq 0xa8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x42f3
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x3220
movq %rbx, %rdi
callq 0x33b0
nop