// File: jvanns[P]verbatim/sub/lmdb/libraries/liblmdb/mdb_dump.c
// Function: main
int main(int argc, char *argv[])
{
	int i, rc;
	MDB_env *env;
	MDB_txn *txn;
	MDB_dbi dbi;
	char *prog = argv[0];
	char *envname;
	char *subname = NULL;
	int alldbs = 0, envflags = 0, list = 0;

	if (argc < 2) {
		usage(prog);
	}

	/* -a: dump main DB and all subDBs
	 * -s: dump only the named subDB
	 * -n: use NOSUBDIR flag on env_open
	 * -p: use printable characters
	 * -f: write to file instead of stdout
	 * -V: print version and exit
	 * (default) dump only the main DB
	 */
	while ((i = getopt(argc, argv, "af:lnps:V")) != EOF) {
		switch(i) {
		case 'V':
			printf("%s\n", MDB_VERSION_STRING);
			exit(0);
			break;
		case 'l':
			list = 1;
			/*FALLTHROUGH*/;
		case 'a':
			if (subname)
				usage(prog);
			alldbs++;
			break;
		case 'f':
			if (freopen(optarg, "w", stdout) == NULL) {
				fprintf(stderr, "%s: %s: reopen: %s\n",
					prog, optarg, strerror(errno));
				exit(EXIT_FAILURE);
			}
			break;
		case 'n':
			envflags |= MDB_NOSUBDIR;
			break;
		case 'p':
			mode |= PRINT;
			break;
		case 's':
			if (alldbs)
				usage(prog);
			subname = optarg;
			break;
		default:
			usage(prog);
		}
	}

	if (optind != argc - 1)
		usage(prog);

#ifdef SIGPIPE
	signal(SIGPIPE, dumpsig);
#endif
#ifdef SIGHUP
	signal(SIGHUP, dumpsig);
#endif
	signal(SIGINT, dumpsig);
	signal(SIGTERM, dumpsig);

	envname = argv[optind];
	rc = mdb_env_create(&env);
	if (rc) {
		fprintf(stderr, "mdb_env_create failed, error %d %s\n", rc, mdb_strerror(rc));
		return EXIT_FAILURE;
	}

	if (alldbs || subname) {
		mdb_env_set_maxdbs(env, 2);
	}

	rc = mdb_env_open(env, envname, envflags | MDB_RDONLY, 0664);
	if (rc) {
		fprintf(stderr, "mdb_env_open failed, error %d %s\n", rc, mdb_strerror(rc));
		goto env_close;
	}

	rc = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn);
	if (rc) {
		fprintf(stderr, "mdb_txn_begin failed, error %d %s\n", rc, mdb_strerror(rc));
		goto env_close;
	}

	rc = mdb_open(txn, subname, 0, &dbi);
	if (rc) {
		fprintf(stderr, "mdb_open failed, error %d %s\n", rc, mdb_strerror(rc));
		goto txn_abort;
	}

	if (alldbs) {
		MDB_cursor *cursor;
		MDB_val key;
		int count = 0;

		rc = mdb_cursor_open(txn, dbi, &cursor);
		if (rc) {
			fprintf(stderr, "mdb_cursor_open failed, error %d %s\n", rc, mdb_strerror(rc));
			goto txn_abort;
		}
		while ((rc = mdb_cursor_get(cursor, &key, NULL, MDB_NEXT_NODUP)) == 0) {
			char *str;
			MDB_dbi db2;
			if (memchr(key.mv_data, '\0', key.mv_size))
				continue;
			count++;
			str = malloc(key.mv_size+1);
			memcpy(str, key.mv_data, key.mv_size);
			str[key.mv_size] = '\0';
			rc = mdb_open(txn, str, 0, &db2);
			if (rc == MDB_SUCCESS) {
				if (list) {
					printf("%s\n", str);
					list++;
				} else {
					rc = dumpit(txn, db2, str);
					if (rc)
						break;
				}
				mdb_close(env, db2);
			}
			free(str);
			if (rc) continue;
		}
		mdb_cursor_close(cursor);
		if (!count) {
			fprintf(stderr, "%s: %s does not contain multiple databases\n", prog, envname);
			rc = MDB_NOTFOUND;
		} else if (rc == MDB_NOTFOUND) {
			rc = MDB_SUCCESS;
		}
	} else {
		rc = dumpit(txn, dbi, subname);
	}
	if (rc && rc != MDB_NOTFOUND)
		fprintf(stderr, "%s: %s: %s\n", prog, envname, mdb_strerror(rc));

	mdb_close(env, dbi);
txn_abort:
	mdb_txn_abort(txn);
env_close:
	mdb_env_close(env);

	return rc ? EXIT_FAILURE : EXIT_SUCCESS;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq $0x0, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
