// File: /workspace/github_c_makefile_star2_O1/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
movq %r9, 0x20(%rsp)
movq %r8, %r15
movq %rcx, 0x10(%rsp)
movq %rdx, %rbx
movq %rsi, %r12
movq %rdi, 0x18(%rsp)
cmpw $0x0, 0x12(%rdi)
movq %rsi, 0x8(%rsp)
je 0x54ab
movq 0x18(%rsp), %rax
cmpw $0x0, 0x12(%rax)
setne %al
je 0x5498
movb %al, 0x28(%rsp)
movq 0x88(%rsp), %r14
movq 0x80(%rsp), %r13
xorl %ebp, %ebp
movq 0x8(%rsp), %r12
cmpq $0x5, %rbx
jl 0x5494
movq %r12, %rdi
callq 0xcd01
movb 0x4(%r12), %al
cmpb $0x7, %al
jne 0x5459
testq %r13, %r13
je 0x5475
movq 0x5(%r12), %rax
bswapq %rax
movq %rax, (%r13)
jmp 0x5475
testq %r14, %r14
je 0x5475
cmpb $0x1, %al
jne 0x5475
leaq 0x5(%r12), %rdi
movl (%r12), %esi
decl %esi
callq 0x2060
movq %rax, (%r14)
movl (%r12), %eax
addq $0x4, %r12
addq %rax, %r12
incl %ebp
movq 0x18(%rsp), %rax
movzwl 0x12(%rax), %eax
cmpl %eax, %ebp
setb %al
jb 0x542f
jmp 0x549d
movb 0x28(%rsp), %al
movq 0x8(%rsp), %r12
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
testb %al, %al
jne 0x56fa
xorl %r13d, %r13d
cmpq $0x0, 0x20(%rsp)
je 0x56fa
movq 0x10(%rsp), %rax
orq %r15, %rax
je 0x56fa
addq %rbx, 0x8(%rsp)
movq 0x18(%rsp), %r14
movzwl 0x14(%r14), %ebx
movq 0x20(%rsp), %rax
cmpl %ebx, (%rax)
jge 0x5513
cmpq $0x0, 0x10(%rsp)
movl %ebx, %edi
je 0x562e
shll $0x6, %edi
callq 0x22e0
testq %rax, %rax
je 0x56f4
movq 0x20(%rsp), %rcx
movl %ebx, (%rcx)
movq 0x10(%rsp), %rcx
movq %rax, (%rcx)
jmp 0x564d
cmpw $0x0, 0x14(%r14)
je 0x56fa
leaq 0x8(%r12), %rax
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
cmpq %rax, 0x8(%rsp)
jb 0x56fa
xorl %ebx, %ebx
movq 0x10(%rsp), %r13
movq 0x18(%rsp), %rbp
movq %r15, 0x30(%rsp)
movq %r12, %rdi
callq 0xccf0
testq %r13, %r13
je 0x555e
movq (%r13), %rax
movq %rax, 0x28(%rsp)
jmp 0x5567
movq $0x0, 0x28(%rsp)
testq %r15, %r15
je 0x5571
movq (%r15), %r14
jmp 0x5574
xorl %r14d, %r14d
movq %rbx, 0x38(%rsp)
movq 0x20(%rsp), %rax
movl (%rax), %eax
movq %rax, 0x8(%rsp)
testl %eax, %eax
jle 0x55e5
leaq 0x8(%r12), %rax
movq %rax, 0x40(%rsp)
movzbl 0x7(%r12), %ebp
movq 0x28(%rsp), %r13
xorl %r15d, %r15d
cmpq $0x0, 0x28(%rsp)
movq %r13, %rbx
cmoveq %r14, %rbx
movq %rbx, %rdi
movq 0x40(%rsp), %rsi
movq %rbp, %rdx
callq 0x2260
testl %eax, %eax
je 0x55d9
incq %r15
addq $0x48, %r14
addq $0x40, %r13
cmpq %r15, 0x8(%rsp)
jne 0x55a1
movq 0x8(%rsp), %r15
movq 0x10(%rsp), %r13
movq 0x18(%rsp), %rbp
jmp 0x55ea
xorl %r15d, %r15d
xorl %ebx, %ebx
cmpl 0x8(%rsp), %r15d
movq 0x38(%rsp), %r14
je 0x5605
addq $0x20, %rbx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x5235
movl (%r12), %eax
leaq 0x4(%r12,%rax), %r12
incl %r14d
movzwl 0x14(%rbp), %eax
cmpl %eax, %r14d
movq 0x30(%rsp), %r15
movq %r14, %rbx
jb 0x5546
xorl %r13d, %r13d
jmp 0x56fa
shll $0x3, %edi
leaq (%rdi,%rdi,8), %rdi
callq 0x22e0
testq %rax, %rax
je 0x56f4
movq 0x20(%rsp), %rcx
movl %ebx, (%rcx)
movq %rax, (%r15)
cmpw $0x0, 0x14(%r14)
je 0x56fa
leaq 0x8(%r12), %rcx
xorl %r14d, %r14d
xorl %ebp, %ebp
movq 0x10(%rsp), %rax
testq %rax, %rax
je 0x5679
movq %rbp, %rbx
shlq $0x6, %rbx
addq (%rax), %rbx
jmp 0x567f
movq (%r15), %rbx
addq %r14, %rbx
cmpq %rcx, 0x8(%rsp)
jb 0x56cf
movq %rcx, %r13
movq %r12, %rdi
callq 0xccf0
movzbl 0x7(%r12), %edx
cmpq $0x20, %rdx
ja 0x56c3
leaq 0x8(%r12), %rsi
movq %rbx, %rdi
callq 0x21b0
movzbl 0x7(%r12), %eax
movb $0x0, (%rbx,%rax)
addq $0x20, %rbx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x5235
movl (%r12), %eax
leaq 0x4(%r12,%rax), %r12
movq %r13, %rcx
cmpq %rcx, 0x8(%rsp)
jb 0x56f4
incq %rbp
movq 0x18(%rsp), %rax
movzwl 0x14(%rax), %eax
addq $0x48, %r14
xorl %r13d, %r13d
cmpq %rax, %rbp
jb 0x5663
jmp 0x56fa
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movl %r13d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq