// File: /workspace/github_c_makefile_star2_O3/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
xorl %eax, %eax
movq %rax, 0x20(%rsp)
movq %rax, 0x28(%rsp)
cmpl $0x4, %edi
jae 0x8cba
movl %edi, %eax
leaq 0x9e26f(%rip), %rcx     # 0xa6f20
movq (%rcx,%rax,8), %rbx
jmp 0x8d56
movq %rsi, %r14
leaq 0xa2544(%rip), %rdi     # 0xab208
leaq 0x7d6d4(%rip), %rsi     # 0x8639f
callq 0x93e4
testl %eax, %eax
je 0x8cdd
leaq 0x7d6cd(%rip), %rbx     # 0x863a8
jmp 0x8d56
movq 0x10(%r14), %rsi
leaq 0x30(%rsp), %rbx
movq %rbx, %rdi
xorl %edx, %edx
callq 0x9919
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x8d0f
movq 0x38(%rsp), %rdx
cmpq $0x101000, %rdx         # imm = 0x101000
jae 0x8d18
leaq 0x7d6bc(%rip), %rbx     # 0x863c9
jmp 0x8d32
leaq 0x7d6a1(%rip), %rbx     # 0x863b7
jmp 0x8d4a
movq 0xa24e9(%rip), %rdi     # 0xab208
movq %rbx, %rsi
callq 0x96c6
testl %eax, %eax
je 0x8d68
leaq 0x7d6a5(%rip), %rbx     # 0x863d7
movq 0x40(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x30(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0x99aa
movq 0xa24b7(%rip), %rdi     # 0xab208
callq 0x9410
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 0xa248d(%rip), %rdi     # 0xab208
movl %eax, %esi
callq 0x9624
movl %eax, %ebp
movq 0xa247d(%rip), %rdi     # 0xab208
movl %r14d, %esi
callq 0x9582
cmpl $-0x1, %ebp
je 0x8e64
movl %eax, %r15d
leaq 0x4a(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x99c4
leaq 0x7d6b4(%rip), %rsi     # 0x8646a
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 0x9870
movq (%r15), %r15
testq %r15, %r15
je 0x8e70
movslq %ebp, %rax
addq %rax, %rbx
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x81d0
movq %r15, %rdi
callq 0x9846
movl %eax, %ebp
movq 0x60(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x50(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0x99aa
movq 0x20(%rsp), %r8
leaq 0x7d660(%rip), %rsi     # 0x86490
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 0x8d32
leaq 0x7d5e4(%rip), %rbx     # 0x8644f
jmp 0x8d32
leaq 0x7d603(%rip), %rbx     # 0x8647a
jmp 0x8d32