// File: /workspace/github_c_makefile_star2_O1/sharpyfox[P]citrusleaf-client/src/cl_info.c
// Function: citrusleaf_info_host_limit
int
citrusleaf_info_host_limit(struct sockaddr_in *sa_in, char *names, char **values, int timeout_ms, bool send_asis, uint64_t max_response_length)
{
	int rv = -1;
    int io_rv;
	*values = 0;
	
	// Deal with the incoming 'names' parameter
	// Translate interior ';'  in the passed-in names to \n
	uint32_t	slen = 0;
	if (names) {
		if (send_asis) {
			slen = strlen(names);
		} else {
			char *_t = names;
			while (*_t) { 
				slen++; 
				if ((*_t == ';') || (*_t == ':') || (*_t == ',')) *_t = '\n'; 
				_t++; 
			}
		}
	}
	
	// Sometimes people forget/cant add the trailing '\n'. Be nice and add it for them.
	// using a stack allocated variable so we dn't have to clean up, Pain in the ass syntactically
	// but a nice little thing
	if (names) {
		if (names[slen-1] == '\n') {
			slen = 0;
		} else { 
			slen++; if (slen > 1024) { return(-1); } 
		}
	}
	char names_with_term[slen+1];
	if (slen) { 
		strcpy(names_with_term, names);
		names_with_term[slen-1] = '\n';
		names_with_term[slen] = 0;
		names = names_with_term;
	}
		
	// Actually doing a non-blocking connect
	int fd = cf_socket_create_and_connect_nb(sa_in);
	if (fd == -1) {
		return -1;
	}

	cl_proto 	*req;
	uint8_t		buf[1024];
	uint		buf_sz;


	// Un-initialized buf can lead to junk lastshiptimes values. 
	// Initialize buf to 0.
	
	bzero(buf, 1024);
	
	if (names) {
		uint sz = strlen(names);
		buf_sz = sz + sizeof(cl_proto);
		if (buf_sz < 1024)
			req = (cl_proto *) buf;
		else
			req = (cl_proto *) malloc(buf_sz);
		if (req == NULL)	goto Done;

		req->sz = sz;
		memcpy(req->data,names,sz);
	}
	else {
		req = (cl_proto *) buf;
		req->sz = 0;
		buf_sz = sizeof(cl_proto);
		names = "";
	}
		
	req->version = CL_PROTO_VERSION;
	req->type = CL_PROTO_TYPE_INFO;
	cl_proto_swap(req);
	
    if (timeout_ms)
        io_rv = cf_socket_write_timeout(fd, (uint8_t *) req, buf_sz, 0, timeout_ms);
    else
        io_rv = cf_socket_write_forever(fd, (uint8_t *) req, buf_sz);
    
	if ((uint8_t *)req != buf)	free(req);
	if (io_rv != 0) {
#ifdef DEBUG        
		cf_debug("info returned error, rv %d errno %d bufsz %d", io_rv, errno, buf_sz);
#endif        
		goto Done;
	}
	
	cl_proto	*rsp = (cl_proto *)buf;
    if (timeout_ms) 
        io_rv = cf_socket_read_timeout(fd, buf, 8, 0, timeout_ms);
    else
        io_rv = cf_socket_read_forever(fd, buf, 8);
    
    if (0 != io_rv) {
#ifdef DEBUG        
		cf_debug("info socket read failed: rv %d errno %d", io_rv, errno);
#endif        
		goto Done;
	}
	cl_proto_swap(rsp);
	
	if (rsp->sz) {
		uint8_t *v_buf = malloc(rsp->sz + 1);
		if (!v_buf) goto Done;
        
        if (timeout_ms)
            io_rv = cf_socket_read_timeout(fd, v_buf, rsp->sz, 0, timeout_ms);
        else
            io_rv = cf_socket_read_forever(fd, v_buf, rsp->sz);
        
        if (io_rv != 0) {
            free(v_buf);
            goto Done;
		}
			
		v_buf[rsp->sz] = 0;
		*values = (char *) v_buf;
		
	}                                                                                               
	else {
		*values = 0;
	}
	rv = 0;

Done:	
	shutdown(fd, SHUT_RDWR);
	close(fd);
	return(rv);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x418, %rsp            # imm = 0x418
movl %ecx, %r14d
movq %rdx, %rbx
movq %rsi, %r13
movq %rdi, %r15
movq $0x0, (%rdx)
xorl %eax, %eax
testq %rsi, %rsi
je 0xef52
testb %r8b, %r8b
je 0xef1e
movq %r13, %rdi
callq 0x20e0
jmp 0xef52
movb (%r13), %dl
testb %dl, %dl
je 0xef52
xorl %eax, %eax
movabsq $0xc00100000000000, %rcx # imm = 0xC00100000000000
cmpb $0x3b, %dl
ja 0xef46
movzbl %dl, %edx
btq %rdx, %rcx
jae 0xef46
movb $0xa, (%r13,%rax)
movb 0x1(%r13,%rax), %dl
incq %rax
testb %dl, %dl
jne 0xef32
movl %r14d, -0x2c(%rbp)
testq %r13, %r13
je 0xef80
leal -0x1(%rax), %ecx
xorl %r14d, %r14d
cmpb $0xa, (%r13,%rcx)
je 0xef83
incl %eax
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
cmpl $0x400, %eax            # imm = 0x400
movl %eax, %r14d
jbe 0xef83
jmp 0xf193
movl %eax, %r14d
movq %rbx, -0x38(%rbp)
movq %rsp, -0x40(%rbp)
leal 0x1(%r14), %eax
movq %rsp, %rbx
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %rbx
movq %rbx, %rsp
testl %r14d, %r14d
je 0xefc1
movq %rbx, %rdi
movq %r13, %rsi
callq 0x2240
leal -0x1(%r14), %eax
movb $0xa, (%rbx,%rax)
movl %r14d, %eax
movb $0x0, (%rbx,%rax)
jmp 0xefc4
movq %r13, %rbx
movq %r15, %rdi
callq 0xe7c0
cmpl $-0x1, %eax
je 0xf03a
movl %eax, %r15d
leaq -0x440(%rbp), %r12
movl $0x400, %edx            # imm = 0x400
movq %r12, %rdi
xorl %esi, %esi
callq 0x2140
testq %rbx, %rbx
je 0xf045
movq %rbx, %rdi
callq 0x20e0
movq %rax, %r14
addl $0x8, %eax
cmpl $0x400, %eax            # imm = 0x400
movl %eax, %r13d
jb 0xf012
movq %r13, %rdi
callq 0x22e0
movq %rax, %r12
testq %r12, %r12
je 0xf0d2
movl %r14d, %edx
movl %edx, 0x2(%r12)
movw $0x0, 0x6(%r12)
leaq 0x8(%r12), %rdi
movq %rbx, %rsi
callq 0x21b0
jmp 0xf05c
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
jmp 0xf18f
movw $0x0, 0x6(%r12)
movl $0x0, 0x2(%r12)
movl $0x8, %r13d
movw $0x102, (%r12)          # imm = 0x102
movq %r12, %rdi
callq 0xccb0
movl -0x2c(%rbp), %r14d
movl %r15d, %edi
movq %r12, %rsi
movq %r13, %rdx
testl %r14d, %r14d
je 0xf089
xorl %ecx, %ecx
movl %r14d, %r8d
callq 0xdf14
jmp 0xf08e
callq 0xe615
movl %eax, %ebx
leaq -0x440(%rbp), %rax
cmpq %rax, %r12
je 0xf0a4
movq %r12, %rdi
callq 0x22c0
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
testl %ebx, %ebx
jne 0xf17a
leaq -0x440(%rbp), %rsi
movl $0x8, %edx
movl %r15d, %edi
testl %r14d, %r14d
je 0xf0dd
xorl %ecx, %ecx
movl %r14d, %r8d
callq 0xd8bc
jmp 0xf0e2
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
jmp 0xf17a
callq 0xe56c
testl %eax, %eax
jne 0xf17a
leaq -0x440(%rbp), %rbx
movq %rbx, %rdi
callq 0xccb0
movl 0x2(%rbx), %eax
movzwl 0x6(%rbx), %r13d
shlq $0x20, %r13
orq %rax, %r13
testq %r13, %r13
je 0xf138
leaq 0x1(%r13), %rdi
callq 0x22e0
testq %rax, %rax
je 0xf17a
movq %rax, %rbx
movl %r15d, %edi
movq %rax, %rsi
movq %r13, %rdx
testl %r14d, %r14d
je 0xf145
xorl %ecx, %ecx
movl %r14d, %r8d
callq 0xd8bc
jmp 0xf14a
movq -0x38(%rbp), %rax
movq $0x0, (%rax)
jmp 0xf177
callq 0xe56c
testl %eax, %eax
je 0xf158
movq %rbx, %rdi
callq 0x22c0
jmp 0xf17a
movl -0x43e(%rbp), %eax
movzwl -0x43a(%rbp), %ecx
shlq $0x20, %rcx
orq %rax, %rcx
movb $0x0, (%rbx,%rcx)
movq -0x38(%rbp), %rax
movq %rbx, (%rax)
xorl %r12d, %r12d
movl %r15d, %edi
movl $0x2, %esi
callq 0x2310
movl %r15d, %edi
callq 0x2550
movq -0x40(%rbp), %rsp
movl %r12d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq