// File: /workspace/github_c_makefile_star2_O1/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, 0x10(%rsp)
movq %rsi, %r14
movq %rdi, %rbx
movq 0x99e0(%rsp), %rdx
leaq 0x5990(%rsp), %rax
movq %rax, 0x20(%rsp)
movq $0x4000, 0x40(%rsp)     # imm = 0x4000
xorl %eax, %eax
testq %rdx, %rdx
je 0xd0f9
movzbl %r9b, %esi
cmpb $0x64, %sil
movl $0x64, %edi
cmovbl %esi, %edi
leaq 0x3e(%rsp), %r10
movb %dil, 0x1(%r10)
movb (%rdx), %sil
movb 0x4(%rdx), %dl
shlb $0x3, %sil
shlb $0x4, %dl
orb %sil, %dl
movb %dl, (%r10)
jmp 0xd0fc
xorl %r10d, %r10d
subq $0x8, %rsp
leaq 0x48(%rsp), %r11
leaq 0x28(%rsp), %r15
movl 0x18(%rsp), %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, (%rsp)     # imm = 0xFFFFFFFF
testl %eax, %eax
je 0xd14c
movl (%rsp), %eax
addq $0x9998, %rsp           # imm = 0x9998
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testq %r14, %r14
je 0xd171
movq %rbx, %rdi
movq %r14, %rsi
callq 0x65c0
testq %rax, %rax
je 0xd198
movq %rax, %r14
movq %rax, %rdi
callq 0x6560
movq %r14, %rdi
jmp 0xd19a
leaq 0x158(%rbx), %r14
movq %r14, %rdi
callq 0x6bc0
movq %rbx, %rdi
callq 0x63f0
movq %rax, %r15
movq %r14, %rdi
callq 0x6680
movq %r15, %rdi
jmp 0xd19a
xorl %edi, %edi
testq %rdi, %rdi
je 0xd137
movzbl 0x15(%rbx), %edx
movq %rdi, 0x48(%rsp)
xorl %esi, %esi
callq 0x6500
cmpl $-0x1, %eax
je 0xd137
movl %eax, %ebx
movq 0x20(%rsp), %rsi
movq 0x40(%rsp), %rdx
movl %eax, %edi
callq 0x6030
testl %eax, %eax
jne 0xd137
leaq 0x18(%rsp), %r14
movq $0x0, 0x28(%rsp)
movq $0x0, 0x8(%rsp)
movl %ebx, 0x4(%rsp)
movl $0x8, %edx
movl %ebx, %edi
movq %r14, %rsi
callq 0x6ae0
testl %eax, %eax
jne 0xd5c1
movq %r14, %rdi
callq 0x64e0
movzbl 0x18(%rsp), %edx
cmpl $0x2, %edx
jne 0xd5f9
movzbl 0x19(%rsp), %edx
cmpl $0x3, %edx
jne 0xd623
movl 0x1a(%rsp), %eax
movzwl 0x1e(%rsp), %r15d
shlq $0x20, %r15
orq %rax, %r15
testq %r15, %r15
je 0xd27e
cmpq $0x4001, %r15           # imm = 0x4001
leaq 0x1990(%rsp), %rsi
jb 0xd257
movq %r15, %rdi
callq 0x66c0
movq %rax, %rsi
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
testq %rsi, %rsi
je 0xd137
movl %ebx, %edi
movq %rsi, 0x8(%rsp)
movq %r15, %rdx
callq 0x6ae0
testl %eax, %eax
jne 0xd662
xorl %ebp, %ebp
movq 0x8(%rsp), %rbx
movl $0x0, (%rsp)
movq %r15, 0x50(%rsp)
movl %ebp, %r12d
cmpq %r12, %r15
jbe 0xd533
movq %r12, 0x58(%rsp)
movq %rbx, %rdi
callq 0x6a60
leaq 0x16(%rbx), %r13
movzbl (%rbx), %ecx
cmpl $0x16, %ecx
jne 0xd3b4
movl %ebp, 0x14(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movb $0x0, 0x80(%rsp)
cmpw $0x0, 0x12(%rbx)
je 0xd3e9
xorl %ebp, %ebp
movq %r13, %r12
xorl %r15d, %r15d
movq $0x0, 0x30(%rsp)
leaq 0x7752(%rip), %r14      # 0x14a48
movq %r12, %rdi
callq 0x6b80
movzbl 0x4(%r12), %eax
cmpq $0x4, %rax
ja 0xd396
movslq (%r14,%rax,4), %rax
addq %r14, %rax
jmpq *%rax
leaq 0x5(%r12), %rsi
movl (%r12), %edx
decl %edx
leaq 0x60(%rsp), %rdi
callq 0x6580
movl (%r12), %eax
decl %eax
movb $0x0, 0x60(%rsp,%rax)
jmp 0xd396
movq 0xdc88(%rip), %rax      # 0x1afc8
cmpl $0x0, (%rax)
js 0xd396
movq 0xdc54(%rip), %rax      # 0x1afa0
movq (%rax), %rcx
xorl %edi, %edi
leaq 0x7667(%rip), %rsi      # 0x149bf
xorl %eax, %eax
callq *%rcx
jmp 0xd396
movl (%r12), %edi
leal -0x1(%rdi), %r14d
callq 0x66c0
movq %rax, %r15
leaq 0x5(%r12), %rsi
movq %rax, %rdi
movq %r14, %rdx
callq 0x6580
movb $0x0, (%r15,%r14)
leaq 0x76be(%rip), %r14      # 0x14a48
jmp 0xd396
leaq 0x5(%r12), %rax
movq %rax, 0x30(%rsp)
movl (%r12), %r13d
addq $0x4, %r12
addq %r12, %r13
incl %ebp
movzwl 0x12(%rbx), %eax
cmpl %eax, %ebp
movq %r13, %r12
jb 0xd2f6
jmp 0xd3f5
movq 0xdc0d(%rip), %rax      # 0x1afc8
cmpl $0x0, (%rax)
js 0xd4b5
movq 0xdbd5(%rip), %rax      # 0x1afa0
movq (%rax), %r8
xorl %r12d, %r12d
movl $0x16, %edx
xorl %edi, %edi
leaq 0x7595(%rip), %rsi      # 0x14974
xorl %eax, %eax
callq *%r8
jmp 0xd4d1
movq $0x0, 0x30(%rsp)
xorl %r15d, %r15d
movzwl 0x14(%rbx), %edi
cmpq $0x64, %rdi
leaq 0x90(%rsp), %r14
jbe 0xd412
shll $0x6, %edi
callq 0x66c0
movq %rax, %r14
testq %r14, %r14
setne %r12b
movl 0x14(%rsp), %ebp
je 0xd4ba
movzwl 0x14(%rbx), %eax
testw %ax, %ax
je 0xd46b
movl %r12d, 0x38(%rsp)
movq %r14, %r12
xorl %ebp, %ebp
movq %r13, %rdi
callq 0x63a0
movq %r13, %rdi
movq %r12, %rsi
callq 0x6120
movl (%r13), %eax
leaq 0x4(%r13,%rax), %r13
incq %rbp
movzwl 0x14(%rbx), %eax
addq $0x40, %r12
cmpq %rax, %rbp
jb 0xd436
movl 0x14(%rsp), %ebp
movl 0x38(%rsp), %r12d
movzbl 0x5(%rbx), %ecx
testl %ecx, %ecx
je 0xd4e2
cmpl $0x2, %ecx
jne 0xd47c
movb $0x0, 0x5(%rbx)
movzbl 0x5(%rbx), %eax
movl %eax, (%rsp)
movb $0x1, %al
movq %rax, 0x28(%rsp)
leaq 0x90(%rsp), %rax
cmpq %rax, %r14
je 0xd49f
movq %r14, %rdi
callq 0x6070
testq %r15, %r15
je 0xd4ac
movq %r15, %rdi
callq 0x6070
movl %r13d, %eax
subl %ebx, %eax
addl %eax, %ebp
jmp 0xd4c7
xorl %r12d, %r12d
jmp 0xd4d1
testq %r15, %r15
je 0xd4c7
movq %r15, %rdi
callq 0x6070
leaq 0x18(%rsp), %r14
movq 0x50(%rsp), %r15
testb %r12b, %r12b
movq %r13, %rbx
jne 0xd291
jmp 0xd564
testb $0x1, 0x3(%rbx)
jne 0xd483
testb $0x20, 0x10(%rsp)
jne 0xd4f4
testw %ax, %ax
je 0xd48a
movzwl %ax, %eax
movl 0x6(%rbx), %ecx
movl 0xa(%rbx), %r8d
subq $0x8, %rsp
leaq 0x68(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq %r15, %rdx
movq %r14, %r9
pushq 0x99e0(%rsp)
pushq $0x0
pushq %rax
callq *0x99f0(%rsp)
addq $0x20, %rsp
movl $0x0, (%rsp)
jmp 0xd48a
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
sete %al
leaq 0x1990(%rsp), %rcx
cmpq %rcx, %rdi
sete %cl
orb %al, %cl
jne 0xd55e
callq 0x6070
movq $0x0, 0x8(%rsp)
movl 0x4(%rsp), %ebx
jmp 0xd56d
movl 0x4(%rsp), %ebx
movq 0x58(%rsp), %r12
cmpq %r12, %r15
ja 0xd656
testb $0x1, 0x28(%rsp)
je 0xd1ea
movq 0x20(%rsp), %rdi
leaq 0x5990(%rsp), %rax
cmpq %rax, %rdi
je 0xd5a1
callq 0x6070
movq $0x0, 0x20(%rsp)
movq 0x48(%rsp), %rbx
movq %rbx, %rdi
movl 0x4(%rsp), %esi
xorl %edx, %edx
callq 0x6110
movq %rbx, %rdi
callq 0x6610
jmp 0xd137
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
movq 0xd9f9(%rip), %rcx      # 0x1afc8
cmpl $0x0, (%rcx)
js 0xd137
movq 0xd9c1(%rip), %rcx      # 0x1afa0
movq (%rcx), %r8
leaq 0x72c9(%rip), %rsi      # 0x148b2
xorl %edi, %edi
movl %eax, %edx
movl %ebx, %ecx
xorl %eax, %eax
callq *%r8
jmp 0xd137
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
movq 0xd9c1(%rip), %rax      # 0x1afc8
cmpl $0x0, (%rax)
js 0xd137
movq 0xd989(%rip), %rax      # 0x1afa0
movq (%rax), %rcx
leaq 0x72af(%rip), %rsi      # 0x148d0
jmp 0xd64b
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
movq 0xd997(%rip), %rax      # 0x1afc8
cmpl $0x0, (%rax)
js 0xd137
movq 0xd95f(%rip), %rax      # 0x1afa0
movq (%rax), %rcx
leaq 0x72c2(%rip), %rsi      # 0x1490d
xorl %edi, %edi
xorl %eax, %eax
callq *%rcx
jmp 0xd137
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
jmp 0xd137
movq 0xd95f(%rip), %rcx      # 0x1afc8
cmpl $0x0, (%rcx)
js 0xd68a
movq 0xd92b(%rip), %rcx      # 0x1afa0
movq (%rcx), %r8
leaq 0x7233(%rip), %rsi      # 0x148b2
xorl %edi, %edi
movl %eax, %edx
movl %ebx, %ecx
xorl %eax, %eax
callq *%r8
leaq 0x1990(%rsp), %rax
movq 0x8(%rsp), %rdi
cmpq %rax, %rdi
je 0xd137
callq 0x6070
jmp 0xd137