// File: jvanns[P]verbatim/sub/lmdb/libraries/liblmdb/mdb_load.c
// Function: main
int main(int argc, char *argv[])
{
	int i, rc;
	MDB_env *env;
	MDB_txn *txn;
	MDB_cursor *mc;
	MDB_dbi dbi;
	char *envname;
	int envflags = 0, putflags = 0;
	int dohdr = 0;

	prog = argv[0];

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

	/* -f: load file instead of stdin
	 * -n: use NOSUBDIR flag on env_open
	 * -s: load into named subDB
	 * -N: use NOOVERWRITE on puts
	 * -T: read plaintext
	 * -V: print version and exit
	 */
	while ((i = getopt(argc, argv, "f:ns:NTV")) != EOF) {
		switch(i) {
		case 'V':
			printf("%s\n", MDB_VERSION_STRING);
			exit(0);
			break;
		case 'f':
			if (freopen(optarg, "r", stdin) == NULL) {
				fprintf(stderr, "%s: %s: reopen: %s\n",
					prog, optarg, strerror(errno));
				exit(EXIT_FAILURE);
			}
			break;
		case 'n':
			envflags |= MDB_NOSUBDIR;
			break;
		case 's':
			subname = strdup(optarg);
			break;
		case 'N':
			putflags = MDB_NOOVERWRITE|MDB_NODUPDATA;
			break;
		case 'T':
			mode |= NOHDR;
			break;
		default:
			usage();
		}
	}

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

	dbuf.mv_size = 4096;
	dbuf.mv_data = malloc(dbuf.mv_size);

	if (!(mode & NOHDR))
		readhdr();

	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;
	}

	mdb_env_set_maxdbs(env, 2);

	if (info.me_maxreaders)
		mdb_env_set_maxreaders(env, info.me_maxreaders);

	if (info.me_mapsize)
		mdb_env_set_mapsize(env, info.me_mapsize);

	if (info.me_mapaddr)
		envflags |= MDB_FIXEDMAP;

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

	kbuf.mv_size = mdb_env_get_maxkeysize(env) * 2 + 2;
	kbuf.mv_data = malloc(kbuf.mv_size);

	while(!Eof) {
		MDB_val key, data;
		int batch = 0;
		flags = 0;

		if (!dohdr) {
			dohdr = 1;
		} else if (!(mode & NOHDR))
			readhdr();
		
		rc = mdb_txn_begin(env, NULL, 0, &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, flags|MDB_CREATE, &dbi);
		if (rc) {
			fprintf(stderr, "mdb_open failed, error %d %s\n", rc, mdb_strerror(rc));
			goto txn_abort;
		}

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

		while(1) {
			rc = readline(&key, &kbuf);
			if (rc == EOF)
				break;
			if (rc)
				goto txn_abort;

			rc = readline(&data, &dbuf);
			if (rc)
				goto txn_abort;
			
			rc = mdb_cursor_put(mc, &key, &data, putflags);
			if (rc == MDB_KEYEXIST && putflags)
				continue;
			if (rc)
				goto txn_abort;
			batch++;
			if (batch == 100) {
				rc = mdb_txn_commit(txn);
				if (rc) {
					fprintf(stderr, "%s: line %" Z "d: txn_commit: %s\n",
						prog, lineno, mdb_strerror(rc));
					goto env_close;
				}
				rc = mdb_txn_begin(env, NULL, 0, &txn);
				if (rc) {
					fprintf(stderr, "mdb_txn_begin failed, error %d %s\n", rc, mdb_strerror(rc));
					goto env_close;
				}
				rc = mdb_cursor_open(txn, dbi, &mc);
				if (rc) {
					fprintf(stderr, "mdb_cursor_open failed, error %d %s\n", rc, mdb_strerror(rc));
					goto txn_abort;
				}
				batch = 0;
			}
		}
		rc = mdb_txn_commit(txn);
		txn = NULL;
		if (rc) {
			fprintf(stderr, "%s: line %" Z "d: txn_commit: %s\n",
				prog, lineno, mdb_strerror(rc));
			goto env_close;
		}
		mdb_dbi_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 $0x140, %rsp            # imm = 0x140
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x44(%rbp)
movl $0x0, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, 0x19d54(%rip)     # 0x1c300
cmpl $0x2, -0x8(%rbp)
jge 0x25b7
callq 0x2ca0
jmp 0x25b9
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0x15b12(%rip), %rdx     # 0x180d9
callq 0x2360
movl %eax, -0x14(%rbp)
cmpl $-0x1, %eax
je 0x26fa
movl -0x14(%rbp), %eax
addl $-0x4e, %eax
movl %eax, %ecx
movq %rcx, -0x80(%rbp)
subl $0x25, %eax
ja 0x26f0
movq -0x80(%rbp), %rax
leaq 0x15a0c(%rip), %rcx     # 0x18004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x15da0(%rip), %rdi     # 0x183a8
leaq 0x15ad3(%rip), %rsi     # 0x180e2
movb $0x0, %al
callq 0x2120
xorl %edi, %edi
callq 0x23b0
movq 0x199ac(%rip), %rax     # 0x1bfd0
movq (%rax), %rdi
movq 0x19992(%rip), %rax     # 0x1bfc0
movq (%rax), %rdx
leaq 0x16599(%rip), %rsi     # 0x18bd1
callq 0x2190
cmpq $0x0, %rax
jne 0x26af
movq 0x199a6(%rip), %rax     # 0x1bff0
movq (%rax), %rax
movq %rax, -0x98(%rbp)
movq 0x19ca5(%rip), %rax     # 0x1c300
movq %rax, -0x90(%rbp)
movq 0x19967(%rip), %rax     # 0x1bfd0
movq (%rax), %rax
movq %rax, -0x88(%rbp)
callq 0x2050
movl (%rax), %edi
callq 0x2400
movq -0x98(%rbp), %rdi
movq -0x90(%rbp), %rdx
movq -0x88(%rbp), %rcx
movq %rax, %r8
leaq 0x15a63(%rip), %rsi     # 0x18101
movb $0x0, %al
callq 0x2240
movl $0x1, %edi
callq 0x23b0
jmp 0x26f5
movl -0x44(%rbp), %eax
orl $0x4000, %eax           # imm = 0x4000
movl %eax, -0x44(%rbp)
jmp 0x26f5
movq 0x1990b(%rip), %rax     # 0x1bfd0
movq (%rax), %rdi
callq 0x23f0
movq %rax, 0x19c34(%rip)     # 0x1c308
jmp 0x26f5
movl $0x30, -0x48(%rbp)
jmp 0x26f5
movl 0x19c2b(%rip), %eax     # 0x1c310
orl $0x2, %eax
movl %eax, 0x19c22(%rip)     # 0x1c310
jmp 0x26f5
callq 0x2ca0
jmp 0x25b9
movq 0x198c7(%rip), %rax     # 0x1bfc8
movl (%rax), %eax
movl -0x8(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
je 0x2712
callq 0x2ca0
movq $0x1000, 0x19bfb(%rip) # imm = 0x1000
movq 0x19bf4(%rip), %rdi     # 0x1c318
callq 0x2290
movq %rax, 0x19bf0(%rip)     # 0x1c320
movl 0x19bda(%rip), %eax     # 0x1c310
andl $0x2, %eax
cmpl $0x0, %eax
jne 0x2743
callq 0x2cd0
movq -0x10(%rbp), %rax
movq 0x1987a(%rip), %rcx     # 0x1bfc8
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x40(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x13b10
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
je 0x27b7
movq 0x1987e(%rip), %rax     # 0x1bff0
movq (%rax), %rax
movq %rax, -0xa8(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x9c(%rbp)
movl -0x18(%rbp), %edi
callq 0x3850
movq -0xa8(%rbp), %rdi
movl -0x9c(%rbp), %edx
movq %rax, %rcx
leaq 0x15971(%rip), %rsi     # 0x18115
movb $0x0, %al
callq 0x2240
movl $0x1, -0x4(%rbp)
jmp 0x2c89
movq -0x20(%rbp), %rdi
movl $0x2, %esi
callq 0x13e50
cmpl $0x0, 0x19b7c(%rip)     # 0x1c348
je 0x27dd
movq -0x20(%rbp), %rdi
movl 0x19b70(%rip), %esi     # 0x1c348
callq 0x13e90
cmpq $0x0, 0x19b4b(%rip)     # 0x1c330
je 0x27f7
movq -0x20(%rbp), %rdi
movq 0x19b3e(%rip), %rsi     # 0x1c330
callq 0x13bc0
cmpq $0x0, 0x19b29(%rip)     # 0x1c328
je 0x280a
movl -0x44(%rbp), %eax
orl $0x1, %eax
movl %eax, -0x44(%rbp)
movq -0x20(%rbp), %rdi
movq -0x40(%rbp), %rsi
movl -0x44(%rbp), %edx
movl $0x1b4, %ecx            # imm = 0x1B4
callq 0x13f10
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
je 0x286d
movq 0x197c1(%rip), %rax     # 0x1bff0
movq (%rax), %rax
movq %rax, -0xb8(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0xac(%rbp)
movl -0x18(%rbp), %edi
callq 0x3850
movq -0xb8(%rbp), %rdi
movl -0xac(%rbp), %edx
movq %rax, %rcx
leaq 0x158d8(%rip), %rsi     # 0x18139
movb $0x0, %al
callq 0x2240
jmp 0x2c6d
movq -0x20(%rbp), %rdi
callq 0x15ba0
shll %eax
addl $0x2, %eax
cltq
movq %rax, 0x19acc(%rip)     # 0x1c350
movq 0x19ac5(%rip), %rdi     # 0x1c350
callq 0x2290
movq %rax, 0x19ac1(%rip)     # 0x1c358
cmpl $0x0, 0x19ac2(%rip)     # 0x1c360
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x28ac
jmp 0x2c62
movl $0x0, -0x74(%rbp)
movl $0x0, 0x19aa7(%rip)     # 0x1c364
cmpl $0x0, -0x4c(%rbp)
jne 0x28cc
movl $0x1, -0x4c(%rbp)
jmp 0x28e1
movl 0x19a3e(%rip), %eax     # 0x1c310
andl $0x2, %eax
cmpl $0x0, %eax
jne 0x28df
callq 0x2cd0
jmp 0x28e1
movq -0x20(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
leaq -0x28(%rbp), %rcx
callq 0x4140
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
je 0x2942
movq 0x196ec(%rip), %rax     # 0x1bff0
movq (%rax), %rax
movq %rax, -0xc8(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0xbc(%rbp)
movl -0x18(%rbp), %edi
callq 0x3850
movq -0xc8(%rbp), %rdi
movl -0xbc(%rbp), %edx
movq %rax, %rcx
leaq 0x15825(%rip), %rsi     # 0x1815b
movb $0x0, %al
callq 0x2240
jmp 0x2c6d
movq -0x28(%rbp), %rdi
movq 0x199bb(%rip), %rsi     # 0x1c308
movl 0x19a11(%rip), %edx     # 0x1c364
orl $0x40000, %edx          # imm = 0x40000
leaq -0x34(%rbp), %rcx
callq 0xe770
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
je 0x29b0
movq 0x1967e(%rip), %rax     # 0x1bff0
movq (%rax), %rax
movq %rax, -0xd8(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0xcc(%rbp)
movl -0x18(%rbp), %edi
callq 0x3850
movq -0xd8(%rbp), %rdi
movl -0xcc(%rbp), %edx
movq %rax, %rcx
leaq 0x157da(%rip), %rsi     # 0x1817e
movb $0x0, %al
callq 0x2240
jmp 0x2c64
movq -0x28(%rbp), %rdi
movl -0x34(%rbp), %esi
leaq -0x30(%rbp), %rdx
callq 0xdfe0
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
je 0x2a0e
movq 0x19620(%rip), %rax     # 0x1bff0
movq (%rax), %rax
movq %rax, -0xe8(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0xdc(%rbp)
movl -0x18(%rbp), %edi
callq 0x3850
movq -0xe8(%rbp), %rdi
movl -0xdc(%rbp), %edx
movq %rax, %rcx
leaq 0x1579a(%rip), %rsi     # 0x1819c
movb $0x0, %al
callq 0x2240
jmp 0x2c64
jmp 0x2a10
leaq -0x60(%rbp), %rdi
leaq 0x19935(%rip), %rsi     # 0x1c350
callq 0x3300
movl %eax, -0x18(%rbp)
cmpl $-0x1, -0x18(%rbp)
jne 0x2a2e
jmp 0x2bda
cmpl $0x0, -0x18(%rbp)
je 0x2a39
jmp 0x2c64
leaq -0x70(%rbp), %rdi
leaq 0x198d4(%rip), %rsi     # 0x1c318
callq 0x3300
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
je 0x2a57
jmp 0x2c64
movq -0x30(%rbp), %rdi
movl -0x48(%rbp), %ecx
leaq -0x60(%rbp), %rsi
leaq -0x70(%rbp), %rdx
callq 0x5590
movl %eax, -0x18(%rbp)
cmpl $0xffff87b1, -0x18(%rbp) # imm = 0xFFFF87B1
jne 0x2a7f
cmpl $0x0, -0x48(%rbp)
je 0x2a7f
jmp 0x2a10
cmpl $0x0, -0x18(%rbp)
je 0x2a8a
jmp 0x2c64
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
cmpl $0x64, -0x74(%rbp)
jne 0x2bd5
movq -0x28(%rbp), %rdi
callq 0x4a40
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
je 0x2b0f
movq 0x1953a(%rip), %rax     # 0x1bff0
movq (%rax), %rax
movq %rax, -0x100(%rbp)
movq 0x19839(%rip), %rax     # 0x1c300
movq %rax, -0xf8(%rbp)
movq 0x19893(%rip), %rax     # 0x1c368
movq %rax, -0xf0(%rbp)
movl -0x18(%rbp), %edi
callq 0x3850
movq -0x100(%rbp), %rdi
movq -0xf8(%rbp), %rdx
movq -0xf0(%rbp), %rcx
movq %rax, %r8
leaq 0x156be(%rip), %rsi     # 0x181c1
movb $0x0, %al
callq 0x2240
jmp 0x2c6d
movq -0x20(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
leaq -0x28(%rbp), %rcx
callq 0x4140
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
je 0x2b70
movq 0x194be(%rip), %rax     # 0x1bff0
movq (%rax), %rax
movq %rax, -0x110(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x104(%rbp)
movl -0x18(%rbp), %edi
callq 0x3850
movq -0x110(%rbp), %rdi
movl -0x104(%rbp), %edx
movq %rax, %rcx
leaq 0x155f7(%rip), %rsi     # 0x1815b
movb $0x0, %al
callq 0x2240
jmp 0x2c6d
movq -0x28(%rbp), %rdi
movl -0x34(%rbp), %esi
leaq -0x30(%rbp), %rdx
callq 0xdfe0
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
je 0x2bce
movq 0x19460(%rip), %rax     # 0x1bff0
movq (%rax), %rax
movq %rax, -0x120(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x114(%rbp)
movl -0x18(%rbp), %edi
callq 0x3850
movq -0x120(%rbp), %rdi
movl -0x114(%rbp), %edx
movq %rax, %rcx
leaq 0x155da(%rip), %rsi     # 0x1819c
movb $0x0, %al
callq 0x2240
jmp 0x2c64
movl $0x0, -0x74(%rbp)
jmp 0x2a10
movq -0x28(%rbp), %rdi
callq 0x4a40
movl %eax, -0x18(%rbp)
movq $0x0, -0x28(%rbp)
cmpl $0x0, -0x18(%rbp)
je 0x2c51
movq 0x193f5(%rip), %rax     # 0x1bff0
movq (%rax), %rax
movq %rax, -0x138(%rbp)
movq 0x196f4(%rip), %rax     # 0x1c300
movq %rax, -0x130(%rbp)
movq 0x1974e(%rip), %rax     # 0x1c368
movq %rax, -0x128(%rbp)
movl -0x18(%rbp), %edi
callq 0x3850
movq -0x138(%rbp), %rdi
movq -0x130(%rbp), %rdx
movq -0x128(%rbp), %rcx
movq %rax, %r8
leaq 0x15579(%rip), %rsi     # 0x181c1
movb $0x0, %al
callq 0x2240
jmp 0x2c6d
movq -0x20(%rbp), %rdi
movl -0x34(%rbp), %esi
callq 0xed60
jmp 0x2897
jmp 0x2c64
movq -0x28(%rbp), %rdi
callq 0x49f0
movq -0x20(%rbp), %rdi
callq 0x14e20
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 $0x140, %rsp            # imm = 0x140
popq %rbp
retq
nopw %cs:(%rax,%rax)