// File: /workspace/github_c_makefile_star2_O2/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 0x12277(%rip), %r13     # 0x152f0
leaq -0x2c(%rbp), %r12
leaq -0xc0(%rbp), %r15
xorl %eax, %eax
callq 0x27f3
movl 0x30(%r13), %ecx
xorl %edx, %edx
divq %rcx
xorl %eax, %eax
movl %edx, -0x2c(%rbp)
callq 0x27f3
movq 0x48(%r13), %rdi
movq %r12, %rsi
xorl %edx, %edx
movq %rax, %r14
callq 0x39bd
testl %eax, %eax
movl $0x0, %eax
jne 0x33ae
movl 0x34(%r13), %eax
movq %rsp, %rbx
andl $0x3, %r14d
movq %rsp, -0x38(%rbp)
incl %eax
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %rbx
movq %rbx, %rsp
movl -0x2c(%rbp), %esi
movl 0x34(%r13), %edx
movq %rbx, %rdi
callq 0x2dec
movq %r15, %rdi
movq %rbx, %rsi
callq 0x42b2
movq 0x18(%r13), %rdi
movq %r15, %rsi
leaq -0x50(%rbp), %rdx
callq 0x632b
movq 0x40(%r13), %rax
movl -0x2c(%rbp), %edi
movq (%rax,%rdi,8), %rax
cmpq $-0x2, %rax
je 0x3147
cmpq $-0x1, %rax
jne 0x31da
movq %r15, %rsi
leaq -0x50(%rbp), %rdx
callq 0x2e1b
testl %eax, %eax
je 0x31c9
cmpb $0x0, 0x2a(%r13)
pushq $0x4
popq %rax
je 0x31c9
jmp 0x32d4
movl 0x74(%r13), %eax
andq $0x0, -0xa0(%rbp)
movq 0x78(%r13), %rdi
movq 0x10(%r13), %rsi
movq 0x18(%r13), %rdx
movq %r15, %rcx
leaq -0xa0(%rbp), %r8
leaq -0x100(%rbp), %r9
pushq $0x0
pushq %rax
callq 0x5fff
addq $0x10, %rsp
cmpl $0x2, %eax
jne 0x33bb
movq -0xa0(%rbp), %rdi
callq 0x22c0
movq 0x50(%r13), %rdi
pushq $0x1
popq %rsi
callq 0x269d
cmpl $0x1, %r14d
ja 0x3399
movl -0x2c(%rbp), %edi
movq %r15, %rsi
leaq -0x50(%rbp), %rdx
callq 0x2e1b
testl %eax, %eax
je 0x31c9
cmpb $0x0, 0x2a(%r13)
je 0x31c9
pushq $0x4
popq %rax
jmp 0x32d4
movq 0x68(%r13), %rdi
pushq $0x1
popq %rsi
callq 0x269d
jmp 0x3399
movq 0x20(%r13), %rsi
leaq -0xa0(%rbp), %rdi
callq 0x2240
leaq -0x80(%rbp), %r12
movq %r12, %rdi
callq 0x42a4
movl 0x38(%r13), %eax
movq %rsp, %rbx
movq %rsp, -0x58(%rbp)
incl %eax
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %rbx
movq %rbx, %rsp
movq 0x40(%r13), %rax
movl -0x2c(%rbp), %ecx
movl 0x38(%r13), %edx
movq %rbx, %rdi
movq (%rax,%rcx,8), %rsi
callq 0x2dec
movq %r12, %rdi
movq %rbx, %rsi
callq 0x42b2
movl 0x74(%r13), %eax
movq 0x78(%r13), %rdi
movq 0x10(%r13), %rsi
movq 0x18(%r13), %rdx
movq %r15, %rcx
leaq -0xa0(%rbp), %r8
pushq $0x1
popq %r9
pushq $0x0
pushq %rax
callq 0x6203
addq $0x10, %rsp
testl %eax, %eax
jne 0x33ec
movl -0x80(%rbp), %edx
leaq -0x2c(%rbp), %r12
cmpl $0x3, %edx
jne 0x342b
movq -0x70(%rbp), %rdi
movq %rbx, %rsi
callq 0x2350
testl %eax, %eax
leaq -0xc0(%rbp), %r15
jne 0x345d
leaq -0x80(%rbp), %rdi
callq 0x434d
movq 0x50(%r13), %rdi
pushq $0x1
popq %rsi
callq 0x269d
cmpl $0x1, %r14d
ja 0x32dd
movl -0x2c(%rbp), %edi
movq %r15, %rsi
leaq -0x50(%rbp), %rdx
callq 0x2e1b
testl %eax, %eax
je 0x3395
cmpb $0x0, 0x2a(%r13)
pushq $0x1
popq %rax
je 0x3395
movq -0x58(%rbp), %rsp
movq -0x38(%rbp), %rbx
jmp 0x33ab
cmpl $0x2, %r14d
jne 0x3395
movq 0x78(%r13), %rdi
movq 0x10(%r13), %rsi
movq 0x18(%r13), %rdx
movq %r15, %rcx
xorl %r8d, %r8d
callq 0x62bf
testl %eax, %eax
jne 0x34c3
movq 0x20(%r13), %rsi
leaq -0x100(%rbp), %r14
movq %r14, %rdi
callq 0x2240
leaq -0xe0(%rbp), %rbx
movq %rbx, %rdi
callq 0x42a4
movl 0x74(%r13), %eax
movq 0x78(%r13), %rdi
movq 0x10(%r13), %rsi
movq 0x18(%r13), %rdx
movq %r15, %rcx
movq %r14, %r8
pushq $0x1
popq %r14
movl %r14d, %r9d
pushq $0x0
pushq %rax
callq 0x51d8
addq $0x10, %rsp
cmpl $0x2, %eax
jne 0x34f7
movq %rbx, %rdi
callq 0x434d
movq 0x40(%r13), %rax
movl -0x2c(%rbp), %ecx
movl %r14d, %esi
movq $-0x2, (%rax,%rcx,8)
movq 0x50(%r13), %rdi
callq 0x269d
movq 0x60(%r13), %rdi
movl %r14d, %esi
callq 0x269d
movq 0x68(%r13), %rdi
pushq $-0x1
popq %rsi
callq 0x269d
movq -0x58(%rbp), %rsp
movq 0x48(%r13), %rdi
movq -0x38(%rbp), %rbx
movq %r12, %rsi
callq 0x3cd0
xorl %eax, %eax
movq %rbx, %rsp
testl %eax, %eax
je 0x3084
jmp 0x3528
movq 0x11c1e(%rip), %rcx     # 0x14fe0
movq -0x50(%rbp), %r8
movl %eax, %edx
leaq 0xb0e2(%rip), %rsi      # 0xe4b1
xorl %eax, %eax
movq (%rcx), %rdi
movq %rbx, %rcx
callq 0x2380
cmpb $0x0, 0x2a(%r13)
je 0x3185
jmp 0x31c1
movq 0x11bed(%rip), %rcx     # 0x14fe0
movq -0x50(%rbp), %r8
movl %eax, %edx
leaq 0xb107(%rip), %rsi      # 0xe507
xorl %eax, %eax
movq (%rcx), %rdi
movl -0x2c(%rbp), %ecx
callq 0x2380
cmpb $0x0, 0x2a(%r13)
pushq $0x4
popq %rax
leaq -0x2c(%rbp), %r12
leaq -0xc0(%rbp), %r15
je 0x32a5
jmp 0x32d0
movq 0x11bae(%rip), %rax     # 0x14fe0
leaq 0xb115(%rip), %rsi      # 0xe54e
movq (%rax), %rdi
xorl %eax, %eax
callq 0x2380
cmpb $0x0, 0x2a(%r13)
pushq $0x1
popq %rax
leaq -0xc0(%rbp), %r15
je 0x3290
jmp 0x32d0
movq 0x11b7c(%rip), %rax     # 0x14fe0
leaq 0xb118(%rip), %rdi      # 0xe583
movq (%rax), %rcx
pushq $0x25
popq %rsi
pushq $0x1
popq %rdx
callq 0x2540
movq 0x11b60(%rip), %rax     # 0x14fe0
leaq 0xb00e(%rip), %rsi      # 0xe495
movq %rbx, %rdx
movq (%rax), %rdi
xorl %eax, %eax
callq 0x2380
movq 0x11b45(%rip), %rax     # 0x14fe0
movq -0x70(%rbp), %rdx
leaq 0xb000(%rip), %rsi      # 0xe4a6
movq (%rax), %rdi
xorl %eax, %eax
callq 0x2380
cmpb $0x0, 0x2a(%r13)
pushq $0x4
popq %rax
je 0x3290
jmp 0x32d0
movq 0x11b16(%rip), %rcx     # 0x14fe0
movq -0x50(%rbp), %r8
movl %eax, %edx
leaq 0xb0d2(%rip), %rsi      # 0xe5a9
xorl %eax, %eax
movq (%rcx), %rdi
movl -0x2c(%rbp), %ecx
callq 0x2380
cmpb $0x0, 0x2a(%r13)
pushq $0x4
popq %rax
jne 0x32d0
jmp 0x3306
movq 0x11ae2(%rip), %rcx     # 0x14fe0
movl %eax, %edx
leaq 0xb0de(%rip), %rsi      # 0xe5e5
xorl %eax, %eax
movq (%rcx), %rdi
movq -0x50(%rbp), %rcx
callq 0x2380
cmpb $0x0, 0x2a(%r13)
je 0x335a
pushq $0x4
popq %rax
jmp 0x32d0
cmpl $0x1, %eax
jne 0x353f
pushq $-0x1
popq %rax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2150