// File: /workspace/github_c_makefile_star2_O2/sharpyfox[P]citrusleaf-client/src/citrusleaf.c
// Function: cl_compile
int
cl_compile(uint info1, uint info2, uint info3, const char *ns, const char *set, const cl_object *key, const cf_digest *digest,
	cl_bin *values, cl_operator operator, cl_operation *operations, int n_values,  
	uint8_t **buf_r, size_t *buf_sz_r, const cl_write_parameters *cl_w_p, cf_digest *d_ret, uint64_t trid, cl_scan_param_field *scan_param_field)
{
	// I hate strlen
	int		ns_len = ns ? strlen(ns) : 0;
	int		set_len = set ? strlen(set) : 0;
	int		i;

	// determine the size
	size_t	msg_sz = sizeof(as_msg); // header
	// fields
	if (ns)     msg_sz += sizeof(cl_msg_field) + ns_len;
	if (set)    msg_sz += sizeof(cl_msg_field) + set_len;
	if (key)    msg_sz += sizeof(cl_msg_field) + 1 + key->sz;
	if (digest) msg_sz += sizeof(cl_msg_field) + 1 + sizeof(cf_digest);
	if (trid)   msg_sz += sizeof(cl_msg_field) + sizeof(trid);
	if (scan_param_field)	msg_sz += sizeof(cl_msg_field) + 1 + sizeof(cl_scan_param_field);

	// ops
	for (i=0;i<n_values;i++) {
		cl_bin *tmpValue = 0;
		if( values ){
			tmpValue = &values[i];
		}else if( operations ){
			tmpValue = &operations[i].bin;
		}
		
		msg_sz += sizeof(cl_msg_op) + strlen(tmpValue->bin_name);

        if (0 != cl_value_to_op_get_size(tmpValue, &msg_sz)) {
			cf_error("illegal parameter: bad type %d write op %d", tmpValue->object.type,i);
			return(-1);
		}
	}
	
	// size too small? malloc!
	uint8_t	*buf;
	uint8_t *mbuf = 0;
	if ((*buf_r) && (msg_sz > *buf_sz_r)) {
		mbuf = buf = malloc(msg_sz);
		if (!buf) 			return(-1);
		*buf_r = buf;
	}
	else
		buf = *buf_r;
	
	*buf_sz_r = msg_sz;
	
	// debug - shouldn't be required
	memset(buf, 0, msg_sz);
	
	// lay in some parameters
	uint32_t generation = 0;
	if (cl_w_p) {
		if (cl_w_p->unique) {
			info2 |= CL_MSG_INFO2_WRITE_UNIQUE;
		} else if (cl_w_p->unique_bin) {
			info2 |= CL_MSG_INFO2_WRITE_BINUNIQUE;
		} else if (cl_w_p->use_generation) {
			info2 |= CL_MSG_INFO2_GENERATION;
			generation = cl_w_p->generation;
		} else if (cl_w_p->use_generation_gt) {
			info2 |= CL_MSG_INFO2_GENERATION_GT;
			generation = cl_w_p->generation;
		} else if (cl_w_p->use_generation_dup) {
			info2 |= CL_MSG_INFO2_GENERATION_DUP;
			generation = cl_w_p->generation;
		}
	}
	
	uint32_t record_ttl = cl_w_p ? cl_w_p->record_ttl : 0;
	uint32_t transaction_ttl = cl_w_p ? cl_w_p->timeout_ms : 0;

	// lay out the header
	int n_fields = ( ns ? 1 : 0 ) + (set ? 1 : 0) + (key ? 1 : 0) + (digest ? 1 : 0) + (trid ? 1 : 0) + (scan_param_field ? 1 : 0);
	buf = cl_write_header(buf, msg_sz, info1, info2, info3, generation, record_ttl, transaction_ttl, n_fields, n_values);
		
	// now the fields
	buf = write_fields(buf, ns, ns_len, set, set_len, key, digest, d_ret, trid,scan_param_field);
	if (!buf) {
		if (mbuf)	free(mbuf);
		return(-1);
	}

	// lay out the ops
	if (n_values) {
		cl_msg_op *op = (cl_msg_op *) buf;
		cl_msg_op *op_tmp;
		for (i = 0; i< n_values;i++) {
			if( values ) {
				cl_value_to_op( &values[i], operator, NULL, op);
			}else if (operations) {
				cl_value_to_op( NULL, 0, &operations[i], op);
			}
	
			op_tmp = cl_msg_op_get_next(op);
			cl_msg_swap_op(op);
			op = op_tmp;
		}
	}
	return(0);	
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
xorl %ebx, %ebx
movq %r9, %r12
movq %r8, %r14
movq %rcx, %r13
testq %rcx, %rcx
movl $0x0, %r15d
movl %edx, 0x1c(%rsp)
movl %esi, 0xc(%rsp)
movl %edi, 0x18(%rsp)
je 0x47bb
movq %r13, %rdi
callq 0x20e0
movq %rax, %r15
testq %r14, %r14
je 0x47cb
movq %r14, %rdi
callq 0x20e0
movq %rax, %rbx
movq 0xd0(%rsp), %rdi
movq 0xc8(%rsp), %r8
movq 0x80(%rsp), %rdx
movslq %r15d, %rax
testq %r13, %r13
movq %rax, 0x38(%rsp)
leaq 0x23(%rax), %rax
pushq $0x1e
popq %rcx
cmovneq %rax, %rcx
movslq %ebx, %rax
testq %r14, %r14
movq %rax, 0x20(%rsp)
leaq 0x5(%rcx,%rax), %rax
cmoveq %rcx, %rax
testq %r12, %r12
movq %rax, (%rsp)
je 0x4824
movq 0x8(%r12), %rcx
leaq 0x6(%rcx,%rax), %rax
movq %rax, (%rsp)
movl 0xa0(%rsp), %esi
movq %rdx, %rcx
movq %r13, 0x40(%rsp)
movq %r12, 0x28(%rsp)
movq %r14, 0x30(%rsp)
orq %r8, %rcx
orq %rdi, %rcx
je 0x486a
leaq 0x1a(%rax), %rcx
testq %rdx, %rdx
cmoveq %rax, %rcx
testq %r8, %r8
leaq 0xd(%rcx), %rax
cmoveq %rcx, %rax
testq %rdi, %rdi
leaq 0x8(%rax), %rcx
cmoveq %rax, %rcx
movq %rcx, (%rsp)
movq 0x88(%rsp), %r14
xorl %ebx, %ebx
testl %esi, %esi
movl $0x0, %r12d
movq %rsp, %r13
cmovgl %esi, %r12d
cmpq %rbx, %r12
pushq $-0x1
popq %rbp
je 0x48d2
cmpq $0x0, 0x88(%rsp)
movq %r14, %r15
jne 0x48a5
imulq $0x48, %rbx, %r15
addq 0x98(%rsp), %r15
movq %r15, %rdi
callq 0x20e0
movq (%rsp), %rcx
movq %r15, %rdi
movq %r13, %rsi
leaq 0x8(%rax,%rcx), %rax
movq %rax, (%rsp)
callq 0x45b1
testl %eax, %eax
jne 0x491c
incq %rbx
addq $0x40, %r14
jmp 0x4883
movq 0xa8(%rsp), %rax
movq (%rsp), %rbx
xorl %r15d, %r15d
movq (%rax), %r13
testq %r13, %r13
je 0x494f
movq 0xb0(%rsp), %r14
cmpq (%r14), %rbx
jbe 0x495a
movq %rbx, %rdi
callq 0x22e0
testq %rax, %rax
je 0x4ceb
movq 0xa8(%rsp), %rcx
movq %rax, %r13
movq %r13, 0x10(%rsp)
movq %r13, (%rcx)
jmp 0x4963
leaq 0x109a5(%rip), %rax     # 0x152c8
cmpl $0x0, (%rax)
js 0x4ceb
leaq 0x1099d(%rip), %rax     # 0x152d0
leaq 0x9f26(%rip), %rsi      # 0xe860
xorl %edi, %edi
movl %ebx, %ecx
movq (%rax), %r8
movl 0x20(%r15), %edx
xorl %eax, %eax
callq *%r8
jmp 0x4ceb
movq 0xb0(%rsp), %r14
xorl %r13d, %r13d
movq $0x0, 0x10(%rsp)
movq %r13, %rdi
xorl %esi, %esi
movq %rbx, %rdx
movq %rbx, (%r14)
callq 0x2140
movq 0xb8(%rsp), %rdx
testq %rdx, %rdx
je 0x498e
cmpb $0x1, (%rdx)
movl 0xc(%rsp), %ecx
jne 0x4999
orl $0x20, %ecx
jmp 0x49a2
movl 0xc(%rsp), %ecx
xorl %r9d, %r9d
xorl %eax, %eax
jmp 0x49d4
cmpb $0x1, 0x1(%rdx)
jne 0x49a7
orl $0x40, %ecx
xorl %r9d, %r9d
jmp 0x49cd
cmpb $0x1, 0x2(%rdx)
jne 0x49b2
orl $0x4, %ecx
jmp 0x49c9
cmpb $0x1, 0x3(%rdx)
jne 0x49bd
orl $0x8, %ecx
jmp 0x49c9
xorl %r9d, %r9d
cmpb $0x1, 0x4(%rdx)
jne 0x49cd
orl $0x10, %ecx
movl 0x8(%rdx), %r9d
movl 0xc(%rdx), %eax
movl 0x10(%rdx), %r15d
movq 0x40(%rsp), %rdx
xorl %r10d, %r10d
movl 0xa0(%rsp), %r11d
movq %r13, %rdi
movl 0x1c(%rsp), %r8d
movq %rbx, %rsi
testq %rdx, %rdx
movq %rdx, %r13
movl 0x18(%rsp), %edx
setne %r10b
cmpq $0x1, 0x30(%rsp)
sbbl $-0x1, %r10d
cmpq $0x1, 0x28(%rsp)
sbbl $-0x1, %r10d
cmpq $0x1, 0x80(%rsp)
sbbl $-0x1, %r10d
cmpq $0x1, 0xc8(%rsp)
sbbl $-0x1, %r10d
cmpq $0x1, 0xd0(%rsp)
sbbl $-0x1, %r10d
pushq %r11
pushq %r10
pushq %rax
pushq %r15
callq 0x4525
addq $0x20, %rsp
movq %rax, %rbx
testq %r13, %r13
je 0x4a7c
movq 0x38(%rsp), %rdx
leaq 0x5(%rbx), %rdi
movq %r13, %rsi
movb $0x0, 0x4(%rbx)
leal 0x1(%rdx), %r14d
movl %r14d, (%rbx)
callq 0x21b0
movq %rbx, %rdi
leaq 0x4(%rbx,%r14), %r14
callq 0xbdb4
movq %r14, %rbx
movq 0x30(%rsp), %r15
movq 0x28(%rsp), %r13
testq %r15, %r15
je 0x4ab7
movq 0x20(%rsp), %rdx
leaq 0x5(%rbx), %rdi
movq %r15, %rsi
movb $0x1, 0x4(%rbx)
leal 0x1(%rdx), %r14d
movl %r14d, (%rbx)
callq 0x21b0
movq %rbx, %rdi
leaq 0x4(%rbx,%r14), %r14
callq 0xbdb4
movq %r14, %rbx
movq 0xc8(%rsp), %rax
testq %rax, %rax
je 0x4ae4
movq %rbx, %rdi
bswapq %rax
movb $0x7, 0x4(%rbx)
movl $0x9, (%rbx)
leaq 0xd(%rbx), %r14
movq %rax, 0x5(%rbx)
callq 0xbdb4
movq %r14, %rbx
movq 0xd0(%rsp), %rax
testq %rax, %rax
je 0x4b11
movb $0x8, 0x4(%rbx)
movl $0x3, (%rbx)
movq %rbx, %rdi
leaq 0x7(%rbx), %r14
movzwl (%rax), %eax
movw %ax, 0x5(%rbx)
callq 0xbdb4
movq %r14, %rbx
testq %r13, %r13
je 0x4baa
movb $0x2, 0x4(%rbx)
movl (%r13), %edx
leal -0x1(%rdx), %eax
cmpl $0xa, %eax
ja 0x4cbe
leaq 0x9c0f(%rip), %rcx      # 0xe744
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movb %dl, 0x5(%rbx)
movl 0x8(%r13), %ebp
leaq 0x6(%rbx), %rdi
addl $0x2, %ebp
movl %ebp, (%rbx)
movq 0x8(%r13), %rdx
movq 0x10(%r13), %rsi
callq 0x21b0
movq %rbx, %rdi
leaq 0x4(%rbx), %rax
movl %ebp, %r14d
addq %rax, %r14
callq 0xbdb4
movq 0xc0(%rsp), %rbp
testq %rbp, %rbp
je 0x4b98
movq 0x8(%r13), %rcx
movq 0x20(%rsp), %rsi
addq $0x5, %rbx
movq %r15, %rdi
movq %rbp, %r8
movq %rbx, %rdx
incq %rcx
callq 0x6408
movq 0x80(%rsp), %r15
movl 0xa0(%rsp), %r13d
jmp 0x4bc5
movq 0x80(%rsp), %r15
movl 0xa0(%rsp), %r13d
movq 0xc0(%rsp), %rbp
movq %rbx, %r14
testq %r15, %r15
je 0x4c16
movb $0x4, 0x4(%r14)
movl $0x15, (%r14)
movq %r14, %rdi
leaq 0x19(%r14), %rbx
movq (%r15), %rax
movq 0x8(%r15), %rcx
movq %rax, 0x5(%r14)
movq %rcx, 0xd(%r14)
movl 0x10(%r15), %eax
movl %eax, 0x15(%r14)
callq 0xbdb4
testq %rbp, %rbp
je 0x4c19
movl 0x10(%r15), %eax
movl %eax, 0x10(%rbp)
movq (%r15), %rax
movq 0x8(%r15), %rcx
movq %rcx, 0x8(%rbp)
movq %rax, (%rbp)
jmp 0x4c19
movq %r14, %rbx
xorl %ebp, %ebp
testl %r13d, %r13d
je 0x4ceb
movq 0x98(%rsp), %r14
movq 0x88(%rsp), %r15
subq $0x1, %r12
jb 0x4ceb
cmpq $0x0, 0x88(%rsp)
je 0x4c57
movl 0x90(%rsp), %esi
movq %r15, %rdi
xorl %edx, %edx
jmp 0x4c69
cmpq $0x0, 0x98(%rsp)
je 0x4c71
xorl %edi, %edi
xorl %esi, %esi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x460c
movl (%rbx), %eax
movq %rbx, %rdi
leaq 0x4(%rbx,%rax), %r13
callq 0xbda3
addq $0x40, %r15
addq $0x48, %r14
movq %r13, %rbx
jmp 0x4c34
movb $0x3, 0x5(%rbx)
jmp 0x4b41
movb $0x1, 0x5(%rbx)
movq 0x10(%r13), %rax
bswapq %rax
movq %rax, 0x6(%rbx)
movl $0xa, (%rbx)
movq 0x10(%r13), %rax
bswapq %rax
movq %rax, 0x6(%rbx)
pushq $0xa
popq %rbp
jmp 0x4b5b
leaq 0x10603(%rip), %rax     # 0x152c8
cmpl $0x0, (%rax)
js 0x4ce1
leaq 0x105ff(%rip), %rax     # 0x152d0
leaq 0x9be2(%rip), %rsi      # 0xe8ba
xorl %edi, %edi
movq (%rax), %rcx
xorl %eax, %eax
callq *%rcx
movq 0x10(%rsp), %rdi
callq 0x22c0
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq