#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 in_addr {
  int /*<<< orphan*/ s_addr;
};
struct hostent {
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ sin;
typedef int /*<<< orphan*/ off_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int fstat(int, struct stat *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int sendfile(int, int, int /*<<< orphan*/ *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  int fd, sd;
  char *src, *host;
  struct stat sbuf;
  int ret;
  off_t offset = 0;
  struct sockaddr_in sin;
  struct hostent *hp;
  if (argc < 3) {
    fputs("Usage: sendfiletest <src> <host>\n", stderr);
    exit(1);
  }
  src = argv[1];
  host = argv[2];
  fd = open(src, O_RDONLY);
  if (fd == -1) {
    perror("open");
    exit(1);
  }
  if (fstat(fd, &sbuf) == -1) {
    perror("fstat");
    exit(1);
  }
  hp = gethostbyname(host);
  if (hp == NULL) {
    perror("gethostbyname");
    exit(1);
  }
  memset(&sin, 0, sizeof(sin));
  sin.sin_family = AF_INET;
  sin.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;
  sin.sin_port = htons(8001);
  sd = socket(AF_INET, SOCK_STREAM, 0);
  if (sd == -1) {
    perror("socket");
    exit(1);
  }
  if (connect(sd, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
    perror("connect");
    exit(1);
  }
  ret = sendfile(sd, fd, &offset, sbuf.st_size);
  if (ret == -1) {
    perror("sendfile");
    exit(1);
  } else if (ret != sbuf.st_size) {
    fputs("sendfile didn't send the whole thing\n", stderr);
    exit(1);
  }
  close(fd);
  close(sd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x38(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    1206 <main+0x46>
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
lea    0xe09(%rip),%rdi
call   1040 <fputs@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1253 <main+0x93>
lea    0xdde(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   10d0 <fstat@plt>
cmp    $0xffffffff,%eax
jne    127e <main+0xbe>
lea    0xdb8(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x28(%rbp),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    12ac <main+0xec>
lea    0xd90(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2d25(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x50(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    $0x1f41,%edi
call   1030 <htons@plt>
mov    %eax,-0x48(%rbp)
mov    0x2d00(%rip),%rax
mov    (%rax),%edi
mov    0x2cd7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    131d <main+0x15d>
lea    0xd2d(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%edi
lea    -0x48(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <connect@plt>
cmp    $0xffffffff,%eax
jne    134d <main+0x18d>
lea    0xd04(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    -0x30(%rbp),%ecx
lea    -0x38(%rbp),%rdx
call   1080 <sendfile@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1382 <main+0x1c2>
lea    0xcd7(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x34(%rbp),%eax
cmp    -0x30(%rbp),%eax
je     13ad <main+0x1ed>
mov    0x2c3b(%rip),%rax
mov    (%rax),%esi
lea    0xcb5(%rip),%rdi
call   1040 <fputs@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    13b2 <main+0x1f2>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
movl   $0x0,0xc(%rsp)
cmp    $0x2,%edi
jle    12c1 <main+0x101>
mov    0x8(%rsi),%rdi
mov    0x10(%rsi),%rbp
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     12d3 <main+0x113>
mov    %eax,%r14d
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   10d0 <fstat@plt>
cmp    $0xffffffff,%eax
je     12dc <main+0x11c>
mov    %rbp,%rdi
call   1070 <gethostbyname@plt>
test   %rax,%rax
je     12e5 <main+0x125>
mov    %rax,%rbp
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2dac(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
mov    0x0(%rbp),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    $0x1f41,%edi
call   1030 <htons@plt>
mov    %eax,0x10(%rsp)
mov    (%rbx),%edi
mov    0x2d65(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
cmp    $0xffffffff,%eax
je     12ee <main+0x12e>
mov    %eax,%ebp
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   10c0 <connect@plt>
cmp    $0xffffffff,%eax
je     12f7 <main+0x137>
mov    0x8(%rsp),%ebx
lea    0xc(%rsp),%rdx
mov    %ebp,%edi
mov    %r14d,%esi
mov    %ebx,%ecx
call   1080 <sendfile@plt>
cmp    $0xffffffff,%eax
je     1300 <main+0x140>
cmp    %ebx,%eax
jne    1316 <main+0x156>
mov    %r14d,%edi
call   1060 <close@plt>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d08(%rip),%rax
mov    (%rax),%esi
lea    0xd2f(%rip),%rdi
jmp    1326 <main+0x166>
lea    0xd48(%rip),%rdi
jmp    1307 <main+0x147>
lea    0xd44(%rip),%rdi
jmp    1307 <main+0x147>
lea    0xd41(%rip),%rdi
jmp    1307 <main+0x147>
lea    0xd46(%rip),%rdi
jmp    1307 <main+0x147>
lea    0xd44(%rip),%rdi
jmp    1307 <main+0x147>
lea    0xd43(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2cb3(%rip),%rax
mov    (%rax),%esi
lea    0xd2d(%rip),%rdi
call   1040 <fputs@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i64 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [34 x i8] c"Usage: sendfiletest <src> <host>\0A\00"
@stderr = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [6 x i8] c"fstat\00"
@.str.3 = [14 x i8] c"gethostbyname\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.4 = [7 x i8] c"socket\00"
@.str.5 = [8 x i8] c"connect\00"
@.str.6 = [9 x i8] c"sendfile\00"
@.str.7 = [38 x i8] c"sendfile didn't send the whole thing\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 i8*
%9 = alloca i8*
%10 = alloca %struct.stat
%11 = alloca i32
%12 = alloca i32
%13 = alloca %struct.sockaddr_in
%14 = alloca %struct.hostent*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %12
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 3
br i1 %16, label %17, label %21
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = call i32 @fputs(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
store i8* %24, i8** %8
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 2
%27 = load i8*, i8** %26
store i8* %27, i8** %9
%28 = load i8*, i8** %8
%29 = load i32, i32* @O_RDONLY
%30 = call i32 @open(i8* %28, i32 %29)
store i32 %30, i32* %6
%31 = load i32, i32* %6
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %21
%34 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %21
%37 = load i32, i32* %6
%38 = call i32 @fstat(i32 %37, %struct.stat* %10)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %36
%44 = load i8*, i8** %9
%45 = call %struct.hostent* @gethostbyname(i8* %44)
store %struct.hostent* %45, %struct.hostent** %14
%46 = load %struct.hostent*, %struct.hostent** %14
%47 = icmp eq %struct.hostent* %46, null
br i1 %47, label %48, label %51
48: ; preds = %43
%49 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %43
%52 = call i32 @memset(%struct.sockaddr_in* %13, i32 0, i32 12)
%53 = load i32, i32* @AF_INET
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 2
store i32 %53, i32* %54
%55 = load %struct.hostent*, %struct.hostent** %14
%56 = getelementptr inbounds %struct.hostent, %struct.hostent* %55, i32 0, i32 0
%57 = load i64, i64* %56
%58 = inttoptr i64 %57 to %struct.in_addr*
%59 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %58, i32 0, i32 0
%60 = load i32, i32* %59
%61 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 1
%62 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %61, i32 0, i32 0
store i32 %60, i32* %62
%63 = call i32 @htons(i32 8001)
%64 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 0
store i32 %63, i32* %64
%65 = load i32, i32* @AF_INET
%66 = load i32, i32* @SOCK_STREAM
%67 = call i32 @socket(i32 %65, i32 %66, i32 0)
store i32 %67, i32* %7
%68 = load i32, i32* %7
%69 = icmp eq i32 %68, -1
br i1 %69, label %70, label %73
70: ; preds = %51
%71 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%72 = call i32 @exit(i32 1)
unreachable
73: ; preds = %51
%74 = load i32, i32* %7
%75 = bitcast %struct.sockaddr_in* %13 to %struct.sockaddr*
%76 = call i32 @connect(i32 %74, %struct.sockaddr* %75, i32 12)
%77 = icmp eq i32 %76, -1
br i1 %77, label %78, label %81
78: ; preds = %73
%79 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
%80 = call i32 @exit(i32 1)
unreachable
81: ; preds = %73
%82 = load i32, i32* %7
%83 = load i32, i32* %6
%84 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%85 = load i32, i32* %84
%86 = call i32 @sendfile(i32 %82, i32 %83, i32* %12, i32 %85)
store i32 %86, i32* %11
%87 = load i32, i32* %11
%88 = icmp eq i32 %87, -1
br i1 %88, label %89, label %92
89: ; preds = %81
%90 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
%91 = call i32 @exit(i32 1)
unreachable
92: ; preds = %81
%93 = load i32, i32* %11
%94 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%95 = load i32, i32* %94
%96 = icmp ne i32 %93, %95
br i1 %96, label %97, label %101
97: ; preds = %92
%98 = load i32, i32* @stderr
%99 = call i32 @fputs(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.7, i64 0, i64 0), i32 %98)
%100 = call i32 @exit(i32 1)
unreachable
101: ; preds = %92
br label %102
102: ; preds = %101
%103 = load i32, i32* %6
%104 = call i32 @close(i32 %103)
%105 = load i32, i32* %7
%106 = call i32 @close(i32 %105)
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i64 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [34 x i8] c"Usage: sendfiletest <src> <host>\0A\00"
@stderr = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [6 x i8] c"fstat\00"
@.str.3 = [14 x i8] c"gethostbyname\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.4 = [7 x i8] c"socket\00"
@.str.5 = [8 x i8] c"connect\00"
@.str.6 = [9 x i8] c"sendfile\00"
@.str.7 = [38 x i8] c"sendfile didn't send the whole thing\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = bitcast %struct.stat* %3 to i8*
%7 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%8 = bitcast %struct.sockaddr_in* %5 to i8*
%9 = icmp slt i32 %0, 3
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = tail call i32 @fputs(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = getelementptr inbounds i8*, i8** %1, i64 2
%18 = load i8*, i8** %17
%19 = load i32, i32* @O_RDONLY
%20 = tail call i32 @open(i8* %16, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %14
%23 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%24 = tail call i32 @exit(i32 1)
unreachable
25: ; preds = %14
%26 = call i32 @fstat(i32 %20, %struct.stat* %3)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %25
%32 = tail call %struct.hostent* @gethostbyname(i8* %18)
%33 = icmp eq %struct.hostent* %32, null
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%36 = tail call i32 @exit(i32 1)
unreachable
37: ; preds = %31
%38 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 12)
%39 = load i32, i32* @AF_INET
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %39, i32* %40
%41 = getelementptr inbounds %struct.hostent, %struct.hostent* %32, i64 0, i32 0
%42 = load i64, i64* %41
%43 = inttoptr i64 %42 to %struct.in_addr*
%44 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %43, i64 0, i32 0
%45 = load i32, i32* %44
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1, i32 0
store i32 %45, i32* %46
%47 = call i32 @htons(i32 8001)
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i32 %47, i32* %48
%49 = load i32, i32* @AF_INET
%50 = load i32, i32* @SOCK_STREAM
%51 = call i32 @socket(i32 %49, i32 %50, i32 0)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %56
53: ; preds = %37
%54 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %37
%57 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%58 = call i32 @connect(i32 %51, %struct.sockaddr* %57, i32 12)
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %63
60: ; preds = %56
%61 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
%62 = call i32 @exit(i32 1)
unreachable
63: ; preds = %56
%64 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%65 = load i32, i32* %64
%66 = call i32 @sendfile(i32 %51, i32 %20, i32* %4, i32 %65)
%67 = icmp eq i32 %66, -1
br i1 %67, label %68, label %71
68: ; preds = %63
%69 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
%70 = call i32 @exit(i32 1)
unreachable
71: ; preds = %63
%72 = icmp eq i32 %66, %65
br i1 %72, label %77, label %73
73: ; preds = %71
%74 = load i32, i32* @stderr
%75 = call i32 @fputs(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.7, i64 0, i64 0), i32 %74)
%76 = call i32 @exit(i32 1)
unreachable
77: ; preds = %71
%78 = call i32 @close(i32 %20)
%79 = call i32 @close(i32 %51)
ret i32 0
}