#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 */
typedef int uint16_t;
typedef int /*<<< orphan*/ time_t;
struct addrinfo {
  int ai_family;
  int ai_socktype;
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_flags;
};

/* Variables and functions */
int /*<<< orphan*/ AI_NUMERICSERV;
#define EAGAIN 129
#define ETIMEDOUT 128
int /*<<< orphan*/ IPPROTO_TCP;
int const SOCK_STREAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int errno;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char const *const, char *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ set_send_timeout(int, int /*<<< orphan*/ const);
int socket(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, int const);
int connect_to_listening_socket(const char *const interface,
                                const uint16_t port, const int pf_family,
                                const int socket_type, const time_t timeout) {
  int res;
  int sock;
  struct addrinfo hint;
  struct addrinfo *ai;
  struct addrinfo *ai_current;
  char pstr[32] = {'\0'};
  int close_socket = 1;
  sock = -1;
  if (SOCK_STREAM != socket_type)
    return sock;
  memset(&hint, 0, sizeof(struct addrinfo));
  hint.ai_flags = AI_NUMERICSERV;
  hint.ai_family = pf_family;
  hint.ai_socktype = socket_type;
  hint.ai_protocol = IPPROTO_TCP;
  sprintf(pstr, "%u", port);
  res = getaddrinfo(interface, pstr, &hint, &ai);
  if (res) {
    return sock;
  }
  for (ai_current = ai; ai_current; ai_current = ai_current->ai_next) {
    if (-1 == sock) {
      close(sock);
      sock = -1;
    }
    sock = socket(ai_current->ai_family, ai_current->ai_socktype,
                  ai_current->ai_protocol);
    if (-1 == sock)
      continue;
    if (!set_send_timeout(sock, timeout)) {
      continue;
    }
    do {
      if (!connect(sock, ai_current->ai_addr, ai_current->ai_addrlen)) {
        close_socket = 0;
        goto done;
      }
      switch (errno) {
      case EAGAIN:
      case ETIMEDOUT:
        goto done;
      default:;
      }
      break;
    } while (1);
  }
done:
  freeaddrinfo(ai);
  if (close_socket) {
    close(sock);
    sock = -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<connect_to_listening_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
lea    -0x80(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
movl   $0x1,-0x84(%rbp)
movl   $0xffffffff,-0x28(%rbp)
mov    0x2e0c(%rip),%rax
mov    (%rax),%eax
cmp    -0x1c(%rbp),%eax
je     11da <connect_to_listening_socket+0x5a>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1368 <connect_to_listening_socket+0x1e8>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2e07(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    0x2dd7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
lea    -0x80(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xde8(%rip),%rsi
call   1060 <sprintf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x80(%rbp),%rsi
lea    -0x50(%rbp),%rdx
lea    -0x58(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     124e <connect_to_listening_socket+0xce>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1368 <connect_to_listening_socket+0x1e8>
mov    -0x58(%rbp),%rax
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
je     1338 <connect_to_listening_socket+0x1b8>
mov    $0xffffffff,%eax
cmp    -0x28(%rbp),%eax
jne    127e <connect_to_listening_socket+0xfe>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x28(%rbp)
mov    -0x60(%rbp),%rax
mov    (%rax),%edi
mov    -0x60(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x60(%rbp),%rax
mov    0x10(%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x28(%rbp)
mov    $0xffffffff,%eax
cmp    -0x28(%rbp),%eax
jne    12ad <connect_to_listening_socket+0x12d>
jmp    1327 <connect_to_listening_socket+0x1a7>
mov    -0x28(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1030 <set_send_timeout@plt>
cmp    $0x0,%eax
jne    12c6 <connect_to_listening_socket+0x146>
jmp    1327 <connect_to_listening_socket+0x1a7>
jmp    12cb <connect_to_listening_socket+0x14b>
mov    -0x28(%rbp),%edi
mov    -0x60(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x60(%rbp),%rax
mov    0x8(%rax),%edx
call   1070 <connect@plt>
cmp    $0x0,%eax
jne    12f9 <connect_to_listening_socket+0x179>
movl   $0x0,-0x84(%rbp)
jmp    133d <connect_to_listening_socket+0x1bd>
mov    0x2cc8(%rip),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
sub    $0x1,%eax
ja     1318 <connect_to_listening_socket+0x198>
jmp    1313 <connect_to_listening_socket+0x193>
jmp    133d <connect_to_listening_socket+0x1bd>
jmp    131d <connect_to_listening_socket+0x19d>
jmp    1322 <connect_to_listening_socket+0x1a2>
jmp    1327 <connect_to_listening_socket+0x1a7>
mov    -0x60(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x60(%rbp)
jmp    1256 <connect_to_listening_socket+0xd6>
jmp    133d <connect_to_listening_socket+0x1bd>
mov    -0x58(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
cmpl   $0x0,-0x84(%rbp)
je     1362 <connect_to_listening_socket+0x1e2>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to_listening_socket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
mov    $0xffffffff,%r14d
test   %ecx,%ecx
jne    12b3 <connect_to_listening_socket+0x133>
mov    %r8d,%r15d
mov    %edx,%ebx
mov    %esi,%ebp
mov    %rdi,%r12
lea    0x8(%rsp),%r13
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    %ebx,0x8(%rsp)
movl   $0x0,0xc(%rsp)
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
lea    0xe10(%rip),%rsi
lea    0x30(%rsp),%rbx
mov    %rbx,%rdi
mov    %ebp,%edx
call   1060 <sprintf@plt>
mov    %rsp,%rcx
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %r13,%rdx
call   1080 <getaddrinfo@plt>
test   %eax,%eax
jne    12b3 <connect_to_listening_socket+0x133>
mov    (%rsp),%rbp
mov    $0xffffffff,%r14d
test   %rbp,%rbp
je     12a0 <connect_to_listening_socket+0x120>
mov    $0xffffffff,%ebx
mov    0x2d99(%rip),%r12
jmp    1249 <connect_to_listening_socket+0xc9>
nopl   0x0(%rax)
mov    0x18(%rbp),%rbp
test   %rbp,%rbp
je     129a <connect_to_listening_socket+0x11a>
cmp    $0xffffffff,%ebx
jne    1258 <connect_to_listening_socket+0xd8>
mov    $0xffffffff,%edi
call   1050 <close@plt>
mov    0x0(%rbp),%edi
mov    0x4(%rbp),%esi
mov    0x10(%rbp),%edx
call   10a0 <socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1240 <connect_to_listening_socket+0xc0>
mov    %ebx,%edi
mov    %r15d,%esi
call   1030 <set_send_timeout@plt>
test   %eax,%eax
je     1240 <connect_to_listening_socket+0xc0>
mov    0x8(%rbp),%edx
mov    0xc(%rbp),%esi
mov    %ebx,%edi
call   1070 <connect@plt>
test   %eax,%eax
je     12c5 <connect_to_listening_socket+0x145>
mov    (%r12),%eax
and    $0xfffffffe,%eax
cmp    $0x80,%eax
jne    1240 <connect_to_listening_socket+0xc0>
mov    (%rsp),%rdi
jmp    12a7 <connect_to_listening_socket+0x127>
xor    %edi,%edi
mov    $0xffffffff,%ebx
call   1090 <freeaddrinfo@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %r14d,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsp),%rdi
call   1090 <freeaddrinfo@plt>
mov    %ebx,%r14d
jmp    12b3 <connect_to_listening_socket+0x133>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32 }
@SOCK_STREAM = constant i32 0
@AI_NUMERICSERV = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [3 x i8] c"%u\00"
@errno = global i32 0
define i32 @connect_to_listening_socket(i8* %0, i32 %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca %struct.addrinfo
%15 = alloca %struct.addrinfo*
%16 = alloca %struct.addrinfo*
%17 = alloca [32 x i8]
%18 = alloca i32
store i8* %0, i8** %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
%19 = bitcast [32 x i8]* %17 to i8*
store i32 1, i32* %18
store i32 -1, i32* %13
%20 = load i32, i32* @SOCK_STREAM
%21 = load i32, i32* %10
%22 = icmp ne i32 %20, %21
br i1 %22, label %23, label %25
23: ; preds = %5
%24 = load i32, i32* %13
store i32 %24, i32* %6
br label %110
25: ; preds = %5
%26 = call i32 @memset(%struct.addrinfo* %14, i32 0, i32 40)
%27 = load i32, i32* @AI_NUMERICSERV
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i32 0, i32 6
store i32 %27, i32* %28
%29 = load i32, i32* %9
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i32 0, i32 0
store i32 %29, i32* %30
%31 = load i32, i32* %10
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i32 0, i32 1
store i32 %31, i32* %32
%33 = load i32, i32* @IPPROTO_TCP
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i32 0, i32 4
store i32 %33, i32* %34
%35 = getelementptr inbounds [32 x i8], [32 x i8]* %17, i64 0, i64 0
%36 = load i32, i32* %8
%37 = call i32 @sprintf(i8* %35, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %36)
%38 = load i8*, i8** %7
%39 = getelementptr inbounds [32 x i8], [32 x i8]* %17, i64 0, i64 0
%40 = call i32 @getaddrinfo(i8* %38, i8* %39, %struct.addrinfo* %14, %struct.addrinfo** %15)
store i32 %40, i32* %12
%41 = load i32, i32* %12
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %25
%44 = load i32, i32* %13
store i32 %44, i32* %6
br label %110
45: ; preds = %25
%46 = load %struct.addrinfo*, %struct.addrinfo** %15
store %struct.addrinfo* %46, %struct.addrinfo** %16
br label %47
47: ; preds = %95, %45
%48 = load %struct.addrinfo*, %struct.addrinfo** %16
%49 = icmp ne %struct.addrinfo* %48, null
br i1 %49, label %50, label %99
50: ; preds = %47
%51 = load i32, i32* %13
%52 = icmp eq i32 -1, %51
br i1 %52, label %53, label %56
53: ; preds = %50
%54 = load i32, i32* %13
%55 = call i32 @close(i32 %54)
store i32 -1, i32* %13
br label %56
56: ; preds = %53, %50
%57 = load %struct.addrinfo*, %struct.addrinfo** %16
%58 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %57, i32 0, i32 0
%59 = load i32, i32* %58
%60 = load %struct.addrinfo*, %struct.addrinfo** %16
%61 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %60, i32 0, i32 1
%62 = load i32, i32* %61
%63 = load %struct.addrinfo*, %struct.addrinfo** %16
%64 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %63, i32 0, i32 4
%65 = load i32, i32* %64
%66 = call i32 @socket(i32 %59, i32 %62, i32 %65)
store i32 %66, i32* %13
%67 = load i32, i32* %13
%68 = icmp eq i32 -1, %67
br i1 %68, label %69, label %70
69: ; preds = %56
br label %95
70: ; preds = %56
%71 = load i32, i32* %13
%72 = load i32, i32* %11
%73 = call i32 @set_send_timeout(i32 %71, i32 %72)
%74 = icmp ne i32 %73, 0
br i1 %74, label %76, label %75
75: ; preds = %70
br label %95
76: ; preds = %70
br label %77
77: ; preds = %93, %76
%78 = load i32, i32* %13
%79 = load %struct.addrinfo*, %struct.addrinfo** %16
%80 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %79, i32 0, i32 3
%81 = load i32, i32* %80
%82 = load %struct.addrinfo*, %struct.addrinfo** %16
%83 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %82, i32 0, i32 2
%84 = load i32, i32* %83
%85 = call i32 @connect(i32 %78, i32 %81, i32 %84)
%86 = icmp ne i32 %85, 0
br i1 %86, label %88, label %87
87: ; preds = %77
store i32 0, i32* %18
br label %100
88: ; preds = %77
%89 = load i32, i32* @errno
switch i32 %89, label %91 [
i32 129, label %90
i32 128, label %90
]
90: ; preds = %88, %88
br label %100
91: ; preds = %88
br label %92
92: ; preds = %91
br label %94
93: ; No predecessors!
br i1 true, label %77, label %94
94: ; preds = %93, %92
br label %95
95: ; preds = %94, %75, %69
%96 = load %struct.addrinfo*, %struct.addrinfo** %16
%97 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %96, i32 0, i32 5
%98 = load %struct.addrinfo*, %struct.addrinfo** %97
store %struct.addrinfo* %98, %struct.addrinfo** %16
br label %47
99: ; preds = %47
br label %100
100: ; preds = %99, %90, %87
%101 = load %struct.addrinfo*, %struct.addrinfo** %15
%102 = call i32 @freeaddrinfo(%struct.addrinfo* %101)
%103 = load i32, i32* %18
%104 = icmp ne i32 %103, 0
br i1 %104, label %105, label %108
105: ; preds = %100
%106 = load i32, i32* %13
%107 = call i32 @close(i32 %106)
store i32 -1, i32* %13
br label %108
108: ; preds = %105, %100
%109 = load i32, i32* %13
store i32 %109, i32* %6
br label %110
110: ; preds = %108, %43, %23
%111 = load i32, i32* %6
ret i32 %111
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32 }
@SOCK_STREAM = constant i32 0
@AI_NUMERICSERV = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [3 x i8] c"%u\00"
@errno = global i32 0
define i32 @connect_to_listening_socket(i8* %0, i32 %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca %struct.addrinfo
%7 = alloca %struct.addrinfo*
%8 = alloca [32 x i8]
%9 = bitcast %struct.addrinfo* %6 to i8*
%10 = bitcast %struct.addrinfo** %7 to i8*
%11 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%12 = icmp eq i32 %3, 0
br i1 %12, label %13, label %70
13: ; preds = %5
%14 = call i32 @memset(%struct.addrinfo* %6, i32 0, i32 40)
%15 = load i32, i32* @AI_NUMERICSERV
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i64 0, i32 6
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i64 0, i32 0
store i32 %2, i32* %17
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i64 0, i32 1
store i32 0, i32* %18
%19 = load i32, i32* @IPPROTO_TCP
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i64 0, i32 4
store i32 %19, i32* %20
%21 = call i32 @sprintf(i8* %11, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %1)
%22 = call i32 @getaddrinfo(i8* %0, i8* %11, %struct.addrinfo* %6, %struct.addrinfo** %7)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %70
24: ; preds = %13
%25 = load %struct.addrinfo*, %struct.addrinfo** %7
%26 = icmp eq %struct.addrinfo* %25, null
br i1 %26, label %65, label %27
27: ; preds = %24, %56
%28 = phi %struct.addrinfo* [ %58, %56 ], [ %25, %24 ]
%29 = phi i32 [ %40, %56 ], [ -1, %24 ]
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = call i32 @close(i32 -1)
br label %33
33: ; preds = %31, %27
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 0
%35 = load i32, i32* %34
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 1
%37 = load i32, i32* %36
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 4
%39 = load i32, i32* %38
%40 = call i32 @socket(i32 %35, i32 %37, i32 %39)
%41 = icmp eq i32 %40, -1
br i1 %41, label %56, label %42
42: ; preds = %33
%43 = call i32 @set_send_timeout(i32 %40, i32 %4)
%44 = icmp eq i32 %43, 0
br i1 %44, label %56, label %45
45: ; preds = %42
%46 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 3
%47 = load i32, i32* %46
%48 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 2
%49 = load i32, i32* %48
%50 = call i32 @connect(i32 %40, i32 %47, i32 %49)
%51 = icmp eq i32 %50, 0
br i1 %51, label %60, label %52
52: ; preds = %45
%53 = load i32, i32* @errno
%54 = and i32 %53, -2
%55 = icmp eq i32 %54, 128
br i1 %55, label %63, label %56
56: ; preds = %52, %42, %33
%57 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 5
%58 = load %struct.addrinfo*, %struct.addrinfo** %57
%59 = icmp eq %struct.addrinfo* %58, null
br i1 %59, label %63, label %27
60: ; preds = %45
%61 = load %struct.addrinfo*, %struct.addrinfo** %7
%62 = call i32 @freeaddrinfo(%struct.addrinfo* %61)
br label %70
63: ; preds = %52, %56
%64 = load %struct.addrinfo*, %struct.addrinfo** %7
br label %65
65: ; preds = %63, %24
%66 = phi %struct.addrinfo* [ null, %24 ], [ %64, %63 ]
%67 = phi i32 [ -1, %24 ], [ %40, %63 ]
%68 = call i32 @freeaddrinfo(%struct.addrinfo* %66)
%69 = call i32 @close(i32 %67)
br label %70
70: ; preds = %60, %65, %13, %5
%71 = phi i32 [ -1, %5 ], [ -1, %13 ], [ -1, %65 ], [ %40, %60 ]
ret i32 %71
}