cmpl $0x2, -0x8(%rbp)
jge 0x25b0
movq -0x38(%rbp), %rdi
callq 0x2bb0
jmp 0x25b2
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0x14b1e(%rip), %rdx     # 0x170de
callq 0x2350
movl %eax, -0x14(%rbp)
cmpl $-0x1, %eax
je 0x271d
movl -0x14(%rbp), %eax
addl $-0x56, %eax
movl %eax, %ecx
movq %rcx, -0x90(%rbp)
subl $0x1d, %eax
ja 0x270f
movq -0x90(%rbp), %rax
leaq 0x14a19(%rip), %rcx     # 0x17010
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x14b12(%rip), %rdi     # 0x17119
leaq 0x14ada(%rip), %rsi     # 0x170e8
movb $0x0, %al
callq 0x2130
xorl %edi, %edi
callq 0x23a0
movl $0x1, -0x54(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x2633
movq -0x38(%rbp), %rdi
callq 0x2bb0
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x2718
movq 0x18988(%rip), %rax     # 0x1afd0
movq (%rax), %rdi
movq 0x1896e(%rip), %rax     # 0x1afc0
movq (%rax), %rdx
leaq 0x14aab(%rip), %rsi     # 0x17107
callq 0x21a0
cmpq $0x0, %rax
jne 0x26d0
movq 0x18982(%rip), %rax     # 0x1aff0
movq (%rax), %rax
movq %rax, -0xa8(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq 0x18946(%rip), %rax     # 0x1afd0
movq (%rax), %rax
movq %rax, -0x98(%rbp)
callq 0x2060
movl (%rax), %edi
callq 0x23f0
movq -0xa8(%rbp), %rdi
movq -0xa0(%rbp), %rdx
movq -0x98(%rbp), %rcx
movq %rax, %r8
leaq 0x14a4a(%rip), %rsi     # 0x17109
movb $0x0, %al
callq 0x2240
movl $0x1, %edi
callq 0x23a0
jmp 0x2718
movl -0x50(%rbp), %eax
orl $0x4000, %eax           # imm = 0x4000
movl %eax, -0x50(%rbp)
jmp 0x2718
movl 0x18bcf(%rip), %eax     # 0x1b2b4
orl $0x1, %eax
movl %eax, 0x18bc6(%rip)     # 0x1b2b4
jmp 0x2718
cmpl $0x0, -0x4c(%rbp)
je 0x26ff
movq -0x38(%rbp), %rdi
callq 0x2bb0
movq 0x188ca(%rip), %rax     # 0x1afd0
movq (%rax), %rax
movq %rax, -0x48(%rbp)
jmp 0x2718
movq -0x38(%rbp), %rdi
callq 0x2bb0
jmp 0x25b2
movq 0x188a4(%rip), %rax     # 0x1afc8
movl (%rax), %eax
movl -0x8(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
je 0x2739
movq -0x38(%rbp), %rdi
callq 0x2bb0
movl $0xd, %edi
leaq 0x4ab(%rip), %rsi       # 0x2bf0
callq 0x2220
movl $0x1, %edi
leaq 0x49a(%rip), %rsi       # 0x2bf0
callq 0x2220
movl $0x2, %edi
leaq 0x489(%rip), %rsi       # 0x2bf0
callq 0x2220
movl $0xf, %edi
leaq 0x478(%rip), %rsi       # 0x2bf0
callq 0x2220
movq -0x10(%rbp), %rax
movq 0x18840(%rip), %rcx     # 0x1afc8
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x40(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x13320
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
je 0x27f1
movq 0x18844(%rip), %rax     # 0x1aff0
movq (%rax), %rax
movq %rax, -0xb8(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0xac(%rbp)
movl -0x18(%rbp), %edi
callq 0x3060
movq -0xb8(%rbp), %rdi
movl -0xac(%rbp), %edx
movq %rax, %rcx
leaq 0x1493f(%rip), %rsi     # 0x1711d
movb $0x0, %al
callq 0x2240
movl $0x1, -0x4(%rbp)
jmp 0x2b99
cmpl $0x0, -0x4c(%rbp)
jne 0x27fe
cmpq $0x0, -0x48(%rbp)
je 0x280c
movq -0x20(%rbp), %rdi
movl $0x2, %esi
callq 0x13660
movq -0x20(%rbp), %rdi
movq -0x40(%rbp), %rsi
movl -0x50(%rbp), %edx
orl $0x20000, %edx          # imm = 0x20000
movl $0x1b4, %ecx            # imm = 0x1B4
callq 0x13720
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
je 0x2875
movq 0x187b9(%rip), %rax     # 0x1aff0
movq (%rax), %rax
movq %rax, -0xc8(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0xbc(%rbp)
movl -0x18(%rbp), %edi
callq 0x3060
movq -0xc8(%rbp), %rdi
movl -0xbc(%rbp), %edx
movq %rax, %rcx
leaq 0x148d8(%rip), %rsi     # 0x17141
movb $0x0, %al
callq 0x2240
jmp 0x2b7d
movq -0x20(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x20000, %edx          # imm = 0x20000
leaq -0x28(%rbp), %rcx
callq 0x3950
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
je 0x28d9
movq 0x18755(%rip), %rax     # 0x1aff0
movq (%rax), %rax
movq %rax, -0xd8(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0xcc(%rbp)
movl -0x18(%rbp), %edi
callq 0x3060
movq -0xd8(%rbp), %rdi
movl -0xcc(%rbp), %edx
movq %rax, %rcx
leaq 0x14896(%rip), %rsi     # 0x17163
movb $0x0, %al
callq 0x2240
jmp 0x2b7d
movq -0x28(%rbp), %rdi
movq -0x48(%rbp), %rsi
xorl %edx, %edx
leaq -0x2c(%rbp), %rcx
callq 0xdf80
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
je 0x293a
movq 0x186f4(%rip), %rax     # 0x1aff0
movq (%rax), %rax
movq %rax, -0xe8(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0xdc(%rbp)
movl -0x18(%rbp), %edi
callq 0x3060
movq -0xe8(%rbp), %rdi
movl -0xdc(%rbp), %edx
movq %rax, %rcx
leaq 0x14858(%rip), %rsi     # 0x17186
movb $0x0, %al
callq 0x2240
jmp 0x2b74
cmpl $0x0, -0x4c(%rbp)
je 0x2af1
movl $0x0, -0x74(%rbp)
movq -0x28(%rbp), %rdi
movl -0x2c(%rbp), %esi
leaq -0x60(%rbp), %rdx
callq 0xd7f0
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
je 0x29a9
movq 0x18685(%rip), %rax     # 0x1aff0
movq (%rax), %rax
movq %rax, -0xf8(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0xec(%rbp)
movl -0x18(%rbp), %edi
callq 0x3060
movq -0xf8(%rbp), %rdi
movl -0xec(%rbp), %edx
movq %rax, %rcx
leaq 0x14807(%rip), %rsi     # 0x171a4
movb $0x0, %al
callq 0x2240
jmp 0x2b74
jmp 0x29ab
movq -0x60(%rbp), %rdi
leaq -0x70(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
movl $0xb, %ecx
callq 0x8040
movl %eax, -0x18(%rbp)
cmpl $0x0, %eax
jne 0x2aa5
movq -0x68(%rbp), %rdi
movq -0x70(%rbp), %rdx
xorl %esi, %esi
callq 0x21d0
cmpq $0x0, %rax
je 0x29e4
jmp 0x29ab
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
movq -0x70(%rbp), %rdi
addq $0x1, %rdi
callq 0x2290
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rdi
movq -0x68(%rbp), %rsi
movq -0x70(%rbp), %rdx
callq 0x2260
movq -0x80(%rbp), %rax
movq -0x70(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq -0x28(%rbp), %rdi
movq -0x80(%rbp), %rsi
xorl %edx, %edx
leaq -0x84(%rbp), %rcx
callq 0xdf80
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
jne 0x2a8c
cmpl $0x0, -0x54(%rbp)
je 0x2a5d
movq -0x80(%rbp), %rsi
leaq 0x146ce(%rip), %rdi     # 0x17119
movb $0x0, %al
callq 0x2130
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x2a7d
movq -0x28(%rbp), %rdi
movl -0x84(%rbp), %esi
movq -0x80(%rbp), %rdx
callq 0x2c10
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
je 0x2a7b
jmp 0x2aa5
jmp 0x2a7d
movq -0x20(%rbp), %rdi
movl -0x84(%rbp), %esi
callq 0xe570
movq -0x80(%rbp), %rdi
callq 0x2030
cmpl $0x0, -0x18(%rbp)
je 0x2aa0
jmp 0x29ab
jmp 0x29ab
movq -0x60(%rbp), %rdi
callq 0xdb50
cmpl $0x0, -0x74(%rbp)
jne 0x2add
movq 0x18535(%rip), %rax     # 0x1aff0
movq (%rax), %rdi
movq -0x38(%rbp), %rdx
movq -0x40(%rbp), %rcx
leaq 0x146fc(%rip), %rsi     # 0x171c9
movb $0x0, %al
callq 0x2240
movl $0xffff87b2, -0x18(%rbp) # imm = 0xFFFF87B2
jmp 0x2aef
cmpl $0xffff87b2, -0x18(%rbp) # imm = 0xFFFF87B2
jne 0x2aed
movl $0x0, -0x18(%rbp)
jmp 0x2aef
jmp 0x2b04
movq -0x28(%rbp), %rdi
movl -0x2c(%rbp), %esi
movq -0x48(%rbp), %rdx
callq 0x2c10
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
je 0x2b68
cmpl $0xffff87b2, -0x18(%rbp) # imm = 0xFFFF87B2
je 0x2b68
movq 0x184d6(%rip), %rax     # 0x1aff0
movq (%rax), %rax
movq %rax, -0x110(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x108(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x100(%rbp)
movl -0x18(%rbp), %edi
callq 0x3060
movq -0x110(%rbp), %rdi
movq -0x108(%rbp), %rdx
movq -0x100(%rbp), %rcx
movq %rax, %r8
leaq 0x14694(%rip), %rsi     # 0x171f5
movb $0x0, %al
callq 0x2240
movq -0x20(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0xe570
movq -0x28(%rbp), %rdi
callq 0x4200
movq -0x20(%rbp), %rdi
callq 0x14630
movl -0x18(%rbp), %edx
xorl %eax, %eax
movl $0x1, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
nopw %cs:(%rax,%rax)