#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 sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
};
struct addrinfo {
  scalar_t__ ai_addr;
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};
typedef int /*<<< orphan*/ ip;
typedef int /*<<< orphan*/ h;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int INET_ADDRSTRLEN;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ inet_ntop(int /*<<< orphan*/, int /*<<< orphan*/ *, char *,
                             int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int client_connect(char *host, char *port) {
  struct sockaddr_in *sin;
  struct addrinfo h, *ai;
  char ip[INET_ADDRSTRLEN];
  int e;
  int fd;
  memset(&h, 0, sizeof(h));
  h.ai_family = AF_INET;
  h.ai_socktype = SOCK_STREAM;
  e = getaddrinfo(host, port, &h, &ai);
  if (e) {
    ;
    exit(1);
  }
  if (ai == NULL) {
    ;
    exit(1);
  }
  fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
  if (fd < 0) {
    ;
    exit(1);
  }
  if (connect(fd, ai->ai_addr, ai->ai_addrlen) < 0) {
    ;
    exit(1);
  }
  sin = (struct sockaddr_in *)ai->ai_addr;
  inet_ntop(AF_INET, &sin->sin_addr, ip, sizeof(ip));
  printf("connected to %s:%d...\n", ip, ntohs(sin->sin_port));
  return fd;
}


x86 assembly (clang, -O0):
<client_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x60(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x48(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2df5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1090 <getaddrinfo@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
je     1214 <client_connect+0x94>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x38(%rbp)
jne    1229 <client_connect+0xa9>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x38(%rbp),%rax
mov    0x14(%rax),%edi
mov    -0x38(%rbp),%rax
mov    0x10(%rax),%esi
mov    -0x38(%rbp),%rax
mov    0xc(%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x50(%rbp)
cmpl   $0x0,-0x50(%rbp)
jge    125a <client_connect+0xda>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x50(%rbp),%edi
mov    -0x38(%rbp),%rax
mov    (%rax),%rsi
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    1284 <client_connect+0x104>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x58(%rbp),%rdx
mov    -0x60(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    (%rcx),%rcx
mov    %rcx,-0x18(%rbp)
mov    0x2d4a(%rip),%rcx
mov    (%rcx),%edi
mov    -0x18(%rbp),%rsi
add    $0x4,%rsi
mov    %eax,%ecx
call   1030 <inet_ntop@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1060 <ntohs@plt>
mov    -0x58(%rbp),%rsi
mov    %eax,%edx
lea    0xd39(%rip),%rdi
call   1040 <printf@plt>
mov    -0x50(%rbp),%eax
mov    -0x40(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<client_connect>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r12
mov    %rdi,%rbx
mov    %rsp,-0x38(%rbp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%r15
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
lea    -0x50(%rbp),%r13
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2dfc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
lea    -0x30(%rbp),%rcx
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1090 <getaddrinfo@plt>
test   %eax,%eax
jne    127d <client_connect+0xfd>
mov    -0x30(%rbp),%rax
test   %rax,%rax
je     127d <client_connect+0xfd>
mov    0x14(%rax),%edi
mov    0xc(%rax),%edx
mov    0x10(%rax),%esi
call   10a0 <socket@plt>
test   %eax,%eax
js     127d <client_connect+0xfd>
mov    %eax,%r12d
mov    -0x30(%rbp),%rax
mov    (%rax),%rsi
mov    0x8(%rax),%edx
mov    %r12d,%edi
call   1080 <connect@plt>
test   %rax,%rax
js     127d <client_connect+0xfd>
mov    -0x30(%rbp),%rax
mov    (%rax),%rbx
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
lea    0x4(%rbx),%rsi
mov    %r15,%rdx
mov    %r14d,%ecx
call   1030 <inet_ntop@plt>
mov    (%rbx),%edi
call   1060 <ntohs@plt>
lea    0xda3(%rip),%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1040 <printf@plt>
mov    -0x38(%rbp),%rsp
mov    %r12d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.addrinfo = type { i64, i32, i32, i32, i32 }
@INET_ADDRSTRLEN = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [23 x i8] c"connected to %s:%d...\0A\00"
define i32 @client_connect(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.sockaddr_in*
%6 = alloca %struct.addrinfo
%7 = alloca %struct.addrinfo*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%12 = load i32, i32* @INET_ADDRSTRLEN
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %8
%15 = alloca i8, i64 %13
store i64 %13, i64* %9
%16 = call i32 @memset(%struct.addrinfo* %6, i32 0, i32 24)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 4
store i32 %17, i32* %18
%19 = load i32, i32* @SOCK_STREAM
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 3
store i32 %19, i32* %20
%21 = load i8*, i8** %3
%22 = load i8*, i8** %4
%23 = call i32 @getaddrinfo(i8* %21, i8* %22, %struct.addrinfo* %6, %struct.addrinfo** %7)
store i32 %23, i32* %10
%24 = load i32, i32* %10
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %2
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %2
%29 = load %struct.addrinfo*, %struct.addrinfo** %7
%30 = icmp eq %struct.addrinfo* %29, null
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %28
%34 = load %struct.addrinfo*, %struct.addrinfo** %7
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %34, i32 0, i32 4
%36 = load i32, i32* %35
%37 = load %struct.addrinfo*, %struct.addrinfo** %7
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i32 0, i32 3
%39 = load i32, i32* %38
%40 = load %struct.addrinfo*, %struct.addrinfo** %7
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %40, i32 0, i32 2
%42 = load i32, i32* %41
%43 = call i32 @socket(i32 %36, i32 %39, i32 %42)
store i32 %43, i32* %11
%44 = load i32, i32* %11
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %33
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %33
%49 = load i32, i32* %11
%50 = load %struct.addrinfo*, %struct.addrinfo** %7
%51 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %50, i32 0, i32 0
%52 = load i64, i64* %51
%53 = load %struct.addrinfo*, %struct.addrinfo** %7
%54 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %53, i32 0, i32 1
%55 = load i32, i32* %54
%56 = call i64 @connect(i32 %49, i64 %52, i32 %55)
%57 = icmp slt i64 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %48
%59 = call i32 @exit(i32 1)
unreachable
60: ; preds = %48
%61 = load %struct.addrinfo*, %struct.addrinfo** %7
%62 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %61, i32 0, i32 0
%63 = load i64, i64* %62
%64 = inttoptr i64 %63 to %struct.sockaddr_in*
store %struct.sockaddr_in* %64, %struct.sockaddr_in** %5
%65 = load i32, i32* @AF_INET
%66 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%67 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %66, i32 0, i32 1
%68 = trunc i64 %13 to i32
%69 = call i32 @inet_ntop(i32 %65, i32* %67, i8* %15, i32 %68)
%70 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%71 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %70, i32 0, i32 0
%72 = load i32, i32* %71
%73 = call i32 @ntohs(i32 %72)
%74 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %15, i32 %73)
%75 = load i32, i32* %11
%76 = load i8*, i8** %8
ret i32 %75
}

LLVM IR (-O2):
%struct.addrinfo = type { i64, i32, i32, i32, i32 }
%struct.sockaddr_in = type { i32, i32 }
@INET_ADDRSTRLEN = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [23 x i8] c"connected to %s:%d...\0A\00"
define i32 @client_connect(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 = load i32, i32* @INET_ADDRSTRLEN
%8 = zext i32 %7 to i64
%9 = tail call i8* @llvm.stacksave()
%10 = alloca i8, i64 %8
%11 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 24)
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
store i32 %12, i32* %13
%14 = load i32, i32* @SOCK_STREAM
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %14, i32* %15
%16 = call i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %3, %struct.addrinfo** %4)
%17 = icmp eq i32 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %2
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load %struct.addrinfo*, %struct.addrinfo** %4
%22 = icmp eq %struct.addrinfo* %21, null
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %20
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i64 0, i32 4
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i64 0, i32 3
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i64 0, i32 2
%31 = load i32, i32* %30
%32 = call i32 @socket(i32 %27, i32 %29, i32 %31)
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %25
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %25
%37 = load %struct.addrinfo*, %struct.addrinfo** %4
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i64 0, i32 0
%39 = load i64, i64* %38
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i64 0, i32 1
%41 = load i32, i32* %40
%42 = call i64 @connect(i32 %32, i64 %39, i32 %41)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %36
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %36
%47 = load %struct.addrinfo*, %struct.addrinfo** %4
%48 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %47, i64 0, i32 0
%49 = load i64, i64* %48
%50 = inttoptr i64 %49 to %struct.sockaddr_in*
%51 = load i32, i32* @AF_INET
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %50, i64 0, i32 1
%53 = call i32 @inet_ntop(i32 %51, i32* %52, i8* %10, i32 %7)
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %50, i64 0, i32 0
%55 = load i32, i32* %54
%56 = call i32 @ntohs(i32 %55)
%57 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %10, i32 %56)
ret i32 %32
}