// File: /workspace/github_c_makefile_star2_O2/sharpyfox[P]citrusleaf-client/src/citrusleaf.c
// Function: cl_parse
int
cl_parse(cl_msg *msg, uint8_t *buf, size_t buf_len, cl_bin **values_r, cl_operation **operations_r, 
	int *n_values_r, uint64_t *trid_r, char **setname_r)
{
	uint8_t *buf_lim = buf + buf_len;
	
	int i;
	if (msg->n_fields) {
		cl_msg_field *mf = (cl_msg_field *)buf;
		
		for (i=0;i<msg->n_fields;i++) {
			
			if (buf_lim < buf + sizeof(cl_msg_field)) {
#ifdef DEBUG
				cf_error("parse: too short message: said there was a field, but too short");
#endif
				return(-1);
			}

			cl_msg_swap_field(mf);
			if (mf->type == CL_MSG_FIELD_TYPE_TRID) {
				uint64_t trid_nbo;
				//We get the transaction-id in network byte order (big-endian)
				//We should convert to host byte order
				memcpy(&trid_nbo, mf->data, sizeof(trid_nbo));
				if (trid_r) {
					*trid_r = __be64_to_cpu(trid_nbo);
				}
			} else if (mf->type == CL_MSG_FIELD_TYPE_SET) {
				// In case of set name, the field size is set to one more than the 
				// size of set name (to accomodate the byte used for 'type' value)
				if (setname_r) {
					*setname_r = strndup(mf->data, (mf->field_sz-1));
				}
			}

			mf = cl_msg_field_get_next(mf);
		}
		buf = (uint8_t *) mf;
	}

	cl_msg_op *op = (cl_msg_op *)buf;
	
	// if we weren't passed in a buffer to complete, we need to make a new one
	// XXX - You've got a likely memory leak here.  If we need *more* bins than the 
	// caller has allocated to us, we allocate a larger block. But we do so by 
	// nuking the reference to the block that the caller has given us, replacing
	// it with our own (malloc'd) memory, and not telling the caller.  - CSW
	if (n_values_r && (values_r || operations_r) ) {
		if (msg->n_ops > *n_values_r) {
			// straight bin path..
			if( values_r ){								   
				cl_bin *values = malloc( sizeof(cl_bin) * msg->n_ops );
				if (values == 0) 		return(-1);
				*n_values_r = msg->n_ops;
				*values_r = values;
			}else{ // operations path 
				cl_operation *operations = (cl_operation *)malloc( sizeof(cl_operation) *msg->n_ops );
				if( operations == 0 )		return(-1);
				*n_values_r = msg->n_ops;
				*operations_r = operations;
			}
			

			// if we already have our filled-out value structure, just copy in
			for (i=0;i<msg->n_ops;i++) {
				cl_bin *value = values_r ? &((*values_r)[i]) : &((*operations_r)[i].bin);
				
				if (buf_lim < buf + sizeof(cl_msg_op)) {
#ifdef DEBUG
					cf_debug("parse: too short message: said there was ops, iteration %d, but too short", i);
#endif
					return(-1);
				}

				cl_msg_swap_op(op);
				
				cl_set_value_particular(op, value);
				
				op = cl_msg_op_get_next(op);
				
			}
			
		}
		else {
			// if we already have our filled-out value structure, just copy in
			for (i=0;i<msg->n_ops;i++) {

				if (buf_lim < buf + sizeof(cl_msg_op)) {
#ifdef DEBUG
					cf_debug("parse: too short message: said there was ops, iteration %d, but too short", i);
#endif
					return(-1);
				}

				cl_msg_swap_op(op);
				
				// This is a little peculiar. We could get a response that wasn't in the result
				// set, would be nice to throw an error
				set_value_search(op, values_r?*values_r:NULL, operations_r?*operations_r:NULL, *n_values_r);
				
				op = cl_msg_op_get_next(op);
			}
		}
	}
	
	
	return(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movzwl 0x12(%rdi), %eax
movq %r8, %r13
movq %rcx, %r12
movq %rdx, %rbp
movq %r9, 0x10(%rsp)
movq %rdi, 0x28(%rsp)
testw %ax, %ax
pushq $-0x1
popq %r14
movq %rsi, 0x20(%rsp)
movq %rsi, 0x8(%rsp)
je 0x4fa6
movq 0x20(%rsp), %rcx
movq 0x90(%rsp), %r15
xorl %r14d, %r14d
movq %rcx, 0x8(%rsp)
movzwl %ax, %eax
cmpl %eax, %r14d
jae 0x4fa2
cmpq $0x5, %rbp
jl 0x51bd
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0xbdb4
movb 0x4(%rbx), %al
cmpb $0x7, %al
jne 0x4f52
testq %r15, %r15
je 0x4f7e
movq 0x8(%rsp), %rax
movq 0x5(%rax), %rax
bswapq %rax
movq %rax, (%r15)
jmp 0x4f7e
cmpq $0x0, 0x98(%rsp)
je 0x4f7e
cmpb $0x1, %al
jne 0x4f7e
movq 0x8(%rsp), %rax
movl (%rax), %esi
leaq 0x5(%rax), %rdi
decl %esi
callq 0x2060
movq 0x98(%rsp), %rcx
movq %rax, (%rcx)
movq 0x8(%rsp), %rcx
incl %r14d
movl (%rcx), %eax
addq $0x4, %rcx
addq %rax, %rcx
movq 0x28(%rsp), %rax
movq %rcx, 0x8(%rsp)
movzwl 0x12(%rax), %eax
jmp 0x4f12
pushq $-0x1
popq %r14
xorl %ebx, %ebx
cmpq $0x0, 0x10(%rsp)
je 0x51c2
movq %r12, %rax
orq %r13, %rax
je 0x51c2
movq 0x28(%rsp), %rax
movq 0x10(%rsp), %rcx
addq %rbp, 0x20(%rsp)
movq %r12, 0x38(%rsp)
movzwl 0x14(%rax), %eax
movzwl %ax, %ebx
cmpl %ebx, (%rcx)
jge 0x500b
testq %r12, %r12
je 0x5128
movl %ebx, %edi
shll $0x6, %edi
callq 0x22e0
testq %rax, %rax
je 0x51d3
movq 0x10(%rsp), %rcx
movl %ebx, (%rcx)
movq %rax, (%r12)
jmp 0x5145
movq 0x8(%rsp), %rcx
movq %r13, 0x40(%rsp)
addq $0x8, %rcx
movq %rcx, 0x48(%rsp)
xorl %ecx, %ecx
movzwl %ax, %eax
cmpl %eax, %ecx
jae 0x511d
movq 0x48(%rsp), %rdx
cmpq %rdx, 0x20(%rsp)
jb 0x511d
movq 0x8(%rsp), %rdi
movq %rcx, 0x50(%rsp)
callq 0xbda3
testq %r12, %r12
je 0x505a
movq (%r12), %rax
movq %rax, 0x18(%rsp)
jmp 0x5063
movq $0x0, 0x18(%rsp)
testq %r13, %r13
je 0x506e
movq (%r13), %r12
jmp 0x5071
xorl %r12d, %r12d
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %r14
movl $0x0, %r13d
movl (%rax), %ecx
movq 0x8(%rsp), %rax
testl %ecx, %ecx
leaq 0x8(%rax), %rbp
movl %ecx, 0x34(%rsp)
cmovgl %ecx, %r13d
xorl %r15d, %r15d
xorl %ebx, %ebx
cmpq %r15, %r13
je 0x50d5
movq 0x8(%rsp), %rax
cmpq $0x0, 0x18(%rsp)
movq %r14, %rbx
movq %rbp, %rsi
movzbl 0x7(%rax), %edx
cmoveq %r12, %rbx
movq %rbx, %rdi
callq 0x2260
testl %eax, %eax
je 0x50d2
incq %r15
addq $0x48, %r12
addq $0x40, %r14
jmp 0x509b
movl %r15d, %r13d
cmpl 0x34(%rsp), %r13d
movq 0x38(%rsp), %r12
je 0x50f2
movq 0x8(%rsp), %rdi
addq $0x20, %rbx
movq %rbx, %rsi
callq 0x4d39
movq 0x8(%rsp), %rcx
movq 0x40(%rsp), %r13
movl (%rcx), %eax
leaq 0x4(%rcx,%rax), %rcx
movq 0x28(%rsp), %rax
movq %rcx, 0x8(%rsp)
movq 0x50(%rsp), %rcx
movzwl 0x14(%rax), %eax
incl %ecx
jmp 0x5020
xorl %ebx, %ebx
cmpl %eax, %ecx
sbbl %ebx, %ebx
jmp 0x51c2
imulq $0x48, %rbx, %rdi
callq 0x22e0
testq %rax, %rax
je 0x51d3
movq 0x10(%rsp), %rcx
movl %ebx, (%rcx)
movq %rax, (%r13)
movq 0x8(%rsp), %rax
xorl %ebx, %ebx
xorl %r15d, %r15d
xorl %r12d, %r12d
addq $0x8, %rax
movq %rax, 0x18(%rsp)
movq 0x28(%rsp), %rax
movzwl 0x14(%rax), %eax
cmpq %rax, %r12
jae 0x51c2
movq 0x38(%rsp), %rax
testq %rax, %rax
je 0x517b
movq (%rax), %rbp
addq %r15, %rbp
jmp 0x5183
imulq $0x48, %r12, %rbp
addq (%r13), %rbp
movq 0x18(%rsp), %rax
cmpq %rax, 0x20(%rsp)
jb 0x51bd
movq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0xbda3
movq %r14, %rdi
movq %rbp, %rsi
callq 0x4cfc
movl (%r14), %eax
incq %r12
addq $0x40, %r15
leaq 0x4(%r14,%rax), %r14
movq %r14, 0x8(%rsp)
jmp 0x515b
pushq $-0x1
popq %rax
movl %eax, %ebx
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %r14d, %ebx
jmp 0x51c2