// File: /workspace/github_cpp_makefile_star2_O3/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, 0x48(%rsp)
addq $0x4, %rax
movdqa 0xc3ef(%rip), %xmm0     # 0xf010
xorl %ecx, %ecx
movdqa 0xc3f5(%rip), %xmm1     # 0xf020
movdqa 0xc3fd(%rip), %xmm2     # 0xf030
pshufd $0xf5, %xmm2, %xmm3     # xmm3 = xmm2[1,1,3,3]
movdqa 0xc400(%rip), %xmm4     # 0xf040
movdqa %xmm0, %xmm5
pxor %xmm1, %xmm5
movdqa %xmm2, %xmm6
pcmpgtd %xmm5, %xmm6
pshufd $0xf5, %xmm5, %xmm5     # xmm5 = xmm5[1,1,3,3]
pcmpeqd %xmm3, %xmm5
pand %xmm6, %xmm5
movd %xmm5, %edx
testb $0x1, %dl
je 0x2c69
movl %ecx, -0x4(%rax)
pextrw $0x4, %xmm5, %edx
testb $0x1, %dl
je 0x2c78
leal 0x5(%rcx), %edx
movl %edx, (%rax)
paddq %xmm4, %xmm0
addq $0x8, %rax
addq $0xa, %rcx
cmpq $0x9f6, %rcx            # imm = 0x9F6
jne 0x2c40
leaq 0x18(%rsp), %rdi
callq 0xc45e
testl %eax, %eax
jne 0x3350
movq 0x18(%rsp), %rdi
movl $0xa00000, %esi         # imm = 0xA00000
callq 0xc4cb
testl %eax, %eax
jne 0x3357
movq 0x18(%rsp), %rdi
movl $0x4, %esi
callq 0xc627
testl %eax, %eax
jne 0x335e
movq 0x18(%rsp), %rdi
leaq 0xc3e2(%rip), %rsi      # 0xf0bb
movl $0x10001, %edx          # imm = 0x10001
movl $0x1b4, %ecx            # imm = 0x1B4
callq 0xc670
testl %eax, %eax
jne 0x3365
movq 0x18(%rsp), %rdi
leaq 0x8(%rsp), %rcx
xorl %esi, %esi
xorl %edx, %edx
callq 0x38a0
testl %eax, %eax
jne 0x336c
movq 0x8(%rsp), %rdi
leaq 0xc40c(%rip), %rsi      # 0xf123
leaq 0x14(%rsp), %rcx
movl $0x40014, %edx          # imm = 0x40014
callq 0x9a8b
testl %eax, %eax
jne 0x3373
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 0xc40c(%rip), %rdi      # 0xf16b
movl $0x1fe, %esi            # imm = 0x1FE
xorl %eax, %eax
callq 0x2130
movl $0x313030, (%r12)       # imm = 0x313030
leaq 0xc403(%rip), %r12      # 0xf17d
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 0x99ec
cmpl $0xffff87b1, %eax       # imm = 0xFFFF87B1
je 0x2dbf
testl %eax, %eax
je 0x2dc1
jmp 0x333b
incl %ebp
incq %r13
cmpq $0x1fe, %r13            # imm = 0x1FE
jne 0x2d7f
testl %ebp, %ebp
je 0x2de1
leaq 0xc3d8(%rip), %rdi      # 0xf1b0
movl %ebp, %esi
xorl %eax, %eax
callq 0x2130
movq 0x8(%rsp), %rdi
callq 0x3f10
testl %eax, %eax
jne 0x337a
movq 0x18(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0xd8ac
testl %eax, %eax
jne 0x3381
movq 0x18(%rsp), %rdi
leaq 0x8(%rsp), %rcx
xorl %esi, %esi
movl $0x20000, %edx          # imm = 0x20000
callq 0x38a0
testl %eax, %eax
jne 0x3388
movq 0x8(%rsp), %rdi
movl 0x14(%rsp), %esi
leaq 0x40(%rsp), %rdx
callq 0x96a7
testl %eax, %eax
jne 0x338f
leaq 0x30(%rsp), %rbx
leaq 0x20(%rsp), %r15
leaq 0xc3ed(%rip), %r12      # 0xf241
movq 0x40(%rsp), %rdi
movq %rbx, %rsi
movq %r15, %rdx
movl $0x8, %ecx
callq 0x6415
testl %eax, %eax
jne 0x2e93
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 0x2e54
cmpl $0xffff87b2, %eax       # imm = 0xFFFF87B2
jne 0x331f
movq 0x40(%rsp), %rdi
callq 0x9856
leaq 0x8(%rsp), %rbx
movq (%rbx), %rdi
callq 0x3eea
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 0xc296(%rip), %rsi      # 0xf17d
movq %r14, %rdi
xorl %eax, %eax
callq 0x2350
movq 0x18(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
movq %rbx, %rcx
callq 0x38a0
testl %eax, %eax
jne 0x3396
movq 0x8(%rsp), %rdi
movl 0x14(%rsp), %esi
leaq 0x30(%rsp), %rdx
leaq 0x20(%rsp), %rcx
movl $0x20, %r8d
callq 0x99ec
cmpl $0xffff87b1, %eax       # imm = 0xFFFF87B1
je 0x2f37
testl %eax, %eax
jne 0x33d5
leaq 0x8(%rsp), %rbx
movq (%rbx), %rdi
callq 0x3eea
movq 0x48(%rsp), %rax
movl 0x3fc(%rax), %edx
incl %edx
leaq 0xc225(%rip), %rsi      # 0xf17d
leaq 0x58(%rsp), %rdi
xorl %eax, %eax
callq 0x2350
movq 0x18(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
movq %rbx, %rcx
callq 0x38a0
testl %eax, %eax
jne 0x339d
movq 0x8(%rsp), %rdi
movl 0x14(%rsp), %esi
leaq 0x30(%rsp), %rdx
leaq 0x20(%rsp), %rcx
movl $0x20, %r8d
callq 0x99ec
cmpl $0xffff87b1, %eax       # imm = 0xFFFF87B1
je 0x2faa
testl %eax, %eax
jne 0x33dc
leaq 0x8(%rsp), %rbx
movq (%rbx), %rdi
callq 0x3eea
movq 0x48(%rsp), %rax
movl 0x7d0(%rax), %edx
incl %edx
leaq 0xc1b2(%rip), %rsi      # 0xf17d
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 0x38a0
testl %eax, %eax
jne 0x33a4
movq 0x8(%rsp), %rdi
movl 0x14(%rsp), %esi
leaq 0x30(%rsp), %rdx
leaq 0x20(%rsp), %rcx
movl $0x20, %r8d
callq 0x99ec
cmpl $0xffff87b1, %eax       # imm = 0xFFFF87B1
je 0x3020
testl %eax, %eax
jne 0x33e3
movq 0x8(%rsp), %rdi
callq 0x3f10
testl %eax, %eax
jne 0x33ab
movq 0x18(%rsp), %rdi
leaq 0x8(%rsp), %rcx
xorl %esi, %esi
xorl %edx, %edx
callq 0x38a0
testl %eax, %eax
jne 0x33b2
movq 0x8(%rsp), %rdi
movl 0x14(%rsp), %esi
leaq 0x40(%rsp), %rdx
callq 0x96a7
testl %eax, %eax
jne 0x33b9
leaq 0x30(%rsp), %rbx
leaq 0x20(%rsp), %r15
leaq 0xc1f4(%rip), %r12      # 0xf26d
movq 0x40(%rsp), %rdi
movq %rbx, %rsi
movq %r15, %rdx
movl $0xa, %ecx
callq 0x6415
testl %eax, %eax
jne 0x30b0
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 0x3079
cmpl $0xffff87b2, %eax       # imm = 0xFFFF87B2
jne 0x3326
movq 0x40(%rsp), %rdi
callq 0x9856
leaq 0x8(%rsp), %rax
movq (%rax), %rdi
callq 0x3eea
movl $0x1fd, %r15d           # imm = 0x1FD
leaq 0xc09e(%rip), %r12      # 0xf17d
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 0x38a0
testl %eax, %eax
jne 0x3318
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 0x98ba
cmpl $0xffff87b2, %eax       # imm = 0xFFFF87B2
je 0x3181
testl %eax, %eax
jne 0x3349
movq 0x8(%rsp), %rdi
callq 0x3f10
testl %eax, %eax
jne 0x3342
incl %ebp
jmp 0x318b
movq 0x8(%rsp), %rdi
callq 0x3eea
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 0x30eb
movq 0x48(%rsp), %rdi
callq 0x2030
leaq 0xc0de(%rip), %rdi      # 0xf2a3
movl %ebp, %esi
xorl %eax, %eax
callq 0x2130
movq 0x18(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0xd8ac
testl %eax, %eax
jne 0x33c0
movq 0x18(%rsp), %rdi
leaq 0x8(%rsp), %rcx
xorl %esi, %esi
movl $0x20000, %edx          # imm = 0x20000
callq 0x38a0
testl %eax, %eax
jne 0x33c7
movq 0x8(%rsp), %rdi
movl 0x14(%rsp), %esi
leaq 0x40(%rsp), %rdx
callq 0x96a7
testl %eax, %eax
jne 0x33ce
leaq 0xc091(%rip), %rdi      # 0xf2b6
callq 0x20a0
leaq 0x30(%rsp), %rbx
leaq 0x20(%rsp), %r14
leaq 0xc032(%rip), %r15      # 0xf26d
movq 0x40(%rsp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
movl $0x8, %ecx
callq 0x6415
testl %eax, %eax
jne 0x3272
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 0x323b
cmpl $0xffff87b2, %eax       # imm = 0xFFFF87B2
jne 0x332d
leaq 0xc03e(%rip), %rdi      # 0xf2c2
callq 0x20a0
leaq 0x30(%rsp), %rbx
leaq 0x20(%rsp), %r14
leaq 0xbfd3(%rip), %r15      # 0xf26d
movq 0x40(%rsp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
movl $0xc, %ecx
callq 0x6415
testl %eax, %eax
jne 0x32d1
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 0x329a
cmpl $0xffff87b2, %eax       # imm = 0xFFFF87B2
jne 0x3334
movq 0x40(%rsp), %rdi
callq 0x9856
movq 0x8(%rsp), %rdi
callq 0x3eea
movq 0x18(%rsp), %rdi
movl 0x14(%rsp), %esi
callq 0x9e16
movq 0x18(%rsp), %rdi
callq 0xd19a
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
nop