#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  unsigned long s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  scalar_t__ *h_addr_list;
};
typedef int /*<<< orphan*/ server_socket_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main(int argc, char **argv) {
  int socket_fd = 0;
  struct sockaddr_in server_socket_addr;
  struct hostent *he = gethostbyname("localhost");
  unsigned long server_addr_nbo = *(unsigned long *)(he->h_addr_list[0]);
  if (0 > (socket_fd = socket(AF_INET, SOCK_STREAM, 0))) {
    fprintf(stderr, "client failed to create socket\n");
    exit(1);
  }
  bzero(&server_socket_addr, sizeof(server_socket_addr));
  server_socket_addr.sin_family = AF_INET;
  server_socket_addr.sin_port = htons(8888);
  server_socket_addr.sin_addr.s_addr = server_addr_nbo;
  if (0 > connect(socket_fd, (struct sockaddr *)&server_socket_addr,
                  sizeof(server_socket_addr))) {
    fprintf(stderr, "client failed to connect to %s:%d!\n", "localhost", 8888);
    close(socket_fd);
    exit(1);
  } else {
    fprintf(stdout, "client connected to to %s:%d!\n", "localhost", 8888);
  }
  close(socket_fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
lea    0xe5c(%rip),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ecx
mov    %ecx,-0x14(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jle    1207 <main+0x87>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
lea    0xe14(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    -0x28(%rbp),%rdi
mov    $0x10,%esi
call   1070 <bzero@plt>
mov    0x2dcc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    $0x22b8,%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x10,%edx
call   1090 <connect@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jle    128a <main+0x10a>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
lea    0xdc5(%rip),%rsi
lea    0xd94(%rip),%rdx
mov    $0x22b8,%ecx
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2d37(%rip),%rax
mov    (%rax),%edi
lea    0xdb4(%rip),%rsi
lea    0xd5f(%rip),%rdx
mov    $0x22b8,%ecx
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xe6e(%rip),%rdi
call   1050 <gethostbyname@plt>
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%r15
mov    0x2e41(%rip),%r12
mov    (%r12),%edi
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     123a <main+0xba>
mov    %eax,%ebx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
mov    $0x10,%esi
call   1070 <bzero@plt>
mov    (%r12),%eax
mov    %eax,0x14(%rsp)
mov    $0x22b8,%edi
call   1030 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %r15,0x8(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x10,%edx
call   1090 <connect@plt>
test   %rax,%rax
js     125b <main+0xdb>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
lea    0xe3c(%rip),%rsi
lea    0xde7(%rip),%rdx
mov    $0x22b8,%ecx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
lea    0xdc0(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
lea    0xdbf(%rip),%rsi
lea    0xd8e(%rip),%rdx
mov    $0x22b8,%ecx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i64 }
%struct.hostent = type { i64* }
%struct.sockaddr = type { i32 }
@.str = [10 x i8] c"localhost\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str.1 = [32 x i8] c"client failed to create socket\0A\00"
@.str.2 = [36 x i8] c"client failed to connect to %s:%d!\0A\00"
@stdout = global i32 0
@.str.3 = [31 x i8] c"client connected to to %s:%d!\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca %struct.hostent*
%9 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
%10 = call %struct.hostent* @gethostbyname(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store %struct.hostent* %10, %struct.hostent** %8
%11 = load %struct.hostent*, %struct.hostent** %8
%12 = getelementptr inbounds %struct.hostent, %struct.hostent* %11, i32 0, i32 0
%13 = load i64*, i64** %12
%14 = getelementptr inbounds i64, i64* %13, i64 0
%15 = load i64, i64* %14
%16 = inttoptr i64 %15 to i64*
%17 = load i64, i64* %16
store i64 %17, i64* %9
%18 = load i32, i32* @AF_INET
%19 = load i32, i32* @SOCK_STREAM
%20 = call i32 @socket(i32 %18, i32 %19, i32 0)
store i32 %20, i32* %6
%21 = icmp sgt i32 0, %20
br i1 %21, label %22, label %26
22: ; preds = %2
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %2
%27 = call i32 @bzero(%struct.sockaddr_in* %7, i32 16)
%28 = load i32, i32* @AF_INET
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %28, i32* %29
%30 = call i32 @htons(i32 8888)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %30, i32* %31
%32 = load i64, i64* %9
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%34 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %33, i32 0, i32 0
store i64 %32, i64* %34
%35 = load i32, i32* %6
%36 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%37 = call i64 @connect(i32 %35, %struct.sockaddr* %36, i32 16)
%38 = icmp sgt i64 0, %37
br i1 %38, label %39, label %45
39: ; preds = %26
%40 = load i32, i32* @stderr
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 8888)
%42 = load i32, i32* %6
%43 = call i32 @close(i32 %42)
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %26
%46 = load i32, i32* @stdout
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 8888)
br label %48
48: ; preds = %45
%49 = load i32, i32* %6
%50 = call i32 @close(i32 %49)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i64 }
%struct.hostent = type { i64* }
%struct.sockaddr = type { i32 }
@.str = [10 x i8] c"localhost\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str.1 = [32 x i8] c"client failed to create socket\0A\00"
@.str.2 = [36 x i8] c"client failed to connect to %s:%d!\0A\00"
@stdout = global i32 0
@.str.3 = [31 x i8] c"client connected to to %s:%d!\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = tail call %struct.hostent* @gethostbyname(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%6 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 0, i32 0
%7 = load i64*, i64** %6
%8 = load i64, i64* %7
%9 = inttoptr i64 %8 to i64*
%10 = load i64, i64* %9
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = tail call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = tail call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = call i32 @bzero(%struct.sockaddr_in* %3, i32 16)
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %21, i32* %22
%23 = tail call i32 @htons(i32 8888)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i64 %10, i64* %25
%26 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%27 = call i64 @connect(i32 %13, %struct.sockaddr* %26, i32 16)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %19
%30 = load i32, i32* @stderr
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 8888)
%32 = call i32 @close(i32 %13)
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %19
%35 = load i32, i32* @stdout
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 8888)
%37 = call i32 @close(i32 %13)
ret i32 0
}