#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 sockaddr_storage {
  int dummy;
};
struct addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
int __socket(struct addrinfo *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memcpy(struct sockaddr_storage *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int socket_tcp_connect(const char *address, const char *port,
                       struct sockaddr_storage *addr) {
  struct addrinfo hints;
  struct addrinfo *info;
  struct addrinfo *p;
  int status;
  int sock;
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  if ((status = getaddrinfo(address, port, &hints, &info)) != 0) {
    fprintf(stderr, "getaddrinfo(): %s", gai_strerror(status));
    return (-1);
  }
  for (p = info; p != NULL; p = p->ai_next) {
    if ((sock = __socket(p, 0, 0)) < 0) {
      perror("socket()");
      continue;
    }
    if (connect(sock, p->ai_addr, p->ai_addrlen) < 0) {
      perror("connect()");
      close(sock);
      sock = -1;
      continue;
    }
    if (addr != NULL)
      memcpy(addr, p->ai_addr, p->ai_addrlen);
    break;
  }
  freeaddrinfo(info);
  return (sock);
}


x86 assembly (clang, -O0):
<socket_tcp_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   10b0 <getaddrinfo@plt>
mov    %eax,-0x4c(%rbp)
cmp    $0x0,%eax
je     122f <socket_tcp_connect+0x8f>
mov    0x2dd4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x4c(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x54(%rbp),%edi
mov    %rax,%rdx
lea    0xde2(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f1 <socket_tcp_connect+0x151>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     12e2 <socket_tcp_connect+0x142>
mov    -0x48(%rbp),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1090 <__socket@plt>
mov    %eax,-0x50(%rbp)
cmp    $0x0,%eax
jge    126c <socket_tcp_connect+0xcc>
lea    0xdb0(%rip),%rdi
call   1080 <perror@plt>
jmp    12d1 <socket_tcp_connect+0x131>
mov    -0x50(%rbp),%edi
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x48(%rbp),%rax
mov    (%rax),%edx
call   10a0 <connect@plt>
cmp    $0x0,%rax
jge    12ab <socket_tcp_connect+0x10b>
lea    0xd89(%rip),%rdi
call   1080 <perror@plt>
mov    -0x50(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x50(%rbp)
jmp    12d1 <socket_tcp_connect+0x131>
cmpq   $0x0,-0x20(%rbp)
je     12cc <socket_tcp_connect+0x12c>
mov    -0x20(%rbp),%rdi
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x48(%rbp),%rax
mov    (%rax),%edx
call   1070 <memcpy@plt>
jmp    12e2 <socket_tcp_connect+0x142>
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    1237 <socket_tcp_connect+0x97>
mov    -0x40(%rbp),%rdi
call   10c0 <freeaddrinfo@plt>
mov    -0x50(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_tcp_connect>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%rbp
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    %rsp,%rcx
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   10b0 <getaddrinfo@plt>
test   %eax,%eax
je     1223 <socket_tcp_connect+0x83>
mov    0x2dd9(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xdf1(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    12a3 <socket_tcp_connect+0x103>
mov    (%rsp),%rbx
test   %rbx,%rbx
je     129a <socket_tcp_connect+0xfa>
lea    0xddf(%rip),%r15
lea    0xde1(%rip),%r12
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <__socket@plt>
mov    %eax,%ebp
test   %eax,%eax
js     127c <socket_tcp_connect+0xdc>
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %ebp,%edi
call   10a0 <connect@plt>
test   %rax,%rax
jns    1286 <socket_tcp_connect+0xe6>
mov    %r12,%rdi
call   1080 <perror@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebp
mov    0x8(%rbx),%rbx
test   %rbx,%rbx
jne    123a <socket_tcp_connect+0x9a>
jmp    129a <socket_tcp_connect+0xfa>
mov    %r15,%rdi
call   1080 <perror@plt>
jmp    1271 <socket_tcp_connect+0xd1>
test   %r14,%r14
je     129a <socket_tcp_connect+0xfa>
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %r14,%rdi
call   1070 <memcpy@plt>
jmp    129a <socket_tcp_connect+0xfa>
mov    (%rsp),%rdi
call   10c0 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.addrinfo = type { i32, i32, %struct.addrinfo*, i32, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [18 x i8] c"getaddrinfo(): %s\00"
@.str.1 = [9 x i8] c"socket()\00"
@.str.2 = [10 x i8] c"connect()\00"
define i32 @socket_tcp_connect(i8* %0, i8* %1, %struct.sockaddr_storage* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca %struct.sockaddr_storage*
%8 = alloca %struct.addrinfo
%9 = alloca %struct.addrinfo*
%10 = alloca %struct.addrinfo*
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store %struct.sockaddr_storage* %2, %struct.sockaddr_storage** %7
%13 = call i32 @memset(%struct.addrinfo* %8, i32 0, i32 24)
%14 = load i32, i32* @AF_UNSPEC
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 4
store i32 %14, i32* %15
%16 = load i32, i32* @SOCK_STREAM
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 3
store i32 %16, i32* %17
%18 = load i8*, i8** %5
%19 = load i8*, i8** %6
%20 = call i32 @getaddrinfo(i8* %18, i8* %19, %struct.addrinfo* %8, %struct.addrinfo** %9)
store i32 %20, i32* %11
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %3
%23 = load i32, i32* @stderr
%24 = load i32, i32* %11
%25 = call i8* @gai_strerror(i32 %24)
%26 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %25)
store i32 -1, i32* %4
br label %73
27: ; preds = %3
%28 = load %struct.addrinfo*, %struct.addrinfo** %9
store %struct.addrinfo* %28, %struct.addrinfo** %10
br label %29
29: ; preds = %65, %27
%30 = load %struct.addrinfo*, %struct.addrinfo** %10
%31 = icmp ne %struct.addrinfo* %30, null
br i1 %31, label %32, label %69
32: ; preds = %29
%33 = load %struct.addrinfo*, %struct.addrinfo** %10
%34 = call i32 @__socket(%struct.addrinfo* %33, i32 0, i32 0)
store i32 %34, i32* %12
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %65
38: ; preds = %32
%39 = load i32, i32* %12
%40 = load %struct.addrinfo*, %struct.addrinfo** %10
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %40, i32 0, i32 1
%42 = load i32, i32* %41
%43 = load %struct.addrinfo*, %struct.addrinfo** %10
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %43, i32 0, i32 0
%45 = load i32, i32* %44
%46 = call i64 @connect(i32 %39, i32 %42, i32 %45)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %52
48: ; preds = %38
%49 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%50 = load i32, i32* %12
%51 = call i32 @close(i32 %50)
store i32 -1, i32* %12
br label %65
52: ; preds = %38
%53 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %7
%54 = icmp ne %struct.sockaddr_storage* %53, null
br i1 %54, label %55, label %64
55: ; preds = %52
%56 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %7
%57 = load %struct.addrinfo*, %struct.addrinfo** %10
%58 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %57, i32 0, i32 1
%59 = load i32, i32* %58
%60 = load %struct.addrinfo*, %struct.addrinfo** %10
%61 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %60, i32 0, i32 0
%62 = load i32, i32* %61
%63 = call i32 @memcpy(%struct.sockaddr_storage* %56, i32 %59, i32 %62)
br label %64
64: ; preds = %55, %52
br label %69
65: ; preds = %48, %36
%66 = load %struct.addrinfo*, %struct.addrinfo** %10
%67 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %66, i32 0, i32 2
%68 = load %struct.addrinfo*, %struct.addrinfo** %67
store %struct.addrinfo* %68, %struct.addrinfo** %10
br label %29
69: ; preds = %64, %29
%70 = load %struct.addrinfo*, %struct.addrinfo** %9
%71 = call i32 @freeaddrinfo(%struct.addrinfo* %70)
%72 = load i32, i32* %12
store i32 %72, i32* %4
br label %73
73: ; preds = %69, %22
%74 = load i32, i32* %4
ret i32 %74
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.addrinfo = type { i32, i32, %struct.addrinfo*, i32, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [18 x i8] c"getaddrinfo(): %s\00"
@.str.1 = [9 x i8] c"socket()\00"
@.str.2 = [10 x i8] c"connect()\00"
define i32 @socket_tcp_connect(i8* %0, i8* %1, %struct.sockaddr_storage* %2) {
%4 = alloca %struct.addrinfo
%5 = alloca %struct.addrinfo*
%6 = bitcast %struct.addrinfo* %4 to i8*
%7 = bitcast %struct.addrinfo** %5 to i8*
%8 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 24)
%9 = load i32, i32* @AF_UNSPEC
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 4
store i32 %9, i32* %10
%11 = load i32, i32* @SOCK_STREAM
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 3
store i32 %11, i32* %12
%13 = call i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %4, %struct.addrinfo** %5)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %3
%16 = load %struct.addrinfo*, %struct.addrinfo** %5
%17 = icmp eq %struct.addrinfo* %16, null
br i1 %17, label %51, label %22
18: ; preds = %3
%19 = load i32, i32* @stderr
%20 = call i8* @gai_strerror(i32 %13)
%21 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %20)
br label %55
22: ; preds = %15, %46
%23 = phi %struct.addrinfo* [ %49, %46 ], [ %16, %15 ]
%24 = call i32 @__socket(%struct.addrinfo* %23, i32 0, i32 0)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %46
28: ; preds = %22
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 1
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 0
%32 = load i32, i32* %31
%33 = call i64 @connect(i32 %24, i32 %30, i32 %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %28
%36 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 @close(i32 %24)
br label %46
38: ; preds = %28
%39 = icmp eq %struct.sockaddr_storage* %2, null
br i1 %39, label %51, label %40
40: ; preds = %38
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 0
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 1
%43 = load i32, i32* %42
%44 = load i32, i32* %41
%45 = call i32 @memcpy(%struct.sockaddr_storage* %2, i32 %43, i32 %44)
br label %51
46: ; preds = %35, %26
%47 = phi i32 [ %24, %26 ], [ -1, %35 ]
%48 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 2
%49 = load %struct.addrinfo*, %struct.addrinfo** %48
%50 = icmp eq %struct.addrinfo* %49, null
br i1 %50, label %51, label %22
51: ; preds = %46, %15, %38, %40
%52 = phi i32 [ %24, %40 ], [ %24, %38 ], [ undef, %15 ], [ %47, %46 ]
%53 = load %struct.addrinfo*, %struct.addrinfo** %5
%54 = call i32 @freeaddrinfo(%struct.addrinfo* %53)
br label %55
55: ; preds = %51, %18
%56 = phi i32 [ -1, %18 ], [ %52, %51 ]
ret i32 %56
}