#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 addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};
typedef int /*<<< orphan*/ in_port_t;

/* Variables and functions */
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
struct addrinfo *lookuphost(char const *, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int connect_server(const char *hostname, in_port_t port, bool nonblock) {
  struct addrinfo *ai = lookuphost(hostname, port);
  int sock = -1;
  if (ai != NULL) {
    if ((sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol)) !=
        -1) {
      if (connect(sock, ai->ai_addr, ai->ai_addrlen) == -1) {
        fprintf(stderr, "Failed to connect socket: %s\n", strerror(errno));
        close(sock);
        sock = -1;
      } else if (nonblock) {
        int flags = fcntl(sock, F_GETFL, 0);
        if (flags < 0 || fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0) {
          fprintf(stderr, "Failed to enable nonblocking mode: %s\n",
                  strerror(errno));
          close(sock);
          sock = -1;
        }
      }
    } else {
      fprintf(stderr, "Failed to create socket: %s\n", strerror(errno));
    }
    freeaddrinfo(ai);
  }
  return sock;
}


x86 assembly (clang, -O0):
<connect_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1060 <lookuphost@plt>
mov    %rax,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1307 <connect_server+0x187>
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x18(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
je     12d2 <connect_server+0x152>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
jne    1238 <connect_server+0xb8>
mov    0x2dd9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xde1(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x1c(%rbp)
jmp    12cd <connect_server+0x14d>
cmpl   $0x0,-0x10(%rbp)
je     12c8 <connect_server+0x148>
mov    -0x1c(%rbp),%edi
mov    0x2d74(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jl     1288 <connect_server+0x108>
mov    -0x1c(%rbp),%edi
mov    0x2d64(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%edx
mov    0x2d70(%rip),%rax
or     (%rax),%edx
call   1030 <fcntl@plt>
cmp    $0x0,%eax
jge    12c3 <connect_server+0x143>
mov    0x2d49(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d2d(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
lea    0xd6f(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x1c(%rbp)
jmp    12c8 <connect_server+0x148>
jmp    12cd <connect_server+0x14d>
jmp    12fe <connect_server+0x17e>
mov    0x2cff(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2ce3(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xd4c(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_server>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
call   1060 <lookuphost@plt>
test   %rax,%rax
je     1229 <connect_server+0xa9>
mov    %rax,%rbx
mov    0x10(%rax),%edi
mov    0x8(%rax),%edx
mov    0xc(%rax),%esi
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     1230 <connect_server+0xb0>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
je     125a <connect_server+0xda>
test   %r14d,%r14d
je     1290 <connect_server+0x110>
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
test   %eax,%eax
js     1208 <connect_server+0x88>
mov    0x2de4(%rip),%rcx
mov    (%rcx),%esi
mov    0x2df3(%rip),%rcx
or     (%rcx),%eax
mov    %ebp,%edi
mov    %eax,%edx
call   1030 <fcntl@plt>
test   %eax,%eax
jns    1290 <connect_server+0x110>
mov    0x2dc9(%rip),%rax
mov    (%rax),%r14d
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdf7(%rip),%rsi
jmp    1279 <connect_server+0xf9>
mov    $0xffffffff,%ebp
jmp    1298 <connect_server+0x118>
mov    0x2da1(%rip),%rax
mov    (%rax),%ebp
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdf7(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
jmp    128b <connect_server+0x10b>
mov    0x2d77(%rip),%rax
mov    (%rax),%r14d
mov    0x2d5d(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xd87(%rip),%rsi
mov    %r14d,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1090 <freeaddrinfo@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@stderr = global i32 0
@.str = [30 x i8] c"Failed to connect socket: %s\0A\00"
@errno = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [39 x i8] c"Failed to enable nonblocking mode: %s\0A\00"
@.str.2 = [29 x i8] c"Failed to create socket: %s\0A\00"
define i32 @connect_server(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.addrinfo*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%10 = load i8*, i8** %4
%11 = load i32, i32* %5
%12 = call %struct.addrinfo* @lookuphost(i8* %10, i32 %11)
store %struct.addrinfo* %12, %struct.addrinfo** %7
store i32 -1, i32* %8
%13 = load %struct.addrinfo*, %struct.addrinfo** %7
%14 = icmp ne %struct.addrinfo* %13, null
br i1 %14, label %15, label %79
15: ; preds = %3
%16 = load %struct.addrinfo*, %struct.addrinfo** %7
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i32 0, i32 4
%18 = load i32, i32* %17
%19 = load %struct.addrinfo*, %struct.addrinfo** %7
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %19, i32 0, i32 3
%21 = load i32, i32* %20
%22 = load %struct.addrinfo*, %struct.addrinfo** %7
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i32 0, i32 2
%24 = load i32, i32* %23
%25 = call i32 @socket(i32 %18, i32 %21, i32 %24)
store i32 %25, i32* %8
%26 = icmp ne i32 %25, -1
br i1 %26, label %27, label %71
27: ; preds = %15
%28 = load i32, i32* %8
%29 = load %struct.addrinfo*, %struct.addrinfo** %7
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %29, i32 0, i32 1
%31 = load i32, i32* %30
%32 = load %struct.addrinfo*, %struct.addrinfo** %7
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %32, i32 0, i32 0
%34 = load i32, i32* %33
%35 = call i32 @connect(i32 %28, i32 %31, i32 %34)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %44
37: ; preds = %27
%38 = load i32, i32* @stderr
%39 = load i32, i32* @errno
%40 = call i8* @strerror(i32 %39)
%41 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %40)
%42 = load i32, i32* %8
%43 = call i32 @close(i32 %42)
store i32 -1, i32* %8
br label %70
44: ; preds = %27
%45 = load i32, i32* %6
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %69
47: ; preds = %44
%48 = load i32, i32* %8
%49 = load i32, i32* @F_GETFL
%50 = call i32 @fcntl(i32 %48, i32 %49, i32 0)
store i32 %50, i32* %9
%51 = load i32, i32* %9
%52 = icmp slt i32 %51, 0
br i1 %52, label %61, label %53
53: ; preds = %47
%54 = load i32, i32* %8
%55 = load i32, i32* @F_SETFL
%56 = load i32, i32* %9
%57 = load i32, i32* @O_NONBLOCK
%58 = or i32 %56, %57
%59 = call i32 @fcntl(i32 %54, i32 %55, i32 %58)
%60 = icmp slt i32 %59, 0
br i1 %60, label %61, label %68
61: ; preds = %53, %47
%62 = load i32, i32* @stderr
%63 = load i32, i32* @errno
%64 = call i8* @strerror(i32 %63)
%65 = call i32 @fprintf(i32 %62, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %64)
%66 = load i32, i32* %8
%67 = call i32 @close(i32 %66)
store i32 -1, i32* %8
br label %68
68: ; preds = %61, %53
br label %69
69: ; preds = %68, %44
br label %70
70: ; preds = %69, %37
br label %76
71: ; preds = %15
%72 = load i32, i32* @stderr
%73 = load i32, i32* @errno
%74 = call i8* @strerror(i32 %73)
%75 = call i32 @fprintf(i32 %72, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %74)
br label %76
76: ; preds = %71, %70
%77 = load %struct.addrinfo*, %struct.addrinfo** %7
%78 = call i32 @freeaddrinfo(%struct.addrinfo* %77)
br label %79
79: ; preds = %76, %3
%80 = load i32, i32* %8
ret i32 %80
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@stderr = global i32 0
@.str = [30 x i8] c"Failed to connect socket: %s\0A\00"
@errno = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [39 x i8] c"Failed to enable nonblocking mode: %s\0A\00"
@.str.2 = [29 x i8] c"Failed to create socket: %s\0A\00"
define i32 @connect_server(i8* %0, i32 %1, i32 %2) {
%4 = tail call %struct.addrinfo* @lookuphost(i8* %0, i32 %1)
%5 = icmp eq %struct.addrinfo* %4, null
br i1 %5, label %54, label %6
6: ; preds = %3
%7 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 4
%8 = load i32, i32* %7
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 3
%10 = load i32, i32* %9
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 2
%12 = load i32, i32* %11
%13 = tail call i32 @socket(i32 %8, i32 %10, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %46, label %15
15: ; preds = %6
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 1
%17 = load i32, i32* %16
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 0
%19 = load i32, i32* %18
%20 = tail call i32 @connect(i32 %13, i32 %17, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %28
22: ; preds = %15
%23 = load i32, i32* @stderr
%24 = load i32, i32* @errno
%25 = tail call i8* @strerror(i32 %24)
%26 = tail call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %25)
%27 = tail call i32 @close(i32 %13)
br label %51
28: ; preds = %15
%29 = icmp eq i32 %2, 0
br i1 %29, label %51, label %30
30: ; preds = %28
%31 = load i32, i32* @F_GETFL
%32 = tail call i32 @fcntl(i32 %13, i32 %31, i32 0)
%33 = icmp slt i32 %32, 0
br i1 %33, label %40, label %34
34: ; preds = %30
%35 = load i32, i32* @F_SETFL
%36 = load i32, i32* @O_NONBLOCK
%37 = or i32 %36, %32
%38 = tail call i32 @fcntl(i32 %13, i32 %35, i32 %37)
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %51
40: ; preds = %34, %30
%41 = load i32, i32* @stderr
%42 = load i32, i32* @errno
%43 = tail call i8* @strerror(i32 %42)
%44 = tail call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %43)
%45 = tail call i32 @close(i32 %13)
br label %51
46: ; preds = %6
%47 = load i32, i32* @stderr
%48 = load i32, i32* @errno
%49 = tail call i8* @strerror(i32 %48)
%50 = tail call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %49)
br label %51
51: ; preds = %34, %40, %22, %28, %46
%52 = phi i32 [ -1, %22 ], [ %13, %28 ], [ -1, %46 ], [ -1, %40 ], [ %13, %34 ]
%53 = tail call i32 @freeaddrinfo(%struct.addrinfo* %4)
br label %54
54: ; preds = %51, %3
%55 = phi i32 [ %52, %51 ], [ -1, %3 ]
ret i32 %55
}