#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*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ getaddrinfo(char *, char *, struct addrinfo *,
                               struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int client_socket(char *hostname, char *port) {
  int sockfd = socket(AF_INET, SOCK_STREAM, 0);
  struct addrinfo their_addrinfo;
  struct addrinfo *their_addr = NULL;
  memset(&their_addrinfo, 0, sizeof(struct addrinfo));
  their_addrinfo.ai_family = AF_INET;
  their_addrinfo.ai_socktype = SOCK_STREAM;
  getaddrinfo(hostname, port, &their_addrinfo, &their_addr);
  int rc = connect(sockfd, their_addr->ai_addr, their_addr->ai_addrlen);
  if (rc == -1) {
    perror("connect");
    exit(1);
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<client_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x14(%rbp)
movq   $0x0,-0x30(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x28(%rbp),%rdx
lea    -0x30(%rbp),%rcx
call   1070 <getaddrinfo@plt>
mov    -0x14(%rbp),%edi
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   1060 <connect@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1209 <client_socket+0xa9>
lea    0xe06(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x14(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<client_socket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e6d(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2e52(%rip),%rbp
mov    0x0(%rbp),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebx
movq   $0x0,(%rsp)
lea    0x8(%rsp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
mov    0x0(%r13),%eax
mov    %eax,0x14(%rsp)
mov    0x0(%rbp),%eax
mov    %eax,0x10(%rsp)
mov    %rsp,%rcx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
call   1070 <getaddrinfo@plt>
mov    (%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebx,%edi
call   1060 <connect@plt>
cmp    $0xffffffff,%eax
je     11f4 <client_socket+0x94>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe05(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [8 x i8] c"connect\00"
define i32 @client_socket(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.addrinfo
%7 = alloca %struct.addrinfo*
%8 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %5
store %struct.addrinfo* null, %struct.addrinfo** %7
%12 = call i32 @memset(%struct.addrinfo* %6, i32 0, i32 16)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 3
store i32 %13, i32* %14
%15 = load i32, i32* @SOCK_STREAM
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 2
store i32 %15, i32* %16
%17 = load i8*, i8** %3
%18 = load i8*, i8** %4
%19 = call i32 @getaddrinfo(i8* %17, i8* %18, %struct.addrinfo* %6, %struct.addrinfo** %7)
%20 = load i32, i32* %5
%21 = load %struct.addrinfo*, %struct.addrinfo** %7
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i32 0, i32 1
%23 = load i32, i32* %22
%24 = load %struct.addrinfo*, %struct.addrinfo** %7
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i32 0, i32 0
%26 = load i32, i32* %25
%27 = call i32 @connect(i32 %20, i32 %23, i32 %26)
store i32 %27, i32* %8
%28 = load i32, i32* %8
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %2
%31 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %2
%34 = load i32, i32* %5
ret i32 %34
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [8 x i8] c"connect\00"
define i32 @client_socket(i8* %0, i8* %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = bitcast %struct.addrinfo* %3 to i8*
%9 = bitcast %struct.addrinfo** %4 to i8*
store %struct.addrinfo* null, %struct.addrinfo** %4
%10 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 16)
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %11, i32* %12
%13 = load i32, i32* @SOCK_STREAM
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 2
store i32 %13, i32* %14
%15 = call i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %3, %struct.addrinfo** %4)
%16 = load %struct.addrinfo*, %struct.addrinfo** %4
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i64 0, i32 1
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i64 0, i32 0
%20 = load i32, i32* %19
%21 = call i32 @connect(i32 %7, i32 %18, i32 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %2
%24 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %2
ret i32 %7
}