// File: /workspace/github_c_makefile_star2_O3/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 $0x48, %rsp
cmpw $0x0, 0x12(%rdi)
movq %r8, %r15
movq %rdx, %r13
movq %rsi, %r12
movq %r9, 0x10(%rsp)
movq %rcx, 0x28(%rsp)
movq %rdi, 0x18(%rsp)
movq %rsi, 0x8(%rsp)
je 0x540e
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpq $0x5, %r13
jl 0x5642
movq 0x88(%rsp), %rbx
movq 0x80(%rsp), %r14
movq 0x8(%rsp), %r12
xorl %ebp, %ebp
movq %r12, %rdi
callq 0xc8f9
movb 0x4(%r12), %al
cmpb $0x7, %al
jne 0x53d8
testq %r14, %r14
je 0x53f4
movq 0x5(%r12), %rax
bswapq %rax
movq %rax, (%r14)
jmp 0x53f4
testq %rbx, %rbx
je 0x53f4
cmpb $0x1, %al
jne 0x53f4
movl (%r12), %esi
leaq 0x5(%r12), %rdi
decl %esi
callq 0x2060
movq %rax, (%rbx)
movl (%r12), %eax
addq $0x4, %r12
incl %ebp
addq %rax, %r12
movq 0x18(%rsp), %rax
movzwl 0x12(%rax), %eax
cmpl %eax, %ebp
jb 0x53b5
xorl %ebx, %ebx
cmpq $0x0, 0x10(%rsp)
je 0x5642
movq 0x28(%rsp), %rbp
movq %rbp, %rax
orq %r15, %rax
je 0x5642
movq 0x18(%rsp), %rax
movq 0x10(%rsp), %rcx
addq %r13, 0x8(%rsp)
movzwl 0x14(%rax), %r14d
cmpl %r14d, (%rcx)
jge 0x5474
testq %rbp, %rbp
je 0x5578
movl %r14d, %edi
shll $0x6, %edi
callq 0x22e0
testq %rax, %rax
je 0x563d
movq 0x10(%rsp), %rcx
movl %r14d, (%rcx)
movq %rax, (%rbp)
jmp 0x559d
testl %r14d, %r14d
je 0x5642
leaq 0x8(%r12), %rax
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpq %rax, 0x8(%rsp)
jb 0x5642
xorl %ecx, %ecx
movq %r15, 0x30(%rsp)
movq %r12, %rdi
movq %rcx, 0x38(%rsp)
callq 0xc8e8
testq %rbp, %rbp
je 0x54b6
movq (%rbp), %rax
movq %rax, 0x8(%rsp)
jmp 0x54bf
movq $0x0, 0x8(%rsp)
testq %r15, %r15
je 0x54c9
movq (%r15), %r15
jmp 0x54cc
xorl %r15d, %r15d
movq 0x10(%rsp), %rax
movslq (%rax), %rcx
testq %rcx, %rcx
movq %rcx, 0x20(%rsp)
jle 0x552b
movzbl 0x7(%r12), %r13d
movq 0x8(%rsp), %rbx
leaq 0x8(%r12), %rax
xorl %r14d, %r14d
movq %rax, 0x40(%rsp)
cmpq $0x0, 0x8(%rsp)
movq 0x40(%rsp), %rsi
movq %rbx, %rbp
movq %r13, %rdx
cmoveq %r15, %rbp
movq %rbp, %rdi
callq 0x2260
testl %eax, %eax
je 0x5530
incq %r14
addq $0x48, %r15
addq $0x40, %rbx
cmpq %r14, 0x20(%rsp)
jne 0x54f6
jmp 0x5546
xorl %r14d, %r14d
xorl %ebp, %ebp
cmpl 0x20(%rsp), %r14d
je 0x5546
addq $0x20, %rbp
movq %r12, %rdi
movq %rbp, %rsi
callq 0x51cf
movl (%r12), %eax
movq 0x38(%rsp), %rcx
movq 0x30(%rsp), %r15
movq 0x28(%rsp), %rbp
leaq 0x4(%r12,%rax), %r12
movq 0x18(%rsp), %rax
incl %ecx
movzwl 0x14(%rax), %eax
cmpl %eax, %ecx
jb 0x5499
xorl %ebx, %ebx
jmp 0x5642
leal (,%r14,8), %eax
leaq (%rax,%rax,8), %rdi
callq 0x22e0
testq %rax, %rax
je 0x563d
movq 0x10(%rsp), %rcx
movl %r14d, (%rcx)
movq %rax, (%r15)
movq 0x18(%rsp), %rax
cmpw $0x0, 0x14(%rax)
je 0x5642
leaq 0x8(%r12), %rax
xorl %r13d, %r13d
xorl %r14d, %r14d
movq %rax, 0x20(%rsp)
testq %rbp, %rbp
je 0x55cf
movq %r14, %rbx
shlq $0x6, %rbx
addq (%rbp), %rbx
jmp 0x55d5
movq (%r15), %rbx
addq %r13, %rbx
movq 0x20(%rsp), %rax
cmpq %rax, 0x8(%rsp)
jb 0x563d
movq %r12, %rdi
callq 0xc8e8
movzbl 0x7(%r12), %edx
cmpq $0x20, %rdx
ja 0x561b
leaq 0x8(%r12), %rsi
movq %rbx, %rdi
callq 0x21b0
movzbl 0x7(%r12), %eax
movq %r12, %rdi
movb $0x0, (%rbx,%rax)
addq $0x20, %rbx
movq %rbx, %rsi
callq 0x51cf
movl (%r12), %eax
movq 0x18(%rsp), %rcx
incq %r14
addq $0x48, %r13
xorl %ebx, %ebx
leaq 0x4(%r12,%rax), %r12
movzwl 0x14(%rcx), %eax
cmpq %rax, %r14
jb 0x55bd
jmp 0x5642
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq