// File: /workspace/github_c_makefile_star2_O1/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 $0x88, %rsp
movq $0x0, 0x28(%rsp)
cmpl $0x3, %edi
jae 0x9040
leaq 0x7e3b1(%rip), %rbx     # 0x873ec
jmp 0x9309
movl %edx, %ebx
movq %rsi, %r14
jne 0x904b
testb %bl, %bl
je 0x90a0
leaq 0xa31b6(%rip), %rdi     # 0xac208
leaq 0x7e346(%rip), %rsi     # 0x8739f
callq 0x939c
testl %eax, %eax
je 0x906e
leaq 0x7e33f(%rip), %rbx     # 0x873a8
jmp 0x9309
movq 0x10(%r14), %rsi
leaq 0x38(%rsp), %r15
movq %r15, %rdi
xorl %edx, %edx
callq 0x992d
movq (%r15), %rsi
testq %rsi, %rsi
je 0x90ac
cmpq $0x101000, 0x40(%rsp)   # imm = 0x101000
jae 0x90b8
leaq 0x7e32e(%rip), %rbx     # 0x873c9
jmp 0x92e5
leaq 0x7e389(%rip), %rbx     # 0x87430
jmp 0x9309
leaq 0x7e304(%rip), %rbx     # 0x873b7
jmp 0x92fd
leaq 0x32(%rsp), %rdi
callq 0x99d8
movq 0xa313f(%rip), %rdi     # 0xac208
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
callq 0x96d2
testl %eax, %eax
je 0x90e8
leaq 0x7e2f4(%rip), %rbx     # 0x873d7
jmp 0x92e5
movq $0x0, 0x20(%rsp)
testb %bl, %bl
je 0x9125
movq 0xa310c(%rip), %rdi     # 0xac208
leaq 0x7e666(%rip), %rsi     # 0x87769
leaq 0x20(%rsp), %rcx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
callq 0x14ef3
testl %eax, %eax
je 0x9160
leaq 0x7e67a(%rip), %rbx     # 0x8779a
jmp 0x92db
movq 0x18(%r14), %rdi
callq 0x82d0
movl %eax, %ebp
movq 0xa30d1(%rip), %rdi     # 0xac208
leaq 0x7e673(%rip), %rsi     # 0x877b1
leaq 0x20(%rsp), %rcx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
callq 0x14ef3
testl %eax, %eax
je 0x9162
leaq 0x7e686(%rip), %rbx     # 0x877e1
jmp 0x92db
jmp 0x9183
movq 0x20(%rsp), %rdi
movl $0x1, %esi
movl %ebp, %edx
callq 0x124d1
testl %eax, %eax
je 0x9183
leaq 0x7e67a(%rip), %rbx     # 0x877f8
jmp 0x92db
testb %bl, %bl
jne 0x919e
movq 0xa307a(%rip), %rdi     # 0xac208
movl %ebp, %esi
callq 0x9606
cmpl $-0x1, %eax
je 0x92af
leaq 0x28(%rsp), %r15
leaq 0x32(%rsp), %r12
leaq 0x7e657(%rip), %rbx     # 0x87806
movq 0x20(%rsp), %rdi
callq 0x10e17
cmpl $0x14, %eax
jle 0x928a
cmpl $0x64, %eax
jne 0x92a1
movq 0x20(%rsp), %rdi
xorl %esi, %esi
callq 0x11faa
movl %eax, %r13d
movq 0x20(%rsp), %rdi
movl $0x1, %esi
callq 0x11faa
movl %eax, %ebp
movq 0x20(%rsp), %rdi
movl $0x2, %esi
callq 0x11faa
movl %eax, %r14d
movq %r15, %rdi
leaq 0x7e263(%rip), %rsi     # 0x8746a
movq %r12, %rdx
movl %r13d, %ecx
xorl %eax, %eax
callq 0x8240
cmpl $-0x1, %eax
je 0x92db
movq 0x28(%rsp), %rsi
movslq %r14d, %r14
leaq 0x70(%rsp), %rdi
movq %r14, %rdx
callq 0x9884
movq 0x80(%rsp), %rax
movq %rax, 0x60(%rsp)
movups 0x70(%rsp), %xmm0
movaps %xmm0, 0x50(%rsp)
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x92ca
movslq %ebp, %rsi
addq 0x38(%rsp), %rsi
movq %r14, %rdx
callq 0x81d0
movq 0x28(%rsp), %rdi
callq 0x8050
movq 0x60(%rsp), %rax
movq %rax, 0x10(%rsp)
movaps 0x50(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0x99be
jmp 0x91af
cmpl $0x5, %eax
je 0x91af
cmpl $0x1, %eax
jne 0x92c1
leaq 0x7e03c(%rip), %rbx     # 0x872db
jmp 0x92db
cmpl $0x15, %eax
je 0x92b8
cmpl $0x65, %eax
jne 0x92c1
xorl %ebx, %ebx
jmp 0x92db
leaq 0x7e199(%rip), %rbx     # 0x8744f
jmp 0x92db
leaq 0x7e03f(%rip), %rbx     # 0x872fe
jmp 0x92db
leaq 0x7e05a(%rip), %rbx     # 0x87322
jmp 0x92db
movq 0x28(%rsp), %rdi
callq 0x8050
leaq 0x7e19f(%rip), %rbx     # 0x8747a
movq 0x20(%rsp), %rdi
callq 0x100e6
movq 0x48(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x38(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0x99be
movq 0xa2f04(%rip), %rdi     # 0xac208
callq 0x93c8
movq %rbx, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq