// File: /workspace/github_c_makefile_star2_O3/jkbenaim[P]psfrag/psfrag.c
// Function: cmd_extract_aux
char *_cmd_extract_aux(int argc, char **argv, bool all)
{
	__label__ out_return, out_dbclose, out_unmap, out_finalize;
	char *msg = NULL;
	int rc;
	struct MappedFile_s m, outfile;
	int num, addr, size;
	char *outname = NULL;
	char pcode[6];

	switch (argc) {
	case 0 ... 2:
		msg = "must specify a Pokemon Stadium rom";
		goto out_return;
		break;
	case 3:
		if (!all) {
			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;
	}

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

	sqlite3_stmt *stmt = NULL;
	if (all) {
		rc = sqlite3_prepare_v2(
			db,
			"select num,addr,romsize from frags order by num;",
			-1, &stmt, NULL
		);
		if (rc != SQLITE_OK) {
			msg = "error in prepare (all)";
			goto out_finalize;
		}
	} else {
		num = atoi(argv[3]);
		rc = sqlite3_prepare_v2(
			db,
			"select num,addr,romsize from frags where num=?;",
			-1, &stmt, NULL
		);
		if (rc != SQLITE_OK) {
			msg = "error in prepare (one)";
			goto out_finalize;
		}
		rc = sqlite3_bind_int(stmt, 1, num);
		if (rc != SQLITE_OK) {
			msg = "error in bind";
			goto out_finalize;
		}
	}

	if (!all and (DB_GetAddrForNum(db, num) == -1)) {
		msg = "no fragment by that number";
		goto out_finalize;
	}

	while (SQLITE_DONE != (rc=sqlite3_step(stmt))) switch (rc) {
	case SQLITE_BUSY:
		break;
	case SQLITE_ERROR:
		msg = "sqlite3_step returned SQLITE_ERROR";
		goto out_finalize;
		break;
	case SQLITE_MISUSE:
		msg = "sqlite3_step returned SQLITE_MISUSE";
		goto out_finalize;
		break;
	default:
		msg = "sqlite3_step returned some other error";
		goto out_finalize;
		break;
	case SQLITE_ROW:
		num  = sqlite3_column_int(stmt, 0);
		addr = sqlite3_column_int(stmt, 1);
		size = sqlite3_column_int(stmt, 2);
		rc = asprintf(&outname, "%s-frag%03d.bin", pcode, num);
		if (rc == -1) {
			msg = "asprintf failed";
			goto out_finalize;
		}
		outfile = MappedFile_Create(outname, size);
		if (!outfile.data) {
			free(outname);
			msg = "couldn't open outfile";
			goto out_finalize;
		}
		memcpy(outfile.data, m.data + addr, size);
		free(outname);
		MappedFile_Close(outfile);
		break;
	}


out_finalize:
	sqlite3_finalize(stmt);
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 $0x78, %rsp
movq $0x0, 0x20(%rsp)
cmpl $0x3, %edi
jae 0x9094
leaq 0x7d35d(%rip), %r14     # 0x863ec
jmp 0x9356
movl %edx, %ebp
movq %rsi, %r14
jne 0x90a0
testb %bpl, %bpl
je 0x90f8
leaq 0xa2161(%rip), %rdi     # 0xab208
leaq 0x7d2f1(%rip), %rsi     # 0x8639f
callq 0x93e4
testl %eax, %eax
je 0x90c3
leaq 0x7d2ea(%rip), %r14     # 0x863a8
jmp 0x9356
movq 0x10(%r14), %rsi
leaq 0x48(%rsp), %rbx
movq %rbx, %rdi
xorl %edx, %edx
callq 0x9919
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x9104
movq 0x50(%rsp), %r15
cmpq $0x101000, %r15         # imm = 0x101000
jae 0x9110
leaq 0x7d2d6(%rip), %r14     # 0x863c9
jmp 0x9332
leaq 0x7d331(%rip), %r14     # 0x86430
jmp 0x9356
leaq 0x7d2ac(%rip), %r14     # 0x863b7
jmp 0x934a
leaq 0x2a(%rsp), %rdi
movq %rbx, %rsi
callq 0x99c4
movq 0xa20e4(%rip), %rdi     # 0xab208
movq %rbx, %rsi
movq %r15, %rdx
callq 0x96c6
testl %eax, %eax
je 0x913f
leaq 0x7d29d(%rip), %r14     # 0x863d7
jmp 0x9332
movq $0x0, 0x18(%rsp)
testb %bpl, %bpl
je 0x917d
movq 0xa20b4(%rip), %rdi     # 0xab208
leaq 0x7d60e(%rip), %rsi     # 0x86769
leaq 0x18(%rsp), %rcx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
callq 0x14dad
testl %eax, %eax
je 0x91f0
leaq 0x7d622(%rip), %r14     # 0x8679a
jmp 0x9328
movq 0x18(%r14), %rdi
callq 0x82d0
movl %eax, %ebp
movq 0xa2079(%rip), %rdi     # 0xab208
leaq 0x7d61b(%rip), %rsi     # 0x867b1
leaq 0x18(%rsp), %rcx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
callq 0x14dad
testl %eax, %eax
je 0x91b8
leaq 0x7d62e(%rip), %r14     # 0x867e1
jmp 0x9328
movq 0x18(%rsp), %rdi
movl $0x1, %esi
movl %ebp, %edx
callq 0x1243c
testl %eax, %eax
je 0x91d9
leaq 0x7d624(%rip), %r14     # 0x867f8
jmp 0x9328
movq 0xa2028(%rip), %rdi     # 0xab208
movl %ebp, %esi
callq 0x9624
cmpl $-0x1, %eax
je 0x9321
leaq 0x7d0e4(%rip), %r14     # 0x862db
leaq 0x2a(%rsp), %r13
movq 0x18(%rsp), %rdi
callq 0x10dca
cmpl $0x14, %eax
jle 0x92d4
cmpl $0x64, %eax
jne 0x92e4
movq 0x18(%rsp), %rdi
xorl %esi, %esi
callq 0x11ee9
movl %eax, %r12d
movq 0x18(%rsp), %rdi
movl $0x1, %esi
callq 0x11ee9
movl %eax, %r15d
movq 0x18(%rsp), %rdi
movl $0x2, %esi
callq 0x11ee9
movl %eax, %ebp
leaq 0x20(%rsp), %rdi
leaq 0x7d214(%rip), %rsi     # 0x8646a
movq %r13, %rdx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x8240
cmpl $-0x1, %eax
je 0x9305
movq 0x20(%rsp), %rsi
movslq %ebp, %rbp
leaq 0x60(%rsp), %rdi
movq %rbp, %rdx
callq 0x9870
movq 0x70(%rsp), %rax
movq %rax, 0x40(%rsp)
movups 0x60(%rsp), %xmm0
movaps %xmm0, 0x30(%rsp)
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x930e
movslq %r15d, %rsi
addq %rbx, %rsi
movq %rbp, %rdx
callq 0x81d0
movq 0x20(%rsp), %rdi
callq 0x8050
movq 0x40(%rsp), %rax
movq %rax, 0x10(%rsp)
movaps 0x30(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0x99aa
jmp 0x91fc
cmpl $0x5, %eax
je 0x91fc
cmpl $0x1, %eax
je 0x9328
jmp 0x92fc
cmpl $0x15, %eax
je 0x92f3
cmpl $0x65, %eax
jne 0x92fc
xorl %r14d, %r14d
jmp 0x9328
leaq 0x7d004(%rip), %r14     # 0x862fe
jmp 0x9328
leaq 0x7d01f(%rip), %r14     # 0x86322
jmp 0x9328
leaq 0x7d4fa(%rip), %r14     # 0x86806
jmp 0x9328
movq 0x20(%rsp), %rdi
callq 0x8050
leaq 0x7d15b(%rip), %r14     # 0x8647a
jmp 0x9328
leaq 0x7d127(%rip), %r14     # 0x8644f
movq 0x18(%rsp), %rdi
callq 0x100a7
movq 0x58(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x48(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0x99aa
movq 0xa1eb7(%rip), %rdi     # 0xab208
callq 0x9410
movq %r14, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq