// File: /workspace/github_c_makefile_star2_O1/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
movq %rdi, %r14
leaq 0x179cd(%rip), %r12     # 0x1c330
cmpb $0x1, 0x28(%r12)
je 0x4e3c
xorl %eax, %eax
cmpl $0x0, 0x4(%r14)
je 0x4e2a
leaq 0x98(%rsp), %r15
leaq 0x10(%rsp), %r13
movq $0x0, 0x20(%rsp)
xorl %ebp, %ebp
movq %r14, 0x28(%rsp)
movl (%r14), %edi
addl %ebp, %edi
movl 0x8(%r14), %esi
movl 0xc(%r14), %edx
callq 0x40c8
movq %rax, %rbx
movl 0x4c(%r12), %edx
cmpl $0x1, %edx
je 0x49cb
cmpl $0x3, %edx
jne 0x49d8
movq 0x8(%rbx), %rsi
movq %r15, %rdi
callq 0x5ee9
jmp 0x49e4
movq (%rbx), %rsi
movq %r15, %rdi
callq 0x5f30
jmp 0x49e4
cmpb $0x0, 0x29(%r12)
je 0x4db1
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
cmpb $0x1, 0x58(%r12)
movq %rbx, 0x30(%rsp)
jne 0x4aa7
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movq 0x20(%r12), %rsi
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
callq 0x2210
movq 0x28(%r14), %rdi
movq 0x10(%r12), %rsi
movq 0x18(%r12), %rdx
movl 0x34(%r12), %eax
movq %r15, %rcx
movq %rbx, %r8
movl $0x1, %r9d
leaq 0xc(%rsp), %r10
pushq %r10
pushq %rax
callq 0x6f6c
addq $0x10, %rsp
testl %eax, %eax
je 0x4a89
cmpl $0x2, %eax
movq 0x30(%rsp), %rbx
je 0x4a7f
cmpb $0x0, 0x29(%r12)
je 0x4e00
cmpb $0x0, 0x2b(%r12)
je 0x4d9d
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
jmp 0x4a9d
movl 0x70(%rsp), %eax
cmpl 0x48(%r12), %eax
movq 0x30(%rsp), %rbx
jne 0x4d93
leaq 0x70(%rsp), %rdi
callq 0x5f99
movq 0x20(%r12), %rsi
leaq 0x50(%rsp), %rdi
callq 0x2210
movl 0x48(%r12), %edx
cmpl $0x1, %edx
je 0x4aee
cmpl $0x4, %edx
je 0x4ada
cmpl $0x3, %edx
jne 0x4afe
movq 0x10(%rbx), %rsi
leaq 0x70(%rsp), %rdi
callq 0x5ee9
jmp 0x4b0a
movq 0x20(%rbx), %rsi
movslq 0x18(%rbx), %rdx
leaq 0x70(%rsp), %rdi
callq 0x5f4b
jmp 0x4b0a
movq 0x28(%rbx), %rsi
leaq 0x70(%rsp), %rdi
callq 0x5f30
jmp 0x4b0a
cmpb $0x0, 0x29(%r12)
je 0x4dce
movabsq $0x200000000, %rax      # imm = 0x200000000
movq %rax, 0x48(%rsp)
movl $0x0, 0x38(%rsp)
movb $0x0, 0x3c(%rsp)
movl 0x34(%r12), %eax
movl 0x38(%r12), %ebx
movl %eax, 0x44(%rsp)
cmpl $0x0, 0x3c(%r12)
movl %ebp, 0x8(%rsp)
je 0x4b53
xorl %eax, %eax
callq 0x31ce
movl 0x3c(%r12), %ecx
xorl %edx, %edx
divq %rcx
jmp 0x4b55
xorl %edx, %edx
addl %ebx, %edx
movl %edx, 0x48(%rsp)
movq %r12, %rbp
cmpb $0x1, 0x58(%r12)
movabsq $0x431bde82d7b634db, %rbx # imm = 0x431BDE82D7B634DB
jne 0x4b89
movl 0xc(%rsp), %eax
cmpl $-0x1, %eax
je 0x4b84
movl %eax, 0x40(%rsp)
movb $0x1, 0x3a(%rsp)
jmp 0x4b89
movb $0x1, 0x38(%rsp)
movl $0x1, %edi
movq %r13, %rsi
callq 0x2200
movq %rbx, %rax
mulq 0x18(%rsp)
shrq $0x12, %rdx
imulq $0x3e8, 0x10(%rsp), %r14 # imm = 0x3E8
addq %rdx, %r14
movq 0x28(%rsp), %rax
movq 0x28(%rax), %rdi
movq %rbp, %r12
movq 0x10(%rbp), %rsi
movq 0x18(%rbp), %rdx
subq $0x8, %rsp
movq %r15, %rcx
leaq 0x58(%rsp), %r8
movl $0x1, %r9d
leaq 0x40(%rsp), %rax
pushq %rax
callq 0x76cb
addq $0x10, %rsp
testl %eax, %eax
je 0x4caa
movl %eax, %r15d
movl $0x1, %edi
movq %r13, %rsi
callq 0x2200
movq %rbx, %rax
mulq 0x18(%rsp)
shrq $0x12, %rdx
imulq $0x3e8, 0x10(%rsp), %rax # imm = 0x3E8
addq %rdx, %rax
subq %r14, %rax
movl $0x0, %ebx
cmovaeq %rax, %rbx
cmpl $0x9, %r15d
je 0x4c2b
cmpl $-0x2, %r15d
jne 0x4c65
movq 0x1776e(%rip), %rdi     # 0x1c3a0
movq %rbx, %rsi
callq 0x5dde
cmpb $0x0, 0x29(%rbp)
jne 0x4c6f
movq 0x15399(%rip), %rax     # 0x19fe0
movq (%rax), %rdi
movl 0x44(%rsp), %r8d
leaq 0xe040(%rip), %rsi      # 0x12c96
movl %r15d, %edx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2390
jmp 0x4c6f
cmpb $0x0, 0x29(%rbp)
je 0x4deb
cmpl $0x5, %r15d
je 0x4c7b
cmpl $0x3, %r15d
jne 0x4c8e
movq 0x28(%rsp), %rax
movq 0x20(%rax), %rdi
movl $0x1, %esi
callq 0x303d
movq %rbp, %r12
cmpb $0x0, 0x2b(%rbp)
leaq 0x10(%rsp), %r13
movabsq $0x431bde82d7b634db, %rbx # imm = 0x431BDE82D7B634DB
je 0x4d7e
movl $0x1, %edi
movq %r13, %rsi
callq 0x2200
movq %rbx, %rax
mulq 0x18(%rsp)
shrq $0x12, %rdx
imulq $0x3e8, 0x10(%rsp), %rax # imm = 0x3E8
addq %rdx, %rax
movq 0x176d2(%rip), %rdi     # 0x1c3a8
subq %r14, %rax
movl $0x0, %esi
cmovaeq %rax, %rsi
callq 0x5dde
movq 0x30(%rsp), %rdi
callq 0x22a0
movq 0x28(%rsp), %r14
movq 0x18(%r14), %rdi
movl $0x1, %esi
callq 0x303d
movl 0x54(%r12), %edx
movb $0x1, %bl
testl %edx, %edx
je 0x4d36
cmpl $0x3e8, %edx            # imm = 0x3E8
movl 0x8(%rsp), %ebp
jb 0x4d45
imull $0x3e8, %edx, %edi      # imm = 0x3E8
addl $0xfff0bdc0, %edi       # imm = 0xFFF0BDC0
movq 0x20(%rsp), %rax
movl %eax, %r15d
leaq 0x10(%rsp), %r13
jmp 0x4d67
leaq 0x98(%rsp), %r15
leaq 0x10(%rsp), %r13
jmp 0x4d8d
movq 0x20(%rsp), %rax
leal 0x1(%rax), %r15d
movl $0x3e8, %edi            # imm = 0x3E8
movl $0x3e8, %ecx            # imm = 0x3E8
subl %edx, %ecx
xorl %edx, %edx
divl %ecx
testl %edx, %edx
leaq 0x10(%rsp), %r13
jne 0x4d6c
callq 0x2370
movl %r15d, %eax
movq %rax, 0x20(%rsp)
leaq 0x98(%rsp), %r15
jmp 0x4d9f
xorl %ebx, %ebx
leaq 0x98(%rsp), %r15
movq 0x28(%rsp), %r14
movl 0x8(%rsp), %ebp
jmp 0x4d9f
movl $0x2, %edi
callq 0x2230
xorl %ebx, %ebx
testb %bl, %bl
je 0x4e1f
incl %ebp
cmpl 0x4(%r14), %ebp
jb 0x4999
jmp 0x4e28
movq 0x15228(%rip), %rax     # 0x19fe0
movq (%rax), %rdi
leaq 0xdb75(%rip), %rsi      # 0x12937
xorl %eax, %eax
callq 0x2390
jmp 0x49e4
movq 0x1520b(%rip), %rax     # 0x19fe0
movq (%rax), %rdi
leaq 0xde8f(%rip), %rsi      # 0x12c6e
xorl %eax, %eax
callq 0x2390
jmp 0x4b0a
leaq 0x98(%rsp), %rdi
movl %r15d, %esi
callq 0x2d0e
jmp 0x4c6f
movq 0x151d9(%rip), %rcx     # 0x19fe0
movq (%rcx), %rdi
leaq 0xde34(%rip), %rsi      # 0x12c45
movl %eax, %edx
xorl %eax, %eax
callq 0x2390
jmp 0x4a73
movq $-0x1, %rax
jmp 0x4e2a
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x2cee
jmp 0x496f