// File: karel-brinda[P]ococo/ext/htslib/knetfile.c
// Function: socket_connect
static int socket_connect(const char *host, const char *port)
{
#define __err_connect(func) do { perror(func); freeaddrinfo(res); return -1; } while (0)

	int ai_err, on = 1, fd;
	struct linger lng = { 0, 0 };
	struct addrinfo hints, *res = 0;
	memset(&hints, 0, sizeof(struct addrinfo));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	/* In Unix/Mac, getaddrinfo() is the most convenient way to get
	 * server information. */
	if ((ai_err = getaddrinfo(host, port, &hints, &res)) != 0) { fprintf(stderr, "can't resolve %s:%s: %s\n", host, port, gai_strerror(ai_err)); return -1; }
	if ((fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1) __err_connect("socket");
	/* The following two setsockopt() are used by ftplib
	 * (http://nbpfaus.net/~pfau/ftplib/). I am not sure if they
	 * necessary. */
	if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) __err_connect("setsockopt");
	if (setsockopt(fd, SOL_SOCKET, SO_LINGER, &lng, sizeof(lng)) == -1) __err_connect("setsockopt");
	if (connect(fd, res->ai_addr, res->ai_addrlen) != 0) __err_connect("connect");
	freeaddrinfo(res);
	return fd;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl $0x1, -0x20(%rbp)
leaq -0x2c(%rbp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x42d0
movq $0x0, -0x68(%rbp)
leaq -0x60(%rbp), %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x42d0
movl $0x0, -0x5c(%rbp)
movl $0x1, -0x58(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
leaq -0x60(%rbp), %rdx
leaq -0x68(%rbp), %rcx
callq 0x4730
movl %eax, -0x1c(%rbp)
cmpl $0x0, %eax
je 0x7625c
movq 0x13dbc(%rip), %rax     # 0x89fd0
movq (%rax), %rax
movq %rax, -0x80(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x70(%rbp)
movl -0x1c(%rbp), %edi
callq 0x47f0
movq -0x80(%rbp), %rdi
movq -0x78(%rbp), %rdx
movq -0x70(%rbp), %rcx
movq %rax, %r8
leaq 0x8c87(%rip), %rsi      # 0x7eed0
movb $0x0, %al
callq 0x4830
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x76371
movq -0x68(%rbp), %rax
movl 0x4(%rax), %edi
movq -0x68(%rbp), %rax
movl 0x8(%rax), %esi
movq -0x68(%rbp), %rax
movl 0xc(%rax), %edx
callq 0x4120
movl %eax, -0x24(%rbp)
cmpl $-0x1, %eax
jne 0x762a1
jmp 0x76280
leaq 0x8c62(%rip), %rdi      # 0x7eee9
callq 0x4480
movq -0x68(%rbp), %rdi
callq 0x4780
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x76371
movl -0x24(%rbp), %edi
movl $0x1, %esi
movl $0x2, %edx
leaq -0x20(%rbp), %rcx
movl $0x4, %r8d
callq 0x4a70
cmpl $-0x1, %eax
jne 0x762e5
jmp 0x762c4
leaq 0x8c25(%rip), %rdi      # 0x7eef0
callq 0x4480
movq -0x68(%rbp), %rdi
callq 0x4780
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x76371
movl -0x24(%rbp), %edi
movl $0x1, %esi
movl $0xd, %edx
leaq -0x2c(%rbp), %rcx
movl $0x8, %r8d
callq 0x4a70
cmpl $-0x1, %eax
jne 0x76326
jmp 0x76308
leaq 0x8be1(%rip), %rdi      # 0x7eef0
callq 0x4480
movq -0x68(%rbp), %rdi
callq 0x4780
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x76371
movl -0x24(%rbp), %edi
movq -0x68(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x68(%rbp), %rax
movl 0x10(%rax), %edx
callq 0x4470
cmpl $0x0, %eax
je 0x76362
jmp 0x76344
leaq 0x8bb0(%rip), %rdi      # 0x7eefb
callq 0x4480
movq -0x68(%rbp), %rdi
callq 0x4780
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x76371
movq -0x68(%rbp), %rdi
callq 0x4780
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax)