// File: /workspace/github_c_makefile_star2_O1/sharpyfox[P]citrusleaf-client/tests/loop_c/src/loop.c
// Function: work_fn
void *
work_fn(void *gcc_is_ass)
{
	// Forever,
	do {
		// Pick a key to use. Look it up to see if anyone else is using it.
		uint32_t	key = rand_64() % g_config.n_keys;
		
		uint32_t    die = rand_64() & 0x03;
		
		if (SHASH_OK == shash_put_unique(g_config.in_progress_hash, &key, 0) ) 
		{
			cl_rv rv;
		
			// Make the key into a string
			char key_s[g_config.key_len+1];
			my_itoa(key_s, key, g_config.key_len);
			
			// Make an cl_object that represents the key
			cl_object key_o;
			citrusleaf_object_init_str(&key_o, key_s);
			

			cf_digest d;
			citrusleaf_calculate_digest(g_config.set, &key_o, &d);

			if (VALUE_UNINIT == g_config.values[key]) {

				// simply set the value to something - can't really check anything because we don't know the state				
				if (0 != write_new_value(key, &key_o, &d)) {
					if (g_config.strict)   					goto Fail;
				}
				atomic_int_add(g_config.key_counter, 1);
			}
			else if (VALUE_DELETED == g_config.values[key]) {
				
				// Shouldn't exist
				cl_bin *cl_v = 0;
				int		cl_v_len;
				rv = citrusleaf_get_all(g_config.asc, g_config.ns, g_config.set, &key_o, &cl_v, &cl_v_len, g_config.timeout_ms, NULL);
				if (rv != CITRUSLEAF_FAIL_NOTFOUND) {
					fprintf(stderr, "Get after delete returned improper value when should be deleted %d key %s digest %"PRIx64"\n",rv,key_s, *(uint64_t *)&d);
					if (g_config.strict)    goto Fail;
				}
				if (cl_v)	free(cl_v);

				atomic_int_add(g_config.read_counter, 1);  // did two ops here						
				
				// write a new value
				if (die < 2) {
					if (0 != write_new_value(key, &key_o, &d)) {
						if (g_config.strict)   goto Fail;
					}
					atomic_int_add(g_config.key_counter, 1);
				}
			}
			// Value is well known. Check to see that it's still right.
			else {			
				
				cl_bin values[1];
				strcpy(values[0].bin_name, g_config.bin);
				citrusleaf_object_init(&values[0].object);
				
				// Make string version of old value for checking
				char new_value_str[g_config.value_len+1];
				my_itoa(new_value_str, g_config.values[key], g_config.value_len); 
				citrusleaf_object_init_str(&values[0].object, new_value_str);
				
				rv = citrusleaf_verify(g_config.asc, g_config.ns, g_config.set, &key_o, values, 1, g_config.timeout_ms, NULL);
				if (rv != 0) {
					fprintf(stderr, "Get returned improper value %d when should be set : key %d digest %"PRIx64"\n",rv,key, *(uint64_t *)&d);
					if (g_config.strict)   goto Fail;
					goto V1;
				}
				
				// test!
				if (values[0].object.type != CL_STR) {
					fprintf(stderr, "read value has wrong type: expect string (3) got %d\n",(int)values[0].object.type);  
					if (g_config.strict)   return((void *)-1);
				}
				else if (strcmp(values[0].object.u.str, new_value_str) != 0) {
					fprintf(stderr, "read value does not match set value.\n");
					fprintf(stderr, "  expecting: %s\n",new_value_str);
					fprintf(stderr, "  got: %s\n",values[0].object.u.str);
					if (g_config.strict)   goto Fail;
				}
				
				citrusleaf_object_free(&values[0].object);
				atomic_int_add(g_config.read_counter, 1);
				
				// Delete, write new value, what's your pleasure?
			V1:				
				if (die < 2) {
					if (0 != write_new_value(key, &key_o, &d)) {
						if (g_config.strict)   return((void *)-1);
					}
				}
				// Delete!
				else if (die == 2) {
					rv = citrusleaf_delete_verify(g_config.asc, g_config.ns, g_config.set, &key_o, 0);
					if (rv != 0) {
						fprintf(stderr, "Delete returned improper value %d, fail: key %d digest %"PRIx64"\n",rv, key, *(uint64_t *)&d);
						if (g_config.strict)   goto Fail;
					}

					cl_bin values[1];
					strcpy(values[0].bin_name, g_config.bin);
					citrusleaf_object_init(&values[0].object);
					
					rv = citrusleaf_get(g_config.asc, g_config.ns, g_config.set, &key_o, values, 1, g_config.timeout_ms, NULL);
					if (rv != CITRUSLEAF_FAIL_NOTFOUND) {
						fprintf(stderr, "Get after delete returned improper value %d digest %"PRIx64"\n",rv, *(uint64_t *)&d);
						if (g_config.strict)   goto Fail;
					}
					
					citrusleaf_object_free(&values[0].object);
					
					g_config.values[key] = VALUE_DELETED;
					atomic_int_add(g_config.read_counter, 1);  // did two ops here
					atomic_int_add(g_config.delete_counter, 1);
					atomic_int_add(g_config.key_counter, -1);
					
				}
				
			}
			
			// remove my lock on this key
			shash_delete(g_config.in_progress_hash, &key);
			

		}		
	} while (1);
	
	
Fail:	
	abort();
	return((void *)-1);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
leaq 0x13e0b(%rip), %r14     # 0x172f0
movabsq $-0x3333333333333333, %r12 # imm = 0xCCCCCCCCCCCCCCCD
xorl %eax, %eax
callq 0x2f09
movl 0x30(%r14), %ecx
xorl %edx, %edx
divq %rcx
movl %edx, -0x2c(%rbp)
xorl %eax, %eax
callq 0x2f09
movq %rax, %rbx
movq 0x48(%r14), %rdi
leaq -0x2c(%rbp), %rsi
xorl %edx, %edx
callq 0x3ceb
testl %eax, %eax
je 0x3527
xorl %r15d, %r15d
jmp 0x38c9
movq %rsp, -0x38(%rbp)
movl 0x34(%r14), %eax
incl %eax
movq %rsp, %r15
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
movl -0x2c(%rbp), %ecx
movl 0x34(%r14), %eax
movb $0x0, (%rax,%r15)
decl %eax
js 0x3579
movl %eax, %esi
incq %rsi
movq %rcx, %rax
mulq %r12
shrq $0x3, %rdx
leal (%rdx,%rdx), %eax
leal (%rax,%rax,4), %eax
subl %eax, %ecx
orb $0x30, %cl
movb %cl, -0x1(%r15,%rsi)
decq %rsi
movq %rdx, %rcx
jg 0x3557
andl $0x3, %ebx
leaq -0x78(%rbp), %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0x4689
movq 0x18(%r14), %rdi
movq %r13, %rsi
leaq -0x50(%rbp), %rdx
callq 0x6819
movq 0x40(%r14), %rax
movl -0x2c(%rbp), %edi
movq (%rax,%rdi,8), %rax
cmpq $-0x2, %rax
je 0x35ef
cmpq $-0x1, %rax
jne 0x368f
leaq -0x78(%rbp), %rsi
leaq -0x50(%rbp), %rdx
callq 0x327d
testl %eax, %eax
movq -0x38(%rbp), %r13
je 0x35dc
movl $0x4, %r15d
cmpb $0x0, 0x2a(%r14)
jne 0x38bd
movq 0x68(%r14), %rdi
movl $0x1, %esi
callq 0x2e1f
jmp 0x38ad
xorl %r10d, %r10d
movq %r10, -0xc0(%rbp)
movq 0x78(%r14), %rdi
movq 0x10(%r14), %rsi
movq 0x18(%r14), %rdx
movl 0x74(%r14), %eax
leaq -0x78(%rbp), %rcx
leaq -0xc0(%rbp), %r8
leaq -0x100(%rbp), %r9
pushq %r10
pushq %rax
callq 0x655c
addq $0x10, %rsp
cmpl $0x2, %eax
jne 0x38d7
movq -0xc0(%rbp), %rdi
testq %rdi, %rdi
je 0x3641
callq 0x22c0
movq 0x50(%r14), %rdi
movl $0x1, %esi
callq 0x2e1f
xorl %r15d, %r15d
movb $0x1, %r13b
cmpl $0x1, %ebx
ja 0x38a4
movl -0x2c(%rbp), %edi
leaq -0x78(%rbp), %rsi
leaq -0x50(%rbp), %rdx
callq 0x327d
testl %eax, %eax
je 0x3896
cmpb $0x0, 0x2a(%r14)
je 0x3896
movl $0x4, %r15d
xorl %r13d, %r13d
jmp 0x38a4
movq 0x20(%r14), %rsi
leaq -0xc0(%rbp), %rdi
callq 0x2240
leaq -0xa0(%rbp), %rdi
callq 0x4678
movq %rsp, -0x58(%rbp)
movl 0x38(%r14), %eax
incl %eax
movq %rsp, %r15
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
movq 0x40(%r14), %rax
movl -0x2c(%rbp), %ecx
movq (%rax,%rcx,8), %rcx
movl 0x38(%r14), %eax
movb $0x0, (%rax,%r15)
decl %eax
movq -0x38(%rbp), %r13
js 0x3709
movl %eax, %esi
incq %rsi
movq %rcx, %rax
mulq %r12
shrq $0x3, %rdx
leal (%rdx,%rdx), %eax
leal (%rax,%rax,4), %eax
subl %eax, %ecx
orb $0x30, %cl
movb %cl, -0x1(%r15,%rsi)
decq %rsi
movq %rdx, %rcx
jg 0x36e7
leaq -0xa0(%rbp), %rdi
movq %r15, %rsi
callq 0x4689
movq 0x78(%r14), %rdi
movq 0x10(%r14), %rsi
movq 0x18(%r14), %rdx
movl 0x74(%r14), %eax
leaq -0x78(%rbp), %rcx
leaq -0xc0(%rbp), %r8
movl $0x1, %r9d
pushq $0x0
pushq %rax
callq 0x6722
addq $0x10, %rsp
testl %eax, %eax
jne 0x390e
movl -0xa0(%rbp), %edx
cmpl $0x3, %edx
jne 0x3945
movq -0x90(%rbp), %rdi
movq %r15, %rsi
callq 0x2350
testl %eax, %eax
jne 0x3973
leaq -0xa0(%rbp), %rdi
callq 0x4739
movq 0x50(%r14), %rdi
movl $0x1, %esi
callq 0x2e1f
cmpl $0x1, %ebx
ja 0x37c0
movl -0x2c(%rbp), %edi
leaq -0x78(%rbp), %rsi
leaq -0x50(%rbp), %rdx
callq 0x327d
testl %eax, %eax
je 0x3888
movl $0x1, %r15d
cmpb $0x0, 0x2a(%r14)
je 0x3888
jmp 0x388b
cmpl $0x2, %ebx
jne 0x3888
movq 0x78(%r14), %rdi
movq 0x10(%r14), %rsi
movq 0x18(%r14), %rdx
leaq -0x78(%rbp), %rcx
xorl %r8d, %r8d
callq 0x67cc
testl %eax, %eax
jne 0x3995
movq 0x20(%r14), %rsi
leaq -0x100(%rbp), %rbx
movq %rbx, %rdi
callq 0x2240
leaq -0xe0(%rbp), %rdi
callq 0x4678
movq 0x78(%r14), %rdi
movq 0x10(%r14), %rsi
movq 0x18(%r14), %rdx
movl 0x74(%r14), %eax
leaq -0x78(%rbp), %rcx
movq %rbx, %r8
movl $0x1, %r9d
pushq $0x0
pushq %rax
callq 0x570c
addq $0x10, %rsp
cmpl $0x2, %eax
jne 0x39cc
leaq -0xe0(%rbp), %rdi
callq 0x4739
movq 0x40(%r14), %rax
movl -0x2c(%rbp), %ecx
movq $-0x2, (%rax,%rcx,8)
movq 0x50(%r14), %rdi
movl $0x1, %esi
callq 0x2e1f
movq 0x60(%r14), %rdi
movl $0x1, %esi
callq 0x2e1f
movq 0x68(%r14), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x2e1f
xorl %r15d, %r15d
movb $0x1, %bl
testb %bl, %bl
je 0x388b
xorl %r15d, %r15d
movq -0x58(%rbp), %rsp
testl %r15d, %r15d
jne 0x38bd
jmp 0x38ad
movq 0x68(%r14), %rdi
movl $0x1, %esi
callq 0x2e1f
testb %r13b, %r13b
movq -0x38(%rbp), %r13
je 0x38bd
movq 0x48(%r14), %rdi
leaq -0x2c(%rbp), %rsi
callq 0x3fcd
xorl %r15d, %r15d
movq %r13, %rsp
testl %r15d, %r15d
je 0x351f
testl %r15d, %r15d
je 0x34ef
jmp 0x3a02
movq 0x13702(%rip), %rcx     # 0x16fe0
movq (%rcx), %rdi
movq -0x50(%rbp), %r8
xorl %r13d, %r13d
leaq 0xcbc2(%rip), %rsi      # 0x104b1
movl %eax, %edx
movq %r15, %rcx
xorl %eax, %eax
callq 0x2380
movl $0x4, %r15d
cmpb $0x0, 0x2a(%r14)
je 0x3630
jmp 0x38a4
movq 0x136cb(%rip), %rcx     # 0x16fe0
movq (%rcx), %rdi
movl -0x2c(%rbp), %ecx
movq -0x50(%rbp), %r8
leaq 0xcbe1(%rip), %rsi      # 0x10507
movl %eax, %edx
xorl %eax, %eax
callq 0x2380
movl $0x4, %r15d
cmpb $0x0, 0x2a(%r14)
je 0x378d
jmp 0x388b
movq 0x13694(%rip), %rax     # 0x16fe0
movq (%rax), %rdi
leaq 0xcbf8(%rip), %rsi      # 0x1054e
xorl %eax, %eax
callq 0x2380
movl $0x1, %r15d
cmpb $0x0, 0x2a(%r14)
je 0x3773
jmp 0x388b
movq %r15, %rdi
leaq -0x90(%rbp), %rsi
callq 0x2bbc
movl $0x4, %r15d
testb $0x1, %al
je 0x3773
jmp 0x388b
movq 0x13644(%rip), %rcx     # 0x16fe0
movq (%rcx), %rdi
movl -0x2c(%rbp), %ecx
movq -0x50(%rbp), %r8
leaq 0xcbfc(%rip), %rsi      # 0x105a9
movl %eax, %edx
xorl %eax, %eax
callq 0x2380
movl $0x4, %r15d
cmpb $0x0, 0x2a(%r14)
je 0x37e9
jmp 0x388b
movq 0x1360d(%rip), %rcx     # 0x16fe0
movq (%rcx), %rdi
movq -0x50(%rbp), %rcx
xorl %ebx, %ebx
leaq 0xcc02(%rip), %rsi      # 0x105e5
movl %eax, %edx
xorl %eax, %eax
callq 0x2380
movl $0x4, %r15d
cmpb $0x0, 0x2a(%r14)
je 0x383a
jmp 0x3884
cmpl $0x1, %r15d
jne 0x3a1e
movq $-0x1, %rax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2150