// File: /workspace/github_c_makefile_star2_O0/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:
subq $0xa8, %rsp
movq 0xb8(%rsp), %rax
movq 0xb0(%rsp), %rax
movq %rdi, 0x98(%rsp)
movq %rsi, 0x90(%rsp)
movq %rdx, 0x88(%rsp)
movq %rcx, 0x80(%rsp)
movq %r8, 0x78(%rsp)
movq %r9, 0x70(%rsp)
movq 0x90(%rsp), %rax
addq 0x88(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0x98(%rsp), %rax
cmpw $0x0, 0x12(%rax)
je 0x6f41
movq 0x90(%rsp), %rax
movq %rax, 0x58(%rsp)
movl $0x0, 0x64(%rsp)
movl 0x64(%rsp), %eax
movq 0x98(%rsp), %rcx
movzwl 0x12(%rcx), %ecx
cmpl %ecx, %eax
jge 0x6f34
movq 0x68(%rsp), %rax
movq 0x90(%rsp), %rcx
addq $0x5, %rcx
cmpq %rcx, %rax
jae 0x6e7c
movl $0xffffffff, 0xa4(%rsp) # imm = 0xFFFFFFFF
jmp 0x71ff
movq 0x58(%rsp), %rdi
callq 0x13cd0
movq 0x58(%rsp), %rax
movzbl 0x4(%rax), %eax
cmpl $0x7, %eax
jne 0x6ece
movq 0x58(%rsp), %rax
movq 0x5(%rax), %rax
movq %rax, 0x50(%rsp)
cmpq $0x0, 0xb0(%rsp)
je 0x6ecc
movq 0x50(%rsp), %rdi
callq 0x7210
movq %rax, 0x28(%rsp)
movq 0x28(%rsp), %rcx
movq 0xb0(%rsp), %rax
movq %rcx, (%rax)
jmp 0x6f15
movq 0x58(%rsp), %rax
movzbl 0x4(%rax), %eax
cmpl $0x1, %eax
jne 0x6f13
cmpq $0x0, 0xb8(%rsp)
je 0x6f11
movq 0x58(%rsp), %rdi
addq $0x5, %rdi
movq 0x58(%rsp), %rax
movl (%rax), %eax
subl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
callq 0x2080
movq %rax, %rcx
movq 0xb8(%rsp), %rax
movq %rcx, (%rax)
jmp 0x6f13
jmp 0x6f15
movq 0x58(%rsp), %rdi
callq 0x7220
movq %rax, 0x58(%rsp)
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0x6e3e
movq 0x58(%rsp), %rax
movq %rax, 0x90(%rsp)
movq 0x90(%rsp), %rax
movq %rax, 0x48(%rsp)
cmpq $0x0, 0x70(%rsp)
je 0x71f4
cmpq $0x0, 0x80(%rsp)
jne 0x6f71
cmpq $0x0, 0x78(%rsp)
je 0x71f4
movq 0x98(%rsp), %rax
movzwl 0x14(%rax), %eax
movq 0x70(%rsp), %rcx
cmpl (%rcx), %eax
jle 0x710f
cmpq $0x0, 0x80(%rsp)
je 0x6fee
movq 0x98(%rsp), %rax
movzwl 0x14(%rax), %eax
movl %eax, %edi
shlq $0x6, %rdi
callq 0x2310
movq %rax, 0x40(%rsp)
cmpq $0x0, 0x40(%rsp)
jne 0x6fc9
movl $0xffffffff, 0xa4(%rsp) # imm = 0xFFFFFFFF
jmp 0x71ff
movq 0x98(%rsp), %rax
movzwl 0x14(%rax), %ecx
movq 0x70(%rsp), %rax
movl %ecx, (%rax)
movq 0x40(%rsp), %rcx
movq 0x80(%rsp), %rax
movq %rcx, (%rax)
jmp 0x7040
movq 0x98(%rsp), %rax
movzwl 0x14(%rax), %eax
imulq $0x48, %rax, %rdi
callq 0x2310
movq %rax, 0x38(%rsp)
cmpq $0x0, 0x38(%rsp)
jne 0x7020
movl $0xffffffff, 0xa4(%rsp) # imm = 0xFFFFFFFF
jmp 0x71ff
movq 0x98(%rsp), %rax
movzwl 0x14(%rax), %ecx
movq 0x70(%rsp), %rax
movl %ecx, (%rax)
movq 0x38(%rsp), %rcx
movq 0x78(%rsp), %rax
movq %rcx, (%rax)
movl $0x0, 0x64(%rsp)
movl 0x64(%rsp), %eax
movq 0x98(%rsp), %rcx
movzwl 0x14(%rcx), %ecx
cmpl %ecx, %eax
jge 0x710a
cmpq $0x0, 0x80(%rsp)
je 0x7089
movq 0x80(%rsp), %rax
movq (%rax), %rax
movslq 0x64(%rsp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movq %rax, 0x20(%rsp)
jmp 0x70a2
movq 0x78(%rsp), %rax
movq (%rax), %rax
movslq 0x64(%rsp), %rcx
imulq $0x48, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x68(%rsp), %rax
movq 0x90(%rsp), %rcx
addq $0x8, %rcx
cmpq %rcx, %rax
jae 0x70d2
movl $0xffffffff, 0xa4(%rsp) # imm = 0xFFFFFFFF
jmp 0x71ff
movq 0x48(%rsp), %rdi
callq 0x13b70
movq 0x48(%rsp), %rdi
movq 0x30(%rsp), %rsi
callq 0x6b20
movq 0x48(%rsp), %rdi
callq 0x6b00
movq %rax, 0x48(%rsp)
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0x7048
jmp 0x71f2
movl $0x0, 0x64(%rsp)
movl 0x64(%rsp), %eax
movq 0x98(%rsp), %rcx
movzwl 0x14(%rcx), %ecx
cmpl %ecx, %eax
jge 0x71f0
movq 0x68(%rsp), %rax
movq 0x90(%rsp), %rcx
addq $0x8, %rcx
cmpq %rcx, %rax
jae 0x7155
movl $0xffffffff, 0xa4(%rsp) # imm = 0xFFFFFFFF
jmp 0x71ff
movq 0x48(%rsp), %rdi
callq 0x13b70
movq 0x48(%rsp), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, 0x80(%rsp)
je 0x7186
movq 0x80(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x10(%rsp)
jmp 0x718f
xorl %eax, %eax
movq %rax, 0x10(%rsp)
jmp 0x718f
movq 0x10(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x78(%rsp)
je 0x71af
movq 0x78(%rsp), %rax
movq (%rax), %rax
movq %rax, (%rsp)
jmp 0x71b7
xorl %eax, %eax
movq %rax, (%rsp)
jmp 0x71b7
movq 0x8(%rsp), %rsi
movq 0x18(%rsp), %rdi
movq (%rsp), %rdx
movq 0x70(%rsp), %rax
movl (%rax), %ecx
callq 0x7240
movq 0x48(%rsp), %rdi
callq 0x6b00
movq %rax, 0x48(%rsp)
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0x7117
jmp 0x71f2
jmp 0x71f4
movl $0x0, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
addq $0xa8, %rsp
retq
nop