// File: /workspace/github_c_makefile_star2_O2/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 %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
andq $0x0, 0x20(%rsp)
andq $0x0, 0x28(%rsp)
cmpl $0x4, %edi
jae 0x8b69
movl %edi, %eax
leaq 0x923e0(%rip), %rcx     # 0x9af40
movq (%rcx,%rax,8), %rbx
jmp 0x8c05
movq %rsi, %r14
leaq 0x96695(%rip), %rdi     # 0x9f208
leaq 0x6b825(%rip), %rsi     # 0x7439f
callq 0x92a8
testl %eax, %eax
je 0x8b8c
leaq 0x6b81e(%rip), %rbx     # 0x743a8
jmp 0x8c05
movq 0x10(%r14), %rsi
leaq 0x30(%rsp), %rbx
movq %rbx, %rdi
xorl %edx, %edx
callq 0x97bb
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x8bbe
movq 0x38(%rsp), %rdx
cmpq $0x101000, %rdx         # imm = 0x101000
jae 0x8bc7
leaq 0x6b80d(%rip), %rbx     # 0x743c9
jmp 0x8be1
leaq 0x6b7f2(%rip), %rbx     # 0x743b7
jmp 0x8bf9
movq 0x9663a(%rip), %rdi     # 0x9f208
movq %rbx, %rsi
callq 0x959b
testl %eax, %eax
je 0x8c17
leaq 0x6b7f6(%rip), %rbx     # 0x743d7
movq 0x40(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x30(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0x9845
movq 0x96608(%rip), %rdi     # 0x9f208
callq 0x92d4
movq %rbx, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%r14), %rdi
callq 0x82d0
movl %eax, %r14d
movq 0x965de(%rip), %rdi     # 0x9f208
movl %eax, %esi
callq 0x94e2
movl %eax, %ebp
movq 0x965ce(%rip), %rdi     # 0x9f208
movl %r14d, %esi
callq 0x9429
cmpl $-0x1, %ebp
je 0x8d13
movl %eax, %r15d
leaq 0x4a(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x9860
leaq 0x6b805(%rip), %rsi     # 0x7446a
leaq 0x20(%rsp), %r13
movq %r13, %rdi
movq %r12, %rdx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x8240
movq (%r13), %rsi
movslq %r15d, %r14
leaq 0x50(%rsp), %r15
movq %r15, %rdi
movq %r14, %rdx
callq 0x9718
movq (%r15), %r15
testq %r15, %r15
je 0x8d1f
movslq %ebp, %rax
addq %rax, %rbx
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x81d0
movq %r15, %rdi
callq 0x96f0
movl %eax, %ebp
movq 0x60(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x50(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0x9845
movq 0x20(%rsp), %r8
leaq 0x6b7b1(%rip), %rsi     # 0x74490
xorl %ebx, %ebx
leaq 0x28(%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 0x20(%rsp), %rdi
callq 0x8050
jmp 0x8be1
leaq 0x6b735(%rip), %rbx     # 0x7444f
jmp 0x8be1
leaq 0x6b754(%rip), %rbx     # 0x7447a
jmp 0x8be1