// File: /workspace/github_c_makefile_star2_O3/sharpyfox[P]citrusleaf-client/tests/key_c/src/key.c
// Function: ops_fn
void *
ops_fn(void *udata)
{
	op_work *work = (op_work *) udata;
	write_work *workw = (write_work * )malloc(sizeof(write_work));
	read_work *workr = (read_work * )malloc(sizeof(read_work));

	int iteration = (int)((work->n_ops)/100);
	int residue = (work->n_ops)%100;
	uint read_start_key =  work->start_key;
	uint write_start_key = work->start_key;
	work->writeratio = my_min(100, work->writeratio);

	//writing snd reading n_ops times in a ratio writeratio : (100-writepesr100)
	for (uint i=0;i<iteration;i++) {
		for(int j=0; j<work->writeratio; j++) {
        		write_start_key = (rand_64()%work->n_keys) + work->start_key;
			//writing
			workw->completed = work->wcompleted;
			workw->gen_errs = work->gen_errs;
			workw->key_len = work->key_len;
			workw->value_len = work->value_len;
			workw->start_key = write_start_key;
			workw->value_len = work->value_len;
			workw->asc = work->asc;
			workw->id = 0;
			workw->n_writes = 1;
			write_fn(workw);
		}

		for(int j=0; j<100-work->writeratio; j++) {	
			//reading
			workr->completed = work->rcompleted;
			workr->key_len = work->key_len;
			workr->value_len = work->value_len;
			workr->start_key = read_start_key;
			workr->value_len = work->value_len;
			workr->asc = work->asc;
			workr->n_keys = work->n_keys;
			workr->id = 0;
			workr->n_reads = 1;
			read_fn(workr);
		}
	}
	//Residue
	int work_residue = (int)(residue*work->writeratio/100);

	for(int j=0; j<work_residue; j++) {
		write_start_key = (rand_64()%work->n_keys) + work->start_key;
		//working
		workw->completed = work->wcompleted;
		workw->gen_errs = work->gen_errs;
		workw->key_len = work->key_len;
		workw->value_len = work->value_len;
		workw->start_key = write_start_key;
		workw->value_len = work->value_len;
		workw->asc = work->asc;
		workw->id = 0;
		workw->n_writes = 1;
		write_fn(workw);
	}
	
	for(int j=0; j<(residue-work_residue); j++) {
		//reading
		workr->completed = work->rcompleted;
		workr->key_len = work->key_len;
		workr->value_len = work->value_len;
		workr->start_key = work->start_key;
		workr->value_len = work->value_len;
		workr->asc = work->asc;
		workr->n_keys = work->n_keys;
		workr->id = 0;
		workr->n_reads = 1;
		read_fn(workr);
	}
	free(workw);
	free(workr);
	return (0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movl $0x30, %edi
callq 0x22f0
movl $0x28, %edi
movq %rax, %r14
callq 0x22f0
movl 0x4(%rbx), %ecx
movq %rax, %r15
movl (%rbx), %r12d
imulq $0x51eb851f, %rcx, %rax # imm = 0x51EB851F
movl %ecx, %edx
shrq $0x25, %rax
movq %rax, 0x10(%rsp)
imull $0x64, %eax, %eax
subl %eax, %edx
movl 0x28(%rbx), %eax
movl %edx, 0xc(%rsp)
movl $0x64, %edx
cmpl $0x64, %eax
cmovgel %edx, %eax
cmpq $0x64, %rcx
movl %eax, 0x28(%rbx)
jb 0x51fb
xorl %ebp, %ebp
testl %eax, %eax
jle 0x51a0
xorl %r13d, %r13d
xorl %eax, %eax
callq 0x31de
movl 0x8(%rbx), %ecx
xorl %edx, %edx
movq %r14, %rdi
divq %rcx
movq 0x20(%rbx), %rax
addl (%rbx), %edx
movq %rax, 0x18(%r14)
movl %edx, (%r14)
movq 0xc(%rbx), %rax
movq %rax, 0x8(%r14)
movups 0x30(%rbx), %xmm0
movups %xmm0, 0x20(%r14)
movl $0x0, 0x10(%r14)
movl $0x1, 0x4(%r14)
callq 0x48d5
movl 0x28(%rbx), %eax
incl %r13d
cmpl %eax, %r13d
jl 0x514b
cmpl $0x63, %eax
jg 0x51ef
xorl %r13d, %r13d
movq 0x18(%rbx), %rax
movq %r15, %rdi
movq %rax, 0x18(%r15)
movl 0x10(%rbx), %eax
movl %r12d, 0x4(%r15)
movl %eax, 0x10(%r15)
movq 0x38(%rbx), %rax
movq %rax, 0x20(%r15)
movq 0x8(%rbx), %rax
movq %rax, 0x8(%r15)
movl $0x0, 0x14(%r15)
movl $0x1, (%r15)
callq 0x3970
movl 0x28(%rbx), %eax
movl $0x64, %ecx
incl %r13d
subl %eax, %ecx
cmpl %ecx, %r13d
jl 0x51a3
incl %ebp
cmpl 0x10(%rsp), %ebp
jne 0x5144
movl 0xc(%rsp), %r13d
imull %r13d, %eax
cltq
imulq $0x51eb851f, %rax, %r12 # imm = 0x51EB851F
movq %r12, %rcx
sarq $0x25, %r12
shrq $0x3f, %rcx
addl %ecx, %r12d
cmpl $0x64, %eax
jl 0x526c
movl %r12d, %ebp
xorl %eax, %eax
callq 0x31de
movl 0x8(%rbx), %ecx
xorl %edx, %edx
movq %r14, %rdi
divq %rcx
movq 0x20(%rbx), %rax
addl (%rbx), %edx
movq %rax, 0x18(%r14)
movl %edx, (%r14)
movq 0xc(%rbx), %rax
movq %rax, 0x8(%r14)
movups 0x30(%rbx), %xmm0
movups %xmm0, 0x20(%r14)
movl $0x0, 0x10(%r14)
movl $0x1, 0x4(%r14)
callq 0x48d5
decl %ebp
jne 0x5223
subl %r12d, %r13d
jle 0x52b6
xorl %ebp, %ebp
movq 0x18(%rbx), %rax
movq %r15, %rdi
movq %rax, 0x18(%r15)
movl (%rbx), %eax
movl 0x10(%rbx), %ecx
movl %eax, 0x4(%r15)
movl %ecx, 0x10(%r15)
movq 0x38(%rbx), %rax
movq %rax, 0x20(%r15)
movq 0x8(%rbx), %rax
movq %rax, 0x8(%r15)
movl $0x0, 0x14(%r15)
movl $0x1, (%r15)
callq 0x3970
incl %ebp
cmpl %r13d, %ebp
jl 0x5273
movq %r14, %rdi
callq 0x22a0
movq %r15, %rdi
callq 0x22a0
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq