// File: /workspace/github_c_makefile_star2_O3/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
movq %rsi, %r12
movq %rdi, %r15
testq %rsi, %rsi
movl %ecx, -0x34(%rbp)
movq %rdx, -0x40(%rbp)
movq $0x0, (%rdx)
je 0xeac9
testb %r8b, %r8b
je 0xea88
movq %r12, %rdi
callq 0x20e0
movq %rax, %rbx
jmp 0xeabb
movb (%r12), %cl
xorl %ebx, %ebx
testb %cl, %cl
je 0xeabb
movabsq $0xc00100000000000, %rax # imm = 0xC00100000000000
cmpb $0x3b, %cl
ja 0xeaaf
movzbl %cl, %ecx
btq %rcx, %rax
jae 0xeaaf
movb $0xa, (%r12,%rbx)
movb 0x1(%r12,%rbx), %cl
incq %rbx
testb %cl, %cl
jne 0xea9c
leal -0x1(%rbx), %eax
cmpb $0xa, (%r12,%rax)
jne 0xeb50
movq %rsp, %r13
movq %r15, %rdi
callq 0xe336
cmpl $-0x1, %eax
je 0xeb45
movq %r13, -0x30(%rbp)
leaq -0x440(%rbp), %r13
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
movl %eax, %r15d
movq %r13, %rdi
callq 0x2140
testq %r12, %r12
je 0xebaa
movq %r12, %rdi
callq 0x20e0
leal 0x8(%rax), %ebx
movq %rax, %r14
cmpl $0x400, %ebx            # imm = 0x400
jb 0xeb29
movq %rbx, %rdi
callq 0x22e0
movq %rax, %r13
testq %rax, %rax
je 0xec8b
movl %r14d, %edx
leaq 0x8(%r13), %rdi
movq %r12, %rsi
movw $0x0, 0x6(%r13)
movl %edx, 0x2(%r13)
callq 0x21b0
jmp 0xebaf
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
jmp 0xeced
leal 0x1(%rbx), %r13d
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
cmpl $0x400, %r13d           # imm = 0x400
ja 0xecf0
leal 0x2(%rbx), %eax
movq %rsp, %r14
movq %rsp, -0x30(%rbp)
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %r14
movq %r14, %rsp
testl %r13d, %r13d
je 0xeba1
movq %r14, %rdi
movq %r12, %rsi
callq 0x2240
movl %ebx, %eax
movq %r14, %r12
movb $0xa, (%r14,%rax)
movl %r13d, %eax
movb $0x0, (%r14,%rax)
movq -0x30(%rbp), %r13
jmp 0xeacc
movl $0x8, %ebx
movq %r13, %rdi
movw $0x102, (%r13)          # imm = 0x102
callq 0xc8a8
movl -0x34(%rbp), %r12d
movl %r15d, %edi
movq %r13, %rsi
movq %rbx, %rdx
testl %r12d, %r12d
je 0xebdc
xorl %ecx, %ecx
movl %r12d, %r8d
callq 0xdae9
jmp 0xebe1
callq 0xe1a4
leaq -0x440(%rbp), %rcx
movl %eax, %ebx
cmpq %rcx, %r13
je 0xebf7
movq %r13, %rdi
callq 0x22c0
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
testl %ebx, %ebx
je 0xec0a
movq -0x30(%rbp), %r13
jmp 0xecd8
movq -0x30(%rbp), %r13
testl %r12d, %r12d
leaq -0x440(%rbp), %rsi
movl $0x8, %edx
movl %r15d, %edi
je 0xec2e
xorl %ecx, %ecx
movl %r12d, %r8d
callq 0xd4c5
jmp 0xec33
callq 0xe10d
testl %eax, %eax
jne 0xecd8
leaq -0x440(%rbp), %rbx
movq %rbx, %rdi
callq 0xc8a8
movzwl 0x6(%rbx), %r12d
movl 0x2(%rbx), %eax
shlq $0x20, %r12
orq %rax, %r12
testq %r12, %r12
je 0xec96
leaq 0x1(%r12), %rdi
callq 0x22e0
testq %rax, %rax
je 0xecd8
movl -0x34(%rbp), %r8d
movq %rax, %rbx
movl %r15d, %edi
movq %rax, %rsi
movq %r12, %rdx
testl %r8d, %r8d
je 0xeca3
xorl %ecx, %ecx
callq 0xd4c5
jmp 0xeca8
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
jmp 0xec01
movq -0x40(%rbp), %rax
movq $0x0, (%rax)
jmp 0xecd5
callq 0xe10d
testl %eax, %eax
je 0xecb6
movq %rbx, %rdi
callq 0x22c0
jmp 0xecd8
movzwl -0x43a(%rbp), %ecx
movl -0x43e(%rbp), %eax
shlq $0x20, %rcx
orq %rax, %rcx
movb $0x0, (%rbx,%rcx)
movq -0x40(%rbp), %rcx
movq %rbx, (%rcx)
xorl %r14d, %r14d
movl %r15d, %edi
movl $0x2, %esi
callq 0x2310
movl %r15d, %edi
callq 0x2550
movq %r13, %rsp
movl %r14d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq