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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ AI_CANONNAME;
int NI_MAXSERV;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, int /*<<< orphan*/, int);
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 /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned short);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int make_tcpip_connection(char *hostname, unsigned short port) {
  int sockfd, err;
  char port_str[NI_MAXSERV];
  struct addrinfo *info, hints;
  if ((hostname == NULL) || (port == 0))
    return -1;
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("socket");
    return -1;
  }
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_flags = AI_CANONNAME;
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  snprintf(port_str, 6, "%u", port);
  if ((err = getaddrinfo(hostname, port_str, &hints, &info)) < 0) {
    fprintf(stderr, "(launcher) getaddrinfo failed: %s\n", gai_strerror(err));
    return -1;
  }
  if (connect(sockfd, info->ai_addr, sizeof(struct sockaddr_in)) < 0) {
    perror("connect");
    return -1;
  }
  freeaddrinfo(info);
  return sockfd;
}


x86 assembly (clang, -O0):
<make_tcpip_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %si,%ax
mov    %rdi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11e7 <make_tcpip_connection+0x57>
movzwl -0x12(%rbp),%eax
cmp    $0x0,%eax
jne    11fa <make_tcpip_connection+0x6a>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    1337 <make_tcpip_connection+0x1a7>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    123e <make_tcpip_connection+0xae>
lea    0xdda(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    1337 <make_tcpip_connection+0x1a7>
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    -0x58(%rbp),%rdi
mov    0x2d7f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2d83(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2d4f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    $0x6,%esi
lea    0xd85(%rip),%rdx
movzwl -0x12(%rbp),%ecx
call   1030 <snprintf@plt>
mov    -0x58(%rbp),%rsi
mov    -0x10(%rbp),%rdi
lea    -0x48(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1090 <getaddrinfo@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    12e5 <make_tcpip_connection+0x155>
mov    0x2d15(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x5c(%rbp),%edi
mov    %rax,%rdx
lea    0xd3d(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    1337 <make_tcpip_connection+0x1a7>
mov    -0x18(%rbp),%edi
mov    -0x38(%rbp),%rax
mov    (%rax),%esi
mov    $0x4,%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    1321 <make_tcpip_connection+0x191>
lea    0xd24(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    1337 <make_tcpip_connection+0x1a7>
mov    -0x38(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_tcpip_connection>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e38(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    $0xffffffff,%r14d
test   %rdi,%rdi
je     12a8 <make_tcpip_connection+0x118>
mov    %esi,%r13d
test   %r13w,%r13w
je     12a8 <make_tcpip_connection+0x118>
mov    %rdi,%r12
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     12ba <make_tcpip_connection+0x12a>
mov    %eax,%r15d
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2dc3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2dc7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d93(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
movzwl %r13w,%ecx
lea    0xdca(%rip),%rdx
mov    %rbx,%rdi
mov    $0x6,%esi
call   1030 <snprintf@plt>
lea    -0x30(%rbp),%rcx
mov    %r12,%rdi
mov    %rbx,%rsi
lea    -0x40(%rbp),%rdx
call   1090 <getaddrinfo@plt>
test   %eax,%eax
js     1287 <make_tcpip_connection+0xf7>
mov    -0x30(%rbp),%rax
mov    (%rax),%esi
mov    %r15d,%edi
mov    $0x4,%edx
call   1080 <connect@plt>
test   %rax,%rax
js     12c8 <make_tcpip_connection+0x138>
mov    -0x30(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    %r15d,%r14d
jmp    12a8 <make_tcpip_connection+0x118>
mov    0x2d3a(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xd6c(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd3f(%rip),%rdi
call   1070 <perror@plt>
jmp    12a8 <make_tcpip_connection+0x118>
lea    0xd5e(%rip),%rdi
call   1070 <perror@plt>
jmp    12a8 <make_tcpip_connection+0x118>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32 }
@NI_MAXSERV = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@AI_CANONNAME = global i32 0
@.str.1 = [3 x i8] c"%u\00"
@stderr = global i32 0
@.str.2 = [35 x i8] c"(launcher) getaddrinfo failed: %s\0A\00"
@.str.3 = [8 x i8] c"connect\00"
define i32 @make_tcpip_connection(i8* %0, i16 zeroext %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i16
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca %struct.addrinfo*
%11 = alloca %struct.addrinfo
%12 = alloca i32
store i8* %0, i8** %4
store i16 %1, i16* %5
%13 = load i32, i32* @NI_MAXSERV
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %8
%16 = alloca i8, i64 %14
store i64 %14, i64* %9
%17 = load i8*, i8** %4
%18 = icmp eq i8* %17, null
br i1 %18, label %23, label %19
19: ; preds = %2
%20 = load i16, i16* %5
%21 = zext i16 %20 to i32
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %19, %2
store i32 -1, i32* %3
store i32 1, i32* %12
br label %62
24: ; preds = %19
%25 = load i32, i32* @AF_INET
%26 = load i32, i32* @SOCK_STREAM
%27 = call i32 @socket(i32 %25, i32 %26, i32 0)
store i32 %27, i32* %6
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
store i32 1, i32* %12
br label %62
31: ; preds = %24
%32 = call i32 @memset(%struct.addrinfo* %11, i32 0, i32 16)
%33 = load i32, i32* @AI_CANONNAME
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %11, i32 0, i32 3
store i32 %33, i32* %34
%35 = load i32, i32* @AF_INET
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %11, i32 0, i32 2
store i32 %35, i32* %36
%37 = load i32, i32* @SOCK_STREAM
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %11, i32 0, i32 1
store i32 %37, i32* %38
%39 = load i16, i16* %5
%40 = call i32 @snprintf(i8* %16, i32 6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i16 zeroext %39)
%41 = load i8*, i8** %4
%42 = call i32 @getaddrinfo(i8* %41, i8* %16, %struct.addrinfo* %11, %struct.addrinfo** %10)
store i32 %42, i32* %7
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %49
44: ; preds = %31
%45 = load i32, i32* @stderr
%46 = load i32, i32* %7
%47 = call i8* @gai_strerror(i32 %46)
%48 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8* %47)
store i32 -1, i32* %3
store i32 1, i32* %12
br label %62
49: ; preds = %31
%50 = load i32, i32* %6
%51 = load %struct.addrinfo*, %struct.addrinfo** %10
%52 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %51, i32 0, i32 0
%53 = load i32, i32* %52
%54 = call i64 @connect(i32 %50, i32 %53, i32 4)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %49
%57 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
store i32 1, i32* %12
br label %62
58: ; preds = %49
%59 = load %struct.addrinfo*, %struct.addrinfo** %10
%60 = call i32 @freeaddrinfo(%struct.addrinfo* %59)
%61 = load i32, i32* %6
store i32 %61, i32* %3
store i32 1, i32* %12
br label %62
62: ; preds = %58, %56, %44, %29, %23
%63 = load i8*, i8** %8
%64 = load i32, i32* %3
ret i32 %64
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32 }
@NI_MAXSERV = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@AI_CANONNAME = global i32 0
@.str.1 = [3 x i8] c"%u\00"
@stderr = global i32 0
@.str.2 = [35 x i8] c"(launcher) getaddrinfo failed: %s\0A\00"
@.str.3 = [8 x i8] c"connect\00"
define i32 @make_tcpip_connection(i8* %0, i16 zeroext %1) {
%3 = alloca %struct.addrinfo*
%4 = alloca %struct.addrinfo
%5 = load i32, i32* @NI_MAXSERV
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = bitcast %struct.addrinfo** %3 to i8*
%9 = bitcast %struct.addrinfo* %4 to i8*
%10 = icmp eq i8* %0, null
%11 = icmp eq i16 %1, 0
%12 = or i1 %10, %11
br i1 %12, label %46, label %13
13: ; preds = %2
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = tail call i32 @socket(i32 %14, i32 %15, i32 0)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %46
20: ; preds = %13
%21 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 16)
%22 = load i32, i32* @AI_CANONNAME
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 3
store i32 %22, i32* %23
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 2
store i32 %24, i32* %25
%26 = load i32, i32* @SOCK_STREAM
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 1
store i32 %26, i32* %27
%28 = call i32 @snprintf(i8* %7, i32 6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i16 zeroext %1)
%29 = call i32 @getaddrinfo(i8* %0, i8* %7, %struct.addrinfo* %4, %struct.addrinfo** %3)
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %20
%32 = load i32, i32* @stderr
%33 = call i8* @gai_strerror(i32 %29)
%34 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8* %33)
br label %46
35: ; preds = %20
%36 = load %struct.addrinfo*, %struct.addrinfo** %3
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %36, i64 0, i32 0
%38 = load i32, i32* %37
%39 = call i64 @connect(i32 %16, i32 %38, i32 4)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %35
%42 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %46
43: ; preds = %35
%44 = load %struct.addrinfo*, %struct.addrinfo** %3
%45 = call i32 @freeaddrinfo(%struct.addrinfo* %44)
br label %46
46: ; preds = %2, %43, %41, %31, %18
%47 = phi i32 [ -1, %18 ], [ -1, %31 ], [ -1, %41 ], [ %16, %43 ], [ -1, %2 ]
ret i32 %47
}