#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_usec;
  scalar_t__ tv_sec;
};
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;
};
typedef int /*<<< orphan*/ hints;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ EINPROGRESS;
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*/ PF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ asprintf(char **, char *, int);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ errno;
scalar_t__ fcntl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char const *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int sm_connect(const char *hostname, int port) {
  struct addrinfo hints;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = PF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  struct addrinfo *res0;
  char *port_str = NULL;
  if (asprintf(&port_str, "%d", port) < 0) {
    return -1;
  }
  int ret = getaddrinfo(hostname, port_str, &hints, &res0);
  free(port_str);
  if (ret) {
    perror("Unknown host");
    return (ret < 0 ? ret : -1);
  }
  ret = -1;
  int fd = 0;
  struct addrinfo *res;
  for (res = res0; res; res = res->ai_next) {
    if (fd > 0) {
      close(fd);
    }
    fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    if (fd < 0) {
      continue;
    }
    int opts = fcntl(fd, F_GETFL);
    if (opts < 0 || fcntl(fd, F_SETFL, (opts | O_NONBLOCK)) < 0 ||
        ((connect(fd, res->ai_addr, res->ai_addrlen) < 0) ==
         (errno != EINPROGRESS))) {
      continue;
    }
    struct timeval to;
    to.tv_sec = 0;
    to.tv_usec = 500 * 1000;
    fd_set error_fds;
    FD_ZERO(&error_fds);
    FD_SET(fd, &error_fds);
    if (fcntl(fd, F_SETFL, opts) < 0) {
      continue;
    }
    int is_error = select(fd + 1, &error_fds, NULL, NULL, &to);
    if (is_error) {
      continue;
    }
    if (fcntl(fd, F_SETFL, (opts | O_NONBLOCK)) < 0) {
      continue;
    }
    ret = fd;
    break;
  }
  if (fd > 0 && ret <= 0) {
    close(fd);
  }
  freeaddrinfo(res0);
  return ret;
}


x86 assembly (clang, -O0):
<sm_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2dbb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x14(%rbp),%edx
lea    -0x48(%rbp),%rdi
lea    0xde0(%rip),%rsi
call   1090 <asprintf@plt>
cmp    $0x0,%rax
jge    123b <sm_connect+0x6b>
movl   $0xffffffff,-0x4(%rbp)
jmp    147c <sm_connect+0x2ac>
mov    -0x10(%rbp),%rdi
mov    -0x48(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   10c0 <getaddrinfo@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0x4c(%rbp)
je     129f <sm_connect+0xcf>
lea    0xd96(%rip),%rdi
call   10a0 <perror@plt>
cmpl   $0x0,-0x4c(%rbp)
jge    1287 <sm_connect+0xb7>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x7c(%rbp)
jmp    1294 <sm_connect+0xc4>
mov    $0xffffffff,%eax
mov    %eax,-0x7c(%rbp)
jmp    1294 <sm_connect+0xc4>
mov    -0x7c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    147c <sm_connect+0x2ac>
movl   $0xffffffff,-0x4c(%rbp)
movl   $0x0,-0x50(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
je     1451 <sm_connect+0x281>
cmpl   $0x0,-0x50(%rbp)
jle    12d2 <sm_connect+0x102>
mov    -0x50(%rbp),%edi
call   1060 <close@plt>
mov    -0x58(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x58(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x58(%rbp),%rax
mov    0x8(%rax),%edx
call   10f0 <socket@plt>
mov    %eax,-0x50(%rbp)
cmpl   $0x0,-0x50(%rbp)
jge    12fe <sm_connect+0x12e>
jmp    1440 <sm_connect+0x270>
mov    -0x50(%rbp),%edi
mov    0x2ca8(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1040 <fcntl@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0x0,-0x5c(%rbp)
jl     138d <sm_connect+0x1bd>
mov    -0x50(%rbp),%edi
mov    0x2ca0(%rip),%rax
mov    (%rax),%esi
mov    -0x5c(%rbp),%edx
mov    0x2cac(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1040 <fcntl@plt>
cmp    $0x0,%rax
jl     138d <sm_connect+0x1bd>
mov    -0x50(%rbp),%edi
mov    -0x58(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x58(%rbp),%rax
mov    (%rax),%edx
call   10b0 <connect@plt>
cmp    $0x0,%rax
setl   %al
and    $0x1,%al
movzbl %al,%eax
mov    0x2c49(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2c5f(%rip),%rdx
cmp    (%rdx),%rcx
setne  %cl
and    $0x1,%cl
movzbl %cl,%ecx
cmp    %ecx,%eax
jne    1392 <sm_connect+0x1c2>
jmp    1440 <sm_connect+0x270>
movq   $0x0,-0x68(%rbp)
movl   $0x7a120,-0x70(%rbp)
lea    -0x74(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    -0x50(%rbp),%edi
lea    -0x74(%rbp),%rsi
call   10d0 <FD_SET@plt>
mov    -0x50(%rbp),%edi
mov    0x2c08(%rip),%rax
mov    (%rax),%esi
mov    -0x5c(%rbp),%edx
mov    $0x0,%al
call   1040 <fcntl@plt>
cmp    $0x0,%rax
jge    13db <sm_connect+0x20b>
jmp    1440 <sm_connect+0x270>
mov    -0x50(%rbp),%edi
add    $0x1,%edi
lea    -0x74(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x70(%rbp),%r8
mov    %rcx,%rdx
call   1080 <select@plt>
mov    %eax,-0x78(%rbp)
cmpl   $0x0,-0x78(%rbp)
je     1407 <sm_connect+0x237>
jmp    1440 <sm_connect+0x270>
mov    -0x50(%rbp),%edi
mov    0x2bb7(%rip),%rax
mov    (%rax),%esi
mov    -0x5c(%rbp),%edx
mov    0x2bc3(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1040 <fcntl@plt>
cmp    $0x0,%rax
jge    1435 <sm_connect+0x265>
jmp    1440 <sm_connect+0x270>
mov    -0x50(%rbp),%eax
mov    %eax,-0x4c(%rbp)
jmp    1451 <sm_connect+0x281>
mov    -0x58(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x58(%rbp)
jmp    12b5 <sm_connect+0xe5>
cmpl   $0x0,-0x50(%rbp)
jle    146d <sm_connect+0x29d>
cmpl   $0x0,-0x4c(%rbp)
jg     146d <sm_connect+0x29d>
mov    -0x50(%rbp),%edi
call   1060 <close@plt>
mov    -0x40(%rbp),%rdi
call   10e0 <freeaddrinfo@plt>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sm_connect>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0x28(%rsp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x38(%rsp)
mov    0x2db8(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x34(%rsp)
movq   $0x0,(%rsp)
lea    0xde3(%rip),%rsi
mov    %rsp,%rdi
mov    %ebp,%edx
call   1090 <asprintf@plt>
test   %rax,%rax
js     13ba <sm_connect+0x1ea>
mov    (%rsp),%rsi
lea    0x28(%rsp),%rdx
lea    0x8(%rsp),%rcx
mov    %rbx,%rdi
call   10c0 <getaddrinfo@plt>
mov    %eax,%ebx
mov    (%rsp),%rdi
call   1030 <free@plt>
test   %ebx,%ebx
jne    13f7 <sm_connect+0x227>
mov    0x8(%rsp),%rbp
test   %rbp,%rbp
je     13d7 <sm_connect+0x207>
mov    0x2d42(%rip),%r13
mov    0x2d53(%rip),%r15
mov    0x2d64(%rip),%r14
xor    %ebx,%ebx
jmp    12ad <sm_connect+0xdd>
mov    0x2d59(%rip),%r14
mov    %r13,%r15
mov    0x2d1f(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x18(%rbp),%rbp
test   %rbp,%rbp
je     13c1 <sm_connect+0x1f1>
test   %ebx,%ebx
jle    12b8 <sm_connect+0xe8>
mov    %ebx,%edi
call   1060 <close@plt>
mov    0x10(%rbp),%edi
mov    0x8(%rbp),%edx
mov    0xc(%rbp),%esi
call   10f0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     12a0 <sm_connect+0xd0>
mov    0x0(%r13),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1040 <fcntl@plt>
mov    %rax,%r12
test   %r12d,%r12d
js     12a0 <sm_connect+0xd0>
mov    (%r15),%esi
mov    (%r14),%edx
or     %r12d,%edx
mov    %ebx,%edi
xor    %eax,%eax
call   1040 <fcntl@plt>
test   %rax,%rax
js     12a0 <sm_connect+0xd0>
mov    0x0(%rbp),%edx
mov    0x4(%rbp),%esi
mov    %ebx,%edi
call   10b0 <connect@plt>
shr    $0x3f,%rax
mov    0x2ca8(%rip),%rcx
mov    (%rcx),%rcx
xor    %edx,%edx
mov    0x2cbc(%rip),%rsi
cmp    (%rsi),%rcx
setne  %dl
cmp    %edx,%eax
je     12a0 <sm_connect+0xd0>
movq   $0x0,0x20(%rsp)
movl   $0x7a120,0x18(%rsp)
lea    0x14(%rsp),%r14
mov    %r14,%rdi
call   1070 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   10d0 <FD_SET@plt>
mov    %r15,%r13
mov    (%r15),%esi
mov    %ebx,%edi
mov    %r12d,%edx
xor    %eax,%eax
call   1040 <fcntl@plt>
test   %rax,%rax
js     1280 <sm_connect+0xb0>
lea    0x1(%rbx),%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
lea    0x18(%rsp),%r8
call   1080 <select@plt>
test   %eax,%eax
jne    1280 <sm_connect+0xb0>
mov    %r13,%r15
mov    0x0(%r13),%esi
mov    0x2c49(%rip),%r14
or     (%r14),%r12d
mov    %ebx,%edi
mov    %r12d,%edx
xor    %eax,%eax
call   1040 <fcntl@plt>
test   %rax,%rax
mov    0x2c00(%rip),%r13
js     12a0 <sm_connect+0xd0>
mov    %ebx,%ebp
jmp    13c6 <sm_connect+0x1f6>
mov    $0xffffffff,%ebp
jmp    13e6 <sm_connect+0x216>
mov    $0xffffffff,%ebp
test   %ebx,%ebx
jle    13dc <sm_connect+0x20c>
test   %ebp,%ebp
jg     13dc <sm_connect+0x20c>
mov    %ebx,%edi
call   1060 <close@plt>
jmp    13dc <sm_connect+0x20c>
mov    $0xffffffff,%ebp
mov    0x8(%rsp),%rdi
call   10e0 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc05(%rip),%rdi
call   10a0 <perror@plt>
test   %ebx,%ebx
mov    $0xffffffff,%ebp
cmovs  %ebx,%ebp
jmp    13e6 <sm_connect+0x216>

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

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
%struct.timeval = type { i32, i64 }
@PF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [3 x i8] c"%d\00"
@.str.1 = [13 x i8] c"Unknown host\00"
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@errno = global i64 0
@EINPROGRESS = global i64 0
define i32 @sm_connect(i8* %0, i32 %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = alloca i8*
%6 = alloca %struct.timeval
%7 = alloca i32
%8 = bitcast %struct.addrinfo* %3 to i8*
%9 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 32)
%10 = load i32, i32* @PF_UNSPEC
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
store i32 %10, i32* %11
%12 = load i32, i32* @SOCK_STREAM
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %12, i32* %13
%14 = bitcast %struct.addrinfo** %4 to i8*
%15 = bitcast i8** %5 to i8*
store i8* null, i8** %5
%16 = call i64 @asprintf(i8** %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %1)
%17 = icmp slt i64 %16, 0
br i1 %17, label %107, label %18
18: ; preds = %2
%19 = load i8*, i8** %5
%20 = call i32 @getaddrinfo(i8* %0, i8* %19, %struct.addrinfo* %3, %struct.addrinfo** %4)
%21 = load i8*, i8** %5
%22 = call i32 @free(i8* %21)
%23 = icmp eq i32 %20, 0
br i1 %23, label %24, label %32
24: ; preds = %18
%25 = load %struct.addrinfo*, %struct.addrinfo** %4
%26 = icmp eq %struct.addrinfo* %25, null
br i1 %26, label %103, label %27
27: ; preds = %24
%28 = bitcast %struct.timeval* %6 to i8*
%29 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
%30 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
%31 = bitcast i32* %7 to i8*
br label %36
32: ; preds = %18
%33 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%34 = icmp slt i32 %20, -1
%35 = select i1 %34, i32 %20, i32 -1
br label %107
36: ; preds = %27, %92
%37 = phi %struct.addrinfo* [ %25, %27 ], [ %94, %92 ]
%38 = phi i32 [ 0, %27 ], [ %49, %92 ]
%39 = icmp sgt i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = call i32 @close(i32 %38)
br label %42
42: ; preds = %40, %36
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i64 0, i32 4
%44 = load i32, i32* %43
%45 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i64 0, i32 3
%46 = load i32, i32* %45
%47 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i64 0, i32 2
%48 = load i32, i32* %47
%49 = call i32 @socket(i32 %44, i32 %46, i32 %48)
%50 = icmp slt i32 %49, 0
br i1 %50, label %92, label %51
51: ; preds = %42
%52 = load i32, i32* @F_GETFL
%53 = call i64 (i32, i32, ...) @fcntl(i32 %49, i32 %52)
%54 = trunc i64 %53 to i32
%55 = icmp slt i32 %54, 0
br i1 %55, label %92, label %56
56: ; preds = %51
%57 = load i32, i32* @F_SETFL
%58 = load i32, i32* @O_NONBLOCK
%59 = or i32 %58, %54
%60 = call i64 (i32, i32, ...) @fcntl(i32 %49, i32 %57, i32 %59)
%61 = icmp slt i64 %60, 0
br i1 %61, label %92, label %62
62: ; preds = %56
%63 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i64 0, i32 1
%64 = load i32, i32* %63
%65 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i64 0, i32 0
%66 = load i32, i32* %65
%67 = call i64 @connect(i32 %49, i32 %64, i32 %66)
%68 = lshr i64 %67, 63
%69 = trunc i64 %68 to i32
%70 = load i64, i64* @errno
%71 = load i64, i64* @EINPROGRESS
%72 = icmp ne i64 %70, %71
%73 = zext i1 %72 to i32
%74 = icmp eq i32 %69, %73
br i1 %74, label %92, label %75
75: ; preds = %62
store i64 0, i64* %29
store i32 500000, i32* %30
%76 = call i32 @FD_ZERO(i32* %7)
%77 = call i32 @FD_SET(i32 %49, i32* %7)
%78 = load i32, i32* @F_SETFL
%79 = call i64 (i32, i32, ...) @fcntl(i32 %49, i32 %78, i32 %54)
%80 = icmp slt i64 %79, 0
br i1 %80, label %85, label %81
81: ; preds = %75
%82 = add nuw nsw i32 %49, 1
%83 = call i32 @select(i32 %82, i32* %7, i32* null, i32* null, %struct.timeval* %6)
%84 = icmp eq i32 %83, 0
br i1 %84, label %86, label %85
85: ; preds = %75, %81
br label %92
86: ; preds = %81
%87 = load i32, i32* @F_SETFL
%88 = load i32, i32* @O_NONBLOCK
%89 = or i32 %88, %54
%90 = call i64 (i32, i32, ...) @fcntl(i32 %49, i32 %87, i32 %89)
%91 = icmp slt i64 %90, 0
br i1 %91, label %92, label %96
92: ; preds = %51, %56, %62, %85, %86, %42
%93 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i64 0, i32 5
%94 = load %struct.addrinfo*, %struct.addrinfo** %93
%95 = icmp eq %struct.addrinfo* %94, null
br i1 %95, label %96, label %36
96: ; preds = %92, %86
%97 = phi i32 [ -1, %92 ], [ %49, %86 ]
%98 = icmp sgt i32 %49, 0
%99 = icmp slt i32 %97, 1
%100 = and i1 %98, %99
br i1 %100, label %101, label %103
101: ; preds = %96
%102 = call i32 @close(i32 %49)
br label %103
103: ; preds = %24, %101, %96
%104 = phi i32 [ %97, %101 ], [ %97, %96 ], [ -1, %24 ]
%105 = load %struct.addrinfo*, %struct.addrinfo** %4
%106 = call i32 @freeaddrinfo(%struct.addrinfo* %105)
br label %107
107: ; preds = %32, %103, %2
%108 = phi i32 [ -1, %2 ], [ %35, %32 ], [ %104, %103 ]
ret i32 %108
}