// File: /workspace/github_c_makefile_star2_O3/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 0x17e65(%rip), %r15     # 0x1c330
movq %rdi, %rbx
cmpb $0x1, 0x28(%r15)
je 0x48c8
movslq 0x30(%r15), %r14
movq %rsp, %rcx
leaq (%r14,%r14,4), %rax
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, %rsp
leaq 0xf(,%r14,8), %rax
movq %rsp, %r12
andq $-0x10, %rax
subq %rax, %r12
movq %r12, %rsp
leaq 0xf(,%r14,4), %rax
movq %rsp, %rdx
andq $-0x10, %rax
subq %rax, %rdx
movq %rdx, %rsp
movq 0x20(%r15), %rsi
leaq -0x100(%rbp), %rdi
xorps %xmm0, %xmm0
movq %rcx, -0xb8(%rbp)
movq %r12, -0xb0(%rbp)
movq %rdx, -0xa8(%rbp)
movq %rcx, -0x40(%rbp)
movq %rdx, -0x58(%rbp)
movl %r14d, -0xa0(%rbp)
movaps %xmm0, 0x30(%rdi)
movaps %xmm0, 0x20(%rdi)
movaps %xmm0, 0x10(%rdi)
movaps %xmm0, (%rdi)
callq 0x2210
xorl %eax, %eax
cmpl (%rbx), %r14d
ja 0x48b9
xorl %r13d, %r13d
movq $0x0, -0x38(%rbp)
movq %r12, -0x50(%rbp)
leaq -0x78(%rbp), %r12
testl %r14d, %r14d
movl %r13d, -0x2c(%rbp)
jle 0x466b
movq -0x40(%rbp), %r13
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x31de
movl 0x8(%rbx), %ecx
xorl %edx, %edx
divq %rcx
movq -0x58(%rbp), %rax
movq %rdx, %rdi
addl 0x4(%rbx), %edi
movl %edi, (%rax,%r14,4)
movl 0xc(%rbx), %esi
movl 0x10(%rbx), %edx
callq 0x4056
movl 0x4c(%r15), %edx
movq -0x50(%rbp), %rcx
cmpl $0x1, %edx
movq %rax, (%rcx,%r14,8)
je 0x45e7
cmpl $0x3, %edx
jne 0x45f7
movq 0x8(%rax), %rsi
movq %r12, %rdi
movq %r12, %r15
callq 0x5dcd
jmp 0x4601
movq (%rax), %rsi
movq %r12, %rdi
movq %r12, %r15
callq 0x5e14
jmp 0x4601
cmpb $0x0, 0x29(%r15)
movq %r12, %r15
je 0x4651
leaq 0x17d28(%rip), %rax     # 0x1c330
movq %r15, %rsi
leaq -0x8c(%rbp), %rdx
movq 0x18(%rax), %rdi
movq %rax, %r15
callq 0x7f01
movq -0x84(%rbp), %rcx
movl -0x7c(%rbp), %eax
movq -0x8c(%rbp), %rdx
incq %r14
movq %rcx, 0x8(%r13)
movslq 0x30(%r15), %rcx
movl %eax, 0x10(%r13)
movq %rdx, (%r13)
addq $0x14, %r13
cmpq %rcx, %r14
jl 0x4598
jmp 0x466b
movq 0x15988(%rip), %rax     # 0x19fe0
leaq 0xe2d4(%rip), %rsi      # 0x12933
movq (%rax), %rdi
xorl %eax, %eax
callq 0x2390
jmp 0x4601
movl $0x1, %edi
movq %r12, %rsi
movq %r12, %r13
callq 0x2200
movabsq $0x431bde82d7b634db, %r14 # imm = 0x431BDE82D7B634DB
movq 0x20(%rbx), %rdi
movq 0x10(%r15), %rsi
movl 0x30(%r15), %ecx
movq %r14, %rax
mulq -0x70(%rbp)
imulq $0x3e8, -0x78(%rbp), %rax # imm = 0x3E8
shrq $0x12, %rdx
addq %rdx, %rax
movq %rax, -0x48(%rbp)
subq $0x8, %rsp
movq -0x40(%rbp), %rdx
leaq -0xb8(%rbp), %rax
leaq -0x100(%rbp), %r8
movl $0x1, %r9d
pushq %rax
leaq -0x4ac(%rip), %rax      # 0x4223
pushq %rax
pushq $0x0
callq 0xa7cc
addq $0x20, %rsp
movq -0x50(%rbp), %r12
testl %eax, %eax
je 0x4791
movl $0x1, %edi
movq %r13, %rsi
movl %eax, -0x30(%rbp)
callq 0x2200
movq %r14, %rax
movl -0x30(%rbp), %r14d
movl $0x0, %r13d
mulq -0x70(%rbp)
imulq $0x3e8, -0x78(%rbp), %rax # imm = 0x3E8
shrq $0x12, %rdx
addq %rdx, %rax
subq -0x48(%rbp), %rax
cmovaeq %rax, %r13
cmpl $0x9, %r14d
je 0x472b
cmpl $-0x2, %r14d
jne 0x4761
movq 0x17c5e(%rip), %rdi     # 0x1c390
movq %r13, %rsi
callq 0x5cc2
cmpb $0x0, 0x29(%r15)
jne 0x476c
movq 0x15898(%rip), %rax     # 0x19fe0
leaq 0xe49e(%rip), %rsi      # 0x12bed
movl %r14d, %edx
movq %r13, %rcx
movq (%rax), %rdi
xorl %eax, %eax
callq 0x2390
jmp 0x476c
cmpb $0x0, 0x29(%r15)
je 0x4869
leaq -0xe0(%rbp), %rdi
callq 0x5e7d
cmpb $0x0, 0x2b(%r15)
movabsq $0x431bde82d7b634db, %r14 # imm = 0x431BDE82D7B634DB
leaq -0x78(%rbp), %r13
je 0x488d
movl $0x1, %edi
movq %r13, %rsi
callq 0x2200
movq %r14, %rax
movq 0x17bf0(%rip), %rdi     # 0x1c398
movl $0x0, %esi
mulq -0x70(%rbp)
imulq $0x3e8, -0x78(%rbp), %rax # imm = 0x3E8
shrq $0x12, %rdx
addq %rdx, %rax
subq -0x48(%rbp), %rax
cmovaeq %rax, %rsi
callq 0x5cc2
movl 0x30(%r15), %esi
testl %esi, %esi
jle 0x47f5
xorl %r14d, %r14d
movq (%r12,%r14,8), %rdi
callq 0x22a0
movslq 0x30(%r15), %rsi
movq $0x0, (%r12,%r14,8)
incq %r14
cmpq %rsi, %r14
jl 0x47d8
movq 0x18(%rbx), %rdi
callq 0x304d
movl 0x54(%r15), %edx
testl %edx, %edx
je 0x4820
cmpl $0x3e8, %edx            # imm = 0x3E8
jb 0x4826
movl -0x2c(%rbp), %r13d
imull $0x3e8, %edx, %edi      # imm = 0x3E8
addl $0xfff0bdc0, %edi       # imm = 0xFFF0BDC0
jmp 0x484b
movl -0x2c(%rbp), %r13d
jmp 0x4850
movq -0x38(%rbp), %rax
movl $0x3e8, %esi            # imm = 0x3E8
movl $0x3e8, %edi            # imm = 0x3E8
subl %edx, %esi
xorl %edx, %edx
leal 0x1(%rax), %ecx
divl %esi
movl -0x2c(%rbp), %r13d
testl %edx, %edx
movl %ecx, %eax
movq %rax, -0x38(%rbp)
jne 0x4850
callq 0x2370
movl (%rbx), %eax
movl 0x30(%r15), %r14d
xorl %edx, %edx
incl %r13d
divl %r14d
cmpl %eax, %r13d
jb 0x4580
jmp 0x4889
movq 0x15770(%rip), %rax     # 0x19fe0
leaq 0xe16f(%rip), %rsi      # 0x129e6
movl %r14d, %edx
movq (%rax), %rdi
xorl %eax, %eax
callq 0x2390
jmp 0x476c
xorl %eax, %eax
jmp 0x48b9
cmpl $0x0, 0x30(%r15)
movq $-0x1, %rax
jle 0x48b9
xorl %ebx, %ebx
movq (%r12,%rbx,8), %rdi
callq 0x22a0
movslq 0x30(%r15), %rax
incq %rbx
cmpq %rax, %rbx
jl 0x489d
movq $-0x1, %rax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0x2cd9
jmp 0x44d9