// File: /workspace/github_c_makefile_star2_O1/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 $0x98, %rsp
movq %rdi, %r14
leaq 0x18981(%rip), %r13     # 0x1c330
cmpb $0x1, 0x28(%r13)
je 0x40bb
xorl %eax, %eax
cmpl $0x0, (%r14)
je 0x40a9
leaq 0x30(%rsp), %r12
leaq 0x10(%rsp), %rbp
movq $0x0, 0x28(%rsp)
xorl %ebx, %ebx
movq %r14, 0x20(%rsp)
xorl %eax, %eax
callq 0x31ce
movl 0x8(%r14), %ecx
movl 0xc(%r14), %esi
xorl %edx, %edx
divq %rcx
movq %rdx, %r15
addl 0x4(%r14), %r15d
movl 0x10(%r14), %edx
movl %r15d, %edi
callq 0x40c8
movq %rax, %r14
movl 0x4c(%r13), %edx
cmpl $0x1, %edx
je 0x3a28
cmpl $0x3, %edx
jne 0x3a37
movq 0x8(%r14), %rsi
leaq 0x78(%rsp), %rdi
callq 0x5ee9
jmp 0x3a42
movq (%r14), %rsi
leaq 0x78(%rsp), %rdi
callq 0x5f30
jmp 0x3a42
cmpb $0x0, 0x29(%r13)
je 0x3e4e
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movq 0x20(%r13), %rsi
movq %r12, %rdi
callq 0x2210
cmpb $0x1, 0x2c(%r13)
movq %r14, 0x8(%rsp)
jne 0x3aad
movl 0x48(%r13), %eax
cmpl $0x1, %eax
je 0x3c10
cmpl $0x4, %eax
movq 0x20(%rsp), %r14
je 0x3bf7
cmpl $0x3, %eax
jne 0x3c25
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rsi
leaq 0x50(%rsp), %rdi
callq 0x5ee9
jmp 0x3c30
movl $0x1, %edi
movq %rbp, %rsi
callq 0x2200
movabsq $0x431bde82d7b634db, %r14 # imm = 0x431BDE82D7B634DB
movq %r14, %rax
mulq 0x18(%rsp)
shrq $0x12, %rdx
movq %r13, %rax
imulq $0x3e8, 0x10(%rsp), %r13 # imm = 0x3E8
addq %rdx, %r13
movq 0x20(%rsp), %rcx
movq 0x20(%rcx), %rdi
movq 0x10(%rax), %rsi
movq 0x18(%rax), %rdx
movl 0x34(%rax), %eax
leaq 0x78(%rsp), %rcx
movq %r12, %r8
movl $0x1, %r9d
pushq $0x0
pushq %rax
callq 0x6f6c
addq $0x10, %rsp
testl %eax, %eax
je 0x3ba6
movl %eax, %r12d
movl $0x1, %edi
movq %rbp, %rsi
callq 0x2200
movq %r14, %rax
mulq 0x18(%rsp)
shrq $0x12, %rdx
imulq $0x3e8, 0x10(%rsp), %rax # imm = 0x3E8
addq %rdx, %rax
subq %r13, %rax
movl $0x0, %r13d
cmovaeq %rax, %r13
cmpl $0x9, %r12d
je 0x3b5a
cmpl $-0x2, %r12d
jne 0x3c83
movq 0x1882f(%rip), %rdi     # 0x1c390
movq %r13, %rsi
callq 0x5dde
leaq 0x187c0(%rip), %rbp     # 0x1c330
cmpb $0x0, 0x29(%rbp)
movq 0x20(%rsp), %r14
jne 0x3c99
movq 0x1645a(%rip), %rax     # 0x19fe0
movq (%rax), %rdi
movl 0x34(%rbp), %r8d
leaq 0xee34(%rip), %rsi      # 0x129c8
movl %r12d, %edx
movq %r13, %rcx
xorl %eax, %eax
callq 0x2390
jmp 0x3c99
movl $0x1, %edi
movq %rbp, %rsi
callq 0x2200
movq %r14, %rax
mulq 0x18(%rsp)
shrq $0x12, %rdx
imulq $0x3e8, 0x10(%rsp), %rax # imm = 0x3E8
addq %rdx, %rax
movq 0x187c6(%rip), %rdi     # 0x1c398
xorl %r12d, %r12d
subq %r13, %rax
movl $0x0, %esi
cmovaeq %rax, %rsi
callq 0x5dde
leaq 0x18743(%rip), %r13     # 0x1c330
movq 0x20(%rsp), %r14
jmp 0x3cc2
movq 0x8(%rsp), %rax
movq 0x20(%rax), %rsi
movslq 0x18(%rax), %rdx
leaq 0x50(%rsp), %rdi
callq 0x5f4b
jmp 0x3c30
movq 0x28(%r14), %rsi
leaq 0x50(%rsp), %rdi
callq 0x5f30
movq 0x20(%rsp), %r14
jmp 0x3c30
cmpb $0x0, 0x29(%r13)
je 0x3ff6
movq 0x20(%r14), %rdi
movq 0x10(%r13), %rsi
movq 0x18(%r13), %rdx
movl 0x34(%r13), %eax
leaq 0x78(%rsp), %rcx
movq %r12, %r8
movl $0x1, %r9d
pushq $0x0
pushq %rax
callq 0x7f82
addq $0x10, %rsp
testl %eax, %eax
je 0x3cda
cmpb $0x0, 0x29(%r13)
je 0x3e6b
leaq 0x50(%rsp), %rdi
callq 0x5f99
cmpb $0x0, 0x2b(%r13)
jne 0x3ecd
jmp 0x3f0b
leaq 0x186a6(%rip), %rbp     # 0x1c330
cmpb $0x0, 0x29(%rbp)
movq 0x20(%rsp), %r14
je 0x4000
leaq 0x50(%rsp), %rdi
callq 0x5f99
movl $0x5, %r12d
cmpb $0x0, 0x2b(%rbp)
movq %rbp, %r13
jne 0x3cc2
movq 0x8(%rsp), %rdi
callq 0x22a0
movl $0x1, %r12d
cmpl $0x5, %r12d
leaq 0x10(%rsp), %rbp
je 0x3ecd
testl %r12d, %r12d
jne 0x3f4a
movl 0x50(%rsp), %ecx
movl 0x48(%r13), %edx
cmpl %edx, %ecx
jne 0x3d6f
cmpl $0x1, %edx
je 0x3e00
cmpl $0x4, %edx
je 0x3d9f
cmpl $0x3, %edx
jne 0x3e10
movq 0x60(%rsp), %r12
testq %r12, %r12
je 0x3ea5
movq 0x58(%rsp), %r15
movq 0x8(%rsp), %rax
movq 0x10(%rax), %r13
movq %r13, %rdi
callq 0x20d0
cmpq %rax, %r15
jne 0x3f9c
movq %r12, %rdi
movq %r13, %rsi
callq 0x2360
testl %eax, %eax
je 0x3e10
leaq 0x185e4(%rip), %r13     # 0x1c330
cmpb $0x0, 0x29(%r13)
jne 0x3eb7
movq 0x8(%rsp), %rdi
addq $0x10, %rdi
leaq 0x60(%rsp), %rsi
callq 0x2ab1
jmp 0x3eb7
cmpb $0x0, 0x29(%r13)
je 0x3fd9
cmpb $0x0, 0x2b(%r13)
je 0x3d90
movl $0x2, %edi
callq 0x2230
jmp 0x3ecd
leaq 0x50(%rsp), %rdi
callq 0x5f99
jmp 0x3f0b
movq 0x60(%rsp), %r12
testq %r12, %r12
je 0x3e97
movq 0x58(%rsp), %rdx
movq 0x8(%rsp), %rax
movslq 0x18(%rax), %r13
movl %r13d, %ecx
cmpq %r13, %rdx
jne 0x3f68
movq %rcx, 0x70(%rsp)
movq 0x20(%rax), %rbp
movq %r12, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x2240
testl %eax, %eax
je 0x3e10
leaq 0x18547(%rip), %rax     # 0x1c330
cmpb $0x0, 0x29(%rax)
je 0x402a
leaq 0x50(%rsp), %rdi
movq %rax, %r13
jmp 0x3ebc
movq 0x60(%rsp), %rax
movq 0x8(%rsp), %rcx
cmpq 0x28(%rcx), %rax
jne 0x3e2b
leaq 0x50(%rsp), %rdi
callq 0x5f99
leaq 0x10(%rsp), %rbp
leaq 0x1850a(%rip), %r13     # 0x1c330
jmp 0x3ecd
leaq 0x184fe(%rip), %r13     # 0x1c330
cmpb $0x0, 0x29(%r13)
jne 0x3eb7
movq 0x8(%rsp), %rdi
addq $0x28, %rdi
leaq 0x60(%rsp), %rsi
callq 0x2a3a
jmp 0x3eb7
movq 0x1618b(%rip), %rax     # 0x19fe0
movq (%rax), %rdi
leaq 0xead8(%rip), %rsi      # 0x12937
xorl %eax, %eax
callq 0x2390
jmp 0x3a42
movq 0x1616e(%rip), %rcx     # 0x19fe0
movq (%rcx), %rdi
movq 0x8(%rsp), %rcx
movq 0x28(%rcx), %rcx
leaq 0xeb12(%rip), %rsi      # 0x12997
movl %r15d, %edx
movl %eax, %r8d
xorl %eax, %eax
callq 0x2390
jmp 0x3c69
cmpb $0x0, 0x29(%r13)
jne 0x3eb7
callq 0x2a8f
jmp 0x3eb7
leaq 0x18484(%rip), %r13     # 0x1c330
cmpb $0x0, 0x29(%r13)
je 0x4020
leaq 0x50(%rsp), %rdi
callq 0x5f99
cmpb $0x0, 0x2b(%r13)
leaq 0x10(%rsp), %rbp
je 0x3f0b
movq 0x8(%rsp), %rdi
callq 0x22a0
movq 0x18(%r14), %rdi
movl $0x1, %esi
callq 0x303d
movl 0x54(%r13), %edx
xorl %r12d, %r12d
testl %edx, %edx
je 0x3f4a
cmpl $0x3e8, %edx            # imm = 0x3E8
jb 0x3f1d
imull $0x3e8, %edx, %edi      # imm = 0x3E8
addl $0xfff0bdc0, %edi       # imm = 0xFFF0BDC0
callq 0x2370
jmp 0x3f4a
movq 0x8(%rsp), %rdi
callq 0x22a0
movl $0x1, %r12d
jmp 0x3f4a
movq 0x28(%rsp), %rax
leal 0x1(%rax), %r15d
movl $0x3e8, %ecx            # imm = 0x3E8
subl %edx, %ecx
xorl %r12d, %r12d
xorl %edx, %edx
divl %ecx
testl %edx, %edx
jne 0x3f42
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x2370
movl %r15d, %eax
movq %rax, 0x28(%rsp)
testl %r12d, %r12d
jne 0x409e
incl %ebx
cmpl (%r14), %ebx
leaq 0x30(%rsp), %r12
jb 0x39e0
jmp 0x40a7
leaq 0x183c1(%rip), %r13     # 0x1c330
cmpb $0x0, 0x29(%r13)
leaq 0x10(%rsp), %rbp
jne 0x3c69
movq 0x1605a(%rip), %rax     # 0x19fe0
movq (%rax), %rdi
leaq 0xeb4c(%rip), %rsi      # 0x12adc
xorl %eax, %eax
callq 0x2390
jmp 0x3c69
leaq 0x1838d(%rip), %r13     # 0x1c330
cmpb $0x0, 0x29(%r13)
leaq 0x10(%rsp), %rbp
jne 0x3c69
movq 0x16026(%rip), %rcx     # 0x19fe0
movq (%rcx), %rdi
incq %rax
leaq 0xea8f(%rip), %rsi      # 0x12a56
movq %r15, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x2390
jmp 0x3c69
movq 0x16000(%rip), %rax     # 0x19fe0
movq (%rax), %rdi
leaq 0xea1c(%rip), %rsi      # 0x12a06
xorl %eax, %eax
callq 0x2390
jmp 0x3d7a
callq 0x2a18
jmp 0x3c30
movq 0x15fd9(%rip), %rax     # 0x19fe0
movq (%rax), %rdi
leaq 0xe9d9(%rip), %rsi      # 0x129ea
movl %r12d, %edx
xorl %eax, %eax
callq 0x2390
jmp 0x3c99
callq 0x2b06
jmp 0x3eb7
xorl %ecx, %ecx
testl %r13d, %r13d
jle 0x404c
xorl %ecx, %ecx
movq 0x70(%rsp), %rdx
movb (%r12,%rcx), %al
cmpb (%rbp,%rcx), %al
jne 0x404c
incq %rcx
cmpq %rcx, %rdx
jne 0x4038
movl %edx, %ecx
movq 0x15f8d(%rip), %rax     # 0x19fe0
movq (%rax), %rdi
leaq 0xeabc(%rip), %rsi      # 0x12b19
movl %r15d, %edx
xorl %eax, %eax
callq 0x2390
movq 0x8(%rsp), %r15
movslq 0x18(%r15), %rdx
leaq 0xeaf8(%rip), %rdi      # 0x12b6f
movq %r12, %rsi
callq 0x2945
movslq 0x18(%r15), %rdx
leaq 0xeaef(%rip), %rdi      # 0x12b79
movq %rbp, %rsi
callq 0x2945
leaq 0x18297(%rip), %rax     # 0x1c330
jmp 0x3df3
movq $-0x1, %rax
jmp 0x40a9
xorl %eax, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x29f7
jmp 0x39ba