#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_socktype;
  int /*<<< orphan*/ ai_family;
};

/* Variables and functions */
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int atoi(char *);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ error(char *, ...);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ 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*/ sprintf(char *, char *, int);
int connect_to(char *hostname, char *port) {
  int status;
  int fd;
  char portstr[32];
  struct addrinfo hint;
  struct addrinfo *info;
  memset(&hint, 0, sizeof(struct addrinfo));
  hint.ai_family = PF_INET;
  hint.ai_socktype = SOCK_STREAM;
  sprintf(portstr, "%d", atoi(port));
  status = getaddrinfo(hostname, portstr, &hint, &info);
  if (status != 0)
    error("failed to get address: %s", gai_strerror(status));
  fd = socket(PF_INET, SOCK_STREAM, 0);
  if (fd < 0)
    error("failed to create socket");
  status = connect(fd, info->ai_addr, info->ai_addrlen);
  if (status < 0)
    error("failed to connect to server");
  freeaddrinfo(info);
  return fd;
}


x86 assembly (clang, -O0):
<connect_to>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <atoi@plt>
mov    -0x60(%rbp),%rdi
mov    %eax,%edx
lea    0xe1a(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x8(%rbp),%rdi
lea    -0x40(%rbp),%rsi
lea    -0x50(%rbp),%rdx
lea    -0x58(%rbp),%rcx
call   1090 <getaddrinfo@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1225 <connect_to+0x95>
mov    -0x14(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    %eax,%esi
lea    0xde5(%rip),%rdi
mov    $0x0,%al
call   1050 <error@plt>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
mov    0x2dab(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1259 <connect_to+0xc9>
lea    0xdcb(%rip),%rdi
mov    $0x0,%al
call   1050 <error@plt>
mov    -0x18(%rbp),%edi
mov    -0x58(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x58(%rbp),%rax
mov    (%rax),%edx
call   1080 <connect@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1289 <connect_to+0xf9>
lea    0xdb3(%rip),%rdi
mov    $0x0,%al
call   1050 <error@plt>
mov    -0x58(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    -0x18(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x40,%rsp
mov    %rsi,%rbx
mov    %rdi,%r15
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2e1a(%rip),%r12
mov    (%r12),%eax
mov    %eax,0x1c(%rsp)
mov    0x2e13(%rip),%r13
mov    0x0(%r13),%eax
mov    %eax,0x18(%rsp)
mov    %rbx,%rdi
call   1060 <atoi@plt>
lea    0xe1c(%rip),%rsi
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    %eax,%edx
call   1070 <sprintf@plt>
lea    0x8(%rsp),%rcx
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1090 <getaddrinfo@plt>
test   %eax,%eax
je     1221 <connect_to+0x91>
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xdeb(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <error@plt>
mov    (%r12),%edi
mov    0x0(%r13),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1244 <connect_to+0xb4>
lea    0xde0(%rip),%rdi
xor    %eax,%eax
call   1050 <error@plt>
mov    0x8(%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebx,%edi
call   1080 <connect@plt>
test   %eax,%eax
jns    1267 <connect_to+0xd7>
lea    0xdd5(%rip),%rdi
xor    %eax,%eax
call   1050 <error@plt>
mov    0x8(%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    %ebx,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [3 x i8] c"%d\00"
@.str.1 = [26 x i8] c"failed to get address: %s\00"
@.str.2 = [24 x i8] c"failed to create socket\00"
@.str.3 = [28 x i8] c"failed to connect to server\00"
define i32 @connect_to(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca [32 x i8]
%8 = alloca %struct.addrinfo
%9 = alloca %struct.addrinfo*
store i8* %0, i8** %3
store i8* %1, i8** %4
%10 = call i32 @memset(%struct.addrinfo* %8, i32 0, i32 16)
%11 = load i32, i32* @PF_INET
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 3
store i32 %11, i32* %12
%13 = load i32, i32* @SOCK_STREAM
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 2
store i32 %13, i32* %14
%15 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%16 = load i8*, i8** %4
%17 = call i32 @atoi(i8* %16)
%18 = call i32 @sprintf(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = load i8*, i8** %3
%20 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%21 = call i32 @getaddrinfo(i8* %19, i8* %20, %struct.addrinfo* %8, %struct.addrinfo** %9)
store i32 %21, i32* %5
%22 = load i32, i32* %5
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %2
%25 = load i32, i32* %5
%26 = call i32 @gai_strerror(i32 %25)
%27 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %26)
br label %28
28: ; preds = %24, %2
%29 = load i32, i32* @PF_INET
%30 = load i32, i32* @SOCK_STREAM
%31 = call i32 @socket(i32 %29, i32 %30, i32 0)
store i32 %31, i32* %6
%32 = load i32, i32* %6
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %28
%35 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %36
36: ; preds = %34, %28
%37 = load i32, i32* %6
%38 = load %struct.addrinfo*, %struct.addrinfo** %9
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i32 0, i32 1
%40 = load i32, i32* %39
%41 = load %struct.addrinfo*, %struct.addrinfo** %9
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %41, i32 0, i32 0
%43 = load i32, i32* %42
%44 = call i32 @connect(i32 %37, i32 %40, i32 %43)
store i32 %44, i32* %5
%45 = load i32, i32* %5
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %36
%48 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %49
49: ; preds = %47, %36
%50 = load %struct.addrinfo*, %struct.addrinfo** %9
%51 = call i32 @freeaddrinfo(%struct.addrinfo* %50)
%52 = load i32, i32* %6
ret i32 %52
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [3 x i8] c"%d\00"
@.str.1 = [26 x i8] c"failed to get address: %s\00"
@.str.2 = [24 x i8] c"failed to create socket\00"
@.str.3 = [28 x i8] c"failed to connect to server\00"
define i32 @connect_to(i8* %0, i8* nocapture %1) {
%3 = alloca [32 x i8]
%4 = alloca %struct.addrinfo
%5 = alloca %struct.addrinfo*
%6 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%7 = bitcast %struct.addrinfo* %4 to i8*
%8 = bitcast %struct.addrinfo** %5 to i8*
%9 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 16)
%10 = load i32, i32* @PF_INET
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 3
store i32 %10, i32* %11
%12 = load i32, i32* @SOCK_STREAM
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 2
store i32 %12, i32* %13
%14 = call i32 @atoi(i8* %1)
%15 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = call i32 @getaddrinfo(i8* %0, i8* %6, %struct.addrinfo* %4, %struct.addrinfo** %5)
%17 = icmp eq i32 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %2
%19 = call i32 @gai_strerror(i32 %16)
%20 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %19)
br label %21
21: ; preds = %18, %2
%22 = load i32, i32* @PF_INET
%23 = load i32, i32* @SOCK_STREAM
%24 = call i32 @socket(i32 %22, i32 %23, i32 0)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %26, %21
%29 = load %struct.addrinfo*, %struct.addrinfo** %5
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %29, i64 0, i32 1
%31 = load i32, i32* %30
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %29, i64 0, i32 0
%33 = load i32, i32* %32
%34 = call i32 @connect(i32 %24, i32 %31, i32 %33)
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %28
%37 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %38
38: ; preds = %36, %28
%39 = load %struct.addrinfo*, %struct.addrinfo** %5
%40 = call i32 @freeaddrinfo(%struct.addrinfo* %39)
ret i32 %24
}