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

	if (g_config.verbose) fprintf(stderr, "write worker thread: n_writes %d start_key %d \n",work->n_writes, work->start_key);
    
	for (uint i=0;i<work->n_writes;i++) {
	
		key_value *kv = make_key_value(work->start_key + i, work->key_len, work->value_len);
	
		// Create a key for accessing
		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);
		}


#ifdef PRINT_KEY
		cf_digest d;
		citrusleaf_calculate_digest(g_config.table, &o_key, &d);
		fprintf(stderr, "set: key %d digest %"PRIx64"\n",(int)kv->key_int, *(uint64_t *)&d);
#endif

		uint32_t r_gen = MAX_UINT32;
		if (g_config.read_modify_write) {
			// bundle it into a value array
			cl_bin rvalues[1];
			memset(rvalues, 0, sizeof(rvalues));
			strcpy(rvalues[0].bin_name, g_config.bin);
		
			// do the actual work of reading
			int rrv;
			if (CITRUSLEAF_OK != (rrv = citrusleaf_get(work->asc, g_config.ns, g_config.table, &o_key, rvalues, 1, g_config.timeout, &r_gen))) {
				if (rrv != CITRUSLEAF_FAIL_NOTFOUND) {
					if (!g_config.silent) fprintf(stderr, "read-modify-write: failed with error %d\n",rrv);
					if (g_config.continuous == false) 	return((void *)-1);
				}
				r_gen = MAX_UINT32;
			}
			else { // success			
				if (rvalues[0].object.type != g_config.value_type) {
					raise(SIGINT);
					return((void *)-1);
				}
			}
			// if (!g_config.silent) fprintf(stderr, "generation %d\n",r_gen);
			citrusleaf_object_free(&rvalues[0].object);
		}
		// set a value
		cl_bin values[1];
		strcpy(values[0].bin_name, g_config.bin);

		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);
// very verbose but sometimes necessary			
//			dump_buf("writing blob: ",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, "set: unknown type %d! misconfig! bail!\n",g_config.value_type);	
		}
		
		cl_write_parameters cl_wp;
		cl_write_parameters_set_default(&cl_wp);
		cl_wp.timeout_ms = g_config.timeout;
		cl_wp.record_ttl = g_config.record_ttl + (g_config.ttl_range != 0 ? rand_64() % g_config.ttl_range : 0);
		if (g_config.read_modify_write) {
			if (r_gen == MAX_UINT32) 
				cl_wp.unique = true; // if it didn't exist before, do set unique
			else
				cl_write_parameters_set_generation(&cl_wp, r_gen);	// did exist, use the correct gen
		}
        
		cl_rv rv;
		uint64_t start_time = cf_getms();
		if (CITRUSLEAF_OK != (rv = citrusleaf_put(work->asc, g_config.ns, g_config.table, &o_key, values, 1, &cl_wp))) {
            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_write_timeout_histogram, diff);

                if (!g_config.silent) {
            		fprintf(stderr, "citrusleaf put timeout %d %"PRIu64" %d\n",rv,diff,cl_wp.timeout_ms);
                }
        	}
        	else {
                if (!g_config.silent) {
            		cf_digest d;
            		citrusleaf_calculate_digest(g_config.table, &o_key, &d);
            		fprintf(stderr, "citrusleaf put failed %d %"PRIx64"\n",rv,*(uint64_t *)&d);
                }
        	}
			
			if ((rv == CITRUSLEAF_FAIL_GENERATION) ||
				(rv == CITRUSLEAF_FAIL_KEY_EXISTS) ) {
				atomic_int_add(work->gen_errs, 1);			
			}
			if (g_config.continuous == false)
				return((void *)-1);
		}
		uint64_t stop_time = cf_getms();

		histogram_insert_data_point(g_p_write_histogram,
		safe_delta_ms(start_time, stop_time));

		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(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
leaq 0x16041(%rip), %rbp     # 0x1a330
movq %rdi, %rbx
cmpb $0x1, 0x28(%rbp)
je 0x46d2
leaq 0x70(%rsp), %r13
xorl %r15d, %r15d
movq $0x0, 0x28(%rsp)
cmpl 0x4(%rbx), %r15d
jae 0x46b1
movl (%rbx), %edi
movl 0x8(%rbx), %esi
movl 0xc(%rbx), %edx
addl %r15d, %edi
callq 0x3983
movl 0x4c(%rbp), %edx
movq %rax, %r12
cmpl $0x1, %edx
je 0x4346
cmpl $0x3, %edx
jne 0x4354
movq 0x8(%r12), %rsi
movq %r13, %rdi
callq 0x564e
jmp 0x435e
movq (%r12), %rsi
movq %r13, %rdi
callq 0x568f
jmp 0x435e
cmpb $0x0, 0x29(%rbp)
je 0x461a
orl $-0x1, 0x8(%rsp)
cmpb $0x1, 0x58(%rbp)
jne 0x43fa
movq 0x20(%rbp), %rsi
leaq 0x30(%rsp), %r14
xorps %xmm0, %xmm0
movq %r14, %rdi
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
callq 0x2210
movl 0x34(%rbp), %eax
movq 0x28(%rbx), %rdi
movq 0x10(%rbp), %rsi
movq 0x18(%rbp), %rdx
movq %r13, %rcx
movq %r14, %r8
pushq $0x1
popq %r9
leaq 0x8(%rsp), %r10
pushq %r10
pushq %rax
callq 0x6574
addq $0x10, %rsp
testl %eax, %eax
je 0x43e3
cmpl $0x2, %eax
je 0x43dc
cmpb $0x0, 0x29(%rbp)
je 0x4692
cmpb $0x0, 0x2b(%rbp)
je 0x46bd
orl $-0x1, 0x8(%rsp)
jmp 0x43f0
movl 0x50(%rsp), %eax
cmpl 0x48(%rbp), %eax
jne 0x46b5
leaq 0x50(%rsp), %rdi
callq 0x56e9
movq 0x20(%rbp), %rsi
leaq 0x30(%rsp), %rdi
callq 0x2210
movl 0x48(%rbp), %edx
cmpl $0x1, %edx
je 0x4441
cmpl $0x4, %edx
je 0x442b
cmpl $0x3, %edx
jne 0x4452
movq 0x10(%r12), %rsi
leaq 0x50(%rsp), %rdi
callq 0x564e
jmp 0x445c
movq 0x20(%r12), %rsi
movslq 0x18(%r12), %rdx
leaq 0x50(%rsp), %rdi
callq 0x56a7
jmp 0x445c
movq 0x28(%r12), %rsi
leaq 0x50(%rsp), %rdi
callq 0x568f
jmp 0x445c
cmpb $0x0, 0x29(%rbp)
je 0x4637
movabsq $0x200000000, %rax      # imm = 0x200000000
movl 0x34(%rbp), %ecx
movl 0x38(%rbp), %r14d
movq %rax, 0x20(%rsp)
andl $0x0, 0x10(%rsp)
cmpl $0x0, 0x3c(%rbp)
movb $0x0, 0x14(%rsp)
movl %ecx, 0x1c(%rsp)
je 0x4497
xorl %eax, %eax
callq 0x2834
movl 0x3c(%rbp), %ecx
xorl %edx, %edx
divq %rcx
jmp 0x4499
xorl %edx, %edx
addl %r14d, %edx
cmpb $0x1, 0x58(%rbp)
movl %edx, 0x20(%rsp)
jne 0x44bf
movl 0x8(%rsp), %eax
cmpl $-0x1, %eax
je 0x44ba
movl %eax, 0x18(%rsp)
movb $0x1, 0x12(%rsp)
jmp 0x44bf
movb $0x1, 0x10(%rsp)
movl %r15d, 0xc(%rsp)
callq 0x5408
movq 0x28(%rbx), %rdi
movq 0x10(%rbp), %rsi
movq 0x18(%rbp), %rdx
movq %rax, %r15
subq $0x8, %rsp
movq %r13, %rcx
leaq 0x38(%rsp), %r8
pushq $0x1
popq %r9
leaq 0x18(%rsp), %rax
pushq %rax
callq 0x6bef
addq $0x10, %rsp
testl %eax, %eax
je 0x4586
movl %eax, %r13d
callq 0x5408
subq %r15, %rax
movl $0x0, %r14d
cmovaeq %rax, %r14
cmpl $0x9, %r13d
je 0x4520
cmpl $-0x2, %r13d
jne 0x455a
movq 0x15e79(%rip), %rdi     # 0x1a3a0
movq %r14, %rsi
callq 0x5595
cmpb $0x0, 0x29(%rbp)
jne 0x4564
movq 0x13aa4(%rip), %rax     # 0x17fe0
movl 0x1c(%rsp), %r8d
leaq 0xc74a(%rip), %rsi      # 0x10c92
movl %r13d, %edx
movq %r14, %rcx
movq (%rax), %rdi
xorl %eax, %eax
callq 0x2390
jmp 0x4564
cmpb $0x0, 0x29(%rbp)
je 0x4654
cmpl $0x5, %r13d
je 0x4570
cmpl $0x3, %r13d
jne 0x457c
movq 0x20(%rbx), %rdi
pushq $0x1
popq %rsi
callq 0x26ab
cmpb $0x0, 0x2b(%rbp)
je 0x46bd
callq 0x5408
movq 0x15e16(%rip), %rdi     # 0x1a3a8
subq %r15, %rax
movl $0x0, %esi
cmovaeq %rax, %rsi
callq 0x5595
movq %r12, %rdi
callq 0x22a0
movq 0x18(%rbx), %rdi
pushq $0x1
popq %rsi
callq 0x26ab
movl 0x54(%rbp), %edx
testl %edx, %edx
je 0x45de
movl 0xc(%rsp), %r15d
cmpl $0x3e8, %edx            # imm = 0x3E8
leaq 0x70(%rsp), %r13
jb 0x45ea
imull $0x3e8, %edx, %edi      # imm = 0x3E8
addl $0xfff0bdc0, %edi       # imm = 0xFFF0BDC0
jmp 0x460d
movl 0xc(%rsp), %r15d
leaq 0x70(%rsp), %r13
jmp 0x4612
movq 0x28(%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, 0x28(%rsp)
jne 0x4612
callq 0x2370
incl %r15d
jmp 0x430d
movq 0x139bf(%rip), %rax     # 0x17fe0
leaq 0xc30b(%rip), %rsi      # 0x10933
movq (%rax), %rdi
xorl %eax, %eax
callq 0x2390
jmp 0x435e
movq 0x139a2(%rip), %rax     # 0x17fe0
leaq 0xc625(%rip), %rsi      # 0x10c6a
movq (%rax), %rdi
xorl %eax, %eax
callq 0x2390
jmp 0x445c
movq 0x18(%rbp), %rdi
leaq 0x70(%rsp), %rsi
leaq 0x90(%rsp), %rdx
callq 0x76c7
movq 0x1396f(%rip), %rax     # 0x17fe0
movq 0x90(%rsp), %rcx
leaq 0xc634(%rip), %rsi      # 0x10cb4
movl %r13d, %edx
movq (%rax), %rdi
xorl %eax, %eax
callq 0x2390
jmp 0x4564
movq 0x13947(%rip), %rcx     # 0x17fe0
movl %eax, %edx
leaq 0xc59f(%rip), %rsi      # 0x10c41
xorl %eax, %eax
movq (%rcx), %rdi
callq 0x2390
jmp 0x43d2
xorl %eax, %eax
jmp 0x46c0
pushq $0x2
popq %rdi
callq 0x2230
pushq $-0x1
popq %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x13907(%rip), %rax     # 0x17fe0
movl (%rbx), %ecx
movl 0x4(%rbx), %edx
leaq 0xc52c(%rip), %rsi      # 0x10c11
movq (%rax), %rdi
xorl %eax, %eax
callq 0x2390
jmp 0x42fc