// File: /workspace/github_c_makefile_star2_O3/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 $0xb8, %rsp
leaq 0x17a43(%rip), %r15     # 0x1c330
movq %rdi, %r14
cmpb $0x1, 0x28(%r15)
je 0x4d7e
xorl %eax, %eax
cmpl $0x0, 0x4(%r14)
je 0x4d9c
leaq 0x98(%rsp), %r13
movabsq $0x431bde82d7b634db, %r12 # imm = 0x431BDE82D7B634DB
xorl %ebp, %ebp
movq $0x0, 0x48(%rsp)
movq %r14, 0x10(%rsp)
movl (%r14), %edi
movl 0x8(%r14), %esi
movl 0xc(%r14), %edx
addl %ebp, %edi
callq 0x4056
movl 0x4c(%r15), %edx
movq %rax, %rbx
cmpl $0x1, %edx
je 0x495b
cmpl $0x3, %edx
jne 0x4968
movq 0x8(%rbx), %rsi
movq %r13, %rdi
callq 0x5dcd
jmp 0x4973
movq (%rbx), %rsi
movq %r13, %rdi
callq 0x5e14
jmp 0x4973
cmpb $0x0, 0x29(%r15)
je 0x4d0c
cmpb $0x1, 0x58(%r15)
movl $0xffffffff, 0x8(%rsp)  # imm = 0xFFFFFFFF
movq %rbx, 0x18(%rsp)
jne 0x4a2e
movq 0x20(%r15), %rsi
leaq 0x50(%rsp), %rbx
xorps %xmm0, %xmm0
movq %rbx, %rdi
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
callq 0x2210
movl 0x34(%r15), %eax
movq 0x28(%r14), %rdi
movq 0x10(%r15), %rsi
movq 0x18(%r15), %rdx
leaq 0x8(%rsp), %r10
movq %r13, %rcx
movq %rbx, %r8
movl $0x1, %r9d
pushq %r10
pushq %rax
callq 0x6df7
addq $0x10, %rsp
testl %eax, %eax
je 0x4a11
movq 0x18(%rsp), %rbx
cmpl $0x2, %eax
je 0x4a07
cmpb $0x0, 0x29(%r15)
je 0x4d5b
cmpb $0x0, 0x2b(%r15)
je 0x4d95
movl $0xffffffff, 0x8(%rsp)  # imm = 0xFFFFFFFF
jmp 0x4a24
movl 0x70(%rsp), %eax
movq 0x18(%rsp), %rbx
cmpl 0x48(%r15), %eax
jne 0x4d8b
leaq 0x70(%rsp), %rdi
callq 0x5e7d
movq 0x20(%r15), %rsi
leaq 0x50(%rsp), %rdi
callq 0x2210
movl 0x48(%r15), %edx
cmpl $0x1, %edx
je 0x4a73
cmpl $0x4, %edx
je 0x4a5f
cmpl $0x3, %edx
jne 0x4a83
movq 0x10(%rbx), %rsi
leaq 0x70(%rsp), %rdi
callq 0x5dcd
jmp 0x4a8e
movq 0x20(%rbx), %rsi
movslq 0x18(%rbx), %rdx
leaq 0x70(%rsp), %rdi
callq 0x5e2f
jmp 0x4a8e
movq 0x28(%rbx), %rsi
leaq 0x70(%rsp), %rdi
callq 0x5e14
jmp 0x4a8e
cmpb $0x0, 0x29(%r15)
je 0x4d29
movl 0x34(%r15), %ecx
movl 0x38(%r15), %ebx
cmpl $0x0, 0x3c(%r15)
movabsq $0x200000000, %rax      # imm = 0x200000000
movq %rax, 0x40(%rsp)
movl $0x0, 0x30(%rsp)
movb $0x0, 0x34(%rsp)
movl %ecx, 0x3c(%rsp)
je 0x4acf
xorl %eax, %eax
callq 0x31de
movl 0x3c(%r15), %ecx
xorl %edx, %edx
divq %rcx
jmp 0x4ad1
xorl %edx, %edx
addl %ebx, %edx
cmpb $0x1, 0x58(%r15)
leaq 0x20(%rsp), %rbx
movl %edx, 0x40(%rsp)
jne 0x4afc
movl 0x8(%rsp), %eax
cmpl $-0x1, %eax
je 0x4af7
movl %eax, 0x38(%rsp)
movb $0x1, 0x32(%rsp)
jmp 0x4afc
movb $0x1, 0x30(%rsp)
movl $0x1, %edi
movq %rbx, %rsi
movl %ebp, 0xc(%rsp)
callq 0x2200
movq %r12, %rax
imulq $0x3e8, 0x20(%rsp), %r14 # imm = 0x3E8
movq 0x10(%r15), %rsi
movq %r15, %rbp
mulq 0x28(%rsp)
movq 0x10(%rsp), %rax
shrq $0x12, %rdx
movq 0x28(%rax), %rdi
addq %rdx, %r14
movq 0x18(%r15), %rdx
subq $0x8, %rsp
leaq 0x38(%rsp), %rax
movq %r13, %rcx
leaq 0x58(%rsp), %r8
movl $0x1, %r9d
pushq %rax
callq 0x7520
addq $0x10, %rsp
testl %eax, %eax
je 0x4c24
movl $0x1, %edi
movq %rbx, %rsi
movl %eax, %r13d
callq 0x2200
movq %r12, %rax
movl $0x0, %ebx
mulq 0x28(%rsp)
imulq $0x3e8, 0x20(%rsp), %rax # imm = 0x3E8
shrq $0x12, %rdx
addq %rdx, %rax
subq %r14, %rax
cmovaeq %rax, %rbx
cmpl $0x9, %r13d
je 0x4ba2
cmpl $-0x2, %r13d
jne 0x4bdd
movq 0x177f7(%rip), %rdi     # 0x1c3a0
movq %rbx, %rsi
callq 0x5cc2
cmpb $0x0, 0x29(%r15)
jne 0x4be8
movq 0x15421(%rip), %rax     # 0x19fe0
movl 0x3c(%rsp), %r8d
leaq 0xe0c7(%rip), %rsi      # 0x12c92
movl %r13d, %edx
movq %rbx, %rcx
movq (%rax), %rdi
xorl %eax, %eax
callq 0x2390
jmp 0x4be8
cmpb $0x0, 0x29(%r15)
je 0x4d46
cmpl $0x5, %r13d
je 0x4bf4
cmpl $0x3, %r13d
jne 0x4c07
movq 0x10(%rsp), %rax
movl $0x1, %esi
movq 0x20(%rax), %rdi
callq 0x304d
cmpb $0x0, 0x2b(%r15)
movq %r15, %rbp
leaq 0x20(%rsp), %rbx
movabsq $0x431bde82d7b634db, %r12 # imm = 0x431BDE82D7B634DB
je 0x4d95
movl $0x1, %edi
movq %rbx, %rsi
callq 0x2200
movq %r12, %rax
movq 0x1776d(%rip), %rdi     # 0x1c3a8
movl $0x0, %esi
mulq 0x28(%rsp)
imulq $0x3e8, 0x20(%rsp), %rax # imm = 0x3E8
shrq $0x12, %rdx
addq %rdx, %rax
subq %r14, %rax
cmovaeq %rax, %rsi
callq 0x5cc2
movq 0x18(%rsp), %rdi
callq 0x22a0
movq 0x10(%rsp), %r14
movl $0x1, %esi
movq 0x18(%r14), %rdi
callq 0x304d
movl 0x54(%rbp), %edx
movq %rbp, %r15
testl %edx, %edx
je 0x4cb4
movl 0xc(%rsp), %ebp
cmpl $0x3e8, %edx            # imm = 0x3E8
leaq 0x98(%rsp), %r13
jb 0x4ccc
imull $0x3e8, %edx, %edi      # imm = 0x3E8
movabsq $0x431bde82d7b634db, %r12 # imm = 0x431BDE82D7B634DB
addl $0xfff0bdc0, %edi       # imm = 0xFFF0BDC0
jmp 0x4cf9
movl 0xc(%rsp), %ebp
leaq 0x98(%rsp), %r13
movabsq $0x431bde82d7b634db, %r12 # imm = 0x431BDE82D7B634DB
jmp 0x4cfe
movq 0x48(%rsp), %rax
movl $0x3e8, %esi            # imm = 0x3E8
movl $0x3e8, %edi            # imm = 0x3E8
movabsq $0x431bde82d7b634db, %r12 # imm = 0x431BDE82D7B634DB
subl %edx, %esi
xorl %edx, %edx
leal 0x1(%rax), %ecx
divl %esi
testl %edx, %edx
movl %ecx, %eax
movq %rax, 0x48(%rsp)
jne 0x4cfe
callq 0x2370
incl %ebp
cmpl 0x4(%r14), %ebp
jb 0x492a
jmp 0x4d7a
movq 0x152cd(%rip), %rax     # 0x19fe0
leaq 0xdc19(%rip), %rsi      # 0x12933
movq (%rax), %rdi
xorl %eax, %eax
callq 0x2390
jmp 0x4973
movq 0x152b0(%rip), %rax     # 0x19fe0
leaq 0xdf33(%rip), %rsi      # 0x12c6a
movq (%rax), %rdi
xorl %eax, %eax
callq 0x2390
jmp 0x4a8e
leaq 0x98(%rsp), %rdi
movl %r13d, %esi
callq 0x2d1a
jmp 0x4be8
movq 0x1527e(%rip), %rcx     # 0x19fe0
movl %eax, %edx
leaq 0xded6(%rip), %rsi      # 0x12c41
xorl %eax, %eax
movq (%rcx), %rdi
callq 0x2390
jmp 0x49fc
xorl %eax, %eax
jmp 0x4d9c
movq %r14, %rdi
callq 0x2cfa
jmp 0x48fb
movl $0x2, %edi
callq 0x2230
movq $-0x1, %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq