// File: /workspace/github_c_makefile_star2_O1/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 $0xc8, %rsp
movq %rdi, %rbx
leaq 0x17e07(%rip), %r15     # 0x1c330
cmpb $0x1, 0x28(%r15)
je 0x493b
movslq 0x30(%r15), %rax
leaq (%rax,%rax,4), %rcx
movq %rsp, %rdx
leaq 0xf(,%rcx,4), %rcx
andq $-0x10, %rcx
subq %rcx, %rdx
movq %rdx, %rsp
movq %rsp, %r12
leaq 0xf(,%rax,8), %rcx
andq $-0x10, %rcx
subq %rcx, %r12
movq %r12, %rsp
movq %rsp, %rsi
leaq 0xf(,%rax,4), %rcx
andq $-0x10, %rcx
subq %rcx, %rsi
movq %rsi, %rsp
movq %rdx, -0x68(%rbp)
movq %rdx, -0xb0(%rbp)
movq %r12, -0xa8(%rbp)
movq %rsi, -0x70(%rbp)
movq %rsi, -0xa0(%rbp)
movl %eax, -0x98(%rbp)
xorps %xmm0, %xmm0
leaq -0xf0(%rbp), %rdi
movaps %xmm0, 0x30(%rdi)
movaps %xmm0, 0x20(%rdi)
movaps %xmm0, 0x10(%rdi)
movaps %xmm0, (%rdi)
movq 0x20(%r15), %rsi
callq 0x2210
movl 0x30(%r15), %ecx
xorl %eax, %eax
cmpl (%rbx), %ecx
ja 0x492c
movq $0x0, -0x30(%rbp)
xorl %ecx, %ecx
movl %ecx, -0x34(%rbp)
cmpl $0x0, 0x30(%r15)
jle 0x46bb
movq -0x68(%rbp), %r14
xorl %r13d, %r13d
xorl %eax, %eax
callq 0x31ce
movl 0x8(%rbx), %ecx
xorl %edx, %edx
divq %rcx
movq %rdx, %rdi
addl 0x4(%rbx), %edi
movq -0x70(%rbp), %rax
movl %edi, (%rax,%r13,4)
movl 0xc(%rbx), %esi
movl 0x10(%rbx), %edx
callq 0x40c8
movq %rax, (%r12,%r13,8)
movl 0x4c(%r15), %edx
cmpl $0x1, %edx
je 0x4639
cmpl $0x3, %edx
jne 0x464a
movq 0x8(%rax), %rsi
leaq -0x58(%rbp), %r15
movq %r15, %rdi
callq 0x5ee9
jmp 0x4655
movq (%rax), %rsi
leaq -0x58(%rbp), %r15
movq %r15, %rdi
callq 0x5f30
jmp 0x4655
cmpb $0x0, 0x29(%r15)
leaq -0x58(%rbp), %r15
je 0x46a1
leaq 0x17cd4(%rip), %rax     # 0x1c330
movq 0x18(%rax), %rdi
movq %r15, %rsi
movq %rax, %r15
leaq -0x84(%rbp), %rdx
callq 0x8079
movl -0x74(%rbp), %eax
movl %eax, 0x10(%r14)
movq -0x84(%rbp), %rax
movq -0x7c(%rbp), %rcx
movq %rcx, 0x8(%r14)
movq %rax, (%r14)
incq %r13
movslq 0x30(%r15), %rax
addq $0x14, %r14
cmpq %rax, %r13
jl 0x45ed
jmp 0x46bb
movq 0x15938(%rip), %rax     # 0x19fe0
movq (%rax), %rdi
leaq 0xe285(%rip), %rsi      # 0x12937
xorl %eax, %eax
callq 0x2390
jmp 0x4655
movl $0x1, %edi
movq %r15, %r13
leaq -0x58(%rbp), %rsi
callq 0x2200
movabsq $0x431bde82d7b634db, %rax # imm = 0x431BDE82D7B634DB
mulq -0x50(%rbp)
shrq $0x12, %rdx
imulq $0x3e8, -0x58(%rbp), %r15 # imm = 0x3E8
addq %rdx, %r15
movq 0x20(%rbx), %rdi
movq 0x10(%r13), %rsi
movl 0x30(%r13), %ecx
subq $0x8, %rsp
movq -0x68(%rbp), %rdx
leaq -0xf0(%rbp), %r8
movl $0x1, %r9d
leaq -0xb0(%rbp), %rax
pushq %rax
leaq -0x49c(%rip), %rax      # 0x427d
pushq %rax
pushq $0x0
callq 0xaaf0
leaq -0x58(%rbp), %rsi
addq $0x20, %rsp
testl %eax, %eax
je 0x481a
movl %eax, %r14d
movl $0x1, %edi
callq 0x2200
movabsq $0x431bde82d7b634db, %rax # imm = 0x431BDE82D7B634DB
mulq -0x50(%rbp)
shrq $0x12, %rdx
imulq $0x3e8, -0x58(%rbp), %rax # imm = 0x3E8
addq %rdx, %rax
movq %r15, -0x60(%rbp)
subq %r15, %rax
movl $0x0, %r15d
cmovaeq %rax, %r15
cmpl $0x9, %r14d
je 0x4778
cmpl $-0x2, %r14d
jne 0x47b5
movq 0x17c11(%rip), %rdi     # 0x1c390
movq %r15, %rsi
callq 0x5dde
leaq 0x17ba2(%rip), %r13     # 0x1c330
cmpb $0x0, 0x29(%r13)
jne 0x47c7
movq 0x15844(%rip), %rax     # 0x19fe0
movq (%rax), %rdi
leaq 0xe44b(%rip), %rsi      # 0x12bf1
movl %r14d, %edx
movq %r15, %rcx
xorl %eax, %eax
callq 0x2390
jmp 0x47c7
leaq 0x17b74(%rip), %r13     # 0x1c330
cmpb $0x0, 0x29(%r13)
je 0x4901
leaq -0xd0(%rbp), %rdi
callq 0x5f99
movb 0x2b(%r13), %r14b
testb %r14b, %r14b
movq -0x60(%rbp), %r15
leaq -0x58(%rbp), %rsi
jne 0x481a
cmpl $0x0, 0x30(%r13)
movq %r13, %r15
jle 0x4806
xorl %r13d, %r13d
movq (%r12,%r13,8), %rdi
callq 0x22a0
incq %r13
movslq 0x30(%r15), %rax
cmpq %rax, %r13
jl 0x47f1
testb %r14b, %r14b
movq %r15, %r13
leaq -0x58(%rbp), %rsi
movq -0x60(%rbp), %r15
je 0x48f9
movl $0x1, %edi
callq 0x2200
movabsq $0x431bde82d7b634db, %rax # imm = 0x431BDE82D7B634DB
mulq -0x50(%rbp)
shrq $0x12, %rdx
imulq $0x3e8, -0x58(%rbp), %rax # imm = 0x3E8
addq %rdx, %rax
movq 0x17b50(%rip), %rdi     # 0x1c398
subq %r15, %rax
movl $0x0, %esi
cmovaeq %rax, %rsi
callq 0x5dde
movl 0x30(%r13), %esi
testl %esi, %esi
movq %r13, %r15
jle 0x4884
xorl %r14d, %r14d
movq (%r12,%r14,8), %rdi
callq 0x22a0
movq $0x0, (%r12,%r14,8)
incq %r14
movslq 0x30(%r15), %rsi
cmpq %rsi, %r14
jl 0x4867
movq 0x18(%rbx), %rdi
callq 0x303d
movl 0x54(%r15), %edx
movb $0x1, %r14b
testl %edx, %edx
je 0x48dd
cmpl $0x3e8, %edx            # imm = 0x3E8
jb 0x48b5
imull $0x3e8, %edx, %edi      # imm = 0x3E8
addl $0xfff0bdc0, %edi       # imm = 0xFFF0BDC0
movq -0x30(%rbp), %rax
movl %eax, %r13d
jmp 0x48d1
movq -0x30(%rbp), %rax
leal 0x1(%rax), %r13d
movl $0x3e8, %edi            # imm = 0x3E8
movl $0x3e8, %ecx            # imm = 0x3E8
subl %edx, %ecx
xorl %edx, %edx
divl %ecx
testl %edx, %edx
jne 0x48d6
callq 0x2370
movl %r13d, %eax
movq %rax, -0x30(%rbp)
testb %r14b, %r14b
movl -0x34(%rbp), %ecx
je 0x4921
incl %ecx
movl (%rbx), %eax
xorl %edx, %edx
divl 0x30(%r15)
cmpl %eax, %ecx
jb 0x45d8
jmp 0x492a
xorl %r14d, %r14d
movq %r13, %r15
jmp 0x48dd
movq 0x156d8(%rip), %rax     # 0x19fe0
movq (%rax), %rdi
leaq 0xe0d8(%rip), %rsi      # 0x129ea
movl %r14d, %edx
xorl %eax, %eax
callq 0x2390
jmp 0x47c7
movq $-0x1, %rax
jmp 0x492c
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0x2ccd
jmp 0x4534