#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
struct timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
struct addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  struct addrinfo *ai_next;
  scalar_t__ ai_flags;
};
typedef int /*<<< orphan*/ sockret;
typedef int socklen_t;
typedef int /*<<< orphan*/ hints;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
scalar_t__ EINPROGRESS;
int /*<<< orphan*/ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_ERROR;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ errno;
int fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ free(struct fdlist *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
scalar_t__ getaddrinfo(char const *, char const *, struct addrinfo *,
                       struct addrinfo **);
scalar_t__ getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *,
                      int *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, struct timeval *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int mod_websocket_tcp_server_connect(const char *host, const char *service) {
  struct addrinfo hints;
  struct addrinfo *res = NULL;
  struct addrinfo *ai = NULL;
  struct fdlist {
    int fd;
    struct fdlist *next;
  };
  struct fdlist *head = NULL, *p, *pn, *fde = NULL;
  int flags, fd, maxfd = -1, connfd = -1, sockret = -1;
  socklen_t socklen = sizeof(sockret);
  fd_set fds;
  struct timeval tv;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = 0;
  if (getaddrinfo(host, service, &hints, &res) != 0) {
    return -1;
  }
  FD_ZERO(&fds);
  for (ai = res; ai; ai = ai->ai_next) {
    fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
    if (fd < 0) {
      goto go_out;
    }
    if ((flags = fcntl(fd, F_GETFL, 0)) < 0 ||
        fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) {
      close(fd);
      goto go_out;
    }
    fde = (struct fdlist *)malloc(sizeof(struct fdlist));
    if (!fde) {
      close(fd);
      goto go_out;
    }
    fde->fd = fd;
    fde->next = head;
    head = fde;
    if (fde->fd > maxfd) {
      maxfd = fde->fd;
    }
    FD_SET(fde->fd, &fds);
    if (connect(fde->fd, ai->ai_addr, ai->ai_addrlen) < 0) {
      if (errno != EINPROGRESS) {
        goto go_out;
      }
    }
  }
  tv.tv_sec = 5;
  tv.tv_usec = 0;
  if (select(maxfd + 1, NULL, &fds, NULL, &tv) == 0) {
    goto go_out;
  } else {
    for (p = head; p; p = p->next) {
      if (!FD_ISSET(p->fd, &fds)) {
        continue;
      }
      if (getsockopt(p->fd, SOL_SOCKET, SO_ERROR, &sockret, &socklen) == 0 &&
          sockret == 0) {
        connfd = p->fd;
        break;
      }
    }
  }
go_out:
  p = head;
  while (p) {
    if (p->fd != connfd) {
      close(p->fd);
    }
    pn = p->next;
    free(p);
    p = pn;
  }
  freeaddrinfo(res);
  return connfd;
}


x86 assembly (clang, -O0):
<mod_websocket_tcp_server_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x48(%rbp)
movq   $0x0,-0x50(%rbp)
movq   $0x0,-0x58(%rbp)
movq   $0x0,-0x70(%rbp)
movl   $0xffffffff,-0x7c(%rbp)
movl   $0xffffffff,-0x80(%rbp)
movl   $0xffffffff,-0x84(%rbp)
ff ff ff
movl   $0x4,-0x88(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1060 <memset@plt>
mov    0x2d84(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d60(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   10d0 <getaddrinfo@plt>
cmp    $0x0,%rax
je     1290 <mod_websocket_tcp_server_connect+0xb0>
movl   $0xffffffff,-0x4(%rbp)
jmp    153b <mod_websocket_tcp_server_connect+0x35b>
lea    -0x8c(%rbp),%rdi
call   1090 <FD_ZERO@plt>
mov    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     13f6 <mod_websocket_tcp_server_connect+0x216>
mov    -0x50(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x50(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%edx
call   1100 <socket@plt>
mov    %eax,-0x78(%rbp)
cmpl   $0x0,-0x78(%rbp)
jge    12db <mod_websocket_tcp_server_connect+0xfb>
jmp    14dd <mod_websocket_tcp_server_connect+0x2fd>
mov    -0x78(%rbp),%edi
mov    0x2cbb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,-0x74(%rbp)
cmp    $0x0,%eax
jl     1320 <mod_websocket_tcp_server_connect+0x140>
mov    -0x78(%rbp),%edi
mov    0x2cbc(%rip),%rax
mov    (%rax),%esi
mov    -0x74(%rbp),%edx
mov    0x2cd0(%rip),%rax
or     (%rax),%edx
call   1040 <fcntl@plt>
cmp    $0x0,%eax
jge    132d <mod_websocket_tcp_server_connect+0x14d>
mov    -0x78(%rbp),%edi
call   1070 <close@plt>
jmp    14dd <mod_websocket_tcp_server_connect+0x2fd>
mov    $0x10,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x70(%rbp)
cmpq   $0x0,-0x70(%rbp)
jne    1353 <mod_websocket_tcp_server_connect+0x173>
mov    -0x78(%rbp),%edi
call   1070 <close@plt>
jmp    14dd <mod_websocket_tcp_server_connect+0x2fd>
mov    -0x78(%rbp),%ecx
mov    -0x70(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x58(%rbp),%rcx
mov    -0x70(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x70(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x70(%rbp),%rax
mov    (%rax),%eax
cmp    -0x7c(%rbp),%eax
jle    1388 <mod_websocket_tcp_server_connect+0x1a8>
mov    -0x70(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x7c(%rbp)
mov    -0x70(%rbp),%rax
mov    (%rax),%edi
lea    -0x8c(%rbp),%rsi
call   10e0 <FD_SET@plt>
mov    -0x70(%rbp),%rax
mov    (%rax),%edi
mov    -0x50(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x50(%rbp),%rax
mov    (%rax),%edx
call   10c0 <connect@plt>
cmp    $0x0,%rax
jge    13e0 <mod_websocket_tcp_server_connect+0x200>
mov    0x2bed(%rip),%rax
mov    (%rax),%rax
mov    0x2c0b(%rip),%rcx
cmp    (%rcx),%rax
je     13db <mod_websocket_tcp_server_connect+0x1fb>
jmp    14dd <mod_websocket_tcp_server_connect+0x2fd>
jmp    13e0 <mod_websocket_tcp_server_connect+0x200>
jmp    13e5 <mod_websocket_tcp_server_connect+0x205>
mov    -0x50(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    12a4 <mod_websocket_tcp_server_connect+0xc4>
movl   $0x5,-0xa0(%rbp)
movq   $0x0,-0x98(%rbp)
mov    -0x7c(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x8c(%rbp),%rdx
lea    -0xa0(%rbp),%r8
mov    %rcx,%rsi
call   10a0 <select@plt>
cmp    $0x0,%rax
jne    143a <mod_websocket_tcp_server_connect+0x25a>
jmp    14dd <mod_websocket_tcp_server_connect+0x2fd>
mov    -0x58(%rbp),%rax
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
je     14d3 <mod_websocket_tcp_server_connect+0x2f3>
mov    -0x60(%rbp),%rax
mov    (%rax),%edi
lea    -0x8c(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%eax
jne    146d <mod_websocket_tcp_server_connect+0x28d>
jmp    14c2 <mod_websocket_tcp_server_connect+0x2e2>
mov    -0x60(%rbp),%rax
mov    (%rax),%edi
mov    0x2b6e(%rip),%rax
mov    (%rax),%esi
mov    0x2b25(%rip),%rax
mov    (%rax),%edx
lea    -0x84(%rbp),%rcx
lea    -0x88(%rbp),%r8
call   1080 <getsockopt@plt>
cmp    $0x0,%rax
jne    14bd <mod_websocket_tcp_server_connect+0x2dd>
cmpl   $0x0,-0x84(%rbp)
jne    14bd <mod_websocket_tcp_server_connect+0x2dd>
mov    -0x60(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x80(%rbp)
jmp    14d3 <mod_websocket_tcp_server_connect+0x2f3>
jmp    14c2 <mod_websocket_tcp_server_connect+0x2e2>
mov    -0x60(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x60(%rbp)
jmp    1442 <mod_websocket_tcp_server_connect+0x262>
jmp    14d8 <mod_websocket_tcp_server_connect+0x2f8>
jmp    14dd <mod_websocket_tcp_server_connect+0x2fd>
mov    -0x58(%rbp),%rax
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
je     152c <mod_websocket_tcp_server_connect+0x34c>
mov    -0x60(%rbp),%rax
mov    (%rax),%eax
cmp    -0x80(%rbp),%eax
je     150a <mod_websocket_tcp_server_connect+0x32a>
mov    -0x60(%rbp),%rax
mov    (%rax),%edi
call   1070 <close@plt>
mov    -0x60(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x68(%rbp)
mov    -0x60(%rbp),%rdi
call   1030 <free@plt>
mov    -0x68(%rbp),%rax
mov    %rax,-0x60(%rbp)
jmp    14e5 <mod_websocket_tcp_server_connect+0x305>
mov    -0x48(%rbp),%rdi
call   10f0 <freeaddrinfo@plt>
mov    -0x80(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mod_websocket_tcp_server_connect>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %rsi,%rbx
mov    %rdi,%rbp
movq   $0x0,0x10(%rsp)
movl   $0xffffffff,0xc(%rsp)
ff
movl   $0x4,0x1c(%rsp)
lea    0x30(%rsp),%r15
xor    %r14d,%r14d
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1060 <memset@plt>
mov    0x2da5(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x40(%rsp)
mov    0x2d80(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x3c(%rsp)
movq   $0x0,0x50(%rsp)
lea    0x10(%rsp),%rcx
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   10d0 <getaddrinfo@plt>
mov    $0xffffffff,%r12d
test   %rax,%rax
jne    1434 <mod_websocket_tcp_server_connect+0x254>
lea    0x8(%rsp),%rdi
call   1090 <FD_ZERO@plt>
mov    0x10(%rsp),%r15
test   %r15,%r15
je     136e <mod_websocket_tcp_server_connect+0x18e>
mov    $0xffffffff,%r12d
xor    %r13d,%r13d
mov    $0xffffffff,%r14d
jmp    12b0 <mod_websocket_tcp_server_connect+0xd0>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x18(%r15),%r15
mov    %rbx,%r13
test   %r15,%r15
je     137f <mod_websocket_tcp_server_connect+0x19f>
mov    0x10(%r15),%edi
mov    0x8(%r15),%edx
mov    0xc(%r15),%esi
call   1100 <socket@plt>
test   %eax,%eax
js     1425 <mod_websocket_tcp_server_connect+0x245>
mov    %eax,%ebp
mov    0x2cce(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1040 <fcntl@plt>
test   %eax,%eax
js     1373 <mod_websocket_tcp_server_connect+0x193>
mov    0x2cd4(%rip),%rcx
mov    (%rcx),%esi
mov    0x2ceb(%rip),%rcx
or     (%rcx),%eax
mov    %ebp,%edi
mov    %eax,%edx
call   1040 <fcntl@plt>
test   %eax,%eax
js     1373 <mod_websocket_tcp_server_connect+0x193>
mov    $0x10,%edi
call   10b0 <malloc@plt>
test   %rax,%rax
je     1373 <mod_websocket_tcp_server_connect+0x193>
mov    %rax,%rbx
mov    %ebp,(%rax)
mov    %r13,0x8(%rax)
cmp    %r14d,%ebp
cmovg  %ebp,%r14d
mov    %ebp,%edi
lea    0x8(%rsp),%rsi
call   10e0 <FD_SET@plt>
mov    (%rbx),%edi
mov    (%r15),%edx
mov    0x4(%r15),%esi
call   10c0 <connect@plt>
test   %rax,%rax
jns    12a0 <mod_websocket_tcp_server_connect+0xc0>
mov    0x2c63(%rip),%rax
mov    (%rax),%rax
mov    0x2c81(%rip),%rcx
cmp    (%rcx),%rax
je     12a0 <mod_websocket_tcp_server_connect+0xc0>
mov    $0xffffffff,%r12d
mov    %rbx,%r13
jmp    1464 <mod_websocket_tcp_server_connect+0x284>
xor    %r13d,%r13d
jmp    1386 <mod_websocket_tcp_server_connect+0x1a6>
mov    %ebp,%edi
call   1070 <close@plt>
jmp    1425 <mod_websocket_tcp_server_connect+0x245>
add    $0x1,%r14d
mov    %rbx,%r13
movl   $0x5,0x20(%rsp)
movq   $0x0,0x28(%rsp)
lea    0x8(%rsp),%rdx
lea    0x20(%rsp),%r8
mov    %r14d,%edi
xor    %esi,%esi
xor    %ecx,%ecx
call   10a0 <select@plt>
mov    $0xffffffff,%r12d
test   %rax,%rax
je     1425 <mod_websocket_tcp_server_connect+0x245>
test   %r13,%r13
je     1425 <mod_websocket_tcp_server_connect+0x245>
lea    0x8(%rsp),%rbp
lea    0xc(%rsp),%r14
lea    0x1c(%rsp),%r15
mov    %r13,%rbx
jmp    13e9 <mod_websocket_tcp_server_connect+0x209>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x8(%rbx),%rbx
test   %rbx,%rbx
je     1425 <mod_websocket_tcp_server_connect+0x245>
mov    (%rbx),%edi
mov    %rbp,%rsi
call   1050 <FD_ISSET@plt>
test   %eax,%eax
je     13e0 <mod_websocket_tcp_server_connect+0x200>
mov    (%rbx),%edi
mov    0x2be8(%rip),%rax
mov    (%rax),%esi
mov    0x2b9f(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rcx
mov    %r15,%r8
call   1080 <getsockopt@plt>
test   %rax,%rax
jne    13e0 <mod_websocket_tcp_server_connect+0x200>
cmpl   $0x0,0xc(%rsp)
jne    13e0 <mod_websocket_tcp_server_connect+0x200>
mov    (%rbx),%r12d
test   %r13,%r13
jne    1464 <mod_websocket_tcp_server_connect+0x284>
mov    0x10(%rsp),%rdi
call   10f0 <freeaddrinfo@plt>
mov    %r12d,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
cs nopw 0x0(%rax,%rax,1)
mov    0x8(%r13),%rbx
mov    %r13,%rdi
call   1030 <free@plt>
mov    %rbx,%r13
test   %rbx,%rbx
je     142a <mod_websocket_tcp_server_connect+0x24a>
mov    0x0(%r13),%edi
cmp    %r12d,%edi
je     1450 <mod_websocket_tcp_server_connect+0x270>
call   1070 <close@plt>
jmp    1450 <mod_websocket_tcp_server_connect+0x270>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i64 }
%struct.fdlist = type { i32, %struct.fdlist* }
%struct.timeval = type { i32, i64 }
%struct.fdlist.0 = type opaque
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@errno = global i64 0
@EINPROGRESS = global i64 0
@SOL_SOCKET = global i32 0
@SO_ERROR = global i32 0
define i32 @mod_websocket_tcp_server_connect(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.addrinfo
%7 = alloca %struct.addrinfo*
%8 = alloca %struct.addrinfo*
%9 = alloca %struct.fdlist*
%10 = alloca %struct.fdlist*
%11 = alloca %struct.fdlist*
%12 = alloca %struct.fdlist*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
%20 = alloca %struct.timeval
store i8* %0, i8** %4
store i8* %1, i8** %5
store %struct.addrinfo* null, %struct.addrinfo** %7
store %struct.addrinfo* null, %struct.addrinfo** %8
store %struct.fdlist* null, %struct.fdlist** %9
store %struct.fdlist* null, %struct.fdlist** %12
store i32 -1, i32* %15
store i32 -1, i32* %16
store i32 -1, i32* %17
store i32 4, i32* %18
%21 = call i32 @memset(%struct.addrinfo* %6, i32 0, i32 40)
%22 = load i32, i32* @AF_UNSPEC
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 4
store i32 %22, i32* %23
%24 = load i32, i32* @SOCK_STREAM
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 3
store i32 %24, i32* %25
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 6
store i64 0, i64* %26
%27 = load i8*, i8** %4
%28 = load i8*, i8** %5
%29 = call i64 @getaddrinfo(i8* %27, i8* %28, %struct.addrinfo* %6, %struct.addrinfo** %7)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %2
store i32 -1, i32* %3
br label %190
32: ; preds = %2
%33 = call i32 @FD_ZERO(i32* %19)
%34 = load %struct.addrinfo*, %struct.addrinfo** %7
store %struct.addrinfo* %34, %struct.addrinfo** %8
br label %35
35: ; preds = %116, %32
%36 = load %struct.addrinfo*, %struct.addrinfo** %8
%37 = icmp ne %struct.addrinfo* %36, null
br i1 %37, label %38, label %120
38: ; preds = %35
%39 = load %struct.addrinfo*, %struct.addrinfo** %8
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %39, i32 0, i32 4
%41 = load i32, i32* %40
%42 = load %struct.addrinfo*, %struct.addrinfo** %8
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %42, i32 0, i32 3
%44 = load i32, i32* %43
%45 = load %struct.addrinfo*, %struct.addrinfo** %8
%46 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %45, i32 0, i32 2
%47 = load i32, i32* %46
%48 = call i32 @socket(i32 %41, i32 %44, i32 %47)
store i32 %48, i32* %14
%49 = load i32, i32* %14
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %38
br label %162
52: ; preds = %38
%53 = load i32, i32* %14
%54 = load i32, i32* @F_GETFL
%55 = call i32 @fcntl(i32 %53, i32 %54, i32 0)
store i32 %55, i32* %13
%56 = icmp slt i32 %55, 0
br i1 %56, label %65, label %57
57: ; preds = %52
%58 = load i32, i32* %14
%59 = load i32, i32* @F_SETFL
%60 = load i32, i32* %13
%61 = load i32, i32* @O_NONBLOCK
%62 = or i32 %60, %61
%63 = call i32 @fcntl(i32 %58, i32 %59, i32 %62)
%64 = icmp slt i32 %63, 0
br i1 %64, label %65, label %68
65: ; preds = %57, %52
%66 = load i32, i32* %14
%67 = call i32 @close(i32 %66)
br label %162
68: ; preds = %57
%69 = call i64 @malloc(i32 16)
%70 = inttoptr i64 %69 to %struct.fdlist*
store %struct.fdlist* %70, %struct.fdlist** %12
%71 = load %struct.fdlist*, %struct.fdlist** %12
%72 = icmp ne %struct.fdlist* %71, null
br i1 %72, label %76, label %73
73: ; preds = %68
%74 = load i32, i32* %14
%75 = call i32 @close(i32 %74)
br label %162
76: ; preds = %68
%77 = load i32, i32* %14
%78 = load %struct.fdlist*, %struct.fdlist** %12
%79 = getelementptr inbounds %struct.fdlist, %struct.fdlist* %78, i32 0, i32 0
store i32 %77, i32* %79
%80 = load %struct.fdlist*, %struct.fdlist** %9
%81 = load %struct.fdlist*, %struct.fdlist** %12
%82 = getelementptr inbounds %struct.fdlist, %struct.fdlist* %81, i32 0, i32 1
store %struct.fdlist* %80, %struct.fdlist** %82
%83 = load %struct.fdlist*, %struct.fdlist** %12
store %struct.fdlist* %83, %struct.fdlist** %9
%84 = load %struct.fdlist*, %struct.fdlist** %12
%85 = getelementptr inbounds %struct.fdlist, %struct.fdlist* %84, i32 0, i32 0
%86 = load i32, i32* %85
%87 = load i32, i32* %15
%88 = icmp sgt i32 %86, %87
br i1 %88, label %89, label %93
89: ; preds = %76
%90 = load %struct.fdlist*, %struct.fdlist** %12
%91 = getelementptr inbounds %struct.fdlist, %struct.fdlist* %90, i32 0, i32 0
%92 = load i32, i32* %91
store i32 %92, i32* %15
br label %93
93: ; preds = %89, %76
%94 = load %struct.fdlist*, %struct.fdlist** %12
%95 = getelementptr inbounds %struct.fdlist, %struct.fdlist* %94, i32 0, i32 0
%96 = load i32, i32* %95
%97 = call i32 @FD_SET(i32 %96, i32* %19)
%98 = load %struct.fdlist*, %struct.fdlist** %12
%99 = getelementptr inbounds %struct.fdlist, %struct.fdlist* %98, i32 0, i32 0
%100 = load i32, i32* %99
%101 = load %struct.addrinfo*, %struct.addrinfo** %8
%102 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %101, i32 0, i32 1
%103 = load i32, i32* %102
%104 = load %struct.addrinfo*, %struct.addrinfo** %8
%105 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %104, i32 0, i32 0
%106 = load i32, i32* %105
%107 = call i64 @connect(i32 %100, i32 %103, i32 %106)
%108 = icmp slt i64 %107, 0
br i1 %108, label %109, label %115
109: ; preds = %93
%110 = load i64, i64* @errno
%111 = load i64, i64* @EINPROGRESS
%112 = icmp ne i64 %110, %111
br i1 %112, label %113, label %114
113: ; preds = %109
br label %162
114: ; preds = %109
br label %115
115: ; preds = %114, %93
br label %116
116: ; preds = %115
%117 = load %struct.addrinfo*, %struct.addrinfo** %8
%118 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %117, i32 0, i32 5
%119 = load %struct.addrinfo*, %struct.addrinfo** %118
store %struct.addrinfo* %119, %struct.addrinfo** %8
br label %35
120: ; preds = %35
%121 = getelementptr inbounds %struct.timeval, %struct.timeval* %20, i32 0, i32 0
store i32 5, i32* %121
%122 = getelementptr inbounds %struct.timeval, %struct.timeval* %20, i32 0, i32 1
store i64 0, i64* %122
%123 = load i32, i32* %15
%124 = add nsw i32 %123, 1
%125 = call i64 @select(i32 %124, i32* null, i32* %19, i32* null, %struct.timeval* %20)
%126 = icmp eq i64 %125, 0
br i1 %126, label %127, label %128
127: ; preds = %120
br label %162
128: ; preds = %120
%129 = load %struct.fdlist*, %struct.fdlist** %9
store %struct.fdlist* %129, %struct.fdlist** %10
br label %130
130: ; preds = %156, %128
%131 = load %struct.fdlist*, %struct.fdlist** %10
%132 = icmp ne %struct.fdlist* %131, null
br i1 %132, label %133, label %160
133: ; preds = %130
%134 = load %struct.fdlist*, %struct.fdlist** %10
%135 = getelementptr inbounds %struct.fdlist, %struct.fdlist* %134, i32 0, i32 0
%136 = load i32, i32* %135
%137 = call i32 @FD_ISSET(i32 %136, i32* %19)
%138 = icmp ne i32 %137, 0
br i1 %138, label %140, label %139
139: ; preds = %133
br label %156
140: ; preds = %133
%141 = load %struct.fdlist*, %struct.fdlist** %10
%142 = getelementptr inbounds %struct.fdlist, %struct.fdlist* %141, i32 0, i32 0
%143 = load i32, i32* %142
%144 = load i32, i32* @SOL_SOCKET
%145 = load i32, i32* @SO_ERROR
%146 = call i64 @getsockopt(i32 %143, i32 %144, i32 %145, i32* %17, i32* %18)
%147 = icmp eq i64 %146, 0
br i1 %147, label %148, label %155
148: ; preds = %140
%149 = load i32, i32* %17
%150 = icmp eq i32 %149, 0
br i1 %150, label %151, label %155
151: ; preds = %148
%152 = load %struct.fdlist*, %struct.fdlist** %10
%153 = getelementptr inbounds %struct.fdlist, %struct.fdlist* %152, i32 0, i32 0
%154 = load i32, i32* %153
store i32 %154, i32* %16
br label %160
155: ; preds = %148, %140
br label %156
156: ; preds = %155, %139
%157 = load %struct.fdlist*, %struct.fdlist** %10
%158 = getelementptr inbounds %struct.fdlist, %struct.fdlist* %157, i32 0, i32 1
%159 = load %struct.fdlist*, %struct.fdlist** %158
store %struct.fdlist* %159, %struct.fdlist** %10
br label %130
160: ; preds = %151, %130
br label %161
161: ; preds = %160
br label %162
162: ; preds = %161, %127, %113, %73, %65, %51
%163 = load %struct.fdlist*, %struct.fdlist** %9
store %struct.fdlist* %163, %struct.fdlist** %10
br label %164
164: ; preds = %178, %162
%165 = load %struct.fdlist*, %struct.fdlist** %10
%166 = icmp ne %struct.fdlist* %165, null
br i1 %166, label %167, label %186
167: ; preds = %164
%168 = load %struct.fdlist*, %struct.fdlist** %10
%169 = getelementptr inbounds %struct.fdlist, %struct.fdlist* %168, i32 0, i32 0
%170 = load i32, i32* %169
%171 = load i32, i32* %16
%172 = icmp ne i32 %170, %171
br i1 %172, label %173, label %178
173: ; preds = %167
%174 = load %struct.fdlist*, %struct.fdlist** %10
%175 = getelementptr inbounds %struct.fdlist, %struct.fdlist* %174, i32 0, i32 0
%176 = load i32, i32* %175
%177 = call i32 @close(i32 %176)
br label %178
178: ; preds = %173, %167
%179 = load %struct.fdlist*, %struct.fdlist** %10
%180 = getelementptr inbounds %struct.fdlist, %struct.fdlist* %179, i32 0, i32 1
%181 = load %struct.fdlist*, %struct.fdlist** %180
store %struct.fdlist* %181, %struct.fdlist** %11
%182 = load %struct.fdlist*, %struct.fdlist** %10
%183 = bitcast %struct.fdlist* %182 to %struct.fdlist.0*
%184 = call i32 @free(%struct.fdlist.0* %183)
%185 = load %struct.fdlist*, %struct.fdlist** %11
store %struct.fdlist* %185, %struct.fdlist** %10
br label %164
186: ; preds = %164
%187 = load %struct.addrinfo*, %struct.addrinfo** %7
%188 = call i32 @freeaddrinfo(%struct.addrinfo* %187)
%189 = load i32, i32* %16
store i32 %189, i32* %3
br label %190
190: ; preds = %186, %31
%191 = load i32, i32* %3
ret i32 %191
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i64 }
%struct.timeval = type { i32, i64 }
%struct.fdlist = type { i32, %struct.fdlist* }
%struct.fdlist.0 = type opaque
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@errno = global i64 0
@EINPROGRESS = global i64 0
@SOL_SOCKET = global i32 0
@SO_ERROR = global i32 0
define i32 @mod_websocket_tcp_server_connect(i8* %0, i8* %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.timeval
%9 = bitcast %struct.addrinfo* %3 to i8*
%10 = bitcast %struct.addrinfo** %4 to i8*
store %struct.addrinfo* null, %struct.addrinfo** %4
%11 = bitcast i32* %5 to i8*
store i32 -1, i32* %5
%12 = bitcast i32* %6 to i8*
store i32 4, i32* %6
%13 = bitcast i32* %7 to i8*
%14 = bitcast %struct.timeval* %8 to i8*
%15 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 40)
%16 = load i32, i32* @AF_UNSPEC
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
store i32 %16, i32* %17
%18 = load i32, i32* @SOCK_STREAM
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 6
store i64 0, i64* %20
%21 = call i64 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %3, %struct.addrinfo** %4)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %137
23: ; preds = %2
%24 = call i32 @FD_ZERO(i32* %7)
%25 = load %struct.addrinfo*, %struct.addrinfo** %4
%26 = icmp eq %struct.addrinfo* %25, null
br i1 %26, label %81, label %27
27: ; preds = %23, %74
%28 = phi %struct.addrinfo* [ %76, %74 ], [ %25, %23 ]
%29 = phi i32 [ %61, %74 ], [ -1, %23 ]
%30 = phi %struct.fdlist* [ %53, %74 ], [ null, %23 ]
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 4
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 3
%34 = load i32, i32* %33
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 2
%36 = load i32, i32* %35
%37 = call i32 @socket(i32 %32, i32 %34, i32 %36)
%38 = icmp slt i32 %37, 0
br i1 %38, label %111, label %39
39: ; preds = %27
%40 = load i32, i32* @F_GETFL
%41 = call i32 @fcntl(i32 %37, i32 %40, i32 0)
%42 = icmp slt i32 %41, 0
br i1 %42, label %49, label %43
43: ; preds = %39
%44 = load i32, i32* @F_SETFL
%45 = load i32, i32* @O_NONBLOCK
%46 = or i32 %45, %41
%47 = call i32 @fcntl(i32 %37, i32 %44, i32 %46)
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %43, %39
%50 = call i32 @close(i32 %37)
br label %111
51: ; preds = %43
%52 = call i64 @malloc(i32 16)
%53 = inttoptr i64 %52 to %struct.fdlist*
%54 = icmp eq i64 %52, 0
br i1 %54, label %55, label %57
55: ; preds = %51
%56 = call i32 @close(i32 %37)
br label %111
57: ; preds = %51
%58 = getelementptr inbounds %struct.fdlist, %struct.fdlist* %53, i64 0, i32 0
store i32 %37, i32* %58
%59 = getelementptr inbounds %struct.fdlist, %struct.fdlist* %53, i64 0, i32 1
store %struct.fdlist* %30, %struct.fdlist** %59
%60 = icmp sgt i32 %37, %29
%61 = select i1 %60, i32 %37, i32 %29
%62 = call i32 @FD_SET(i32 %37, i32* %7)
%63 = load i32, i32* %58
%64 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 1
%65 = load i32, i32* %64
%66 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 0
%67 = load i32, i32* %66
%68 = call i64 @connect(i32 %63, i32 %65, i32 %67)
%69 = icmp slt i64 %68, 0
br i1 %69, label %70, label %74
70: ; preds = %57
%71 = load i64, i64* @errno
%72 = load i64, i64* @EINPROGRESS
%73 = icmp eq i64 %71, %72
br i1 %73, label %74, label %115
74: ; preds = %57, %70
%75 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 5
%76 = load %struct.addrinfo*, %struct.addrinfo** %75
%77 = icmp eq %struct.addrinfo* %76, null
br i1 %77, label %78, label %27
78: ; preds = %74
%79 = inttoptr i64 %52 to %struct.fdlist*
%80 = add nuw i32 %61, 1
br label %81
81: ; preds = %78, %23
%82 = phi %struct.fdlist* [ %79, %78 ], [ null, %23 ]
%83 = phi i32 [ %80, %78 ], [ 0, %23 ]
%84 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i64 0, i32 0
store i32 5, i32* %84
%85 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i64 0, i32 1
store i64 0, i64* %85
%86 = call i64 @select(i32 %83, i32* null, i32* %7, i32* null, %struct.timeval* %8)
%87 = icmp eq i64 %86, 0
%88 = icmp eq %struct.fdlist* %82, null
%89 = select i1 %87, i1 true, i1 %88
br i1 %89, label %111, label %90
90: ; preds = %81, %107
%91 = phi %struct.fdlist* [ %109, %107 ], [ %82, %81 ]
%92 = getelementptr inbounds %struct.fdlist, %struct.fdlist* %91, i64 0, i32 0
%93 = load i32, i32* %92
%94 = call i32 @FD_ISSET(i32 %93, i32* %7)
%95 = icmp eq i32 %94, 0
br i1 %95, label %107, label %96
96: ; preds = %90
%97 = load i32, i32* %92
%98 = load i32, i32* @SOL_SOCKET
%99 = load i32, i32* @SO_ERROR
%100 = call i64 @getsockopt(i32 %97, i32 %98, i32 %99, i32* %5, i32* %6)
%101 = icmp eq i64 %100, 0
%102 = load i32, i32* %5
%103 = icmp eq i32 %102, 0
%104 = select i1 %101, i1 %103, i1 false
br i1 %104, label %105, label %107
105: ; preds = %96
%106 = load i32, i32* %92
br label %111
107: ; preds = %96, %90
%108 = getelementptr inbounds %struct.fdlist, %struct.fdlist* %91, i64 0, i32 1
%109 = load %struct.fdlist*, %struct.fdlist** %108
%110 = icmp eq %struct.fdlist* %109, null
br i1 %110, label %111, label %90
111: ; preds = %27, %107, %105, %81, %55, %49
%112 = phi %struct.fdlist* [ %30, %49 ], [ %30, %55 ], [ %82, %81 ], [ %82, %105 ], [ %82, %107 ], [ %30, %27 ]
%113 = phi i32 [ -1, %49 ], [ -1, %55 ], [ -1, %81 ], [ %106, %105 ], [ -1, %107 ], [ -1, %27 ]
%114 = icmp eq %struct.fdlist* %112, null
br i1 %114, label %133, label %117
115: ; preds = %70
%116 = inttoptr i64 %52 to %struct.fdlist*
br label %117
117: ; preds = %115, %111
%118 = phi i32 [ %113, %111 ], [ -1, %115 ]
%119 = phi %struct.fdlist* [ %112, %111 ], [ %116, %115 ]
br label %120
120: ; preds = %117, %127
%121 = phi %struct.fdlist* [ %129, %127 ], [ %119, %117 ]
%122 = getelementptr inbounds %struct.fdlist, %struct.fdlist* %121, i64 0, i32 0
%123 = load i32, i32* %122
%124 = icmp eq i32 %123, %118
br i1 %124, label %127, label %125
125: ; preds = %120
%126 = call i32 @close(i32 %123)
br label %127
127: ; preds = %125, %120
%128 = getelementptr inbounds %struct.fdlist, %struct.fdlist* %121, i64 0, i32 1
%129 = load %struct.fdlist*, %struct.fdlist** %128
%130 = bitcast %struct.fdlist* %121 to %struct.fdlist.0*
%131 = call i32 @free(%struct.fdlist.0* %130)
%132 = icmp eq %struct.fdlist* %129, null
br i1 %132, label %133, label %120
133: ; preds = %127, %111
%134 = phi i32 [ %113, %111 ], [ %118, %127 ]
%135 = load %struct.addrinfo*, %struct.addrinfo** %4
%136 = call i32 @freeaddrinfo(%struct.addrinfo* %135)
br label %137
137: ; preds = %2, %133
%138 = phi i32 [ %134, %133 ], [ -1, %2 ]
ret i32 %138
}