// File: /workspace/github_c_makefile_star2_O2/sharpyfox[P]citrusleaf-client/tests/key_c/src/key.c
// Function: read_batch_fn
void *
read_batch_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);
	
	cf_digest *digests = alloca( sizeof(cf_digest) * g_config.n_batch );
	if (!digests) return(0);
	
	key_value **kvs = alloca( sizeof(key_value *) * g_config.n_batch);
	if (!kvs) return(0);
	
	uint *key_ints = alloca( sizeof(uint) * g_config.n_batch );
	if (!key_ints) return(0);

	getmany_userdata gm_udata;
	gm_udata.digests = digests;
	gm_udata.kvs = kvs;
	gm_udata.key_ints = key_ints;
	gm_udata.batch_factor = g_config.n_batch;
	
	// copy the bin name we want to fetch
	cl_bin values[1];
	memset(values, 0, sizeof(values));
	strcpy(values[0].bin_name, g_config.bin);

	cl_rv rv;
	
	// grab an array of digests to fetch
	for (uint i=0;i<work->n_reads / g_config.n_batch;i++) {
	
		for (int j=0; j < g_config.n_batch; j++) {
		
			uint key_int = key_ints[j] = (rand_64() % work->n_keys) + work->start_key;
			key_value *kv = kvs[j] = 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);
			}
		
			cf_digest d;
			citrusleaf_calculate_digest(g_config.table, &o_key, &d);
#ifdef PRINT_KEY			
			fprintf(stderr, "get: key %d digest %"PRIx64"\n",key_int, *(uint64_t *)&d);
#endif			

			memcpy( &digests[j], &d, sizeof(cf_digest) );
			
		}
		
		// do the actual work of reading
		uint64_t start_time = cf_getms();
		if (CITRUSLEAF_OK != (rv = citrusleaf_get_many_digest(work->asc, g_config.ns, digests, g_config.n_batch, values, 1, false, batch_getmany_cb, &gm_udata))) {
            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 many timeout %d %"PRIu64"\n",rv,diff);
                }
        	}
        	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) {
				for (int j = 0; j < g_config.n_batch; j++)
					free(kvs[j]);
				return((void *)-1);
			}
		}
		uint64_t stop_time = cf_getms();
		histogram_insert_data_point(g_p_read_histogram,
			safe_delta_ms(start_time, stop_time));
		
		for (int j = 0; j < g_config.n_batch; j++) {
			free(kvs[j]);
			kvs[j] = 0;
		}
		
		atomic_int_add(work->completed, g_config.n_batch);
		
		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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
