// File: /workspace/github_cpp_makefile_star2_O1/jvanns[P]verbatim/sub/lmdb/libraries/liblmdb/mtest4.c
// Function: main
int main(int argc,char * argv[])
{
	int i = 0, j = 0, rc;
	MDB_env *env;
	MDB_dbi dbi;
	MDB_val key, data;
	MDB_txn *txn;
	MDB_stat mst;
	MDB_cursor *cursor;
	int count;
	int *values;
	char sval[8];
	char kval[sizeof(int)];

	memset(sval, 0, sizeof(sval));

	count = 510;
	values = (int *)malloc(count*sizeof(int));

	for(i = 0;i<count;i++) {
		values[i] = i*5;
	}

	E(mdb_env_create(&env));
	E(mdb_env_set_mapsize(env, 10485760));
	E(mdb_env_set_maxdbs(env, 4));
	E(mdb_env_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664));

	E(mdb_txn_begin(env, NULL, 0, &txn));
	E(mdb_dbi_open(txn, "id4", MDB_CREATE|MDB_DUPSORT|MDB_DUPFIXED, &dbi));

	key.mv_size = sizeof(int);
	key.mv_data = kval;
	data.mv_size = sizeof(sval);
	data.mv_data = sval;

	printf("Adding %d values\n", count);
	strcpy(kval, "001");
	for (i=0;i<count;i++) {
		sprintf(sval, "%07x", values[i]);
		if (RES(MDB_KEYEXIST, mdb_put(txn, dbi, &key, &data, MDB_NODUPDATA)))
			j++;
	}
	if (j) printf("%d duplicates skipped\n", j);
	E(mdb_txn_commit(txn));
	E(mdb_env_stat(env, &mst));

	/* there should be one full page of dups now.
	 */
	E(mdb_txn_begin(env, NULL, MDB_RDONLY, &txn));
	E(mdb_cursor_open(txn, dbi, &cursor));
	while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
		printf("key: %p %.*s, data: %p %.*s\n",
			key.mv_data,  (int) key.mv_size,  (char *) key.mv_data,
			data.mv_data, (int) data.mv_size, (char *) data.mv_data);
	}
	CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get");
	mdb_cursor_close(cursor);
	mdb_txn_abort(txn);

	/* test all 3 branches of split code:
	 * 1: new key in lower half
	 * 2: new key at split point
	 * 3: new key in upper half
	 */

	key.mv_size = sizeof(int);
	key.mv_data = kval;
	data.mv_size = sizeof(sval);
	data.mv_data = sval;

	sprintf(sval, "%07x", values[3]+1);
	E(mdb_txn_begin(env, NULL, 0, &txn));
	(void)RES(MDB_KEYEXIST, mdb_put(txn, dbi, &key, &data, MDB_NODUPDATA));
	mdb_txn_abort(txn);

	sprintf(sval, "%07x", values[255]+1);
	E(mdb_txn_begin(env, NULL, 0, &txn));
	(void)RES(MDB_KEYEXIST, mdb_put(txn, dbi, &key, &data, MDB_NODUPDATA));
	mdb_txn_abort(txn);

	sprintf(sval, "%07x", values[500]+1);
	E(mdb_txn_begin(env, NULL, 0, &txn));
	(void)RES(MDB_KEYEXIST, mdb_put(txn, dbi, &key, &data, MDB_NODUPDATA));
	E(mdb_txn_commit(txn));

	/* Try MDB_NEXT_MULTIPLE */
	E(mdb_txn_begin(env, NULL, 0, &txn));
	E(mdb_cursor_open(txn, dbi, &cursor));
	while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT_MULTIPLE)) == 0) {
		printf("key: %.*s, data: %.*s\n",
			(int) key.mv_size,  (char *) key.mv_data,
			(int) data.mv_size, (char *) data.mv_data);
	}
	CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get");
	mdb_cursor_close(cursor);
	mdb_txn_abort(txn);
	j=0;

	for (i= count - 1; i > -1; i-= (rand()%3)) {
		j++;
		txn=NULL;
		E(mdb_txn_begin(env, NULL, 0, &txn));
		sprintf(sval, "%07x", values[i]);
		key.mv_size = sizeof(int);
		key.mv_data = kval;
		data.mv_size = sizeof(sval);
		data.mv_data = sval;
		if (RES(MDB_NOTFOUND, mdb_del(txn, dbi, &key, &data))) {
			j--;
			mdb_txn_abort(txn);
		} else {
			E(mdb_txn_commit(txn));
		}
	}
	free(values);
	printf("Deleted %d values\n", j);

	E(mdb_env_stat(env, &mst));
	E(mdb_txn_begin(env, NULL, MDB_RDONLY, &txn));
	E(mdb_cursor_open(txn, dbi, &cursor));
	printf("Cursor next\n");
	while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
		printf("key: %.*s, data: %.*s\n",
			(int) key.mv_size,  (char *) key.mv_data,
			(int) data.mv_size, (char *) data.mv_data);
	}
	CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get");
	printf("Cursor prev\n");
	while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_PREV)) == 0) {
		printf("key: %.*s, data: %.*s\n",
			(int) key.mv_size,  (char *) key.mv_data,
			(int) data.mv_size, (char *) data.mv_data);
	}
	CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get");
	mdb_cursor_close(cursor);
	mdb_txn_abort(txn);

	mdb_dbi_close(env, dbi);
	mdb_env_close(env);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq $0x0, 0x58(%rsp)
