// File: 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
movq %rsp, %rbp
subq $0x2470, %rsp           # imm = 0x2470
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
je 0x4733
movq 0x348c2(%rip), %rax     # 0x38fd8
movq (%rax), %rdi
leaq 0x21228(%rip), %rsi     # 0x25948
movb $0x0, %al
callq 0x4480
movl $0x1, -0x4(%rbp)
jmp 0x5141
movq 0x34876(%rip), %rax     # 0x38fb0
movq (%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x2378(%rbp)
leaq -0x39(%rbp), %rdi
movq %rdi, -0x2370(%rbp)
callq 0x4560
movq -0x2378(%rbp), %rsi
movq -0x2370(%rbp), %rdx
leaq -0x38(%rbp), %rdi
callq 0x5430
jmp 0x4779
leaq -0x39(%rbp), %rdi
callq 0x4360
xorl %eax, %eax
movl %eax, %esi
leaq -0x60(%rbp), %rdi
movq %rdi, -0x2388(%rbp)
callq 0x54e0
movq -0x2388(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x5160
movb %al, -0x2379(%rbp)
jmp 0x47ae
movb -0x2379(%rbp), %al
testb $0x1, %al
jne 0x4810
jmp 0x47ba
movq 0x34817(%rip), %rax     # 0x38fd8
movq (%rax), %rdi
leaq 0x211a4(%rip), %rsi     # 0x2596f
movb $0x0, %al
callq 0x4480
movl $0x1, -0x4(%rbp)
movl $0x1, -0x64(%rbp)
jmp 0x5119
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x39(%rbp), %rdi
callq 0x4360
jmp 0x514d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x512d
leaq -0x88(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x4160
jmp 0x4822
jmp 0x4824
leaq -0x88(%rbp), %rdi
callq 0x4520
movb %al, %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x2389(%rbp)
jne 0x4869
leaq -0x88(%rbp), %rdi
callq 0x4270
movsbl (%rax), %edi
callq 0x5400
movb %al, -0x238a(%rbp)
jmp 0x485b
movb -0x238a(%rbp), %al
xorb $-0x1, %al
movb %al, -0x2389(%rbp)
movb -0x2389(%rbp), %al
testb $0x1, %al
jne 0x4875
jmp 0x4894
leaq -0x88(%rbp), %rdi
callq 0x43c0
jmp 0x4824
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x510b
leaq -0x88(%rbp), %rdi
callq 0x4520
testb $0x1, %al
jne 0x48a6
jmp 0x48bd
leaq 0x210f0(%rip), %rsi     # 0x2599d
leaq -0x88(%rbp), %rdi
callq 0x4440
jmp 0x48bb
jmp 0x48bd
leaq -0xc0(%rbp), %rdi
callq 0x5520
leaq -0x60(%rbp), %rdi
callq 0x5540
movq %rax, -0xd0(%rbp)
leaq -0xd0(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rsi
leaq -0x140(%rbp), %rdi
callq 0x5570
movq -0xc8(%rbp), %rsi
leaq -0x1b0(%rbp), %rdi
callq 0x55c0
leaq -0x140(%rbp), %rdi
leaq -0x1b0(%rbp), %rsi
callq 0x5610
movb %al, -0x238b(%rbp)
jmp 0x4928
movb -0x238b(%rbp), %al
testb $0x1, %al
jne 0x4951
jmp 0x4934
movl $0x4, -0x64(%rbp)
jmp 0x4cdb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x4d00
leaq -0x140(%rbp), %rdi
callq 0x5640
movq %rax, -0x2398(%rbp)
jmp 0x4966
movq -0x2398(%rbp), %rax
movq %rax, -0x1b8(%rbp)
movq -0x1b8(%rbp), %rdi
callq 0x5690
movq %rax, -0x23a0(%rbp)
jmp 0x4989
movq -0x23a0(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x5650
movl %eax, -0x23a4(%rbp)
jmp 0x49a6
movl -0x23a4(%rbp), %eax
movl %eax, -0x1bc(%rbp)
movq -0x1b8(%rbp), %rdi
callq 0x5750
movq %rax, -0x23b0(%rbp)
jmp 0x49c7
movq -0x23b0(%rbp), %rdi
leaq 0x20fcb(%rip), %rsi     # 0x259a0
callq 0x5770
movq %rax, -0x23b8(%rbp)
jmp 0x49e3
movq -0x23b8(%rbp), %rsi
leaq -0x1e0(%rbp), %rdi
callq 0x59b0
jmp 0x49f8
leaq -0x200(%rbp), %rdi
leaq -0x88(%rbp), %rsi
leaq -0x1e0(%rbp), %rdx
callq 0x59e0
jmp 0x4a14
movq -0x18(%rbp), %rdi
movl -0x1bc(%rbp), %edx
leaq 0x20f80(%rip), %rsi     # 0x259a5
xorl %eax, %eax
callq 0x4480
movl $0x0, -0x204(%rbp)
leaq -0x200(%rbp), %rdi
callq 0x4140
movq %rax, %rdi
leaq 0x20f81(%rip), %rsi     # 0x259cd
callq 0x43b0
movq %rax, -0x23c0(%rbp)
jmp 0x4a5a
movq -0x23c0(%rbp), %rsi
leaq -0x210(%rbp), %rdi
callq 0x5a60
leaq -0x210(%rbp), %rdi
callq 0x5a90
testb $0x1, %al
jne 0x4ae7
movq 0x34554(%rip), %rax     # 0x38fd8
movq (%rax), %rax
movq %rax, -0x23c8(%rbp)
leaq -0x200(%rbp), %rdi
callq 0x4140
movq -0x23c8(%rbp), %rdi
movq %rax, %rdx
leaq 0x20f25(%rip), %rsi     # 0x259d0
movb $0x0, %al
callq 0x4480
movl $0x1, -0x4(%rbp)
movl $0x1, -0x64(%rbp)
jmp 0x4c7f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x4ccd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x4cc1
jmp 0x4ae9
leaq -0x210(%rbp), %rdi
callq 0x5ac0
movq %rax, %rcx
leaq -0x2210(%rbp), %rdi
movl $0x1, %esi
movl $0x2000, %edx           # imm = 0x2000
callq 0x44b0
movq %rax, -0x23d0(%rbp)
jmp 0x4b17
movq -0x23d0(%rbp), %rax
movq %rax, -0x2218(%rbp)
cmpq $0x0, %rax
jbe 0x4bb7
movq -0x2218(%rbp), %rcx
movl -0x204(%rbp), %eax
addq %rcx, %rax
movl %eax, -0x204(%rbp)
movq $0x0, -0x2220(%rbp)
movq -0x2220(%rbp), %rax
cmpq -0x2218(%rbp), %rax
jae 0x4bb2
movq -0x18(%rbp), %rdi
movq -0x2220(%rbp), %rax
movzbl -0x2210(%rbp,%rax), %edx
leaq 0x20e7a(%rip), %rsi     # 0x259f4
movb $0x0, %al
callq 0x4480
movq -0x2220(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x2220(%rbp)
jmp 0x4b50
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x210(%rbp), %rdi
callq 0x5b50
jmp 0x4cc1
jmp 0x4ae9
leaq -0x210(%rbp), %rdi
callq 0x5ac0
movq %rax, %rdi
callq 0x4590
cmpl $0x0, %eax
je 0x4c15
movq 0x34401(%rip), %rax     # 0x38fd8
movq (%rax), %rax
movq %rax, -0x23d8(%rbp)
leaq -0x200(%rbp), %rdi
callq 0x4140
movq -0x23d8(%rbp), %rdi
movq %rax, %rdx
leaq 0x20dfa(%rip), %rsi     # 0x259f8
movb $0x0, %al
callq 0x4480
movl $0x1, -0x4(%rbp)
movl $0x1, -0x64(%rbp)
jmp 0x4c7f
xorl %eax, %eax
movl %eax, -0x23e8(%rbp)
movl %eax, %esi
leaq -0x210(%rbp), %rdi
callq 0x5af0
movl -0x23e8(%rbp), %eax
movq -0x18(%rbp), %rdi
leaq 0x20e48(%rip), %rsi     # 0x25a84
callq 0x4480
movl -0x204(%rbp), %eax
movl %eax, -0x23e4(%rbp)
leaq -0xc0(%rbp), %rdi
leaq -0x1bc(%rbp), %rsi
callq 0x5b20
movq %rax, -0x23e0(%rbp)
jmp 0x4c69
movq -0x23e0(%rbp), %rax
movl -0x23e4(%rbp), %ecx
movl %ecx, (%rax)
movl $0x0, -0x64(%rbp)
leaq -0x210(%rbp), %rdi
callq 0x5b50
leaq -0x200(%rbp), %rdi
callq 0x4200
leaq -0x1e0(%rbp), %rdi
callq 0x4200
movl -0x64(%rbp), %eax
testl %eax, %eax
jne 0x4cdb
jmp 0x4cac
jmp 0x4cae
leaq -0x140(%rbp), %rdi
callq 0x5ba0
jmp 0x4cbc
jmp 0x490d
leaq -0x200(%rbp), %rdi
callq 0x4200
leaq -0x1e0(%rbp), %rdi
callq 0x4200
jmp 0x4d00
leaq -0x1b0(%rbp), %rdi
callq 0x5bd0
leaq -0x140(%rbp), %rdi
callq 0x5bd0
movl -0x64(%rbp), %eax
subl $0x4, %eax
je 0x4d1d
jmp 0x50e5
leaq -0x1b0(%rbp), %rdi
callq 0x5bd0
leaq -0x140(%rbp), %rdi
callq 0x5bd0
jmp 0x50ff
movq -0x18(%rbp), %rdi
leaq 0x20cf4(%rip), %rsi     # 0x25a1c
movb $0x0, %al
callq 0x4480
movq -0x18(%rbp), %rdi
leaq 0x20d4e(%rip), %rsi     # 0x25a88
movb $0x0, %al
callq 0x4480
leaq -0x60(%rbp), %rdi
callq 0x5540
movq %rax, -0x2230(%rbp)
leaq -0x2230(%rbp), %rax
movq %rax, -0x2228(%rbp)
movq -0x2228(%rbp), %rsi
leaq -0x22a0(%rbp), %rdi
callq 0x5570
movq -0x2228(%rbp), %rsi
leaq -0x2310(%rbp), %rdi
callq 0x55c0
leaq -0x22a0(%rbp), %rdi
leaq -0x2310(%rbp), %rsi
callq 0x5610
movb %al, -0x23e9(%rbp)
jmp 0x4da0
movb -0x23e9(%rbp), %al
testb $0x1, %al
jne 0x4dc9
jmp 0x4dac
movl $0xb, -0x64(%rbp)
jmp 0x505d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x507f
leaq -0x22a0(%rbp), %rdi
callq 0x5640
movq %rax, -0x23f8(%rbp)
jmp 0x4dde
movq -0x23f8(%rbp), %rax
movq %rax, -0x2318(%rbp)
movq -0x2318(%rbp), %rdi
callq 0x5690
movq %rax, -0x2400(%rbp)
jmp 0x4e01
movq -0x2400(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x5650
movl %eax, -0x2404(%rbp)
jmp 0x4e1e
movl -0x2404(%rbp), %eax
movl %eax, -0x231c(%rbp)
movq -0x2318(%rbp), %rdi
callq 0x5750
movq %rax, -0x2410(%rbp)
jmp 0x4e3f
movq -0x2410(%rbp), %rdi
leaq 0x20c5c(%rip), %rsi     # 0x25aa9
callq 0x5770
movq %rax, -0x2418(%rbp)
jmp 0x4e5b
movq -0x2418(%rbp), %rsi
leaq -0x2340(%rbp), %rdi
callq 0x59b0
jmp 0x4e70
movq -0x2318(%rbp), %rdi
callq 0x5750
movq %rax, -0x2420(%rbp)
jmp 0x4e85
movq -0x2420(%rbp), %rdi
leaq 0x20b0d(%rip), %rsi     # 0x259a0
callq 0x5770
movq %rax, -0x2428(%rbp)
jmp 0x4ea1
movq -0x2428(%rbp), %rsi
leaq -0x2360(%rbp), %rdi
callq 0x59b0
jmp 0x4eb6
leaq -0xc0(%rbp), %rdi
leaq -0x231c(%rbp), %rsi
callq 0x5c00
movq %rax, -0x2430(%rbp)
jmp 0x4ed2
movq -0x2430(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x2364(%rbp)
leaq -0x2340(%rbp), %rdi
callq 0x4250
cmpq $0x4, %rax
jae 0x4f36
leaq -0x2340(%rbp), %rdi
movl $0x20, %esi
callq 0x4210
jmp 0x4f06
jmp 0x4ee1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x504f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x2360(%rbp), %rdi
callq 0x4200
jmp 0x504f
leaq -0x2340(%rbp), %rdi
callq 0x4250
cmpq $0x4, %rax
je 0x4f90
movq 0x34089(%rip), %rax     # 0x38fd8
movq (%rax), %rax
movq %rax, -0x2438(%rbp)
leaq -0x2340(%rbp), %rdi
callq 0x4140
movq -0x2438(%rbp), %rdi
movq %rax, %rdx
leaq 0x20b38(%rip), %rsi     # 0x25aae
movb $0x0, %al
callq 0x4480
movl $0x1, -0x4(%rbp)
movl $0x1, -0x64(%rbp)
jmp 0x5019
movq -0x18(%rbp), %rax
movq %rax, -0x2458(%rbp)
movl -0x231c(%rbp), %eax
movl %eax, -0x244c(%rbp)
leaq -0x2340(%rbp), %rdi
callq 0x4140
movq %rax, -0x2448(%rbp)
movl -0x231c(%rbp), %eax
movl %eax, -0x2440(%rbp)
movl -0x2364(%rbp), %eax
movl %eax, -0x243c(%rbp)
leaq -0x2360(%rbp), %rdi
callq 0x4140
movq -0x2458(%rbp), %rdi
movl -0x244c(%rbp), %edx
movq -0x2448(%rbp), %rcx
movl -0x2440(%rbp), %r8d
movl -0x243c(%rbp), %r9d
leaq 0x20ac9(%rip), %rsi     # 0x25ad0
movq %rax, (%rsp)
movb $0x0, %al
callq 0x4480
movl $0x0, -0x64(%rbp)
leaq -0x2360(%rbp), %rdi
callq 0x4200
leaq -0x2340(%rbp), %rdi
callq 0x4200
movl -0x64(%rbp), %eax
testl %eax, %eax
jne 0x505d
jmp 0x503a
jmp 0x503c
leaq -0x22a0(%rbp), %rdi
callq 0x5ba0
jmp 0x504a
jmp 0x4d85
leaq -0x2340(%rbp), %rdi
callq 0x4200
jmp 0x507f
leaq -0x2310(%rbp), %rdi
callq 0x5bd0
leaq -0x22a0(%rbp), %rdi
callq 0x5bd0
movl -0x64(%rbp), %eax
subl $0xb, %eax
je 0x5099
jmp 0x50e5
leaq -0x2310(%rbp), %rdi
callq 0x5bd0
leaq -0x22a0(%rbp), %rdi
callq 0x5bd0
jmp 0x50ff
movq -0x18(%rbp), %rdi
leaq 0x209e0(%rip), %rsi     # 0x25a84
movb $0x0, %al
callq 0x4480
movq -0x18(%rbp), %rax
movq %rax, -0x2460(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x5c30
movq -0x2460(%rbp), %rdi
movq %rax, %rdx
leaq 0x20a23(%rip), %rsi     # 0x25af3
movb $0x0, %al
callq 0x4480
movl $0x0, -0x4(%rbp)
movl $0x1, -0x64(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x5cb0
leaq -0x88(%rbp), %rdi
callq 0x4200
jmp 0x5119
leaq -0xc0(%rbp), %rdi
callq 0x5cb0
leaq -0x88(%rbp), %rdi
callq 0x4200
jmp 0x512d
leaq -0x60(%rbp), %rdi
callq 0x5cd0
leaq -0x38(%rbp), %rdi
callq 0x4200
jmp 0x5141
leaq -0x60(%rbp), %rdi
callq 0x5cd0
leaq -0x38(%rbp), %rdi
callq 0x4200
jmp 0x514d
movl -0x4(%rbp), %eax
addq $0x2470, %rsp           # imm = 0x2470
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x4550
nopw %cs:(%rax,%rax)