// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xd0, %rsp
movl %edi, -0xc(%rbp)
movq %rsi, -0x18(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
movl -0xc(%rbp), %eax
movl %eax, -0xb4(%rbp)
subl $0x3, %eax
jb 0x8fe7
jmp 0x8fda
movl -0xb4(%rbp), %eax
subl $0x3, %eax
je 0x8ff7
jmp 0x9007
leaq 0xad423(%rip), %rax     # 0xb6411
movq %rax, -0x60(%rbp)
jmp 0x9231
leaq 0xad457(%rip), %rax     # 0xb6455
movq %rax, -0x60(%rbp)
jmp 0x9231
jmp 0x9009
leaq 0xd4308(%rip), %rdi     # 0xdd318
leaq 0xad3ad(%rip), %rsi     # 0xb63c4
callq 0x9aa0
movl %eax, -0x7c(%rbp)
cmpl $0x0, -0x7c(%rbp)
je 0x9035
leaq 0xad3a1(%rip), %rax     # 0xb63cd
movq %rax, -0x60(%rbp)
jmp 0x9231
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
leaq -0x98(%rbp), %rdi
xorl %edx, %edx
callq 0xa410
movq -0x98(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x90(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x9083
leaq 0xad362(%rip), %rax     # 0xb63dc
movq %rax, -0x60(%rbp)
jmp 0x9225
cmpq $0x101000, -0x28(%rbp)  # imm = 0x101000
jae 0x909d
leaq 0xad35a(%rip), %rax     # 0xb63ee
movq %rax, -0x60(%rbp)
jmp 0x9203
movq 0xd4274(%rip), %rdi     # 0xdd318
movq -0x30(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0xa020
movl %eax, -0x7c(%rbp)
cmpl $0x0, -0x7c(%rbp)
je 0x90ca
leaq 0xad33b(%rip), %rax     # 0xb63fc
movq %rax, -0x60(%rbp)
jmp 0x9203
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x82c0
movl %eax, -0x4c(%rbp)
movq 0xd4237(%rip), %rdi     # 0xdd318
movl -0x4c(%rbp), %esi
callq 0x9f00
movl %eax, -0x50(%rbp)
movq 0xd4225(%rip), %rdi     # 0xdd318
movl -0x4c(%rbp), %esi
callq 0x9de0
movl %eax, -0x54(%rbp)
cmpl $-0x1, -0x50(%rbp)
jne 0x9114
leaq 0xad369(%rip), %rax     # 0xb6474
movq %rax, -0x60(%rbp)
jmp 0x9203
leaq -0x76(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0xa540
leaq -0x76(%rbp), %rdx
movl -0x4c(%rbp), %ecx
leaq -0x68(%rbp), %rdi
leaq 0xad35c(%rip), %rsi     # 0xb648f
movb $0x0, %al
callq 0x8240
movl %eax, -0x7c(%rbp)
movq -0x68(%rbp), %rsi
movslq -0x54(%rbp), %rdx
leaq -0xb0(%rbp), %rdi
callq 0xa300
movq -0xb0(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0xa8(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0xa0(%rbp), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x9186
leaq 0xad31f(%rip), %rax     # 0xb649f
movq %rax, -0x60(%rbp)
jmp 0x9203
movq -0x48(%rbp), %rdi
movq -0x30(%rbp), %rsi
movslq -0x50(%rbp), %rax
addq %rax, %rsi
movslq -0x54(%rbp), %rdx
callq 0x81d0
movq -0x48(%rbp), %rdi
callq 0xa290
movl %eax, -0x58(%rbp)
leaq -0x48(%rbp), %rax
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0xa510
movl -0x58(%rbp), %edx
movl -0x58(%rbp), %ecx
movq -0x68(%rbp), %r8
leaq -0x70(%rbp), %rdi
leaq 0xad2d4(%rip), %rsi     # 0xb64b5
movb $0x0, %al
callq 0x8240
movq -0x70(%rbp), %rdi
callq 0x80c0
movq -0x70(%rbp), %rdi
callq 0x8050
movq -0x68(%rbp), %rdi
callq 0x8050
leaq -0x30(%rbp), %rax
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0xa510
movq 0xd40ec(%rip), %rdi     # 0xdd318
callq 0x9b20
cmpq $0x0, -0x60(%rbp)
je 0x9242
movq -0x60(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x924a
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xd0, %rsp
popq %rbp
retq
nopw (%rax,%rax)