#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;
};
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *,
                           char const *, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char const *, char const *, int /*<<< orphan*/ *,
                struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int connect_socket(const char *host, const char *port) {
  struct addrinfo hints;
  struct addrinfo *result, *rp;
  int s, res;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_protocol = IPPROTO_TCP;
  res = getaddrinfo(host, port, NULL, &result);
  if (res != 0) {
    fprintf(stderr, "Invalid hostname/IP address '%s' or port '%s': %s.\n",
            host, port, gai_strerror(res));
    return -1;
  }
  for (rp = result; rp; rp = rp->ai_next) {
    res = socket(rp->ai_family, hints.ai_socktype, hints.ai_protocol);
    if (res == -1)
      continue;
    if ((s = connect(res, rp->ai_addr, rp->ai_addrlen)) != -1)
      break;
    close(res);
  }
  if (rp == NULL) {
    fprintf(stderr,
            "Could not connect to hostname/IP address '%s', port '%s': %s.\n",
            host, port, strerror(errno));
    res = -1;
  }
  freeaddrinfo(result);
  return res;
}


x86 assembly (clang, -O0):
<connect_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e02(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x40(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,-0x50(%rbp)
cmpl   $0x0,-0x50(%rbp)
je     1243 <connect_socket+0xb3>
mov    0x2dd0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x50(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x64(%rbp),%edi
mov    -0x60(%rbp),%rdx
mov    -0x58(%rbp),%rcx
mov    %rax,%r8
lea    0xdce(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    131e <connect_socket+0x18e>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     12b9 <connect_socket+0x129>
mov    -0x48(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x2c(%rbp),%esi
mov    -0x30(%rbp),%edx
call   10b0 <socket@plt>
mov    %eax,-0x50(%rbp)
cmpl   $0xffffffff,-0x50(%rbp)
jne    127a <connect_socket+0xea>
jmp    12a8 <connect_socket+0x118>
mov    -0x50(%rbp),%edi
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x48(%rbp),%rax
mov    (%rax),%edx
call   1070 <connect@plt>
mov    %eax,-0x4c(%rbp)
cmp    $0xffffffff,%eax
je     12a0 <connect_socket+0x110>
jmp    12b9 <connect_socket+0x129>
mov    -0x50(%rbp),%edi
call   1050 <close@plt>
mov    -0x48(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    124b <connect_socket+0xbb>
cmpq   $0x0,-0x48(%rbp)
jne    130f <connect_socket+0x17f>
mov    0x2d05(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x7c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    0x2cd9(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x7c(%rbp),%edi
mov    -0x78(%rbp),%rdx
mov    -0x70(%rbp),%rcx
mov    %rax,%r8
lea    0xd31(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x50(%rbp)
mov    -0x40(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    -0x50(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e30(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e03(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %rsp,%rcx
mov    %r15,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   1080 <getaddrinfo@plt>
test   %eax,%eax
je     121a <connect_socket+0x8a>
mov    0x2ddd(%rip),%rcx
mov    (%rcx),%ebp
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xdfd(%rip),%rsi
mov    %ebp,%edi
mov    %r15,%rdx
mov    %r14,%rcx
mov    %rax,%r8
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    1296 <connect_socket+0x106>
mov    (%rsp),%rbx
jmp    1224 <connect_socket+0x94>
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     125a <connect_socket+0xca>
mov    0x10(%rbx),%edi
mov    0x10(%rsp),%edx
mov    0x14(%rsp),%esi
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     1220 <connect_socket+0x90>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
jne    128d <connect_socket+0xfd>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    1220 <connect_socket+0x90>
mov    0x2d6f(%rip),%rax
mov    (%rax),%ebp
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdbc(%rip),%rsi
mov    %ebp,%edi
mov    %r15,%rdx
mov    %r14,%rcx
mov    %rax,%r8
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    (%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@stderr = global i32 0
@.str = [52 x i8] c"Invalid hostname/IP address '%s' or port '%s': %s.\0A\00"
@.str.1 = [63 x i8] c"Could not connect to hostname/IP address '%s', port '%s': %s.\0A\00"
@errno = global i32 0
define i32 @connect_socket(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_INET
%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 = load i32, i32* @IPPROTO_TCP
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 2
store i32 %12, i32* %13
%14 = call i32 @getaddrinfo(i8* %0, i8* %1, i32* null, %struct.addrinfo** %4)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = load %struct.addrinfo*, %struct.addrinfo** %4
%18 = icmp eq %struct.addrinfo* %17, null
br i1 %18, label %44, label %23
19: ; preds = %2
%20 = load i32, i32* @stderr
%21 = call i8* @gai_strerror(i32 %14)
%22 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1, i8* %21)
br label %53
23: ; preds = %16, %40
%24 = phi %struct.addrinfo* [ %42, %40 ], [ %17, %16 ]
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 4
%26 = load i32, i32* %25
%27 = load i32, i32* %11
%28 = load i32, i32* %13
%29 = call i32 @socket(i32 %26, i32 %27, i32 %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %40, label %31
31: ; preds = %23
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 1
%33 = load i32, i32* %32
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, 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 %49
38: ; preds = %31
%39 = call i32 @close(i32 %29)
br label %40
40: ; preds = %23, %38
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 5
%42 = load %struct.addrinfo*, %struct.addrinfo** %41
%43 = icmp eq %struct.addrinfo* %42, null
br i1 %43, label %44, label %23
44: ; preds = %40, %16
%45 = load i32, i32* @stderr
%46 = load i32, i32* @errno
%47 = call i8* @strerror(i32 %46)
%48 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %1, i8* %47)
br label %49
49: ; preds = %31, %44
%50 = phi i32 [ -1, %44 ], [ %29, %31 ]
%51 = load %struct.addrinfo*, %struct.addrinfo** %4
%52 = call i32 @freeaddrinfo(%struct.addrinfo* %51)
br label %53
53: ; preds = %49, %19
%54 = phi i32 [ -1, %19 ], [ %50, %49 ]
ret i32 %54
}