// File: /workspace/github_c_makefile_star2_O2/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
andq $0x0, (%rdx)
xorl %ebx, %ebx
movl %ecx, %r13d
movq %rdx, %r14
movq %rsi, %r12
movq %rdi, %r15
testq %rsi, %rsi
je 0xdc31
testb %r8b, %r8b
je 0xdced
movq %r12, %rdi
callq 0x20e0
leal -0x1(%rax), %ecx
cmpb $0xa, (%r12,%rcx)
je 0xdc31
incl %eax
cmpl $0x400, %eax            # imm = 0x400
movl %eax, %ebx
jbe 0xdc31
pushq $-0x1
popq %r12
jmp 0xde4a
movq %r14, -0x38(%rbp)
movl %ebx, %r14d
movl %r13d, -0x2c(%rbp)
movq %rsp, %r13
movq %rsp, -0x40(%rbp)
leaq 0x10(%r14), %rax
andq $-0x10, %rax
subq %rax, %r13
movq %r13, %rsp
testl %ebx, %ebx
je 0xdc6a
movq %r13, %rdi
movq %r12, %rsi
callq 0x2240
movw $0xa, -0x1(%r13,%r14)
jmp 0xdc6d
movq %r12, %r13
movq %r15, %rdi
callq 0xd4bf
cmpl $-0x1, %eax
je 0xdce4
leaq -0x440(%rbp), %r12
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
movl %eax, %r15d
movq %r12, %rdi
callq 0x2140
testq %r13, %r13
je 0xdd1d
movq %r13, %rdi
callq 0x20e0
leal 0x8(%rax), %ebx
movq %rax, %r14
cmpl $0x400, %ebx            # imm = 0x400
jb 0xdcc6
movq %rbx, %rdi
callq 0x22e0
movq %rax, %r12
testq %rax, %rax
je 0xddee
andw $0x0, 0x6(%r12)
movl %r14d, %edx
leaq 0x8(%r12), %rdi
movq %r13, %rsi
movl %edx, 0x2(%r12)
callq 0x21b0
jmp 0xdd20
pushq $-0x1
popq %r12
jmp 0xde46
xorl %eax, %eax
movabsq $0xc00100000000000, %rcx # imm = 0xC00100000000000
movzbl (%r12,%rax), %edx
testl %edx, %edx
je 0xdc13
cmpl $0x3b, %edx
ja 0xdd18
movl %edx, %edx
btq %rdx, %rcx
jae 0xdd18
movb $0xa, (%r12,%rax)
incq %rax
jmp 0xdcf9
pushq $0x8
popq %rbx
movq %r12, %rdi
movw $0x102, (%r12)          # imm = 0x102
callq 0xbd64
movl -0x2c(%rbp), %r14d
movl %r15d, %edi
movq %r12, %rsi
movq %rbx, %rdx
testl %r14d, %r14d
je 0xdd4d
xorl %ecx, %ecx
movl %r14d, %r8d
callq 0xcdd4
jmp 0xdd52
callq 0xd343
leaq -0x440(%rbp), %rcx
movl %eax, %ebx
cmpq %rcx, %r12
je 0xdd68
movq %r12, %rdi
callq 0x22c0
pushq $-0x1
popq %r12
testl %ebx, %ebx
jne 0xde33
leaq -0x440(%rbp), %rsi
pushq $0x8
popq %rdx
movl %r15d, %edi
testl %r14d, %r14d
je 0xdd92
xorl %ecx, %ecx
movl %r14d, %r8d
callq 0xc8d4
jmp 0xdd97
callq 0xd2b1
testl %eax, %eax
jne 0xde33
leaq -0x440(%rbp), %rbx
movq %rbx, %rdi
callq 0xbd64
movzwl 0x6(%rbx), %r13d
movl 0x2(%rbx), %eax
shlq $0x20, %r13
orq %rax, %r13
testq %r13, %r13
je 0xddf4
leaq 0x1(%r13), %rdi
callq 0x22e0
testq %rax, %rax
je 0xde33
movl -0x2c(%rbp), %r8d
movq %rax, %rbx
movl %r15d, %edi
movq %rax, %rsi
movq %r13, %rdx
testl %r8d, %r8d
je 0xddfe
xorl %ecx, %ecx
callq 0xc8d4
jmp 0xde03
pushq $-0x1
popq %r12
jmp 0xde33
movq -0x38(%rbp), %rax
andq $0x0, (%rax)
jmp 0xde30
callq 0xd2b1
movq -0x38(%rbp), %rdx
testl %eax, %eax
je 0xde15
movq %rbx, %rdi
callq 0x22c0
jmp 0xde33
movzwl -0x43a(%rbp), %ecx
movl -0x43e(%rbp), %eax
shlq $0x20, %rcx
orq %rax, %rcx
movb $0x0, (%rbx,%rcx)
movq %rbx, (%rdx)
xorl %r12d, %r12d
pushq $0x2
popq %rsi
movl %r15d, %edi
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