#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;
  scalar_t__ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  scalar_t__ ai_flags;
};
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ getaddrinfo(char const *, char const *, struct addrinfo *,
                               struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, scalar_t__);
int open_tcp_connection(char *host) {
  const char *hostname = host;
  const char *portname = "80";
  struct addrinfo hints;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_protocol = 0;
  hints.ai_flags = 0;
  struct addrinfo *res = 0;
  getaddrinfo(hostname, portname, &hints, &res);
  int sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  if (sockfd < 0) {
    perror("ERROR opening socket");
    return -1;
  }
  int ret = connect(sockfd, res->ai_addr, res->ai_addrlen);
  if (ret < 0) {
    perror("ERROR connecting");
    close(sockfd);
    return -1;
  }
  freeaddrinfo(res);
  return sockfd;
}


x86 assembly (clang, -O0):
<open_tcp_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
lea    0xe75(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e26(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   1070 <getaddrinfo@plt>
mov    -0x48(%rbp),%rax
mov    0x14(%rax),%edi
mov    -0x48(%rbp),%rax
mov    0x10(%rax),%esi
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%rdx
call   1090 <socket@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jge    1224 <open_tcp_connection+0xb4>
lea    0xdf0(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1275 <open_tcp_connection+0x105>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x48(%rbp),%rax
mov    (%rax),%edx
call   1060 <connect@plt>
mov    %eax,-0x50(%rbp)
cmpl   $0x0,-0x50(%rbp)
jge    1266 <open_tcp_connection+0xf6>
lea    0xdcb(%rip),%rdi
call   1050 <perror@plt>
mov    -0x4c(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1275 <open_tcp_connection+0x105>
mov    -0x48(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_tcp_connection>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
mov    0x2e53(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
movq   $0x0,0x10(%rsp)
movq   $0x0,0x20(%rsp)
movq   $0x0,(%rsp)
lea    0xe37(%rip),%rsi
mov    %rsp,%rcx
mov    %rbx,%rdi
mov    %r14,%rdx
call   1070 <getaddrinfo@plt>
mov    (%rsp),%rax
mov    0x14(%rax),%edi
mov    0x10(%rax),%esi
mov    0x8(%rax),%rdx
call   1090 <socket@plt>
test   %eax,%eax
js     1217 <open_tcp_connection+0xa7>
mov    %eax,%ebx
mov    (%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebx,%edi
call   1060 <connect@plt>
test   %eax,%eax
js     122a <open_tcp_connection+0xba>
mov    (%rsp),%rdi
call   1080 <freeaddrinfo@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
lea    0xde5(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%ebx
jmp    120d <open_tcp_connection+0x9d>
lea    0xde7(%rip),%rdi
call   1050 <perror@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebx
jmp    120d <open_tcp_connection+0x9d>

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

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i64, i32, i32, i64 }
@.str = [3 x i8] c"80\00"
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [21 x i8] c"ERROR opening socket\00"
@.str.2 = [17 x i8] c"ERROR connecting\00"
define i32 @open_tcp_connection(i8* %0) {
%2 = alloca %struct.addrinfo
%3 = alloca %struct.addrinfo*
%4 = bitcast %struct.addrinfo* %2 to i8*
%5 = call i32 @memset(%struct.addrinfo* %2, i32 0, i32 32)
%6 = load i32, i32* @AF_UNSPEC
%7 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 4
store i32 %6, i32* %7
%8 = load i32, i32* @SOCK_STREAM
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 3
store i32 %8, i32* %9
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 2
store i64 0, i64* %10
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 5
store i64 0, i64* %11
%12 = bitcast %struct.addrinfo** %3 to i8*
store %struct.addrinfo* null, %struct.addrinfo** %3
%13 = call i32 @getaddrinfo(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), %struct.addrinfo* %2, %struct.addrinfo** %3)
%14 = load %struct.addrinfo*, %struct.addrinfo** %3
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i64 0, i32 4
%16 = load i32, i32* %15
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i64 0, i32 3
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i64 0, i32 2
%20 = load i64, i64* %19
%21 = call i32 @socket(i32 %16, i32 %18, i64 %20)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %1
%24 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %39
25: ; preds = %1
%26 = load %struct.addrinfo*, %struct.addrinfo** %3
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 1
%28 = load i32, i32* %27
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 0
%30 = load i32, i32* %29
%31 = call i32 @connect(i32 %21, i32 %28, i32 %30)
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %25
%34 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%35 = call i32 @close(i32 %21)
br label %39
36: ; preds = %25
%37 = load %struct.addrinfo*, %struct.addrinfo** %3
%38 = call i32 @freeaddrinfo(%struct.addrinfo* %37)
br label %39
39: ; preds = %33, %36, %23
%40 = phi i32 [ -1, %23 ], [ -1, %33 ], [ %21, %36 ]
ret i32 %40
}