#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__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ serv_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bcopy(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ printf(char *, unsigned int);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char *);
int write(int, char *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int sockfd, n, m;
  struct sockaddr_in serv_addr;
  struct hostent *server;
  char read_buffer[256];
  char write_buffer[256] = "hello";
  int portnum = 12321;
  unsigned int start_time, end_time;
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0)
    error("ERROR opening socket");
  server = gethostbyname(argv[1]);
  if (server == NULL) {
    fprintf(stderr, "ERROR, no such host\n");
    exit(0);
  }
  bzero((char *)&serv_addr, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr,
        server->h_length);
  serv_addr.sin_port = htons(portnum);
  if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
    error("ERROR connecting");
  bzero(read_buffer, 256);
  n = write(sockfd, write_buffer, strlen(write_buffer));
  m = read(sockfd, read_buffer, 255);
  if (m < 0) {
    error("ERROR reading from socket");
  }
  bzero(read_buffer, 256);
  close(sockfd);
  printf("%d\n", end_time - start_time);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x250,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x230(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1070 <memset@plt>
movb   $0x68,-0x230(%rbp)
movb   $0x65,-0x22f(%rbp)
movb   $0x6c,-0x22e(%rbp)
movb   $0x6c,-0x22d(%rbp)
movb   $0x6f,-0x22c(%rbp)
movl   $0x3021,-0x234(%rbp)
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
mov    0x2d77(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    127b <main+0x8b>
lea    0xd8a(%rip),%rdi
call   10d0 <error@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12b3 <main+0xc3>
mov    0x2d3a(%rip),%rax
mov    (%rax),%edi
lea    0xd6e(%rip),%rsi
call   10b0 <fprintf@plt>
xor    %edi,%edi
call   10f0 <exit@plt>
lea    -0x28(%rbp),%rdi
mov    $0xc,%esi
call   10e0 <bzero@plt>
mov    0x2d20(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x28(%rbp),%rsi
add    $0x4,%rsi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   10c0 <bcopy@plt>
mov    -0x234(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1100 <connect@plt>
cmp    $0x0,%rax
jge    131d <main+0x12d>
lea    0xd12(%rip),%rdi
call   10d0 <error@plt>
lea    -0x130(%rbp),%rdi
mov    $0x100,%esi
call   10e0 <bzero@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x24c(%rbp)
lea    -0x230(%rbp),%rax
mov    %rax,-0x248(%rbp)
lea    -0x230(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x24c(%rbp),%edi
mov    -0x248(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    $0xff,%edx
call   1090 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1395 <main+0x1a5>
lea    0xcab(%rip),%rdi
call   10d0 <error@plt>
lea    -0x130(%rbp),%rdi
mov    $0x100,%esi
call   10e0 <bzero@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
mov    -0x23c(%rbp),%esi
sub    -0x238(%rbp),%esi
lea    0xc94(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x250,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x218,%rsp
mov    %rsi,%r14
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movaps %xmm0,0x100(%rsp)
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movl   $0x6c6c6568,0x10(%rsp)
6c
movb   $0x6f,0x14(%rsp)
mov    0x2d76(%rip),%r15
mov    (%r15),%edi
mov    0x2d54(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1297 <main+0xb7>
lea    0xd6e(%rip),%rdi
call   10c0 <error@plt>
mov    0x8(%r14),%rdi
call   1090 <gethostbyname@plt>
test   %rax,%rax
je     1370 <main+0x190>
mov    %rax,%rbx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   10d0 <bzero@plt>
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
mov    0x8(%rbx),%rsi
lea    0x4(%rsp),%rdi
mov    (%rbx),%edx
call   10b0 <memmove@plt>
mov    $0x3021,%edi
call   1050 <htons@plt>
mov    %eax,(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10f0 <connect@plt>
test   %rax,%rax
jns    1300 <main+0x120>
lea    0xd2f(%rip),%rdi
call   10c0 <error@plt>
lea    0x110(%rsp),%r14
mov    %r14,%rdi
mov    $0x100,%esi
call   10d0 <bzero@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0xff,%edx
call   1080 <read@plt>
test   %eax,%eax
jns    134d <main+0x16d>
lea    0xcf3(%rip),%rdi
call   10c0 <error@plt>
mov    %ebp,%edi
call   1070 <close@plt>
lea    0xcfa(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c61(%rip),%rax
mov    (%rax),%edi
lea    0xc95(%rip),%rsi
call   10a0 <fprintf@plt>
xor    %edi,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [21 x i8] c"ERROR opening socket\00"
@stderr = global i32 0
@.str.1 = [21 x i8] c"ERROR, no such host\0A\00"
@.str.2 = [17 x i8] c"ERROR connecting\00"
@.str.3 = [26 x i8] c"ERROR reading from socket\00"
@.str.4 = [4 x i8] c"%d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
%10 = alloca %struct.hostent*
%11 = alloca [256 x i8]
%12 = alloca [256 x i8]
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = bitcast [256 x i8]* %12 to i8*
%17 = bitcast i8* %16 to [256 x i8]*
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %17, i32 0, i32 0
store i8 104, i8* %18
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %17, i32 0, i32 1
store i8 101, i8* %19
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %17, i32 0, i32 2
store i8 108, i8* %20
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %17, i32 0, i32 3
store i8 108, i8* %21
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %17, i32 0, i32 4
store i8 111, i8* %22
store i32 12321, i32* %13
%23 = load i32, i32* @AF_INET
%24 = load i32, i32* @SOCK_STREAM
%25 = call i32 @socket(i32 %23, i32 %24, i32 0)
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %2
%29 = call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %30
30: ; preds = %28, %2
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = call %struct.hostent* @gethostbyname(i8* %33)
store %struct.hostent* %34, %struct.hostent** %10
%35 = load %struct.hostent*, %struct.hostent** %10
%36 = icmp eq %struct.hostent* %35, null
br i1 %36, label %37, label %41
37: ; preds = %30
%38 = load i32, i32* @stderr
%39 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%40 = call i32 @exit(i32 0)
unreachable
41: ; preds = %30
%42 = bitcast %struct.sockaddr_in* %9 to i8*
%43 = call i32 @bzero(i8* %42, i32 12)
%44 = load i32, i32* @AF_INET
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %44, i32* %45
%46 = load %struct.hostent*, %struct.hostent** %10
%47 = getelementptr inbounds %struct.hostent, %struct.hostent* %46, i32 0, i32 1
%48 = load i64, i64* %47
%49 = inttoptr i64 %48 to i8*
%50 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%51 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %50, i32 0, i32 0
%52 = bitcast i32* %51 to i8*
%53 = load %struct.hostent*, %struct.hostent** %10
%54 = getelementptr inbounds %struct.hostent, %struct.hostent* %53, i32 0, i32 0
%55 = load i32, i32* %54
%56 = call i32 @bcopy(i8* %49, i8* %52, i32 %55)
%57 = load i32, i32* %13
%58 = call i32 @htons(i32 %57)
%59 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %58, i32* %59
%60 = load i32, i32* %6
%61 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%62 = call i64 @connect(i32 %60, %struct.sockaddr* %61, i32 12)
%63 = icmp slt i64 %62, 0
br i1 %63, label %64, label %66
64: ; preds = %41
%65 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %66
66: ; preds = %64, %41
%67 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%68 = call i32 @bzero(i8* %67, i32 256)
%69 = load i32, i32* %6
%70 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%71 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%72 = call i32 @strlen(i8* %71)
%73 = call i32 @write(i32 %69, i8* %70, i32 %72)
store i32 %73, i32* %7
%74 = load i32, i32* %6
%75 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%76 = call i32 @read(i32 %74, i8* %75, i32 255)
store i32 %76, i32* %8
%77 = load i32, i32* %8
%78 = icmp slt i32 %77, 0
br i1 %78, label %79, label %81
79: ; preds = %66
%80 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %81
81: ; preds = %79, %66
%82 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%83 = call i32 @bzero(i8* %82, i32 256)
%84 = load i32, i32* %6
%85 = call i32 @close(i32 %84)
%86 = load i32, i32* %15
%87 = load i32, i32* %14
%88 = sub i32 %86, %87
%89 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %88)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [21 x i8] c"ERROR opening socket\00"
@stderr = global i32 0
@.str.1 = [21 x i8] c"ERROR, no such host\0A\00"
@.str.2 = [17 x i8] c"ERROR connecting\00"
@.str.3 = [26 x i8] c"ERROR reading from socket\00"
@.str.4 = [4 x i8] c"%d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [256 x i8]
%5 = alloca [256 x i8]
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
store i8 104, i8* %8
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 1
store i8 101, i8* %9
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 2
store i8 108, i8* %10
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 3
store i8 108, i8* %11
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 4
store i8 111, i8* %12
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = tail call i32 @socket(i32 %13, i32 %14, i32 0)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = tail call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %2
%20 = getelementptr inbounds i8*, i8** %1, i64 1
%21 = load i8*, i8** %20
%22 = tail call %struct.hostent* @gethostbyname(i8* %21)
%23 = icmp eq %struct.hostent* %22, null
br i1 %23, label %24, label %28
24: ; preds = %19
%25 = load i32, i32* @stderr
%26 = tail call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%27 = tail call i32 @exit(i32 0)
unreachable
28: ; preds = %19
%29 = call i32 @bzero(i8* %6, i32 12)
%30 = load i32, i32* @AF_INET
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %30, i32* %31
%32 = getelementptr inbounds %struct.hostent, %struct.hostent* %22, i64 0, i32 1
%33 = load i64, i64* %32
%34 = inttoptr i64 %33 to i8*
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
%36 = bitcast i32* %35 to i8*
%37 = getelementptr inbounds %struct.hostent, %struct.hostent* %22, i64 0, i32 0
%38 = load i32, i32* %37
%39 = tail call i32 @htons(i32 12321)
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %39, i32* %40
%41 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%42 = call i64 @connect(i32 %15, %struct.sockaddr* %41, i32 12)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %28
%45 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %46
46: ; preds = %44, %28
%47 = call i32 @bzero(i8* %7, i32 256)
%48 = call i32 @strlen(i8* %8)
%49 = call i32 @write(i32 %15, i8* %8, i32 %48)
%50 = call i32 @read(i32 %15, i8* %7, i32 255)
%51 = icmp slt i32 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %46
%53 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %54
54: ; preds = %52, %46
%55 = call i32 @close(i32 %15)
%56 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 undef)
ret i32 0
}