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

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, 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 ConnectToFreedb() {
  int status;
  struct addrinfo hints;
  struct addrinfo *servinfo;
  int sockfd;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  status = getaddrinfo("freedb.org", "8880", &hints, &servinfo);
  if (status != 0) {
    fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
    exit(1);
  }
  sockfd =
      socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol);
  ;
  status = connect(sockfd, servinfo->ai_addr, servinfo->ai_addrlen);
  if (status != 0) {
    fprintf(stderr, "Connection failed\n");
    exit(1);
  }
  freeaddrinfo(servinfo);
  return sockfd;
}


x86 assembly (clang, -O0):
<ConnectToFreedb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1040 <memset@plt>
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
lea    0xe49(%rip),%rdi
lea    0xe4d(%rip),%rsi
lea    -0x18(%rbp),%rdx
lea    -0x20(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     120a <ConnectToFreedb+0x8a>
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x4(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
lea    0xe17(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x20(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   1070 <connect@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     126a <ConnectToFreedb+0xea>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
lea    0xdce(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x20(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ConnectToFreedb>:
push   %rbx
sub    $0x20,%rsp
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1040 <memset@plt>
mov    0x2e48(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e23(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
lea    0xe46(%rip),%rdi
lea    0xe4a(%rip),%rsi
mov    %rsp,%rcx
mov    %rbx,%rdx
call   1080 <getaddrinfo@plt>
test   %eax,%eax
jne    1209 <ConnectToFreedb+0x89>
mov    (%rsp),%rax
mov    0x10(%rax),%edi
mov    0x8(%rax),%edx
mov    0xc(%rax),%esi
call   10a0 <socket@plt>
mov    %eax,%ebx
mov    (%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebx,%edi
call   1070 <connect@plt>
test   %eax,%eax
jne    1236 <ConnectToFreedb+0xb6>
mov    (%rsp),%rdi
call   1090 <freeaddrinfo@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
ret
mov    0x2dc8(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xdf0(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
lea    0xde1(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [11 x i8] c"freedb.org\00"
@.str.1 = [5 x i8] c"8880\00"
@stderr = global i32 0
@.str.2 = [23 x i8] c"getaddrinfo error: %s\0A\00"
@.str.3 = [19 x i8] c"Connection failed\0A\00"
define i32 @ConnectToFreedb() {
%1 = alloca i32
%2 = alloca %struct.addrinfo
%3 = alloca %struct.addrinfo*
%4 = alloca i32
%5 = call i32 @memset(%struct.addrinfo* %2, i32 0, i32 20)
%6 = load i32, i32* @AF_UNSPEC
%7 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i32 0, i32 4
store i32 %6, i32* %7
%8 = load i32, i32* @SOCK_STREAM
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i32 0, i32 3
store i32 %8, i32* %9
%10 = call i32 @getaddrinfo(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %struct.addrinfo* %2, %struct.addrinfo** %3)
store i32 %10, i32* %1
%11 = load i32, i32* %1
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %0
%14 = load i32, i32* @stderr
%15 = load i32, i32* %1
%16 = call i8* @gai_strerror(i32 %15)
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %16)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %0
%20 = load %struct.addrinfo*, %struct.addrinfo** %3
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i32 0, i32 4
%22 = load i32, i32* %21
%23 = load %struct.addrinfo*, %struct.addrinfo** %3
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i32 0, i32 3
%25 = load i32, i32* %24
%26 = load %struct.addrinfo*, %struct.addrinfo** %3
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i32 0, i32 2
%28 = load i32, i32* %27
%29 = call i32 @socket(i32 %22, i32 %25, i32 %28)
store i32 %29, i32* %4
%30 = load i32, i32* %4
%31 = load %struct.addrinfo*, %struct.addrinfo** %3
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i32 0, i32 1
%33 = load i32, i32* %32
%34 = load %struct.addrinfo*, %struct.addrinfo** %3
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %34, i32 0, i32 0
%36 = load i32, i32* %35
%37 = call i32 @connect(i32 %30, i32 %33, i32 %36)
store i32 %37, i32* %1
%38 = load i32, i32* %1
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %19
%41 = load i32, i32* @stderr
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %19
%45 = load %struct.addrinfo*, %struct.addrinfo** %3
%46 = call i32 @freeaddrinfo(%struct.addrinfo* %45)
%47 = load i32, i32* %4
ret i32 %47
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [11 x i8] c"freedb.org\00"
@.str.1 = [5 x i8] c"8880\00"
@stderr = global i32 0
@.str.2 = [23 x i8] c"getaddrinfo error: %s\0A\00"
@.str.3 = [19 x i8] c"Connection failed\0A\00"
define i32 @ConnectToFreedb() {
%1 = alloca %struct.addrinfo
%2 = alloca %struct.addrinfo*
%3 = bitcast %struct.addrinfo* %1 to i8*
%4 = bitcast %struct.addrinfo** %2 to i8*
%5 = call i32 @memset(%struct.addrinfo* %1, i32 0, i32 20)
%6 = load i32, i32* @AF_UNSPEC
%7 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 4
store i32 %6, i32* %7
%8 = load i32, i32* @SOCK_STREAM
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 3
store i32 %8, i32* %9
%10 = call i32 @getaddrinfo(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %struct.addrinfo* %1, %struct.addrinfo** %2)
%11 = icmp eq i32 %10, 0
br i1 %11, label %17, label %12
12: ; preds = %0
%13 = load i32, i32* @stderr
%14 = call i8* @gai_strerror(i32 %10)
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %14)
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %0
%18 = load %struct.addrinfo*, %struct.addrinfo** %2
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i64 0, i32 4
%20 = load i32, i32* %19
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i64 0, i32 3
%22 = load i32, i32* %21
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i64 0, i32 2
%24 = load i32, i32* %23
%25 = call i32 @socket(i32 %20, i32 %22, i32 %24)
%26 = load %struct.addrinfo*, %struct.addrinfo** %2
%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 %25, i32 %28, i32 %30)
%32 = icmp eq i32 %31, 0
br i1 %32, label %37, label %33
33: ; preds = %17
%34 = load i32, i32* @stderr
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %17
%38 = load %struct.addrinfo*, %struct.addrinfo** %2
%39 = call i32 @freeaddrinfo(%struct.addrinfo* %38)
ret i32 %25
}