leaq 0x163e6(%rip), %r14     # 0x1a330
movq %rdi, %rbx
cmpb $0x1, 0x28(%r14)
je 0x42b4
movq %r14, %rcx
movslq 0x30(%r14), %r14
movq %rsp, %r12
imulq $0x14, %r14, %rax
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %r12
movq %r12, %rsp
leaq 0xf(,%r14,8), %rax
movq %rsp, %r15
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
leaq 0xf(,%r14,4), %rax
movq %rsp, %rdx
andq $-0x10, %rax
subq %rax, %rdx
movq %rdx, %rsp
movq 0x20(%rcx), %rsi
leaq -0xf8(%rbp), %rax
leaq -0xd0(%rbp), %rdi
xorps %xmm0, %xmm0
movq %rdx, -0x58(%rbp)
movq %r12, (%rax)
movq %r15, 0x8(%rax)
movq %rdx, 0x10(%rax)
movl %r14d, 0x18(%rax)
leaq -0xb0(%rbp), %rax
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
movaps %xmm0, -0x10(%rax)
movaps %xmm0, -0x20(%rax)
callq 0x2210
xorl %ecx, %ecx
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq %r15, -0x50(%rbp)
movq %r12, -0x48(%rbp)
movl (%rbx), %eax
xorl %edx, %edx
divl %r14d
cmpl %eax, %ecx
jae 0x42a1
xorl %r13d, %r13d
movl %ecx, -0x30(%rbp)
movslq %r14d, %rax
cmpq %rax, %r13
jge 0x40eb
xorl %eax, %eax
callq 0x2834
movl 0x8(%rbx), %ecx
xorl %edx, %edx
divq %rcx
movq -0x58(%rbp), %rax
movq %rdx, %rdi
addl 0x4(%rbx), %edi
movl %edi, (%rax,%r13,4)
movl 0xc(%rbx), %esi
movl 0x10(%rbx), %edx
callq 0x3983
leaq 0x162e5(%rip), %r14     # 0x1a330
movq %rax, (%r15,%r13,8)
movl 0x4c(%r14), %edx
cmpl $0x1, %edx
je 0x4072
cmpl $0x3, %edx
jne 0x4086
movq 0x8(%rax), %rsi
leaq -0x90(%rbp), %r15
movq %r15, %rdi
callq 0x564e
jmp 0x4094
movq (%rax), %rsi
leaq -0x90(%rbp), %r15
movq %r15, %rdi
callq 0x568f
jmp 0x4094
cmpb $0x0, 0x29(%r14)
leaq -0x90(%rbp), %r15
je 0x40d1
movq 0x18(%r14), %rdi
movq %r15, %rsi
leaq -0x6c(%rbp), %rdx
callq 0x76c7
movl -0x5c(%rbp), %eax
movq -0x6c(%rbp), %rdx
movq -0x64(%rbp), %rcx
movl 0x30(%r14), %r14d
movq -0x50(%rbp), %r15
incq %r13
movl %eax, 0x10(%r12)
movq %rcx, 0x8(%r12)
movq %rdx, (%r12)
addq $0x14, %r12
jmp 0x4010
movq 0x13f08(%rip), %rax     # 0x17fe0
leaq 0xc854(%rip), %rsi      # 0x10933
movq (%rax), %rdi
xorl %eax, %eax
callq 0x2390
jmp 0x4094
callq 0x5408
leaq 0x16239(%rip), %r14     # 0x1a330
movq 0x20(%rbx), %rdi
movq %rax, %r13
movq 0x10(%r14), %rsi
movl 0x30(%r14), %ecx
subq $0x8, %rsp
movq -0x48(%rbp), %r12
leaq -0xd0(%rbp), %r8
movq %r12, %rdx
pushq $0x1
popq %r9
leaq -0xf8(%rbp), %rax
pushq %rax
leaq -0x591(%rip), %rax      # 0x3b9a
pushq %rax
pushq $0x0
callq 0x9c50
addq $0x20, %rsp
testl %eax, %eax
je 0x41b9
movl %eax, -0x2c(%rbp)
callq 0x5408
subq %r13, %rax
movl $0x0, %r12d
cmovaeq %rax, %r12
movl -0x2c(%rbp), %eax
cmpl $0x9, %eax
je 0x415d
cmpl $-0x2, %eax
jne 0x4193
movq 0x1622c(%rip), %rdi     # 0x1a390
movq %r12, %rsi
callq 0x5595
cmpb $0x0, 0x29(%r14)
jne 0x419e
movq 0x13e66(%rip), %rax     # 0x17fe0
movl -0x2c(%rbp), %edx
leaq 0xca69(%rip), %rsi      # 0x10bed
movq %r12, %rcx
movq (%rax), %rdi
xorl %eax, %eax
callq 0x2390
jmp 0x419e
cmpb $0x0, 0x29(%r14)
je 0x4261
leaq -0xb0(%rbp), %rdi
callq 0x56e9
cmpb $0x0, 0x2b(%r14)
movq -0x48(%rbp), %r12
je 0x4281
callq 0x5408
movq 0x161d3(%rip), %rdi     # 0x1a398
subq %r13, %rax
movl $0x0, %ecx
cmovbq %rcx, %rax
movq %rax, %rsi
callq 0x5595
xorl %r14d, %r14d
leaq 0x1614d(%rip), %rax     # 0x1a330
movslq 0x30(%rax), %rsi
cmpq %rsi, %r14
jge 0x41ff
movq (%r15,%r14,8), %rdi
callq 0x22a0
andq $0x0, (%r15,%r14,8)
incq %r14
jmp 0x41dc
movq 0x18(%rbx), %rdi
callq 0x26ab
leaq 0x16121(%rip), %r14     # 0x1a330
movl 0x54(%r14), %edx
testl %edx, %edx
je 0x4253
cmpl $0x3e8, %edx            # imm = 0x3E8
jb 0x422d
imull $0x3e8, %edx, %edi      # imm = 0x3E8
addl $0xfff0bdc0, %edi       # imm = 0xFFF0BDC0
jmp 0x424e
movq -0x40(%rbp), %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, -0x40(%rbp)
jne 0x4253
callq 0x2370
movl -0x30(%rbp), %ecx
movl 0x30(%r14), %r14d
incl %ecx
jmp 0x3ffb
movq 0x13d78(%rip), %rax     # 0x17fe0
movl -0x2c(%rbp), %edx
leaq 0xc774(%rip), %rsi      # 0x109e6
movq (%rax), %rdi
xorl %eax, %eax
callq 0x2390
jmp 0x419e
xorl %ebx, %ebx
pushq $-0x1
popq %rax
movq %rax, -0x38(%rbp)
movslq 0x30(%r14), %rax
cmpq %rax, %rbx
jge 0x42a1
movq (%r15,%rbx,8), %rdi
callq 0x22a0
incq %rbx
jmp 0x428a
movq -0x38(%rbp), %rax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x13d25(%rip), %rax     # 0x17fe0
movl 0x4(%rbx), %edx
movl 0x8(%rbx), %ecx
leaq 0xc63e(%rip), %rsi      # 0x10906
movq (%rax), %rdi
xorl %eax, %eax
callq 0x2390
jmp 0x3f58