#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 stat {
  int st_size;
};
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;
typedef scalar_t__ off_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bcopy(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fstat(int, struct stat *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, unsigned long long);
unsigned long long rdtsc();
int sendfile(int, int, scalar_t__ *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  int sockfd, sock_ret, n, m;
  int filefd;
  struct sockaddr_in serv_addr;
  struct hostent *server;
  struct stat file_buf;
  off_t offset = 0;
  char read_buffer[256];
  char write_buffer[256];
  char filename[] = "send.txt";
  int portnum = 12321;
  unsigned long long 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);
  sock_ret = connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
  if (sock_ret < 0) {
    error("ERROR connecting");
  } else {
    filefd = open(filename, O_RDONLY);
    fstat(filefd, &file_buf);
    offset = 0;
    start_time = rdtsc();
    n = sendfile(sockfd, filefd, &offset, file_buf.st_size);
    if (n < 0) {
      error("ERROR sending file");
    }
    if (n == file_buf.st_size) {
      end_time = rdtsc();
      close(sockfd);
    }
    printf("time: %lu\n", (end_time - start_time));
  }
  close(sockfd);
  close(filefd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x270,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x48(%rbp)
mov    0xde8(%rip),%rax
mov    %rax,-0x259(%rbp)
mov    0xde3(%rip),%al
mov    %al,-0x251(%rbp)
movl   $0x3021,-0x260(%rbp)
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
mov    0x2d83(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1267 <main+0x77>
lea    0xda7(%rip),%rdi
call   10b0 <error@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    129f <main+0xaf>
mov    0x2d46(%rip),%rax
mov    (%rax),%edi
lea    0xd8b(%rip),%rsi
call   1080 <fprintf@plt>
xor    %edi,%edi
call   10e0 <exit@plt>
lea    -0x30(%rbp),%rdi
mov    $0xc,%esi
call   10d0 <bzero@plt>
mov    0x2d34(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x30(%rbp),%rsi
add    $0x4,%rsi
mov    -0x38(%rbp),%rax
mov    (%rax),%edx
call   10a0 <bcopy@plt>
mov    -0x260(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   10f0 <connect@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1311 <main+0x121>
lea    0xd2c(%rip),%rdi
call   10b0 <error@plt>
jmp    13b2 <main+0x1c2>
lea    -0x259(%rbp),%rdi
mov    0x2cc1(%rip),%rax
mov    (%rax),%esi
call   10c0 <open@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x40(%rbp),%rsi
call   1100 <fstat@plt>
movq   $0x0,-0x48(%rbp)
mov    $0x0,%al
call   1050 <rdtsc@plt>
mov    %rax,-0x268(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x24(%rbp),%esi
mov    -0x40(%rbp),%ecx
lea    -0x48(%rbp),%rdx
call   1090 <sendfile@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1376 <main+0x186>
lea    0xcd3(%rip),%rdi
call   10b0 <error@plt>
mov    -0x1c(%rbp),%eax
cmp    -0x40(%rbp),%eax
jne    1398 <main+0x1a8>
mov    $0x0,%al
call   1050 <rdtsc@plt>
mov    %rax,-0x270(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x270(%rbp),%rsi
sub    -0x268(%rbp),%rsi
lea    0xcaa(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x270,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%rbp
movq   $0x0,0x10(%rsp)
movabs $0x7478742e646e6573,%rax
74 78 74
mov    %rax,0x18(%rsp)
movb   $0x0,0x20(%rsp)
mov    0x2dc3(%rip),%r14
mov    (%r14),%edi
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,%r12d
test   %eax,%eax
jns    124b <main+0x5b>
lea    0xdc3(%rip),%rdi
call   10b0 <error@plt>
mov    0x8(%rbp),%rdi
call   1070 <gethostbyname@plt>
test   %rax,%rax
je     136a <main+0x17a>
mov    %rax,%rbp
lea    0x28(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xc,%esi
call   10d0 <bzero@plt>
mov    (%r14),%eax
mov    %eax,0x30(%rsp)
mov    0x8(%rbp),%rsi
lea    0x2c(%rsp),%rdi
mov    0x0(%rbp),%edx
call   10a0 <memmove@plt>
mov    $0x3021,%edi
call   1030 <htons@plt>
mov    %eax,0x28(%rsp)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10f0 <connect@plt>
test   %eax,%eax
js     132a <main+0x13a>
mov    0x2d2d(%rip),%rax
mov    (%rax),%esi
lea    0x18(%rsp),%rdi
call   10c0 <open@plt>
mov    %eax,%r15d
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1100 <fstat@plt>
movq   $0x0,0x10(%rsp)
xor    %eax,%eax
call   1050 <rdtsc@plt>
mov    %rax,%r14
mov    0x8(%rsp),%r13d
lea    0x10(%rsp),%rdx
mov    %r12d,%edi
mov    %r15d,%esi
mov    %r13d,%ecx
call   1090 <sendfile@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1357 <main+0x167>
cmp    %r13d,%ebp
jne    1316 <main+0x126>
xor    %eax,%eax
call   1050 <rdtsc@plt>
mov    %rax,%rbx
mov    %r12d,%edi
call   1060 <close@plt>
sub    %r14,%rbx
lea    0xd37(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
jmp    1336 <main+0x146>
lea    0xd02(%rip),%rdi
call   10b0 <error@plt>
mov    %r12d,%edi
call   1060 <close@plt>
mov    %r15d,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xce6(%rip),%rdi
call   10b0 <error@plt>
cmp    %r13d,%ebp
jne    1316 <main+0x126>
jmp    1304 <main+0x114>
mov    0x2c5f(%rip),%rax
mov    (%rax),%edi
lea    0xca4(%rip),%rsi
call   1080 <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.stat = type { i32 }
%struct.sockaddr = type { i32 }
@__const.main.filename = [9 x i8] c"send.txt\00"
@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"
@O_RDONLY = global i32 0
@.str.3 = [19 x i8] c"ERROR sending file\00"
@.str.4 = [11 x i8] c"time: %lu\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 i32
%10 = alloca i32
%11 = alloca %struct.sockaddr_in
%12 = alloca %struct.hostent*
%13 = alloca %struct.stat
%14 = alloca i64
%15 = alloca [256 x i8]
%16 = alloca [256 x i8]
%17 = alloca [9 x i8]
%18 = alloca i32
%19 = alloca i64
%20 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 0, i64* %14
%21 = bitcast [9 x i8]* %17 to i8*
store i32 12321, i32* %18
%22 = load i32, i32* @AF_INET
%23 = load i32, i32* @SOCK_STREAM
%24 = call i32 @socket(i32 %22, i32 %23, i32 0)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %2
%28 = call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %29
29: ; preds = %27, %2
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = call %struct.hostent* @gethostbyname(i8* %32)
store %struct.hostent* %33, %struct.hostent** %12
%34 = load %struct.hostent*, %struct.hostent** %12
%35 = icmp eq %struct.hostent* %34, null
br i1 %35, label %36, label %40
36: ; preds = %29
%37 = load i32, i32* @stderr
%38 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%39 = call i32 @exit(i32 0)
unreachable
40: ; preds = %29
%41 = bitcast %struct.sockaddr_in* %11 to i8*
%42 = call i32 @bzero(i8* %41, i32 12)
%43 = load i32, i32* @AF_INET
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %43, i32* %44
%45 = load %struct.hostent*, %struct.hostent** %12
%46 = getelementptr inbounds %struct.hostent, %struct.hostent* %45, i32 0, i32 1
%47 = load i64, i64* %46
%48 = inttoptr i64 %47 to i8*
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
%50 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %49, i32 0, i32 0
%51 = bitcast i32* %50 to i8*
%52 = load %struct.hostent*, %struct.hostent** %12
%53 = getelementptr inbounds %struct.hostent, %struct.hostent* %52, i32 0, i32 0
%54 = load i32, i32* %53
%55 = call i32 @bcopy(i8* %48, i8* %51, i32 %54)
%56 = load i32, i32* %18
%57 = call i32 @htons(i32 %56)
%58 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
store i32 %57, i32* %58
%59 = load i32, i32* %6
%60 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%61 = call i32 @connect(i32 %59, %struct.sockaddr* %60, i32 12)
store i32 %61, i32* %7
%62 = load i32, i32* %7
%63 = icmp slt i32 %62, 0
br i1 %63, label %64, label %66
64: ; preds = %40
%65 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %96
66: ; preds = %40
%67 = getelementptr inbounds [9 x i8], [9 x i8]* %17, i64 0, i64 0
%68 = load i32, i32* @O_RDONLY
%69 = call i32 @open(i8* %67, i32 %68)
store i32 %69, i32* %10
%70 = load i32, i32* %10
%71 = call i32 @fstat(i32 %70, %struct.stat* %13)
store i64 0, i64* %14
%72 = call i64 (...) @rdtsc()
store i64 %72, i64* %19
%73 = load i32, i32* %6
%74 = load i32, i32* %10
%75 = getelementptr inbounds %struct.stat, %struct.stat* %13, i32 0, i32 0
%76 = load i32, i32* %75
%77 = call i32 @sendfile(i32 %73, i32 %74, i64* %14, i32 %76)
store i32 %77, i32* %8
%78 = load i32, i32* %8
%79 = icmp slt i32 %78, 0
br i1 %79, label %80, label %82
80: ; preds = %66
%81 = call i32 @error(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %82
82: ; preds = %80, %66
%83 = load i32, i32* %8
%84 = getelementptr inbounds %struct.stat, %struct.stat* %13, i32 0, i32 0
%85 = load i32, i32* %84
%86 = icmp eq i32 %83, %85
br i1 %86, label %87, label %91
87: ; preds = %82
%88 = call i64 (...) @rdtsc()
store i64 %88, i64* %20
%89 = load i32, i32* %6
%90 = call i32 @close(i32 %89)
br label %91
91: ; preds = %87, %82
%92 = load i64, i64* %20
%93 = load i64, i64* %19
%94 = sub i64 %92, %93
%95 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i64 %94)
br label %96
96: ; preds = %91, %64
%97 = load i32, i32* %6
%98 = call i32 @close(i32 %97)
%99 = load i32, i32* %10
%100 = call i32 @close(i32 %99)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.stat = type { i32 }
%struct.hostent = type { i32, i64 }
%struct.sockaddr = type { i32 }
@__const.main.filename = [9 x i8] c"send.txt\00"
@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"
@O_RDONLY = global i32 0
@.str.3 = [19 x i8] c"ERROR sending file\00"
@.str.4 = [11 x i8] c"time: %lu\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.stat
%5 = alloca i64
%6 = alloca [9 x i8]
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = bitcast %struct.stat* %4 to i8*
%9 = bitcast i64* %5 to i8*
store i64 0, i64* %5
%10 = getelementptr inbounds [9 x i8], [9 x i8]* %6, i64 0, i64 0
%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 %17
15: ; preds = %2
%16 = tail call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %2
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
%20 = tail call %struct.hostent* @gethostbyname(i8* %19)
%21 = icmp eq %struct.hostent* %20, null
br i1 %21, label %22, label %26
22: ; preds = %17
%23 = load i32, i32* @stderr
%24 = tail call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%25 = tail call i32 @exit(i32 0)
unreachable
26: ; preds = %17
%27 = call i32 @bzero(i8* %7, i32 12)
%28 = load i32, i32* @AF_INET
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %28, i32* %29
%30 = getelementptr inbounds %struct.hostent, %struct.hostent* %20, i64 0, i32 1
%31 = load i64, i64* %30
%32 = inttoptr i64 %31 to i8*
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
%34 = bitcast i32* %33 to i8*
%35 = getelementptr inbounds %struct.hostent, %struct.hostent* %20, i64 0, i32 0
%36 = load i32, i32* %35
%37 = tail call i32 @htons(i32 12321)
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %37, i32* %38
%39 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%40 = call i32 @connect(i32 %13, %struct.sockaddr* %39, i32 12)
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %26
%43 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %64
44: ; preds = %26
%45 = load i32, i32* @O_RDONLY
%46 = call i32 @open(i8* %10, i32 %45)
%47 = call i32 @fstat(i32 %46, %struct.stat* %4)
store i64 0, i64* %5
%48 = call i64 (...) @rdtsc()
%49 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%50 = load i32, i32* %49
%51 = call i32 @sendfile(i32 %13, i32 %46, i64* %5, i32 %50)
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %44
%54 = call i32 @error(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %55
55: ; preds = %53, %44
%56 = icmp eq i32 %51, %50
br i1 %56, label %57, label %60
57: ; preds = %55
%58 = call i64 (...) @rdtsc()
%59 = call i32 @close(i32 %13)
br label %60
60: ; preds = %57, %55
%61 = phi i64 [ %58, %57 ], [ undef, %55 ]
%62 = sub i64 %61, %48
%63 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i64 %62)
br label %64
64: ; preds = %60, %42
%65 = phi i32 [ undef, %42 ], [ %46, %60 ]
%66 = call i32 @close(i32 %13)
%67 = call i32 @close(i32 %65)
ret i32 0
}