// File: /workspace/github_c_makefile_star2_O2/sharpyfox[P]citrusleaf-client/src/cl_scan.c
// Function: do_scan_monte
static int
do_scan_monte(cl_cluster *asc, char *node_name, uint operation_info, uint operation_info2, const char *ns, const char *set, 
	cl_bin *bins, int n_bins, uint8_t scan_pct, 
	citrusleaf_get_many_cb cb, void *udata, cl_scan_parameters *scan_opt)
{
	int rv = -1;

	uint8_t		rd_stack_buf[STACK_BUF_SZ];	
	uint8_t		*rd_buf = 0;
	size_t		rd_buf_sz = 0;
	uint8_t		wr_stack_buf[STACK_BUF_SZ];
	uint8_t		*wr_buf = wr_stack_buf;
	size_t		wr_buf_sz = sizeof(wr_stack_buf);

	cl_scan_param_field	scan_param_field;

	if (scan_opt) {
		scan_param_field.scan_pct = scan_pct>100? 100:scan_pct;
		scan_param_field.byte1 = (scan_opt->priority<<4) | (scan_opt->fail_on_cluster_change<<3);
	}

	// we have a single namespace and/or set to get
	if (cl_compile(operation_info, operation_info2, 0, ns, set, 0, 0, 0, 0, 0, 0, &wr_buf, &wr_buf_sz, 0, NULL, 0,
			scan_opt ? &scan_param_field : NULL)) {
		return(rv);
	}
	
#ifdef DEBUG_VERBOSE
	dump_buf("sending request to cluster:", wr_buf, wr_buf_sz);
#endif

	int fd;
	cl_cluster_node *node = 0;

	// Get an FD from a cluster
	if (node_name) {
		node = cl_cluster_node_get_byname(asc,node_name);
		// grab a reservation
		if (node)
			cf_client_rc_reserve(node);
	} else {
		pthread_mutex_lock(&asc->LOCK);
		node = cl_cluster_node_get_random(asc);
		pthread_mutex_unlock(&asc->LOCK);
	}
	if (!node) {
#ifdef DEBUG
		cf_debug("warning: no healthy nodes in cluster, failing");
#endif			
		return(-1);
	}
	fd = cl_cluster_node_fd_get(node, false, asc->nbconnect);
	if (fd == -1) {
#ifdef DEBUG			
		cf_debug("warning: node %s has no file descriptors, retrying transaction", node->name);
#endif
		return(-1);
	}
	
	// send it to the cluster - non blocking socket, but we're blocking
	if (0 != cf_socket_write_forever(fd, wr_buf, wr_buf_sz)) {
#ifdef DEBUG			
		cf_debug("Citrusleaf: write timeout or error when writing header to server - %d fd %d errno %d", rv, fd, errno);
#endif
		return(-1);
	}

	cl_proto 		proto;
	bool done = false;
	
	do { // multiple CL proto per response
		
		// Now turn around and read a fine cl_pro - that's the first 8 bytes that has types and lengths
		if ((rv = cf_socket_read_forever(fd, (uint8_t *) &proto, sizeof(cl_proto) ) ) ) {
			cf_error("network error: errno %d fd %d",rv, fd);
			return(-1);
		}
#ifdef DEBUG_VERBOSE
		dump_buf("read proto header from cluster", (uint8_t *) &proto, sizeof(cl_proto));
#endif	
		cl_proto_swap(&proto);

		if (proto.version != CL_PROTO_VERSION) {
			cf_error("network error: received protocol message of wrong version %d", proto.version);
			return(-1);
		}
		if (proto.type != CL_PROTO_TYPE_CL_MSG) {
			cf_error("network error: received incorrect message version %d", proto.type);
			return(-1);
		}
		
		// second read for the remainder of the message - expect this to cover lots of data, many lines
		//
		// if there's no error
		rd_buf_sz =  proto.sz;
		if (rd_buf_sz > 0) {
                                                         
//            cf_debug("message read: size %u",(uint)proto.sz);

			if (rd_buf_sz > sizeof(rd_stack_buf))
				rd_buf = malloc(rd_buf_sz);
			else
				rd_buf = rd_stack_buf;
			if (rd_buf == NULL) 		return (-1);

			if ((rv = cf_socket_read_forever(fd, rd_buf, rd_buf_sz))) {
				cf_error("network error: errno %d fd %d", rv, fd);
				if (rd_buf != rd_stack_buf)	{ free(rd_buf); }
				return(-1);
			}
// this one's a little much: printing the entire body before printing the other bits			
#ifdef DEBUG_VERBOSE
			dump_buf("read msg body header (multiple msgs)", rd_buf, rd_buf_sz);
#endif	
		}
		
		// process all the cl_msg in this proto
		uint8_t *buf = rd_buf;
		uint pos = 0;
		cl_bin stack_bins[STACK_BINS];
		cl_bin *bins_local;
		
		while (pos < rd_buf_sz) {

#ifdef DEBUG_VERBOSE
			dump_buf("individual message header", buf, sizeof(cl_msg));
#endif	
			
			uint8_t *buf_start = buf;
			cl_msg *msg = (cl_msg *) buf;
			cl_msg_swap_header(msg);
			buf += sizeof(cl_msg);
			
			if (msg->header_sz != sizeof(cl_msg)) {
				cf_error("received cl msg of unexpected size: expecting %zd found %d, internal error",
					sizeof(cl_msg),msg->header_sz);
				return(-1);
			}

			// parse through the fields
			cf_digest *keyd = 0;
			char ns_ret[33] = {0};
			char *set_ret = NULL;
			cl_msg_field *mf = (cl_msg_field *)buf;
			for (int i=0;i<msg->n_fields;i++) {
				cl_msg_swap_field(mf);
				if (mf->type == CL_MSG_FIELD_TYPE_KEY) {
					cf_error("read: found a key - unexpected");
				}
				else if (mf->type == CL_MSG_FIELD_TYPE_DIGEST_RIPE) {
					keyd = (cf_digest *) mf->data;
				}
				else if (mf->type == CL_MSG_FIELD_TYPE_NAMESPACE) {
					memcpy(ns_ret, mf->data, cl_msg_field_get_value_sz(mf));
					ns_ret[ cl_msg_field_get_value_sz(mf) ] = 0;
				}
				else if (mf->type == CL_MSG_FIELD_TYPE_SET) {
					uint32_t set_name_len = cl_msg_field_get_value_sz(mf);
					set_ret = (char *)malloc(set_name_len + 1);
					memcpy(set_ret, mf->data, set_name_len);
					set_ret[ set_name_len ] = '\0';
				}

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

#ifdef DEBUG_VERBOSE
			cf_debug("message header fields: nfields %u nops %u", msg->n_fields, msg->n_ops);
#endif


			if (msg->n_ops > STACK_BINS) {
				bins_local = malloc(sizeof(cl_bin) * msg->n_ops);
			}
			else {
				bins_local = stack_bins;
			}
			if (bins_local == NULL) {
				if (set_ret) {
					free(set_ret);
				}
				return (-1);
			}
			
			// parse through the bins/ops
			cl_msg_op *op = (cl_msg_op *)buf;
			for (int i=0;i<msg->n_ops;i++) {

				cl_msg_swap_op(op);

#ifdef DEBUG_VERBOSE
				cf_debug("op receive: %p size %d op %d ptype %d pversion %d namesz %d",
					op,op->op_sz, op->op, op->particle_type, op->version, op->name_sz);				
#endif			

#ifdef DEBUG_VERBOSE
				dump_buf("individual op (host order)", (uint8_t *) op, op->op_sz + sizeof(uint32_t));
#endif	

				cl_set_value_particular(op, &bins_local[i]);
				op = cl_msg_op_get_next(op);
			}
			buf = (uint8_t *) op;
			
			if (msg->result_code != CL_RESULT_OK) {
				// Special case - if we scan a set name that doesn't exist on a
				// node, it will return "not found" - we unify this with the
				// case where OK is returned and no callbacks were made. [AKG]
				if (msg->result_code == CL_RESULT_NOTFOUND) {
					msg->result_code = CL_RESULT_OK;
				}
				rv = (int)msg->result_code;
				done = true;
			}
			else if (msg->info3 & CL_MSG_INFO3_LAST)	{
#ifdef DEBUG
				cf_debug("received final message");
#endif
				done = true;
			}
			else if ((msg->n_ops) || (operation_info & CL_MSG_INFO1_NOBINDATA)) {
				// got one good value? call it a success!
				(*cb) ( ns_ret, keyd, set_ret, msg->generation, msg->record_ttl, bins_local, msg->n_ops, false /*islast*/, udata);
				rv = 0;
			}
//			else
//				cf_debug("received message with no bins, signal of an error");

			if (bins_local != stack_bins) {
				free(bins_local);
				bins_local = 0;
			}

			if (set_ret) {
				free(set_ret);
				set_ret = NULL;
			}

			// don't have to free object internals. They point into the read buffer, where
			// a pointer is required
			pos += buf - buf_start;
			
		}
		
		if (rd_buf && (rd_buf != rd_stack_buf))	{
			free(rd_buf);
			rd_buf = 0;
		}

	} while ( done == false );

	if (wr_buf != wr_stack_buf) {
		free(wr_buf);
		wr_buf = 0;
	}

	cl_cluster_node_fd_put(node, fd, false);
	cl_cluster_node_put(node);
	node = 0;
	
#ifdef DEBUG_VERBOSE	
	cf_debug("exited loop: rv %d", rv );
#endif	
	
	return(rv);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x99f8, %rsp           # imm = 0x99F8
movl %edx, %ebp
movq 0x9a40(%rsp), %rdx
leaq 0x59f0(%rsp), %rax
movq %rsi, %r14
movq %rdi, %rbx
movq $0x4000, 0xa8(%rsp) # imm = 0x4000
movq %rax, 0x78(%rsp)
testq %rdx, %rdx
je 0xc8e0
movzbl %r9b, %eax
cmpb $0x64, %al
pushq $0x64
popq %rsi
cmovbl %eax, %esi
leaq 0x96(%rsp), %rax
movb %sil, 0x1(%rax)
movb (%rdx), %sil
movb 0x4(%rdx), %dl
shlb $0x3, %sil
shlb $0x4, %dl
orb %sil, %dl
movb %dl, (%rax)
jmp 0xc8e2
xorl %eax, %eax
andq $0x0, 0x48(%rsp)
andq $0x0, 0x40(%rsp)
andq $0x0, 0x38(%rsp)
andl $0x0, 0x20(%rsp)
andq $0x0, 0x18(%rsp)
andl $0x0, 0x10(%rsp)
andq $0x0, 0x8(%rsp)
andq $0x0, (%rsp)
movq %rax, 0x50(%rsp)
leaq 0xa8(%rsp), %rax
movl %ebp, %edi
xorl %esi, %esi
xorl %edx, %edx
xorl %r9d, %r9d
movq %rax, 0x30(%rsp)
leaq 0x78(%rsp), %rax
movq %rax, 0x28(%rsp)
callq 0x63a0
pushq $-0x1
popq %rcx
testl %eax, %eax
jne 0xcd87
movl %ebp, 0x8c(%rsp)
movl %ecx, %ebp
testq %r14, %r14
je 0xc976
movq %rbx, %rdi
movq %r14, %rsi
callq 0x65f0
testq %rax, %rax
je 0xcd85
movq %rax, %rdi
movq %rax, %r14
callq 0x6590
movq %r14, %rdi
jmp 0xc9a4
leaq 0x158(%rbx), %r14
movq %r14, %rdi
callq 0x6c50
movq %rbx, %rdi
callq 0x6420
movq %r14, %rdi
movq %rax, %r15
callq 0x66c0
movq %r15, %rdi
testq %r15, %r15
je 0xcd85
movzbl 0x15(%rbx), %edx
xorl %esi, %esi
movq %rdi, 0xb0(%rsp)
callq 0x6530
cmpl $-0x1, %eax
je 0xcd85
movq 0x78(%rsp), %rsi
movq 0xa8(%rsp), %rdx
movl %eax, %edi
movl %eax, %ebx
callq 0x6030
testl %eax, %eax
movl %ebp, %ecx
jne 0xcd87
leaq 0x68(%rsp), %r14
pushq $0x8
popq %rdx
movq 0xd5b1(%rip), %r12      # 0x19fa0
leaq 0x7082(%rip), %r15      # 0x13a78
xorl %edi, %edi
movq $0x0, 0x80(%rsp)
movl %ebx, 0x60(%rsp)
movq %rdi, 0x70(%rsp)
movl %ebx, %edi
movq %r14, %rsi
callq 0x6b70
testl %eax, %eax
jne 0xcdcb
movq %r14, %rdi
callq 0x6510
movzbl 0x68(%rsp), %edx
cmpl $0x2, %edx
jne 0xcdef
movzbl 0x69(%rsp), %edx
cmpl $0x3, %edx
jne 0xce08
movzwl 0x6e(%rsp), %r14d
movl 0x6a(%rsp), %eax
shlq $0x20, %r14
orq %rax, %r14
testq %r14, %r14
je 0xca95
cmpq $0x4001, %r14           # imm = 0x4001
leaq 0x19f0(%rsp), %rax
jb 0xca7b
movq %r14, %rdi
callq 0x6700
testq %rax, %rax
je 0xcdc6
movl %ebx, %edi
movq %rax, %rsi
movq %r14, %rdx
movq %rax, 0x70(%rsp)
callq 0x6b70
testl %eax, %eax
jne 0xce27
movq 0x70(%rsp), %rbx
xorl %ebp, %ebp
movl $0x0, 0x64(%rsp)
movq %r14, 0xb8(%rsp)
movl %ebp, %eax
cmpq %rax, %r14
jbe 0xcd06
movq %rbx, %rdi
callq 0x6ae0
movzbl (%rbx), %ecx
cmpl $0x16, %ecx
jne 0xcd9b
xorps %xmm0, %xmm0
movl %ebp, 0x90(%rsp)
leaq 0x16(%rbx), %r14
xorl %ebp, %ebp
xorl %r13d, %r13d
movb $0x0, 0xe0(%rsp)
movq $0x0, 0xa0(%rsp)
movaps %xmm0, 0xd0(%rsp)
movaps %xmm0, 0xc0(%rsp)
movzwl 0x12(%rbx), %eax
cmpl %eax, %r13d
jae 0xcbbe
movq %r14, %rdi
callq 0x6c10
movzbl 0x4(%r14), %eax
cmpq $0x4, %rax
ja 0xcbac
movslq (%r15,%rax,4), %rax
addq %r15, %rax
jmpq *%rax
movl (%r14), %edx
leaq 0x5(%r14), %rsi
leaq 0xc0(%rsp), %rdi
decl %edx
callq 0x65b0
movl (%r14), %eax
decl %eax
movb $0x0, 0xc0(%rsp,%rax)
jmp 0xcbac
movq 0xd46d(%rip), %rax      # 0x19fc8
cmpl $0x0, (%rax)
js 0xcbac
movq (%r12), %rcx
xorl %edi, %edi
leaq 0x6e82(%rip), %rsi      # 0x139ef
xorl %eax, %eax
callq *%rcx
jmp 0xcbac
movl (%r14), %edi
leal -0x1(%rdi), %r15d
callq 0x6700
leaq 0x5(%r14), %rsi
movq %rax, %rdi
movq %r15, %rdx
movq %rax, %rbp
callq 0x65b0
movb $0x0, (%rbp,%r15)
leaq 0x6eda(%rip), %r15      # 0x13a78
jmp 0xcbac
leaq 0x5(%r14), %rax
movq %rax, 0xa0(%rsp)
movl (%r14), %eax
addq $0x4, %r14
incl %r13d
addq %rax, %r14
jmp 0xcb02
movzwl 0x14(%rbx), %r13d
leaq 0xf0(%rsp), %rax
cmpq $0x64, %r13
jbe 0xcbe5
movl %r13d, %edi
shll $0x6, %edi
callq 0x6700
testq %rax, %rax
je 0xcdbe
movq %rax, %r15
xorl %r12d, %r12d
movq %rax, 0x98(%rsp)
movzwl %r13w, %eax
cmpq %rax, %r12
jae 0xcc25
movq %r14, %rdi
callq 0x63c0
movq %r14, %rdi
movq %r15, %rsi
callq 0x6120
movl (%r14), %eax
movzwl 0x14(%rbx), %r13d
incq %r12
addq $0x40, %r15
leaq 0x4(%r14,%rax), %r14
jmp 0xcbf3
movzbl 0x5(%rbx), %ecx
testl %ecx, %ecx
je 0xcc97
movq 0xd36c(%rip), %r12      # 0x19fa0
cmpl $0x2, %ecx
leaq 0x6e3a(%rip), %r15      # 0x13a78
jne 0xcc46
xorl %ecx, %ecx
movb $0x0, 0x5(%rbx)
movzbl %cl, %eax
movl %eax, 0x64(%rsp)
movb $0x1, %al
movq %rax, 0x80(%rsp)
movq 0x98(%rsp), %rdi
leaq 0xf0(%rsp), %rax
cmpq %rax, %rdi
je 0xcc71
callq 0x6070
movq %rbp, %rdi
callq 0x6070
movl %r14d, %eax
movl 0x90(%rsp), %ebp
subl %ebx, %eax
movq %r14, %rbx
movq 0xb8(%rsp), %r14
addl %eax, %ebp
jmp 0xcaac
testb $0x1, 0x3(%rbx)
movq 0xd2fe(%rip), %r12      # 0x19fa0
leaq 0x6dcf(%rip), %r15      # 0x13a78
jne 0xcc4d
testb $0x20, 0x8c(%rsp)
jne 0xccbb
testw %r13w, %r13w
je 0xcc57
movq 0x9a38(%rsp), %rdx
movl 0x6(%rbx), %ecx
movl 0xa(%rbx), %r8d
movq 0xa0(%rsp), %rsi
movq 0x98(%rsp), %r9
andl $0x0, 0x8(%rsp)
leaq 0xc0(%rsp), %rdi
movl %eax, (%rsp)
movq %rdx, 0x10(%rsp)
movq %rbp, %rdx
callq *0x9a30(%rsp)
movl $0x0, 0x64(%rsp)
jmp 0xcc57
movq 0x70(%rsp), %rdi
leaq 0x19f0(%rsp), %rcx
testq %rdi, %rdi
sete %al
cmpq %rcx, %rdi
sete %cl
orb %al, %cl
jne 0xcd2a
callq 0x6070
xorl %edi, %edi
movl 0x60(%rsp), %ebx
leaq 0x68(%rsp), %r14
pushq $0x8
popq %rdx
testb $0x1, 0x80(%rsp)
je 0xca08
movq 0x78(%rsp), %rdi
leaq 0x59f0(%rsp), %rax
cmpq %rax, %rdi
je 0xcd61
callq 0x6070
andq $0x0, 0x78(%rsp)
movq 0xb0(%rsp), %rbx
movl 0x60(%rsp), %esi
xorl %edx, %edx
movq %rbx, %rdi
callq 0x6110
movq %rbx, %rdi
callq 0x6640
movl 0x64(%rsp), %ecx
jmp 0xcd87
movl %ebp, %ecx
movl %ecx, %eax
addq $0x99f8, %rsp           # imm = 0x99F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xd226(%rip), %rax      # 0x19fc8
cmpl $0x0, (%rax)
js 0xcdc6
movq (%r12), %r8
leaq 0x6bf2(%rip), %rsi      # 0x139a4
pushq $0x16
popq %rdx
xorl %edi, %edi
xorl %eax, %eax
callq *%r8
jmp 0xcdc6
movq %rbp, %rdi
callq 0x6070
pushq $-0x1
popq %rcx
jmp 0xcd87
movq 0xd1f6(%rip), %rcx      # 0x19fc8
cmpl $0x0, (%rcx)
js 0xcdc6
movq (%r12), %r8
movl %eax, %edx
leaq 0x6afe(%rip), %rsi      # 0x138e2
xorl %edi, %edi
movl %ebx, %ecx
xorl %eax, %eax
callq *%r8
jmp 0xcdc6
movq 0xd1d2(%rip), %rax      # 0x19fc8
cmpl $0x0, (%rax)
js 0xcdc6
movq (%r12), %rcx
leaq 0x6afa(%rip), %rsi      # 0x13900
jmp 0xce1f
movq 0xd1b9(%rip), %rax      # 0x19fc8
cmpl $0x0, (%rax)
js 0xcdc6
movq (%r12), %rcx
leaq 0x6b1e(%rip), %rsi      # 0x1393d
xorl %edi, %edi
xorl %eax, %eax
callq *%rcx
jmp 0xcdc6
movq 0xd19a(%rip), %rcx      # 0x19fc8
cmpl $0x0, (%rcx)
js 0xce49
movq (%r12), %r8
movl %eax, %edx
leaq 0x6aa2(%rip), %rsi      # 0x138e2
xorl %edi, %edi
movl %ebx, %ecx
xorl %eax, %eax
callq *%r8
movq 0x70(%rsp), %rdi
leaq 0x19f0(%rsp), %rax
cmpq %rax, %rdi
jne 0xcdc1
jmp 0xcdc6