// File: /workspace/github_c_makefile_star2_O0/sharpyfox[P]citrusleaf-client/src/cl_lookup.c
// Function: cl_lookup
int
cl_lookup(cl_cluster *asc, char *hostname, short port, cf_vector *sockaddr_in_v)
{
	// do the gethostbyname to find the IP address
	size_t hstbuflen = 1024;
	uint8_t	stack_hstbuf[hstbuflen];
	void *tmphstbuf = stack_hstbuf;
	int rv, herr, addrmapsz;
	struct hostent hostbuf, *hp;
	cl_addrmap *map;
	int retry = 0;
	//Find if there is an alternate address that should be used for this hostname.
	if (asc && (asc->host_addr_map_v.len > 0)) {
		addrmapsz = asc->host_addr_map_v.len;
		for (int i=0; i<addrmapsz; i++) {
			map = cf_vector_pointer_get(&asc->host_addr_map_v, i);
			if (map && strcmp(map->orig, hostname) == 0) {
				//found a mapping for this address. Use the alternate one.
				cf_debug("Using %s instead of %s", map->alt, hostname);
				hostname = map->alt;
				break;
			}
		}
	}

	do {
#ifdef OSX // on OSX, gethostbyname is thread safe and there is no '_r' version 
		hp = gethostbyname2(hostname, AF_INET);
		rv = 0;
		if(hp == NULL){
			herr = h_errno; // I'm hoping this is thread-safe too, in the Mac world...
		}
#else
		rv = gethostbyname2_r(hostname, AF_INET, &hostbuf, tmphstbuf, hstbuflen,
				&hp, &herr);
#endif
		/* TRY_AGAIN for a maximun of 3 times, after which throw an error */
		if(retry > 2) {
			cf_error("gethostbyname of %s - maxmimum retries failed", hostname);
			retry = 0;
			return -1;
		}
		if (hp == NULL) {
			hostname = hostname ? hostname : "NONAME";
			switch(herr) {
				case HOST_NOT_FOUND:
					cf_error("gethostbyname says no host at %s", hostname);
					break;
				case NO_ADDRESS:
					cf_error("gethostbyname of %s says invalid address (errno %d)", hostname, herr);
					break;
				case NO_RECOVERY:
					cf_error("gethostbyname of %s says form error (errno %d)", hostname, herr);
					break;
				case TRY_AGAIN:
					cf_error("gethostbyname of %s returned TRY_AGAIN, try again (rv=%d)", hostname, rv);
					retry++;
					continue;
				default:
					cf_error("gethostbyname of %s returned an unknown error (errno %d)", hostname, herr);
					break;
			}
			if (tmphstbuf != stack_hstbuf)		free(tmphstbuf);
			return(-1);
		}
		else if (rv != 0) {
			if (rv == ERANGE) {
				hstbuflen *= 2;
				if (tmphstbuf == stack_hstbuf)
					tmphstbuf = malloc(hstbuflen);
				else
					tmphstbuf = realloc (tmphstbuf, hstbuflen);
				if (!tmphstbuf) {
					cf_error("malloc fail");
					return(-1);
				}
			}
			else if (rv == EAGAIN || herr == TRY_AGAIN) {
				cf_error("gethostbyname returned EAGAIN, try again");
				retry++;
			}
			else if (rv == ETIMEDOUT) {
				cf_error("gethostbyname for %s timed out", hostname ? (hostname): "NONAME");
				if (tmphstbuf != stack_hstbuf)		free(tmphstbuf);
				return(-1);
			}
			else {
				cf_error("gethostbyname returned an unknown error %d %d (errno %d)",rv,herr, errno);
				if (tmphstbuf != stack_hstbuf)		free(tmphstbuf);
				return(-1);
			}
		}
	} while ((rv != 0) || (hp == NULL));

#ifdef DEBUG
	cf_debug("host lookup: %s canonical: %s addrtype %d length: %d",
		hostname, hp->h_name, hp->h_addrtype, hp->h_length);

	for (int i=0;hp->h_aliases[i];i++) {
		cf_debug("  alias %d: %s",i, hp->h_aliases[i]);
	}
	for (int i=0;hp->h_addr_list[i];i++) {
		// todo: print something about the actual address
		cf_debug("  address %d: %x",i,*(uint32_t *) hp->h_addr_list[i]);
	}
#endif

	if (hp->h_addrtype != AF_INET) {
		cf_error("unknown address type %d", hp->h_addrtype);
		if (tmphstbuf != stack_hstbuf)		free(tmphstbuf);
		return(-1);
	}
	
	// sockaddr_in_v is passed as NULL from caller which needs
	// to only check if lookup succeeds. If reach here it is 
	// a successful lookup. 
	if (sockaddr_in_v == NULL) {
		goto ret_success;
	}
	
	// Move into vector
	for (int i=0;hp->h_addr_list[i];i++) {
		struct sockaddr_in 	addr;
		memset(&addr,0,sizeof(addr));
		addr.sin_family = hp->h_addrtype;
		addr.sin_addr.s_addr = *(uint32_t *) hp->h_addr_list[i];
		addr.sin_port = htons(port);
		
		cf_vector_append_unique(sockaddr_in_v, &addr);
	}

ret_success:
	if (tmphstbuf != stack_hstbuf)		free(tmphstbuf);
	
	return(0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movw %dx, %ax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movw %ax, -0x1a(%rbp)
movq %rcx, -0x28(%rbp)
movq $0x400, -0x30(%rbp)     # imm = 0x400
movq -0x30(%rbp), %rcx
movq %rsp, %rax
movq %rax, -0x38(%rbp)
movq %rcx, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rax
subq %rdx, %rax
movq %rax, -0xb0(%rbp)
movq %rax, %rsp
movq %rcx, -0x40(%rbp)
movq %rax, -0x48(%rbp)
movl $0x0, -0x8c(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0xd5a4
movq -0x10(%rbp), %rax
cmpl $0x0, 0xb4(%rax)
jbe 0xd5a4
movq -0x10(%rbp), %rax
movl 0xb4(%rax), %eax
movl %eax, -0x54(%rbp)
movl $0x0, -0x90(%rbp)
movl -0x90(%rbp), %eax
cmpl -0x54(%rbp), %eax
jge 0xd5a2
movq -0x10(%rbp), %rdi
addq $0xa8, %rdi
movl -0x90(%rbp), %esi
callq 0xdb30
movq %rax, -0x88(%rbp)
cmpq $0x0, -0x88(%rbp)
je 0xd58c
movq -0x88(%rbp), %rax
movq (%rax), %rdi
movq -0x18(%rbp), %rsi
callq 0x2380
cmpl $0x0, %eax
jne 0xd58c
leaq 0x10d92(%rip), %rax     # 0x1e2d8
movl (%rax), %ecx
movl $0x3, %eax
cmpl %ecx, %eax
jg 0xd57b
leaq 0x10d88(%rip), %rax     # 0x1e2e0
movq (%rax), %r8
movq -0x88(%rbp), %rax
movq 0x8(%rax), %rdx
movq -0x18(%rbp), %rcx
movl $0x3, %edi
leaq 0xb702(%rip), %rsi      # 0x18c78
movb $0x0, %al
callq *%r8
movq -0x88(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x18(%rbp)
jmp 0xd5a2
jmp 0xd58e
movl -0x90(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x90(%rbp)
jmp 0xd4f1
jmp 0xd5a4
jmp 0xd5a6
movq -0x18(%rbp), %rdi
movq -0x48(%rbp), %rcx
movq -0x30(%rbp), %r8
movl $0x2, %esi
leaq -0x78(%rbp), %rdx
leaq -0x80(%rbp), %r9
leaq -0x50(%rbp), %rax
subq $0x10, %rsp
movq %rax, (%rsp)
callq 0x2410
addq $0x10, %rsp
movl %eax, -0x4c(%rbp)
cmpl $0x2, -0x8c(%rbp)
jle 0xd62a
leaq 0x10cf1(%rip), %rax     # 0x1e2d8
movl (%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jg 0xd60a
leaq 0x10cea(%rip), %rax     # 0x1e2e0
movq (%rax), %rcx
movq -0x18(%rbp), %rdx
xorl %edi, %edi
leaq 0xb689(%rip), %rsi      # 0x18c8f
movb $0x0, %al
callq *%rcx
movl $0x0, -0x8c(%rbp)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x94(%rbp)
jmp 0xdb12
cmpq $0x0, -0x80(%rbp)
jne 0xd7c2
cmpq $0x0, -0x18(%rbp)
je 0xd649
movq -0x18(%rbp), %rax
movq %rax, -0xb8(%rbp)
jmp 0xd659
leaq 0xb66d(%rip), %rax      # 0x18cbd
movq %rax, -0xb8(%rbp)
jmp 0xd659
movq -0xb8(%rbp), %rax
movq %rax, -0x18(%rbp)
movl -0x50(%rbp), %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0xc0(%rbp)
subl $0x3, %eax
ja 0xd766
movq -0xc0(%rbp), %rax
leaq 0xb5df(%rip), %rcx      # 0x18c68
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x10c3f(%rip), %rax     # 0x1e2d8
movl (%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jg 0xd6bc
leaq 0x10c38(%rip), %rax     # 0x1e2e0
movq (%rax), %rcx
movq -0x18(%rbp), %rdx
xorl %edi, %edi
leaq 0xb60c(%rip), %rsi      # 0x18cc4
movb $0x0, %al
callq *%rcx
jmp 0xd796
leaq 0x10c10(%rip), %rax     # 0x1e2d8
movl (%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jg 0xd6ef
leaq 0x10c09(%rip), %rax     # 0x1e2e0
movq (%rax), %r8
movq -0x18(%rbp), %rdx
movl -0x50(%rbp), %ecx
xorl %edi, %edi
leaq 0xb5fb(%rip), %rsi      # 0x18ce5
movb $0x0, %al
callq *%r8
jmp 0xd796
leaq 0x10bdd(%rip), %rax     # 0x1e2d8
movl (%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jg 0xd722
leaq 0x10bd6(%rip), %rax     # 0x1e2e0
movq (%rax), %r8
movq -0x18(%rbp), %rdx
movl -0x50(%rbp), %ecx
xorl %edi, %edi
leaq 0xb5fc(%rip), %rsi      # 0x18d19
movb $0x0, %al
callq *%r8
jmp 0xd796
leaq 0x10bad(%rip), %rax     # 0x1e2d8
movl (%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jg 0xd752
leaq 0x10ba6(%rip), %rax     # 0x1e2e0
movq (%rax), %r8
movq -0x18(%rbp), %rdx
movl -0x4c(%rbp), %ecx
xorl %edi, %edi
leaq 0xb5fb(%rip), %rsi      # 0x18d48
movb $0x0, %al
callq *%r8
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0xd9c0
leaq 0x10b6b(%rip), %rax     # 0x1e2d8
movl (%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jg 0xd794
leaq 0x10b64(%rip), %rax     # 0x1e2e0
movq (%rax), %r8
movq -0x18(%rbp), %rdx
movl -0x50(%rbp), %ecx
xorl %edi, %edi
leaq 0xb5f3(%rip), %rsi      # 0x18d82
movb $0x0, %al
callq *%r8
jmp 0xd796
movq -0xb0(%rbp), %rax
cmpq %rax, -0x48(%rbp)
je 0xd7ac
movq -0x48(%rbp), %rdi
callq 0x22e0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x94(%rbp)
jmp 0xdb12
cmpl $0x0, -0x4c(%rbp)
je 0xd9bc
cmpl $0x22, -0x4c(%rbp)
jne 0xd856
movq -0xb0(%rbp), %rax
movq -0x30(%rbp), %rcx
shlq %rcx
movq %rcx, -0x30(%rbp)
cmpq %rax, -0x48(%rbp)
jne 0xd7fd
movq -0x30(%rbp), %rdi
callq 0x2310
movq %rax, -0x48(%rbp)
jmp 0xd80e
movq -0x48(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x2490
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0xd851
leaq 0x10abc(%rip), %rax     # 0x1e2d8
movl (%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jg 0xd83b
leaq 0x10ab5(%rip), %rax     # 0x1e2e0
movq (%rax), %rcx
xorl %edi, %edi
leaq 0xb584(%rip), %rsi      # 0x18dbb
movb $0x0, %al
callq *%rcx
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x94(%rbp)
jmp 0xdb12
jmp 0xd9ba
cmpl $0xb, -0x4c(%rbp)
je 0xd862
cmpl $0x2, -0x50(%rbp)
jne 0xd89c
leaq 0x10a6f(%rip), %rax     # 0x1e2d8
movl (%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jg 0xd888
leaq 0x10a68(%rip), %rax     # 0x1e2e0
movq (%rax), %rcx
xorl %edi, %edi
leaq 0xb543(%rip), %rsi      # 0x18dc7
movb $0x0, %al
callq *%rcx
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0xd9b8
cmpl $0x6e, -0x4c(%rbp)
jne 0xd931
leaq 0x10a2b(%rip), %rax     # 0x1e2d8
movl (%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jg 0xd905
leaq 0x10a24(%rip), %rax     # 0x1e2e0
movq (%rax), %rax
movq %rax, -0xc8(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0xd8da
movq -0x18(%rbp), %rax
movq %rax, -0xd0(%rbp)
jmp 0xd8ea
leaq 0xb3dc(%rip), %rax      # 0x18cbd
movq %rax, -0xd0(%rbp)
jmp 0xd8ea
movq -0xc8(%rbp), %rcx
movq -0xd0(%rbp), %rdx
xorl %edi, %edi
leaq 0xb4ef(%rip), %rsi      # 0x18df0
movb $0x0, %al
callq *%rcx
movq -0xb0(%rbp), %rax
cmpq %rax, -0x48(%rbp)
je 0xd91b
movq -0x48(%rbp), %rdi
callq 0x22e0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x94(%rbp)
jmp 0xdb12
leaq 0x109a0(%rip), %rax     # 0x1e2d8
movl (%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jg 0xd98c
leaq 0x10999(%rip), %rax     # 0x1e2e0
movq (%rax), %rax
movq %rax, -0xd8(%rbp)
movl -0x4c(%rbp), %eax
movl %eax, -0xe0(%rbp)
movl -0x50(%rbp), %eax
movl %eax, -0xdc(%rbp)
callq 0x2040
movl -0xe0(%rbp), %edx
movl -0xdc(%rbp), %ecx
movq -0xd8(%rbp), %r9
movl (%rax), %r8d
xorl %edi, %edi
leaq 0xb488(%rip), %rsi      # 0x18e0f
movb $0x0, %al
callq *%r9
movq -0xb0(%rbp), %rax
cmpq %rax, -0x48(%rbp)
je 0xd9a2
movq -0x48(%rbp), %rdi
callq 0x22e0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x94(%rbp)
jmp 0xdb12
jmp 0xd9ba
jmp 0xd9bc
jmp 0xd9be
jmp 0xd9c0
movb $0x1, %al
cmpl $0x0, -0x4c(%rbp)
movb %al, -0xe1(%rbp)
jne 0xd9dc
cmpq $0x0, -0x80(%rbp)
sete %al
movb %al, -0xe1(%rbp)
movb -0xe1(%rbp), %al
testb $0x1, %al
jne 0xd5a6
movq -0x80(%rbp), %rax
cmpl $0x2, 0x10(%rax)
je 0xda4d
leaq 0x108dd(%rip), %rax     # 0x1e2d8
movl (%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jg 0xda21
leaq 0x108d6(%rip), %rax     # 0x1e2e0
movq (%rax), %rcx
movq -0x80(%rbp), %rax
movl 0x10(%rax), %edx
xorl %edi, %edi
leaq 0xb42b(%rip), %rsi      # 0x18e48
movb $0x0, %al
callq *%rcx
movq -0xb0(%rbp), %rax
cmpq %rax, -0x48(%rbp)
je 0xda37
movq -0x48(%rbp), %rdi
callq 0x22e0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x94(%rbp)
jmp 0xdb12
cmpq $0x0, -0x28(%rbp)
jne 0xda59
jmp 0xdaeb
movl $0x0, -0x98(%rbp)
movq -0x80(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x98(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0xdae9
leaq -0xa8(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x2170
movq -0x80(%rbp), %rax
movl 0x10(%rax), %eax
movw %ax, -0xa8(%rbp)
movq -0x80(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x98(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl (%rax), %eax
movl %eax, -0xa4(%rbp)
movzwl -0x1a(%rbp), %edi
callq 0x2070
movw %ax, -0xa6(%rbp)
movq -0x28(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x171b0
movl -0x98(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x98(%rbp)
jmp 0xda63
jmp 0xdaeb
movq -0xb0(%rbp), %rax
cmpq %rax, -0x48(%rbp)
je 0xdb01
movq -0x48(%rbp), %rdi
callq 0x22e0
movl $0x0, -0x4(%rbp)
movl $0x1, -0x94(%rbp)
movq -0x38(%rbp), %rax
movq %rax, %rsp
movl -0x4(%rbp), %eax
movq %rbp, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax,%rax)