movl $0x7f8, %edi            # imm = 0x7F8
callq 0x2260
movq %rax, %rcx
xorl %eax, %eax
movq %rcx, 0x48(%rsp)
movl %eax, (%rcx)
addq $0x5, %rax
addq $0x4, %rcx
cmpq $0x9f6, %rax            # imm = 0x9F6
jne 0x2c1a
leaq 0x18(%rsp), %rdi
callq 0xc86f
testl %eax, %eax
jne 0x32ef
movq 0x18(%rsp), %rdi
movl $0xa00000, %esi         # imm = 0xA00000
callq 0xc8dc
testl %eax, %eax
jne 0x32f6
movq 0x18(%rsp), %rdi
movl $0x4, %esi
callq 0xca40
testl %eax, %eax
jne 0x32fd
movq 0x18(%rsp), %rdi
leaq 0xc3f7(%rip), %rsi      # 0xf06f
movl $0x10001, %edx          # imm = 0x10001
movl $0x1b4, %ecx            # imm = 0x1B4
callq 0xca89
testl %eax, %eax
jne 0x3304
movq 0x18(%rsp), %rdi
leaq 0x8(%rsp), %rcx
xorl %esi, %esi
xorl %edx, %edx
callq 0x387a
testl %eax, %eax
jne 0x330b
movq 0x8(%rsp), %rdi
leaq 0xc421(%rip), %rsi      # 0xf0d7
leaq 0x14(%rsp), %rcx
movl $0x40014, %edx          # imm = 0x40014
callq 0x9dd3
testl %eax, %eax
jne 0x3312
leaq 0x30(%rsp), %rbx
movq $0x4, (%rbx)
leaq 0x54(%rsp), %r12
movq %r12, 0x8(%rbx)
leaq 0x20(%rsp), %r15
movq $0x8, (%r15)
leaq 0x58(%rsp), %r14
movq %r14, 0x8(%r15)
leaq 0xc421(%rip), %rdi      # 0xf11f
movl $0x1fe, %esi            # imm = 0x1FE
xorl %eax, %eax
callq 0x2130
movl $0x313030, (%r12)       # imm = 0x313030
leaq 0xc418(%rip), %r12      # 0xf131
xorl %r13d, %r13d
xorl %ebp, %ebp
movq 0x48(%rsp), %rax
movl (%rax,%r13,4), %edx
movq %r14, %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0x2350
movq 0x8(%rsp), %rdi
movl 0x14(%rsp), %esi
movq %rbx, %rdx
movq %r15, %rcx
movl $0x20, %r8d
callq 0x9d34
cmpl $0xffff87b1, %eax       # imm = 0xFFFF87B1
je 0x2d5e
testl %eax, %eax
je 0x2d60
jmp 0x32da
incl %ebp
incq %r13
cmpq $0x1fe, %r13            # imm = 0x1FE
jne 0x2d1e
testl %ebp, %ebp
je 0x2d80
leaq 0xc3ed(%rip), %rdi      # 0xf164
movl %ebp, %esi
xorl %eax, %eax
callq 0x2130
movq 0x8(%rsp), %rdi
callq 0x3f13
testl %eax, %eax
jne 0x3319
movq 0x18(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0xdca1
testl %eax, %eax
jne 0x3320
movq 0x18(%rsp), %rdi
leaq 0x8(%rsp), %rcx
xorl %esi, %esi
movl $0x20000, %edx          # imm = 0x20000
callq 0x387a
testl %eax, %eax
jne 0x3327
movq 0x8(%rsp), %rdi
movl 0x14(%rsp), %esi
leaq 0x40(%rsp), %rdx
callq 0x9a00
testl %eax, %eax
jne 0x332e
leaq 0x30(%rsp), %rbx
leaq 0x20(%rsp), %r15
leaq 0xc402(%rip), %r12      # 0xf1f5
movq 0x40(%rsp), %rdi
movq %rbx, %rsi
movq %r15, %rdx
movl $0x8, %ecx
callq 0x66a4
testl %eax, %eax
jne 0x2e32
movq 0x38(%rsp), %rcx
movl 0x30(%rsp), %edx
movq 0x28(%rsp), %r8
movl 0x20(%rsp), %r9d
movq %r8, (%rsp)
movq %r12, %rdi
movq %rcx, %rsi
xorl %eax, %eax
callq 0x2130
jmp 0x2df3
cmpl $0xffff87b2, %eax       # imm = 0xFFFF87B2
jne 0x32be
movq 0x40(%rsp), %rdi
callq 0x9baf
leaq 0x8(%rsp), %rbx
movq (%rbx), %rdi
callq 0x3eed
movq $0x4, 0x30(%rsp)
leaq 0x54(%rsp), %rax
movq %rax, 0x38(%rsp)
movq $0x8, 0x20(%rsp)
movq %r14, 0x28(%rsp)
movq 0x48(%rsp), %rax
movl 0xc(%rax), %edx
incl %edx
leaq 0xc2ab(%rip), %rsi      # 0xf131
movq %r14, %rdi
xorl %eax, %eax
callq 0x2350
movq 0x18(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
movq %rbx, %rcx
callq 0x387a
testl %eax, %eax
jne 0x3335
movq 0x8(%rsp), %rdi
movl 0x14(%rsp), %esi
leaq 0x30(%rsp), %rdx
leaq 0x20(%rsp), %rcx
movl $0x20, %r8d
callq 0x9d34
cmpl $0xffff87b1, %eax       # imm = 0xFFFF87B1
je 0x2ed6
testl %eax, %eax
jne 0x3374
leaq 0x8(%rsp), %rbx
movq (%rbx), %rdi
callq 0x3eed
movq 0x48(%rsp), %rax
movl 0x3fc(%rax), %edx
incl %edx
leaq 0xc23a(%rip), %rsi      # 0xf131
leaq 0x58(%rsp), %rdi
xorl %eax, %eax
callq 0x2350
movq 0x18(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
movq %rbx, %rcx
callq 0x387a
testl %eax, %eax
jne 0x333c
movq 0x8(%rsp), %rdi
movl 0x14(%rsp), %esi
leaq 0x30(%rsp), %rdx
leaq 0x20(%rsp), %rcx
movl $0x20, %r8d
callq 0x9d34
cmpl $0xffff87b1, %eax       # imm = 0xFFFF87B1
je 0x2f49
testl %eax, %eax
jne 0x337b
leaq 0x8(%rsp), %rbx
movq (%rbx), %rdi
callq 0x3eed
movq 0x48(%rsp), %rax
movl 0x7d0(%rax), %edx
incl %edx
leaq 0xc1c7(%rip), %rsi      # 0xf131
leaq 0x58(%rsp), %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0x2350
movq 0x18(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
movq %rbx, %rcx
callq 0x387a
testl %eax, %eax
jne 0x3343
movq 0x8(%rsp), %rdi
movl 0x14(%rsp), %esi
leaq 0x30(%rsp), %rdx
leaq 0x20(%rsp), %rcx
movl $0x20, %r8d
callq 0x9d34
cmpl $0xffff87b1, %eax       # imm = 0xFFFF87B1
je 0x2fbf
testl %eax, %eax
jne 0x3382
movq 0x8(%rsp), %rdi
callq 0x3f13
testl %eax, %eax
jne 0x334a
movq 0x18(%rsp), %rdi
leaq 0x8(%rsp), %rcx
xorl %esi, %esi
xorl %edx, %edx
callq 0x387a
testl %eax, %eax
jne 0x3351
movq 0x8(%rsp), %rdi
movl 0x14(%rsp), %esi
leaq 0x40(%rsp), %rdx
callq 0x9a00
testl %eax, %eax
jne 0x3358
leaq 0x30(%rsp), %rbx
leaq 0x20(%rsp), %r15
leaq 0xc209(%rip), %r12      # 0xf221
movq 0x40(%rsp), %rdi
movq %rbx, %rsi
movq %r15, %rdx
movl $0xa, %ecx
callq 0x66a4
testl %eax, %eax
jne 0x304f
movl 0x30(%rsp), %esi
movq 0x38(%rsp), %rdx
movl 0x20(%rsp), %ecx
movq 0x28(%rsp), %r8
movq %r12, %rdi
xorl %eax, %eax
callq 0x2130
jmp 0x3018
cmpl $0xffff87b2, %eax       # imm = 0xFFFF87B2
jne 0x32c5
movq 0x40(%rsp), %rdi
callq 0x9baf
leaq 0x8(%rsp), %rax
movq (%rax), %rdi
callq 0x3eed
movl $0x1fd, %r15d           # imm = 0x1FD
leaq 0xc0b3(%rip), %r12      # 0xf131
leaq 0x30(%rsp), %r13
leaq 0x20(%rsp), %rbx
xorl %ebp, %ebp
movq $0x0, 0x8(%rsp)
movq 0x18(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
leaq 0x8(%rsp), %rcx
callq 0x387a
testl %eax, %eax
jne 0x32b7
movl %r15d, %eax
movq 0x48(%rsp), %rcx
movl (%rcx,%rax,4), %edx
movq %r14, %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0x2350
movq $0x4, 0x30(%rsp)
leaq 0x54(%rsp), %rax
movq %rax, 0x38(%rsp)
movq $0x8, 0x20(%rsp)
movq %r14, 0x28(%rsp)
movq 0x8(%rsp), %rdi
movl 0x14(%rsp), %esi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x9c13
cmpl $0xffff87b2, %eax       # imm = 0xFFFF87B2
je 0x3120
testl %eax, %eax
jne 0x32e8
movq 0x8(%rsp), %rdi
callq 0x3f13
testl %eax, %eax
jne 0x32e1
incl %ebp
jmp 0x312a
movq 0x8(%rsp), %rdi
callq 0x3eed
callq 0x23f0
cltq
imulq $0x55555556, %rax, %rcx # imm = 0x55555556
movq %rcx, %rdx
shrq $0x3f, %rdx
shrq $0x20, %rcx
addl %edx, %ecx
leal (%rcx,%rcx,2), %ecx
subl %eax, %ecx
addl %ecx, %r15d
jns 0x308a
movq 0x48(%rsp), %rdi
callq 0x2030
leaq 0xc0f3(%rip), %rdi      # 0xf257
movl %ebp, %esi
xorl %eax, %eax
callq 0x2130
movq 0x18(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0xdca1
testl %eax, %eax
jne 0x335f
movq 0x18(%rsp), %rdi
leaq 0x8(%rsp), %rcx
xorl %esi, %esi
movl $0x20000, %edx          # imm = 0x20000
callq 0x387a
testl %eax, %eax
jne 0x3366
movq 0x8(%rsp), %rdi
movl 0x14(%rsp), %esi
leaq 0x40(%rsp), %rdx
callq 0x9a00
testl %eax, %eax
jne 0x336d
leaq 0xc0a6(%rip), %rdi      # 0xf26a
callq 0x20a0
leaq 0x30(%rsp), %rbx
leaq 0x20(%rsp), %r14
leaq 0xc047(%rip), %r15      # 0xf221
movq 0x40(%rsp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
movl $0x8, %ecx
callq 0x66a4
testl %eax, %eax
jne 0x3211
movl 0x30(%rsp), %esi
movq 0x38(%rsp), %rdx
movl 0x20(%rsp), %ecx
movq 0x28(%rsp), %r8
movq %r15, %rdi
xorl %eax, %eax
callq 0x2130
jmp 0x31da
cmpl $0xffff87b2, %eax       # imm = 0xFFFF87B2
jne 0x32cc
leaq 0xc053(%rip), %rdi      # 0xf276
callq 0x20a0
leaq 0x30(%rsp), %rbx
leaq 0x20(%rsp), %r14
leaq 0xbfe8(%rip), %r15      # 0xf221
movq 0x40(%rsp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
movl $0xc, %ecx
callq 0x66a4
testl %eax, %eax
jne 0x3270
movl 0x30(%rsp), %esi
movq 0x38(%rsp), %rdx
movl 0x20(%rsp), %ecx
movq 0x28(%rsp), %r8
movq %r15, %rdi
xorl %eax, %eax
callq 0x2130
jmp 0x3239
cmpl $0xffff87b2, %eax       # imm = 0xFFFF87B2
jne 0x32d3
movq 0x40(%rsp), %rdi
callq 0x9baf
movq 0x8(%rsp), %rdi
callq 0x3eed
movq 0x18(%rsp), %rdi
movl 0x14(%rsp), %esi
callq 0xa181
movq 0x18(%rsp), %rdi
callq 0xd598
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %edi
callq 0x27c0
movl %eax, %edi
callq 0x2a88
movl %eax, %edi
callq 0x29a0
movl %eax, %edi
callq 0x2928
movl %eax, %edi
callq 0x28ec
movl %eax, %edi
callq 0x2ac2
movl %eax, %edi
callq 0x27fc
movl %eax, %edi
callq 0x2964
movl %eax, %edi
callq 0x2420
movl %eax, %edi
callq 0x245a
movl %eax, %edi
callq 0x2494
movl %eax, %edi
callq 0x24ce
movl %eax, %edi
callq 0x2508
movl %eax, %edi
callq 0x2542
movl %eax, %edi
callq 0x257c
movl %eax, %edi
callq 0x25b6
movl %eax, %edi
callq 0x25f0
movl %eax, %edi
callq 0x262a
movl %eax, %edi
callq 0x2664
movl %eax, %edi
callq 0x269e
movl %eax, %edi
callq 0x26d8
movl %eax, %edi
callq 0x2712
movl %eax, %edi
callq 0x274c
movl %eax, %edi
callq 0x2786
movl %eax, %edi
callq 0x2838
movl %eax, %edi
callq 0x2874
movl %eax, %edi
callq 0x28b0
movl %eax, %edi
callq 0x2a4e
movl %eax, %edi
callq 0x2a14
movl %eax, %edi
callq 0x29da
nopl (%rax)