// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x168, %rsp            # imm = 0x168
movq %rdi, 0x158(%rsp)
movq 0x158(%rsp), %rax
movq %rax, 0x150(%rsp)
movl $0x0, 0x14c(%rsp)
leaq 0x2017f(%rip), %rax     # 0x25330
testb $0x1, 0x28(%rax)
je 0x51e4
movq 0x1de22(%rip), %rax     # 0x22fe0
movq (%rax), %rdi
movq 0x150(%rsp), %rax
movl 0x4(%rax), %edx
movq 0x150(%rsp), %rax
movl (%rax), %ecx
leaq 0x17a19(%rip), %rsi     # 0x1cbf6
movb $0x0, %al
callq 0x23b0
movl $0x0, 0x148(%rsp)
movl 0x148(%rsp), %eax
movq 0x150(%rsp), %rcx
cmpl 0x4(%rcx), %eax
jae 0x57e6
movq 0x150(%rsp), %rax
movl (%rax), %edi
addl 0x148(%rsp), %edi
movq 0x150(%rsp), %rax
movl 0x8(%rax), %esi
movq 0x150(%rsp), %rax
movl 0xc(%rax), %edx
callq 0x4140
movq %rax, 0x140(%rsp)
leaq 0x200ee(%rip), %rax     # 0x25330
cmpl $0x3, 0x4c(%rax)
jne 0x5263
movq 0x140(%rsp), %rax
movq 0x8(%rax), %rsi
leaq 0x120(%rsp), %rdi
callq 0x72b0
jmp 0x52bd
leaq 0x200c6(%rip), %rax     # 0x25330
cmpl $0x1, 0x4c(%rax)
jne 0x528a
movq 0x140(%rsp), %rax
movq (%rax), %rsi
leaq 0x120(%rsp), %rdi
callq 0x7350
jmp 0x52bb
leaq 0x2009f(%rip), %rax     # 0x25330
testb $0x1, 0x29(%rax)
jne 0x52b9
movq 0x1dd42(%rip), %rax     # 0x22fe0
movq (%rax), %rdi
leaq 0x20088(%rip), %rax     # 0x25330
movl 0x4c(%rax), %edx
leaq 0x17666(%rip), %rsi     # 0x1c918
movb $0x0, %al
callq 0x23b0
jmp 0x52bb
jmp 0x52bd
movl $0xffffffff, 0x11c(%rsp) # imm = 0xFFFFFFFF
leaq 0x20061(%rip), %rax     # 0x25330
testb $0x1, 0x58(%rax)
je 0x541e
leaq 0xd0(%rsp), %rdi
xorl %esi, %esi
movl $0x40, %edx
callq 0x2150
leaq 0xd0(%rsp), %rdi
leaq 0x20034(%rip), %rax     # 0x25330
movq 0x20(%rax), %rsi
callq 0x2240
movq 0x150(%rsp), %rax
movq 0x28(%rax), %rdi
leaq 0x20018(%rip), %rax     # 0x25330
movq 0x10(%rax), %rsi
leaq 0x2000d(%rip), %rax     # 0x25330
movq 0x18(%rax), %rdx
leaq 0xd0(%rsp), %r8
leaq 0x1fffa(%rip), %rax     # 0x25330
movl 0x34(%rax), %r10d
leaq 0x120(%rsp), %rcx
movl $0x1, %r9d
leaq 0x11c(%rsp), %rax
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x9130
movl %eax, %ecx
movl %ecx, 0xcc(%rsp)
xorl %eax, %eax
cmpl %ecx, %eax
je 0x53da
cmpl $0x2, 0xcc(%rsp)
je 0x53cd
leaq 0x1ffb2(%rip), %rax     # 0x25330
testb $0x1, 0x29(%rax)
jne 0x53a3
movq 0x1dc55(%rip), %rax     # 0x22fe0
movq (%rax), %rdi
movl 0xcc(%rsp), %edx
leaq 0x1788a(%rip), %rsi     # 0x1cc26
movb $0x0, %al
callq 0x23b0
leaq 0x1ff86(%rip), %rax     # 0x25330
movb 0x2b(%rax), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x53cb
movq $-0x1, %rax
movq %rax, 0x160(%rsp)
jmp 0x57f2
jmp 0x53cd
movl $0xffffffff, 0x11c(%rsp) # imm = 0xFFFFFFFF
jmp 0x540d
movl 0xf0(%rsp), %eax
leaq 0x1ff48(%rip), %rcx     # 0x25330
cmpl 0x48(%rcx), %eax
je 0x540b
movl $0x2, %edi
callq 0x2260
movq $-0x1, %rax
movq %rax, 0x160(%rsp)
jmp 0x57f2
jmp 0x540d
leaq 0xd0(%rsp), %rdi
addq $0x20, %rdi
callq 0x74b0
leaq 0x80(%rsp), %rdi
leaq 0x1ff03(%rip), %rax     # 0x25330
movq 0x20(%rax), %rsi
callq 0x2240
leaq 0x1fef3(%rip), %rax     # 0x25330
cmpl $0x3, 0x48(%rax)
jne 0x5465
leaq 0x80(%rsp), %rdi
addq $0x20, %rdi
movq 0x140(%rsp), %rax
movq 0x10(%rax), %rsi
callq 0x72b0
jmp 0x54fe
leaq 0x1fec4(%rip), %rax     # 0x25330
cmpl $0x4, 0x48(%rax)
jne 0x549d
leaq 0x80(%rsp), %rdi
addq $0x20, %rdi
movq 0x140(%rsp), %rax
movq 0x20(%rax), %rsi
movq 0x140(%rsp), %rax
movslq 0x18(%rax), %rdx
callq 0x7390
jmp 0x54fc
leaq 0x1fe8c(%rip), %rax     # 0x25330
cmpl $0x1, 0x48(%rax)
jne 0x54c9
leaq 0x80(%rsp), %rdi
addq $0x20, %rdi
movq 0x140(%rsp), %rax
movq 0x28(%rax), %rsi
callq 0x7350
jmp 0x54fa
leaq 0x1fe60(%rip), %rax     # 0x25330
testb $0x1, 0x29(%rax)
jne 0x54f8
movq 0x1db03(%rip), %rax     # 0x22fe0
movq (%rax), %rdi
leaq 0x1fe49(%rip), %rax     # 0x25330
movl 0x48(%rax), %edx
leaq 0x1775e(%rip), %rsi     # 0x1cc4f
movb $0x0, %al
callq 0x23b0
jmp 0x54fa
jmp 0x54fc
jmp 0x54fe
leaq 0x68(%rsp), %rdi
callq 0x5810
leaq 0x1fe21(%rip), %rax     # 0x25330
movl 0x34(%rax), %eax
movl %eax, 0x74(%rsp)
leaq 0x1fe13(%rip), %rax     # 0x25330
movl 0x38(%rax), %eax
movq %rax, 0x20(%rsp)
leaq 0x1fe04(%rip), %rax     # 0x25330
cmpl $0x0, 0x3c(%rax)
je 0x554f
movb $0x0, %al
callq 0x2960
leaq 0x1fdf0(%rip), %rcx     # 0x25330
movl 0x3c(%rcx), %ecx
xorl %edx, %edx
divq %rcx
movq %rdx, 0x18(%rsp)
jmp 0x5558
xorl %eax, %eax
movq %rax, 0x18(%rsp)
jmp 0x5558
movq 0x20(%rsp), %rax
movq 0x18(%rsp), %rcx
addq %rcx, %rax
movl %eax, 0x78(%rsp)
leaq 0x1fdc0(%rip), %rax     # 0x25330
testb $0x1, 0x58(%rax)
je 0x559a
cmpl $-0x1, 0x11c(%rsp)
jne 0x5587
movb $0x1, 0x68(%rsp)
jmp 0x5598
movl 0x11c(%rsp), %esi
leaq 0x68(%rsp), %rdi
callq 0x5870
jmp 0x559a
callq 0x6e30
movq %rax, 0x58(%rsp)
movq 0x150(%rsp), %rax
movq 0x28(%rax), %rdi
leaq 0x1fd79(%rip), %rax     # 0x25330
movq 0x10(%rax), %rsi
leaq 0x1fd6e(%rip), %rax     # 0x25330
movq 0x18(%rax), %rdx
leaq 0x80(%rsp), %r8
leaq 0x120(%rsp), %rcx
movl $0x1, %r9d
leaq 0x68(%rsp), %rax
movq %rax, (%rsp)
callq 0x9f00
movl %eax, %ecx
movl %ecx, 0x64(%rsp)
xorl %eax, %eax
cmpl %ecx, %eax
je 0x5707
callq 0x6e30
movq %rax, 0x50(%rsp)
movq 0x58(%rsp), %rdi
movq 0x50(%rsp), %rsi
callq 0x43b0
movq %rax, 0x48(%rsp)
cmpl $-0x2, 0x64(%rsp)
je 0x5626
cmpl $0x9, 0x64(%rsp)
jne 0x566c
movq 0x1fd73(%rip), %rdi     # 0x253a0
movq 0x48(%rsp), %rsi
callq 0x70d0
leaq 0x1fcf2(%rip), %rax     # 0x25330
testb $0x1, 0x29(%rax)
jne 0x566a
movq 0x1d995(%rip), %rax     # 0x22fe0
movq (%rax), %rdi
movl 0x64(%rsp), %edx
movq 0x48(%rsp), %rcx
movl 0x74(%rsp), %r8d
leaq 0x17614(%rip), %rsi     # 0x1cc77
movb $0x0, %al
callq 0x23b0
jmp 0x56b9
leaq 0x1fcbd(%rip), %rax     # 0x25330
testb $0x1, 0x29(%rax)
jne 0x56b7
leaq 0x1fcb0(%rip), %rax     # 0x25330
movq 0x18(%rax), %rdi
leaq 0x120(%rsp), %rsi
leaq 0x34(%rsp), %rdx
callq 0xb360
movq 0x1d943(%rip), %rax     # 0x22fe0
movq (%rax), %rdi
movl 0x64(%rsp), %edx
movq 0x34(%rsp), %rcx
leaq 0x175e9(%rip), %rsi     # 0x1cc99
movb $0x0, %al
callq 0x23b0
jmp 0x56b9
cmpl $0x3, 0x64(%rsp)
je 0x56c7
cmpl $0x5, 0x64(%rsp)
jne 0x56dd
movq 0x150(%rsp), %rax
movq 0x20(%rax), %rdi
movl $0x1, %esi
callq 0x26e0
leaq 0x1fc4c(%rip), %rax     # 0x25330
movb 0x2b(%rax), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x5705
movq $-0x1, %rax
movq %rax, 0x160(%rsp)
jmp 0x57f2
jmp 0x5707
callq 0x6e30
movq %rax, 0x28(%rsp)
movq 0x1fc90(%rip), %rax     # 0x253a8
movq %rax, 0x10(%rsp)
movq 0x58(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x43b0
movq 0x10(%rsp), %rdi
movq %rax, %rsi
callq 0x70d0
movq 0x140(%rsp), %rdi
callq 0x22c0
movq 0x150(%rsp), %rax
movq 0x18(%rax), %rdi
movl $0x1, %esi
callq 0x26e0
leaq 0x1fbcd(%rip), %rax     # 0x25330
cmpl $0x0, 0x54(%rax)
je 0x57ce
leaq 0x1fbc0(%rip), %rax     # 0x25330
cmpl $0x3e8, 0x54(%rax)      # imm = 0x3E8
jb 0x5795
leaq 0x1fbb0(%rip), %rax     # 0x25330
movl 0x54(%rax), %eax
subl $0x3e8, %eax            # imm = 0x3E8
imull $0x3e8, %eax, %edi      # imm = 0x3E8
callq 0x2390
jmp 0x57cc
movl 0x14c(%rsp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0x14c(%rsp)
movl $0x3e8, %ecx            # imm = 0x3E8
leaq 0x1fb7c(%rip), %rdx     # 0x25330
subl 0x54(%rdx), %ecx
xorl %edx, %edx
divl %ecx
cmpl $0x0, %edx
jne 0x57ca
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x2390
jmp 0x57cc
jmp 0x57ce
jmp 0x57d0
movl 0x148(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x148(%rsp)
jmp 0x51ef
movq $0x0, 0x160(%rsp)
movq 0x160(%rsp), %rax
addq $0x168, %rsp            # imm = 0x168
retq
nopw %cs:(%rax,%rax)
nopl (%rax)