// File: /workspace/github_c_makefile_star2_O3/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 $0x9998, %rsp           # imm = 0x9998
movl %edx, %ebp
movq 0x99e0(%rsp), %rdx
leaq 0x5990(%rsp), %r12
xorl %eax, %eax
movq %rsi, %r14
movq %rdi, %rbx
movq $0x4000, 0x38(%rsp)     # imm = 0x4000
movq %r12, 0x10(%rsp)
testq %rdx, %rdx
je 0xce3b
movzbl %r9b, %esi
movl $0x64, %edi
leaq 0x2e(%rsp), %r10
cmpb $0x64, %sil
cmovbl %esi, %edi
movb %dil, 0x1(%r10)
movb (%rdx), %sil
movb 0x4(%rdx), %dl
shlb $0x3, %sil
shlb $0x4, %dl
orb %sil, %dl
movb %dl, (%r10)
jmp 0xce3e
xorl %r10d, %r10d
subq $0x8, %rsp
leaq 0x18(%rsp), %r15
leaq 0x40(%rsp), %r11
movl %ebp, %edi
xorl %esi, %esi
xorl %edx, %edx
xorl %r9d, %r9d
pushq %r10
pushq %rax
pushq %rax
pushq %rax
pushq %r11
pushq %r15
pushq %rax
pushq %rax
pushq %rax
pushq %rax
pushq %rax
callq 0x6380
addq $0x60, %rsp
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
testl %eax, %eax
je 0xce8b
movl %r15d, %eax
addq $0x9998, %rsp           # imm = 0x9998
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testq %r14, %r14
je 0xcead
movq %rbx, %rdi
movq %r14, %rsi
callq 0x65c0
testq %rax, %rax
je 0xce76
movq %rax, %rdi
movq %rax, %r13
callq 0x6560
jmp 0xced4
leaq 0x158(%rbx), %r14
movq %r14, %rdi
callq 0x6bc0
movq %rbx, %rdi
callq 0x63f0
movq %r14, %rdi
movq %rax, %r13
callq 0x6680
testq %r13, %r13
je 0xce76
movzbl 0x15(%rbx), %edx
movq %r13, %rdi
xorl %esi, %esi
callq 0x6500
cmpl $-0x1, %eax
je 0xce76
movq 0x10(%rsp), %rsi
movq 0x38(%rsp), %rdx
movl %eax, %edi
movl %eax, %ebx
callq 0x6030
testl %eax, %eax
jne 0xce76
movl %ebx, (%rsp)
leaq 0x8(%rsp), %r14
leaq 0x7b57(%rip), %rbx      # 0x14a68
xorl %edi, %edi
movq $0x0, 0x18(%rsp)
movl %ebp, 0x28(%rsp)
movq %r13, 0x40(%rsp)
movq %rdi, 0x30(%rsp)
movl (%rsp), %edi
movl $0x8, %edx
movq %r14, %rsi
callq 0x6ae0
testl %eax, %eax
jne 0xd2d0
movq %r14, %rdi
callq 0x64e0
movzbl 0x8(%rsp), %edx
cmpl $0x2, %edx
jne 0xd308
movzbl 0x9(%rsp), %edx
cmpl $0x3, %edx
jne 0xd32e
movzwl 0xe(%rsp), %r15d
movl 0xa(%rsp), %eax
shlq $0x20, %r15
orq %rax, %r15
testq %r15, %r15
je 0xd210
cmpq $0x4001, %r15           # imm = 0x4001
leaq 0x1990(%rsp), %rax
jb 0xcfa2
movq %r15, %rdi
callq 0x66c0
testq %rax, %rax
je 0xd2c5
movl (%rsp), %edi
movq %rax, %rsi
movq %r15, %rdx
movq %rax, %r14
callq 0x6ae0
testl %eax, %eax
jne 0xd360
xorl %ebp, %ebp
movq %r14, %r13
movl $0x0, 0x4(%rsp)
movq %r14, 0x30(%rsp)
movq %r15, 0x48(%rsp)
movq %r13, %rdi
callq 0x6a60
movzbl (%r13), %ecx
cmpl $0x16, %ecx
jne 0xd28d
cmpw $0x0, 0x12(%r13)
xorps %xmm0, %xmm0
leaq 0x16(%r13), %r14
movq %rbp, 0x58(%rsp)
movb $0x0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
je 0xd0cf
xorl %ebp, %ebp
movq %r14, %r15
xorl %r12d, %r12d
movq $0x0, 0x20(%rsp)
movq %r15, %rdi
callq 0x6b80
movzbl 0x4(%r15), %eax
cmpq $0x4, %rax
ja 0xd0b1
movslq (%rbx,%rax,4), %rax
addq %rbx, %rax
jmpq *%rax
movl (%r15), %edx
leaq 0x5(%r15), %rsi
leaq 0x60(%rsp), %rdi
decl %edx
callq 0x6580
movl (%r15), %eax
decl %eax
movb $0x0, 0x60(%rsp,%rax)
jmp 0xd0b1
movq 0xdf63(%rip), %rax      # 0x1afc8
cmpl $0x0, (%rax)
js 0xd0b1
movq 0xdf2f(%rip), %rax      # 0x1afa0
xorl %edi, %edi
leaq 0x7965(%rip), %rsi      # 0x149df
movq (%rax), %rcx
xorl %eax, %eax
callq *%rcx
jmp 0xd0b1
movl (%r15), %edi
leal -0x1(%rdi), %r14d
callq 0x66c0
leaq 0x5(%r15), %rsi
movq %rax, %rdi
movq %r14, %rdx
movq %rax, %r12
callq 0x6580
movb $0x0, (%r12,%r14)
jmp 0xd0b1
leaq 0x5(%r15), %rax
movq %rax, 0x20(%rsp)
movl (%r15), %r14d
movzwl 0x12(%r13), %eax
addq $0x4, %r15
incl %ebp
addq %r15, %r14
cmpl %eax, %ebp
movq %r14, %r15
jb 0xd023
jmp 0xd0db
xorl %r12d, %r12d
movq $0x0, 0x20(%rsp)
movzwl 0x14(%r13), %edi
cmpq $0x64, %rdi
jbe 0xd0f8
shll $0x6, %edi
callq 0x66c0
testq %rax, %rax
jne 0xd109
jmp 0xd2b8
testq %rdi, %rdi
leaq 0x90(%rsp), %rax
je 0xd1ff
movq %rax, %r15
xorl %ebp, %ebp
movq %rax, 0x50(%rsp)
movq %r14, %rdi
callq 0x63a0
movq %r14, %rdi
movq %r15, %rsi
callq 0x6120
movl (%r14), %eax
incq %rbp
addq $0x40, %r15
leaq 0x4(%r14,%rax), %r14
movzwl 0x14(%r13), %eax
cmpq %rax, %rbp
jb 0xd113
movq 0x50(%rsp), %rbp
testl %eax, %eax
sete %cl
movzbl 0x5(%r13), %edx
movq 0x48(%rsp), %r15
testl %edx, %edx
je 0xd1ae
cmpl $0x2, %edx
jne 0xd163
xorl %edx, %edx
movb $0x0, 0x5(%r13)
movzbl %dl, %eax
movl %eax, 0x4(%rsp)
movb $0x1, %al
movq %rax, 0x18(%rsp)
leaq 0x90(%rsp), %rax
cmpq %rax, %rbp
je 0xd186
movq %rbp, %rdi
callq 0x6070
movq 0x58(%rsp), %rbp
testq %r12, %r12
je 0xd198
movq %r12, %rdi
callq 0x6070
movl %r14d, %eax
subl %r13d, %eax
movq %r14, %r13
addl %eax, %ebp
cmpq %rbp, %r15
ja 0xcfd2
jmp 0xd215
testb $0x1, 0x3(%r13)
jne 0xd16a
testb $0x20, 0x28(%rsp)
sete %dl
testb %cl, %dl
jne 0xd171
movl 0x6(%r13), %ecx
movl 0xa(%r13), %r8d
subq $0x8, %rsp
movq 0x28(%rsp), %rsi
leaq 0x68(%rsp), %rdi
movq %r12, %rdx
movq %rbp, %r9
pushq 0x99e0(%rsp)
pushq $0x0
pushq %rax
callq *0x99f0(%rsp)
addq $0x20, %rsp
movl $0x0, 0x4(%rsp)
jmp 0xd171
xorl %eax, %eax
movb $0x1, %cl
leaq 0x90(%rsp), %rbp
jmp 0xd149
xorl %r15d, %r15d
jmp 0xd22c
movl 0x4(%rsp), %r15d
movq 0x40(%rsp), %r13
leaq 0x5990(%rsp), %r12
leaq 0x8(%rsp), %r14
movq 0x30(%rsp), %rdi
leaq 0x1990(%rsp), %rcx
testq %rdi, %rdi
sete %al
cmpq %rcx, %rdi
sete %cl
orb %al, %cl
jne 0xd250
callq 0x6070
xorl %edi, %edi
testb $0x1, 0x18(%rsp)
je 0xcf25
movq 0x10(%rsp), %rdi
cmpq %r12, %rdi
je 0xd273
callq 0x6070
movq $0x0, 0x10(%rsp)
movl (%rsp), %esi
movq %r13, %rdi
xorl %edx, %edx
callq 0x6110
movq %r13, %rdi
callq 0x6610
jmp 0xce76
movq 0xdd34(%rip), %rax      # 0x1afc8
cmpl $0x0, (%rax)
js 0xd2c5
movq 0xdd00(%rip), %rax      # 0x1afa0
leaq 0x76ed(%rip), %rsi      # 0x14994
movl $0x16, %edx
xorl %edi, %edi
movq (%rax), %r8
xorl %eax, %eax
callq *%r8
jmp 0xd2c5
testq %r12, %r12
je 0xd2c5
movq %r12, %rdi
callq 0x6070
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
jmp 0xce76
movq 0xdcf1(%rip), %rcx      # 0x1afc8
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
cmpl $0x0, (%rcx)
js 0xce76
movq 0xdcb3(%rip), %rcx      # 0x1afa0
movl %eax, %edx
leaq 0x75dc(%rip), %rsi      # 0x148d2
xorl %edi, %edi
xorl %eax, %eax
movq (%rcx), %r8
movl (%rsp), %ecx
callq *%r8
jmp 0xce76
movq 0xdcb9(%rip), %rax      # 0x1afc8
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
cmpl $0x0, (%rax)
js 0xce76
movq 0xdc7b(%rip), %rax      # 0x1afa0
leaq 0x75c4(%rip), %rsi      # 0x148f0
jmp 0xd352
movq 0xdc93(%rip), %rax      # 0x1afc8
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
cmpl $0x0, (%rax)
js 0xce76
movq 0xdc55(%rip), %rax      # 0x1afa0
leaq 0x75db(%rip), %rsi      # 0x1492d
movq (%rax), %rcx
xorl %edi, %edi
xorl %eax, %eax
callq *%rcx
jmp 0xce76
movq 0xdc61(%rip), %rcx      # 0x1afc8
movq %r14, %rbx
cmpl $0x0, (%rcx)
js 0xd38c
movq 0xdc2a(%rip), %rcx      # 0x1afa0
movl %eax, %edx
leaq 0x7553(%rip), %rsi      # 0x148d2
xorl %edi, %edi
xorl %eax, %eax
movq (%rcx), %r8
movl (%rsp), %ecx
callq *%r8
leaq 0x1990(%rsp), %rax
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
cmpq %rax, %rbx
je 0xce76
movq %rbx, %rdi
callq 0x6070
jmp 0xce76