#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;
  struct addrinfo *ai_next;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int 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 *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int _tcp_connect(char *host, char *port) {
  int sfd = -1;
  struct addrinfo hints, *result, *rp;
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  int s = getaddrinfo(host, port, &hints, &result);
  if (s) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
    return -1;
  }
  for (rp = result; rp != NULL; rp = rp->ai_next) {
    sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
    if (sfd == -1)
      continue;
    if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
      break;
    close(sfd);
  }
  freeaddrinfo(result);
  if (rp == NULL) {
    fprintf(stderr, "Could not connect to '%s:%s'\n", host, port);
    return -1;
  }
  return sfd;
}


x86 assembly (clang, -O0):
<_tcp_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
je     1215 <_tcp_connect+0x95>
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
mov    -0x54(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x58(%rbp),%edi
mov    %rax,%rdx
lea    0xdfe(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d5 <_tcp_connect+0x155>
mov    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     1290 <_tcp_connect+0x110>
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   10a0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1254 <_tcp_connect+0xd4>
jmp    127f <_tcp_connect+0xff>
mov    -0x1c(%rbp),%edi
mov    -0x50(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x50(%rbp),%rax
mov    (%rax),%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
je     1277 <_tcp_connect+0xf7>
jmp    1290 <_tcp_connect+0x110>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x50(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    121d <_tcp_connect+0x9d>
mov    -0x48(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
cmpq   $0x0,-0x50(%rbp)
jne    12cf <_tcp_connect+0x14f>
mov    0x2d2d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xd55(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d5 <_tcp_connect+0x155>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_tcp_connect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    %rsp,%rcx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbp,%rdx
call   1080 <getaddrinfo@plt>
test   %eax,%eax
je     11f8 <_tcp_connect+0x78>
mov    0x2dfe(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xe16(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    126e <_tcp_connect+0xee>
mov    (%rsp),%rbx
test   %rbx,%rbx
jne    1219 <_tcp_connect+0x99>
xor    %edi,%edi
jmp    124c <_tcp_connect+0xcc>
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     1248 <_tcp_connect+0xc8>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     1210 <_tcp_connect+0x90>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
jne    1280 <_tcp_connect+0x100>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    1210 <_tcp_connect+0x90>
mov    (%rsp),%rdi
call   1090 <freeaddrinfo@plt>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
lea    0xdb0(%rip),%rsi
mov    %r15,%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsp),%rdi
call   1090 <freeaddrinfo@plt>
jmp    1273 <_tcp_connect+0xf3>

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

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