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

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int get_socket(const char *host, const char *port) {
  int rc;
  int s;
  struct addrinfo hints, *res;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  if ((rc = getaddrinfo(host, port, &hints, &res)) < 0) {
    fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(rc));
    return -1;
  }
  s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  if (s < 0) {
    fprintf(stderr, "Couldn't get socket.\n");
    goto error;
  }
  if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
    fprintf(stderr, "Couldn't connect.\n");
    goto error;
  }
  freeaddrinfo(res);
  return s;
error:
  freeaddrinfo(res);
  return -1;
}


x86 assembly (clang, -O0):
<get_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1070 <getaddrinfo@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1209 <get_socket+0x99>
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x1c(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x44(%rbp),%edi
mov    %rax,%rdx
lea    0xe0a(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ab <get_socket+0x13b>
mov    -0x40(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x40(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    124c <get_socket+0xdc>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
lea    0xdd7(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    129b <get_socket+0x12b>
mov    -0x20(%rbp),%edi
mov    -0x40(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x40(%rbp),%rax
mov    (%rax),%edx
call   1060 <connect@plt>
cmp    $0x0,%rax
jge    1287 <get_socket+0x117>
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
lea    0xdb2(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    129b <get_socket+0x12b>
mov    -0x40(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12ab <get_socket+0x13b>
mov    -0x40(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_socket>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2e34(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    %rsp,%rcx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1070 <getaddrinfo@plt>
test   %eax,%eax
js     1207 <get_socket+0x97>
mov    (%rsp),%rax
mov    0x10(%rax),%edi
mov    0x8(%rax),%edx
mov    0xc(%rax),%esi
call   1090 <socket@plt>
test   %eax,%eax
js     122c <get_socket+0xbc>
mov    %eax,%ebx
mov    (%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebx,%edi
call   1060 <connect@plt>
test   %rax,%rax
js     1235 <get_socket+0xc5>
mov    (%rsp),%rdi
call   1080 <freeaddrinfo@plt>
jmp    125a <get_socket+0xea>
mov    0x2dc2(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xde2(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1255 <get_socket+0xe5>
lea    0xde4(%rip),%rsi
jmp    123c <get_socket+0xcc>
lea    0xdf1(%rip),%rsi
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%rsp),%rdi
call   1080 <freeaddrinfo@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@stderr = global i32 0
@.str = [23 x i8] c"getaddrinfo error: %s\0A\00"
@.str.1 = [22 x i8] c"Couldn't get socket.\0A\00"
@.str.2 = [19 x i8] c"Couldn't connect.\0A\00"
define i32 @get_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 24)
%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 = load i32, i32* @AI_PASSIVE
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 5
store i32 %12, i32* %13
%14 = call i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %3, %struct.addrinfo** %4)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = call i8* @gai_strerror(i32 %14)
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %18)
br label %47
20: ; preds = %2
%21 = load %struct.addrinfo*, %struct.addrinfo** %4
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i64 0, i32 4
%23 = load i32, i32* %22
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i64 0, i32 3
%25 = load i32, i32* %24
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i64 0, i32 2
%27 = load i32, i32* %26
%28 = call i32 @socket(i32 %23, i32 %25, i32 %27)
%29 = icmp slt i32 %28, 0
br i1 %29, label %41, label %30
30: ; preds = %20
%31 = load %struct.addrinfo*, %struct.addrinfo** %4
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i64 0, i32 1
%33 = load i32, i32* %32
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i64 0, i32 0
%35 = load i32, i32* %34
%36 = call i64 @connect(i32 %28, i32 %33, i32 %35)
%37 = icmp slt i64 %36, 0
br i1 %37, label %41, label %38
38: ; preds = %30
%39 = load %struct.addrinfo*, %struct.addrinfo** %4
%40 = call i32 @freeaddrinfo(%struct.addrinfo* %39)
br label %47
41: ; preds = %30, %20
%42 = phi i8* [ getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), %20 ], [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), %30 ]
%43 = load i32, i32* @stderr
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* %42)
%45 = load %struct.addrinfo*, %struct.addrinfo** %4
%46 = call i32 @freeaddrinfo(%struct.addrinfo* %45)
br label %47
47: ; preds = %41, %38, %16
%48 = phi i32 [ -1, %16 ], [ -1, %41 ], [ %28, %38 ]
ret i32 %48
}