// File: /workspace/github_c_makefile_star2_O2/sharpyfox[P]citrusleaf-client/tests/key_c/src/key.c
// Function: read_fn
void *
read_fn(void *udata)
{
	read_work *work = (read_work *) udata;
	uint32_t delay_factor = 0;

	if (g_config.verbose) fprintf(stderr, " read thread started: key_start %d nkeys %d\n",work->start_key,work->n_keys);
	
	for (uint i=0;i<work->n_reads;i++) {
	
		cl_rv rv;
		uint key_int = (rand_64() % work->n_keys) + work->start_key;
		key_value *kv = make_key_value(key_int, work->key_len, work->value_len);

		// Create the key we want to read
		cl_object o_key;
		if (g_config.key_type == CL_STR) {
			citrusleaf_object_init_str(&o_key,kv->key_str);
		}
		else if (g_config.key_type == CL_INT) {
			citrusleaf_object_init_int(&o_key,kv->key_int);
		}
		else {
			if (!g_config.silent) fprintf(stderr, " read thread: unsupport key type %d\n",g_config.key_type);
		}
	
		// bundle it into a value array
		cl_bin values[1];
		memset(values, 0, sizeof(values));
		strcpy(values[0].bin_name, g_config.bin);
	
#ifdef PRINT_KEY
		cf_digest d;
		citrusleaf_calculate_digest(g_config.table, &o_key, &d);
		fprintf(stderr, "get: key %d digest %"PRIx64"\n",key_int, *(uint64_t *)&d);
#endif
		
		
		if (g_config.server_validate) {
	
			if (g_config.value_type == CL_STR) {
				citrusleaf_object_init_str(&values[0].object, kv->value_str);
			}
			else if (g_config.value_type == CL_BLOB) {
				citrusleaf_object_init_blob(&values[0].object, kv->value_blob, kv->value_blob_len);
			}
			else if (g_config.value_type == CL_INT) {
				citrusleaf_object_init_int(&values[0].object, kv->value_int);
			}
			else {
				if (!g_config.silent) fprintf(stderr, "INTERNAL ERROR: unexpected value type under verify, help!\n");
			}
			if (CITRUSLEAF_OK != (rv = citrusleaf_verify(work->asc, g_config.ns, g_config.table, &o_key,values, 1, g_config.timeout, NULL) ) ) 
			{
				if (!g_config.silent) fprintf(stderr, "read: validate: key %d value %"PRIu64" failed with %d\n",key_int,kv->value_int, rv);
				citrusleaf_object_free(&values[0].object);
				if (g_config.continuous == false) {
					free(kv);
					return((void *)-1);
				}
				goto NextRead;
			}
		}

		else {
			// do the actual work of reading
			uint64_t start_time = cf_getms();
			if (CITRUSLEAF_OK != (rv = citrusleaf_get(work->asc, g_config.ns, g_config.table, &o_key, values, 1, g_config.timeout, 0))) {
	            uint64_t stop_time = cf_getms();
	            uint64_t diff = safe_delta_ms(start_time, stop_time);

	        	if (rv == CITRUSLEAF_FAIL_TIMEOUT || rv == CITRUSLEAF_FAIL_SERVERSIDE_TIMEOUT) {
	                histogram_insert_data_point(g_p_read_timeout_histogram, diff);

	                if (!g_config.silent) {
                        fprintf(stderr, "citrusleaf get timeout %d %"PRIu64" %d\n",rv,diff,g_config.timeout);
	                }
	        	}
	        	else {
	                if (!g_config.silent) {
	                	fprintf(stderr, "read: failed with error %d\n",rv);
	                }
	        	}
				citrusleaf_object_free(&values[0].object);
				if (g_config.continuous == false) {
					free(kv);
					return((void *)-1);
				}
				goto NextRead;
			}
			uint64_t stop_time = cf_getms();
 			histogram_insert_data_point(g_p_read_histogram,
			safe_delta_ms(start_time, stop_time));
		}
		
		// validate
		
		
		if (values[0].object.type != g_config.value_type) {
			if (!g_config.silent) fprintf(stderr, "read value has wrong type: expect %d got %d\n",(int) g_config.value_type, (int) values[0].object.type);
			if (g_config.continuous == false) {
				citrusleaf_object_free(&values[0].object);
				free(kv);
				return((void *)-1);
			}
			raise(SIGINT);
			goto NextRead;
		}
		
		if (g_config.value_type == CL_STR) {
			if (values[0].object.u.str == 0) {
				if (!g_config.silent) fprintf(stderr, "read value pointer is null. Yuck.\n");
				citrusleaf_object_free(&values[0].object);
				if (g_config.continuous == false) {
					free(kv);
					return((void *)-1);
				}
				goto NextRead;
			}
						
			if (values[0].object.sz  != strlen(kv->value_str)) {
				if (!g_config.silent) fprintf(stderr, "read value size doesn't match expected. Is %zd expecting %zd\n",
					values[0].object.sz,strlen(kv->value_str)+1);
				citrusleaf_object_free(&values[0].object);
				if (g_config.continuous == false) {
					free(kv);					
					return((void *)-1);
				}
				goto NextRead;
			}			
			
			if (strcmp(values[0].object.u.str, kv->value_str) != 0) {
				if (!g_config.silent) {
					fprintf(stderr, "read value does not match set value.\n");
					fprintf(stderr, "  expecting: %s\n",kv->value_str);
					fprintf(stderr, "  got:       %s\n",values[0].object.u.str);
				}
				citrusleaf_object_free(&values[0].object);

				if (g_config.continuous == false) {
					free(kv);
					return((void *)-1);
				}
				goto NextRead;
			}
		}
		else if (g_config.value_type == CL_BLOB) {
			if (values[0].object.u.blob == 0) {
				if (!g_config.silent) fprintf(stderr, "read value pointer is null. Yuck.\n");
				citrusleaf_object_free(&values[0].object);
				if (g_config.continuous == false) {
					free(kv);
					return((void *)-1);
				}
				goto NextRead;
			}
			
			if (values[0].object.sz  != kv->value_blob_len) {
				if (!g_config.silent) fprintf(stderr, "read value size doesn't match expected. Is %zd expecting %d\n",
					values[0].object.sz,kv->value_blob_len);
				citrusleaf_object_free(&values[0].object);

				if (g_config.continuous == false) {
					free(kv);					
					return((void *)-1);
				}
				goto NextRead;
			}			
			
			if (memcmp(values[0].object.u.blob, kv->value_blob, kv->value_blob_len) != 0) {
				if (!g_config.silent) {
					int k;
					uint8_t *b1 = (uint8_t *) values[0].object.u.blob;
					uint8_t *b2 = (uint8_t *) kv->value_blob;
					for (k=0;k<kv->value_blob_len;k++) {
						if (b1[k] != b2[k]) break;
					}
					
					fprintf(stderr, "read value does not match set value, key int %d, differ at byte %d (length correct).\n",key_int,k);
					dump_buf("got buf: ", b1, kv->value_blob_len);
					dump_buf("expecting buf: ", b2, kv->value_blob_len);

				}
				citrusleaf_object_free(&values[0].object);

				if (g_config.continuous == false) {
					free(kv);
					return((void *)-1);
				}
				goto NextRead;
			}

			
		}
		else if (g_config.value_type == CL_INT) {
			
			if (values[0].object.u.i64 != kv->value_int) {
				if (!g_config.silent) {
					fprintf(stderr, "read value does not match set value.\n");
					fprintf(stderr, "  expecting: %"PRIu64"\n",kv->value_int);
					fprintf(stderr, "  got:       %"PRIu64"\n",values[0].object.u.i64);
				}
				citrusleaf_object_free(&values[0].object);
				if (g_config.continuous == false) {
					free(kv);
					return((void *)-1);
				}
				goto NextRead;

			}			
			
		}

		// have to hand back any possible malloced memory
		citrusleaf_object_free(&values[0].object);
NextRead:
		free(kv);
		
		atomic_int_add(work->completed, 1);
		
		if (g_config.delay) {
			if (g_config.delay >= 1000) {
				usleep( (g_config.delay - 1000) * 1000 );
			}
			else {
				if ( (delay_factor++ % (1000 - g_config.delay)) == 0) {
					usleep( 1000 );
				}
			}
		}
		
	}
	return((void *)0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
leaq 0x1702c(%rip), %r13     # 0x1a330
movq %rdi, %rbx
cmpb $0x1, 0x28(%r13)
je 0x3960
leaq 0x60(%rsp), %rbp
leaq 0x10(%rsp), %r14
xorl %r15d, %r15d
movq $0x0, 0x58(%rsp)
cmpl (%rbx), %r15d
jae 0x3935
xorl %eax, %eax
movl %r15d, 0xc(%rsp)
callq 0x2834
movl 0x8(%rbx), %ecx
xorl %edx, %edx
movl 0xc(%rbx), %esi
divq %rcx
movq %rdx, %r12
movl 0x10(%rbx), %edx
addl 0x4(%rbx), %r12d
movl %r12d, %edi
callq 0x3983
movl 0x4c(%r13), %edx
movq %rax, %r15
cmpl $0x1, %edx
je 0x3379
cmpl $0x3, %edx
jne 0x3386
movq 0x8(%r15), %rsi
movq %rbp, %rdi
callq 0x564e
jmp 0x3391
movq (%r15), %rsi
movq %rbp, %rdi
callq 0x568f
jmp 0x3391
cmpb $0x0, 0x29(%r13)
je 0x377c
movq 0x20(%r13), %rsi
xorps %xmm0, %xmm0
movq %r14, %rdi
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
callq 0x2210
cmpb $0x1, 0x2c(%r13)
jne 0x33ed
movl 0x48(%r13), %eax
cmpl $0x1, %eax
je 0x34b7
cmpl $0x4, %eax
je 0x34a3
cmpl $0x3, %eax
jne 0x34c7
movq 0x10(%r15), %rsi
leaq 0x30(%rsp), %rdi
callq 0x564e
jmp 0x34d2
callq 0x5408
movq %r14, %r8
movq %rax, %r14
movl 0x34(%r13), %eax
movq 0x20(%rbx), %rdi
movq 0x10(%r13), %rsi
movq 0x18(%r13), %rdx
movq %rbp, %rcx
pushq $0x1
popq %r9
pushq $0x0
pushq %rax
callq 0x6574
popq %rcx
popq %rdx
movl %eax, %ebp
callq 0x5408
testl %ebp, %ebp
je 0x347f
subq %r14, %rax
movl $0x0, %r14d
cmovaeq %rax, %r14
cmpl $0x9, %ebp
je 0x343f
cmpl $-0x2, %ebp
jne 0x35f3
movq 0x16f4a(%rip), %rdi     # 0x1a390
movq %r14, %rsi
callq 0x5595
cmpb $0x0, 0x29(%r13)
jne 0x35fe
movq 0x14b80(%rip), %rax     # 0x17fe0
movl 0x34(%r13), %r8d
leaq 0xd559(%rip), %rsi      # 0x109c4
movl %ebp, %edx
movq %r14, %rcx
movq (%rax), %rdi
xorl %eax, %eax
callq 0x2390
jmp 0x35fe
movq 0x16f12(%rip), %rdi     # 0x1a398
subq %r14, %rax
movl $0x0, %esi
cmovaeq %rax, %rsi
callq 0x5595
leaq 0x60(%rsp), %rbp
leaq 0x10(%rsp), %r14
jmp 0x3521
movq 0x20(%r15), %rsi
movslq 0x18(%r15), %rdx
leaq 0x30(%rsp), %rdi
callq 0x56a7
jmp 0x34d2
movq 0x28(%r15), %rsi
leaq 0x30(%rsp), %rdi
callq 0x568f
jmp 0x34d2
cmpb $0x0, 0x29(%r13)
je 0x3887
movl 0x34(%r13), %eax
movq 0x20(%rbx), %rdi
movq 0x10(%r13), %rsi
movq 0x18(%r13), %rdx
movq %rbp, %rcx
movq %r14, %r8
pushq $0x1
popq %r9
pushq $0x0
pushq %rax
callq 0x759f
addq $0x10, %rsp
testl %eax, %eax
je 0x3521
cmpb $0x0, 0x29(%r13)
je 0x3799
leaq 0x30(%rsp), %rdi
callq 0x56e9
cmpb $0x0, 0x2b(%r13)
jne 0x361d
jmp 0x3943
movl 0x30(%rsp), %ecx
movl 0x48(%r13), %edx
cmpl %edx, %ecx
jne 0x35d3
cmpl $0x1, %ecx
je 0x3701
cmpl $0x4, %ecx
je 0x368b
cmpl $0x3, %ecx
jne 0x370c
movq 0x40(%rsp), %r12
testq %r12, %r12
je 0x37f1
movq 0x10(%r15), %rbp
movq 0x38(%rsp), %r14
movq %rbp, %rdi
callq 0x20d0
cmpq %rax, %r14
jne 0x384a
movq %r12, %rdi
movq %rbp, %rsi
callq 0x2360
testl %eax, %eax
je 0x370c
cmpb $0x0, 0x29(%r13)
jne 0x35fe
movq 0x14a4b(%rip), %r14     # 0x17fe0
leaq 0xd4f4(%rip), %rdi      # 0x10a90
movq (%r14), %rcx
pushq $0x25
popq %rsi
pushq $0x1
popq %rdx
callq 0x2550
movq (%r14), %rdi
movq 0x10(%r15), %rdx
leaq 0xd4fe(%rip), %rsi      # 0x10ab6
xorl %eax, %eax
callq 0x2390
movq (%r14), %rdi
movq 0x40(%rsp), %rdx
leaq 0xd4f9(%rip), %rsi      # 0x10ac7
jmp 0x3770
cmpb $0x0, 0x29(%r13)
je 0x386a
cmpb $0x0, 0x2b(%r13)
je 0x3939
pushq $0x2
popq %rdi
callq 0x2230
jmp 0x361d
cmpb $0x0, 0x29(%r13)
je 0x38a8
leaq 0x30(%rsp), %rdi
callq 0x56e9
cmpb $0x0, 0x2b(%r13)
leaq 0x60(%rsp), %rbp
leaq 0x10(%rsp), %r14
je 0x3943
movq %r15, %rdi
callq 0x22a0
movq 0x18(%rbx), %rdi
pushq $0x1
popq %rsi
callq 0x26ab
movl 0x54(%r13), %edx
testl %edx, %edx
je 0x3654
movl 0xc(%rsp), %r15d
cmpl $0x3e8, %edx            # imm = 0x3E8
jb 0x365b
imull $0x3e8, %edx, %edi      # imm = 0x3E8
addl $0xfff0bdc0, %edi       # imm = 0xFFF0BDC0
jmp 0x367e
movl 0xc(%rsp), %r15d
jmp 0x3683
movq 0x58(%rsp), %rax
movl $0x3e8, %esi            # imm = 0x3E8
movl $0x3e8, %edi            # imm = 0x3E8
subl %edx, %esi
xorl %edx, %edx
leal 0x1(%rax), %ecx
divl %esi
testl %edx, %edx
movl %ecx, %eax
movq %rax, 0x58(%rsp)
jne 0x3683
callq 0x2370
incl %r15d
jmp 0x3328
movq 0x40(%rsp), %r14
testq %r14, %r14
je 0x37c0
movq 0x38(%rsp), %rdx
movslq 0x18(%r15), %rcx
cmpq %rcx, %rdx
jne 0x381d
movq 0x20(%r15), %rbp
movq %r14, %rdi
movq %rcx, 0x80(%rsp)
movq %rbp, %rsi
callq 0x2240
testl %eax, %eax
je 0x370c
cmpb $0x0, 0x29(%r13)
jne 0x35fe
movq 0x80(%rsp), %rax
movl $0x0, %ecx
testl %eax, %eax
cmovgl %eax, %ecx
xorl %eax, %eax
cmpq %rax, %rcx
je 0x38ef
movb (%r14,%rax), %dl
cmpb (%rbp,%rax), %dl
jne 0x38ed
incq %rax
jmp 0x36e5
movq 0x40(%rsp), %rax
cmpq 0x28(%r15), %rax
jne 0x3725
leaq 0x30(%rsp), %rdi
callq 0x56e9
leaq 0x60(%rsp), %rbp
leaq 0x10(%rsp), %r14
jmp 0x361d
cmpb $0x0, 0x29(%r13)
jne 0x35fe
movq 0x148a9(%rip), %r14     # 0x17fe0
leaq 0xd352(%rip), %rdi      # 0x10a90
movq (%r14), %rcx
pushq $0x25
popq %rsi
pushq $0x1
popq %rdx
callq 0x2550
movq (%r14), %rdi
movq 0x28(%r15), %rdx
leaq 0xd42b(%rip), %rsi      # 0x10b85
xorl %eax, %eax
callq 0x2390
movq (%r14), %rdi
movq 0x40(%rsp), %rdx
leaq 0xd427(%rip), %rsi      # 0x10b97
xorl %eax, %eax
callq 0x2390
jmp 0x35fe
movq 0x1485d(%rip), %rax     # 0x17fe0
leaq 0xd1a9(%rip), %rsi      # 0x10933
movq (%rax), %rdi
xorl %eax, %eax
callq 0x2390
jmp 0x3391
movq 0x14840(%rip), %rcx     # 0x17fe0
movl %eax, %r8d
leaq 0xd1e9(%rip), %rsi      # 0x10993
movl %r12d, %edx
xorl %eax, %eax
movq (%rcx), %rdi
movq 0x28(%r15), %rcx
callq 0x2390
jmp 0x3507
cmpb $0x0, 0x29(%r13)
leaq 0x10(%rsp), %r14
jne 0x3507
movq 0x14809(%rip), %rax     # 0x17fe0
leaq 0xd251(%rip), %rdi      # 0x10a2f
movq (%rax), %rcx
pushq $0x22
popq %rsi
pushq $0x1
popq %rdx
callq 0x2550
jmp 0x3507
cmpb $0x0, 0x29(%r13)
jne 0x35fe
movq 0x147dd(%rip), %rax     # 0x17fe0
leaq 0xd225(%rip), %rdi      # 0x10a2f
movq (%rax), %rcx
pushq $0x22
popq %rsi
pushq $0x1
popq %rdx
callq 0x2550
jmp 0x35fe
cmpb $0x0, 0x29(%r13)
leaq 0x10(%rsp), %r14
jne 0x3507
movq 0x147ac(%rip), %rax     # 0x17fe0
leaq 0xd29d(%rip), %rsi      # 0x10ad8
movq (%rax), %rdi
xorl %eax, %eax
callq 0x2390
jmp 0x3507
cmpb $0x0, 0x29(%r13)
leaq 0x60(%rsp), %rbp
je 0x38c7
leaq 0x30(%rsp), %rdi
callq 0x56e9
cmpb $0x0, 0x2b(%r13)
jmp 0x3612
movq 0x1476f(%rip), %rax     # 0x17fe0
leaq 0xd18a(%rip), %rsi      # 0x10a02
movq (%rax), %rdi
xorl %eax, %eax
callq 0x2390
jmp 0x35de
movq 0x14752(%rip), %rax     # 0x17fe0
leaq 0xd0c3(%rip), %rdi      # 0x10958
movq (%rax), %rcx
pushq $0x3a
popq %rsi
pushq $0x1
popq %rdx
callq 0x2550
jmp 0x34d2
movq 0x14731(%rip), %rax     # 0x17fe0
leaq 0xd130(%rip), %rsi      # 0x109e6
movl %ebp, %edx
movq (%rax), %rdi
xorl %eax, %eax
callq 0x2390
jmp 0x35fe
movq 0x14712(%rip), %rcx     # 0x17fe0
incq %rax
leaq 0xd17a(%rip), %rsi      # 0x10a52
movq %r14, %rdx
movq (%rcx), %rdi
movq %rax, %rcx
xorl %eax, %eax
callq 0x2390
jmp 0x3856
movl %eax, %ecx
movq 0x146ea(%rip), %rax     # 0x17fe0
leaq 0xd218(%rip), %rsi      # 0x10b15
movl %r12d, %edx
movq (%rax), %rdi
xorl %eax, %eax
callq 0x2390
movslq 0x18(%r15), %rdx
leaq 0xd256(%rip), %rdi      # 0x10b6b
movq %r14, %rsi
callq 0x3adb
movslq 0x18(%r15), %rdx
leaq 0xd24d(%rip), %rdi      # 0x10b75
movq %rbp, %rsi
callq 0x3adb
jmp 0x35fe
xorl %eax, %eax
jmp 0x394e
leaq 0x30(%rsp), %rdi
callq 0x56e9
movq %r15, %rdi
callq 0x22a0
pushq $-0x1
popq %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x14679(%rip), %rax     # 0x17fe0
movl 0x4(%rbx), %edx
movl 0x8(%rbx), %ecx
leaq 0xcf92(%rip), %rsi      # 0x10906
movq (%rax), %rdi
xorl %eax, %eax
callq 0x2390
jmp 0x3312