// File: /workspace/github_c_makefile_star2_O2/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
andq $0x0, 0x20(%rsp)
cmpl $0x3, %edi
jae 0x8f75
leaq 0x6b47c(%rip), %r14     # 0x743ec
jmp 0x921c
movl %edx, %ebp
movq %rsi, %r14
jne 0x8f81
testb %bpl, %bpl
je 0x8fd9
leaq 0x96280(%rip), %rdi     # 0x9f208
leaq 0x6b410(%rip), %rsi     # 0x7439f
callq 0x92a8
testl %eax, %eax
je 0x8fa4
leaq 0x6b409(%rip), %r14     # 0x743a8
jmp 0x921c
movq 0x10(%r14), %rsi
leaq 0x48(%rsp), %rbx
movq %rbx, %rdi
xorl %edx, %edx
callq 0x97bb
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x8fe5
movq 0x50(%rsp), %r15
cmpq $0x101000, %r15         # imm = 0x101000
jae 0x8ff1
leaq 0x6b3f5(%rip), %r14     # 0x743c9
jmp 0x91f8
leaq 0x6b450(%rip), %r14     # 0x74430
jmp 0x921c
leaq 0x6b3cb(%rip), %r14     # 0x743b7
jmp 0x9210
leaq 0x2a(%rsp), %rdi
movq %rbx, %rsi
callq 0x9860
movq 0x96203(%rip), %rdi     # 0x9f208
movq %rbx, %rsi
movq %r15, %rdx
callq 0x959b
testl %eax, %eax
je 0x9020
leaq 0x6b3bc(%rip), %r14     # 0x743d7
jmp 0x91f8
andq $0x0, 0x18(%rsp)
testb %bpl, %bpl
je 0x9059
movq 0x961d6(%rip), %rdi     # 0x9f208
leaq 0x6b730(%rip), %rsi     # 0x74769
pushq $-0x1
popq %rdx
leaq 0x18(%rsp), %rcx
xorl %r8d, %r8d
callq 0x13571
testl %eax, %eax
je 0x90c8
leaq 0x6b746(%rip), %r14     # 0x7479a
jmp 0x91ee
movq 0x18(%r14), %rdi
callq 0x82d0
movl %eax, %ebp
movq 0x9619d(%rip), %rdi     # 0x9f208
leaq 0x6b73f(%rip), %rsi     # 0x747b1
pushq $-0x1
popq %rdx
leaq 0x18(%rsp), %rcx
xorl %r8d, %r8d
callq 0x13571
testl %eax, %eax
je 0x9092
leaq 0x6b754(%rip), %r14     # 0x747e1
jmp 0x91ee
movq 0x18(%rsp), %rdi
pushq $0x1
popq %rsi
movl %ebp, %edx
callq 0x11113
testl %eax, %eax
je 0x90b1
leaq 0x6b74c(%rip), %r14     # 0x747f8
jmp 0x91ee
movq 0x96150(%rip), %rdi     # 0x9f208
movl %ebp, %esi
callq 0x94e2
cmpl $-0x1, %eax
je 0x91e7
leaq 0x6b20c(%rip), %r14     # 0x742db
leaq 0x2a(%rsp), %rbp
movq 0x18(%rsp), %rdi
callq 0xfe1e
cmpl $0x5, %eax
je 0x90d4
cmpl $0x64, %eax
jne 0x91a5
movq 0x18(%rsp), %rdi
xorl %esi, %esi
callq 0x10cc5
movl %eax, %r13d
movq 0x18(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x10cc5
movl %eax, %r12d
movq 0x18(%rsp), %rdi
pushq $0x2
popq %rsi
callq 0x10cc5
movl %eax, %r15d
leaq 0x20(%rsp), %rdi
leaq 0x6b343(%rip), %rsi     # 0x7446a
movq %rbp, %rdx
movl %r13d, %ecx
xorl %eax, %eax
callq 0x8240
cmpl $-0x1, %eax
je 0x91cb
movq 0x20(%rsp), %rsi
movslq %r15d, %r15
leaq 0x60(%rsp), %rdi
movq %r15, %rdx
callq 0x9718
movq 0x70(%rsp), %rax
movq %rax, 0x40(%rsp)
movups 0x60(%rsp), %xmm0
movaps %xmm0, 0x30(%rsp)
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x91d4
movslq %r12d, %rsi
addq %rbx, %rsi
movq %r15, %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 0x9845
jmp 0x90d4
cmpl $0x1, %eax
je 0x91ee
cmpl $0x15, %eax
je 0x91b9
cmpl $0x65, %eax
jne 0x91c2
xorl %r14d, %r14d
jmp 0x91ee
leaq 0x6b13e(%rip), %r14     # 0x742fe
jmp 0x91ee
leaq 0x6b159(%rip), %r14     # 0x74322
jmp 0x91ee
leaq 0x6b634(%rip), %r14     # 0x74806
jmp 0x91ee
movq 0x20(%rsp), %rdi
callq 0x8050
leaq 0x6b295(%rip), %r14     # 0x7447a
jmp 0x91ee
leaq 0x6b261(%rip), %r14     # 0x7444f
movq 0x18(%rsp), %rdi
callq 0xf24e
movq 0x58(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x48(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0x9845
movq 0x95ff1(%rip), %rdi     # 0x9f208
callq 0x92d4
movq %r14, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq