// File: /workspace/github_c_makefile_star2_O1/jkbenaim[P]psfrag/psfrag.c
// Function: cmd_decompile
char *cmd_decompile(int argc, char **argv)
{
	__label__ out_return, out_dbclose, out_unmap;
	struct MappedFile_s m, outfile;
	int fragnum, fragaddr, fragsize, vma;
	char *msg = NULL, *outname = NULL, *command = NULL;
	char pcode[6];
	int rc;

	switch (argc) {
	case 0 ... 2:
		msg = "must specify a Pokemon Stadium rom";
		goto out_return;
		break;
	case 3:
		msg = "must specify a fragment number";
		goto out_return;
		break;
	default:
		break;
	}

	rc = DB_Init(&db, ":memory:");
	if (rc != SQLITE_OK) {
		msg = "DB_Init oopsed";
		goto out_return;
	}

	m = MappedFile_Open(argv[2], false);
	if (m.data == NULL) {
		msg = "couldn't open rom";
		goto out_dbclose;
	}

	if (m.size < (1048576 + 4096)) {
		msg = "rom too small";
		goto out_unmap;
	}

	rc = DB_FragSearch(db, m.data, m.size);
	if (rc != SQLITE_OK) {
		msg = "DB_FragSearch oopsed";
		goto out_unmap;
	}

	fragnum = atoi(argv[3]);
	fragaddr = DB_GetAddrForNum(db, fragnum);
	fragsize = DB_GetRomSizeForNum(db, fragnum);
	if (fragaddr == -1) {
		msg = "no fragment by that number";
		goto out_unmap;
	}

	get_pcode(pcode, m.data);
	rc = asprintf(&outname, "%s-frag%03d.bin", pcode, fragnum);
	outfile = MappedFile_Create(outname, fragsize);
	if (!outfile.data) {
		msg = "couldn't open outfile";
		goto out_unmap;
	}

	memcpy(outfile.data, m.data + fragaddr, fragsize);
	vma = get_vma(outfile.data);
	MappedFile_Close(outfile);

	asprintf(&command, "retdec-decompiler.py -k -a mips -e big -m raw --cleanup --backend-find-patterns all --backend-var-renamer simple --backend-no-debug-comments --raw-entry-point 0x%x --raw-section-vma 0x%x \"%s\"\n",
		vma,
		vma,
		outname
	);

	system(command);
	free(command);
	free(outname);
	goto out_unmap;


out_unmap:
	MappedFile_Close(m);
out_dbclose:
	DB_Close(db);
out_return:
	if (msg) {
		return msg;
	} else {
		return NULL;
	}

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x60, %rsp
xorl %eax, %eax
movq %rax, 0x18(%rsp)
movq %rax, 0x20(%rsp)
cmpl $0x4, %edi
jae 0x8c69
movl %edi, %eax
leaq 0x9f2c0(%rip), %rcx     # 0xa7f20
movq (%rcx,%rax,8), %rbx
jmp 0x8d02
movq %rsi, %rbx
leaq 0xa3595(%rip), %rdi     # 0xac208
leaq 0x7e725(%rip), %rsi     # 0x8739f
callq 0x939c
testl %eax, %eax
je 0x8c8c
leaq 0x7e71e(%rip), %rbx     # 0x873a8
jmp 0x8d02
movq 0x10(%rbx), %rsi
leaq 0x28(%rsp), %r14
movq %r14, %rdi
xorl %edx, %edx
callq 0x992d
movq (%r14), %rsi
testq %rsi, %rsi
je 0x8cbe
movq 0x30(%rsp), %rdx
cmpq $0x101000, %rdx         # imm = 0x101000
jae 0x8cc7
leaq 0x7e70d(%rip), %rbx     # 0x873c9
jmp 0x8cde
leaq 0x7e6f2(%rip), %rbx     # 0x873b7
jmp 0x8cf6
movq 0xa353a(%rip), %rdi     # 0xac208
callq 0x96d2
testl %eax, %eax
je 0x8d12
leaq 0x7e6f9(%rip), %rbx     # 0x873d7
movq 0x38(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x28(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0x99be
movq 0xa350b(%rip), %rdi     # 0xac208
callq 0x93c8
movq %rbx, %rax
addq $0x60, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%rbx), %rdi
callq 0x82d0
movl %eax, %ebp
movq 0xa34e4(%rip), %rdi     # 0xac208
movl %eax, %esi
callq 0x9606
movl %eax, %ebx
movq 0xa34d4(%rip), %rdi     # 0xac208
movl %ebp, %esi
callq 0x953a
cmpl $-0x1, %ebx
je 0x8e0b
movl %eax, %r14d
movq 0x28(%rsp), %rsi
leaq 0x5a(%rsp), %r15
movq %r15, %rdi
callq 0x99d8
leaq 0x7e70a(%rip), %rsi     # 0x8746a
leaq 0x18(%rsp), %r12
movq %r12, %rdi
movq %r15, %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x8240
movq (%r12), %rsi
movslq %r14d, %r14
leaq 0x40(%rsp), %r15
movq %r15, %rdi
movq %r14, %rdx
callq 0x9884
movq (%r15), %rdi
testq %rdi, %rdi
je 0x8e17
movslq %ebx, %rsi
addq 0x28(%rsp), %rsi
movq %r14, %rdx
callq 0x81d0
movq 0x40(%rsp), %rdi
callq 0x985a
movl %eax, %ebp
movq 0x50(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x40(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0x99be
movq 0x18(%rsp), %r8
leaq 0x7e6b9(%rip), %rsi     # 0x87490
xorl %ebx, %ebx
leaq 0x20(%rsp), %r14
movq %r14, %rdi
movl %ebp, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x8240
movq (%r14), %rdi
callq 0x80d0
movq (%r14), %rdi
callq 0x8050
movq 0x18(%rsp), %rdi
callq 0x8050
jmp 0x8cde
leaq 0x7e63d(%rip), %rbx     # 0x8744f
jmp 0x8cde
leaq 0x7e65c(%rip), %rbx     # 0x8747a
jmp 0x8cde