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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err(int, char *, char const *);
int lstat(char const *, struct stat *);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char const *, scalar_t__);
scalar_t__ sendfile(int, int, int /*<<< orphan*/ *, scalar_t__);
int /*<<< orphan*/ txsize(int, scalar_t__);
void send_file(int sock, const char *path) {
  struct stat info;
  int ret;
  ret = lstat(path, &info);
  if (ret == -1)
    err(1, "lstat %s", path);
  int fd = open(path, O_RDONLY);
  if (ret == -1)
    err(1, "open %s", path);
  if (info.st_size > 0) {
    ssize_t sent;
    txsize(sock, info.st_size);
    sent = sendfile(sock, fd, NULL, info.st_size);
    if (sent < 0)
      err(1, "sendfile %s", path);
  }
  close(fd);
  printf("transmitting %s %zu\n", path, info.st_size);
}


x86 assembly (clang, -O0):
<send_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <lstat@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11ae <send_file+0x3e>
mov    -0x10(%rbp),%rdx
mov    $0x1,%edi
lea    0xe57(%rip),%rsi
call   1060 <err@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11e2 <send_file+0x72>
mov    -0x10(%rbp),%rdx
mov    $0x1,%edi
lea    0xe2c(%rip),%rsi
call   1060 <err@plt>
cmpq   $0x0,-0x18(%rbp)
jle    1235 <send_file+0xc5>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1080 <txsize@plt>
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%esi
mov    -0x18(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%edx
call   1070 <sendfile@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    1230 <send_file+0xc0>
mov    -0x10(%rbp),%rdx
mov    $0x1,%edi
lea    0xde6(%rip),%rsi
call   1060 <err@plt>
jmp    1235 <send_file+0xc5>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xdd1(%rip),%rdi
call   1030 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%r14d
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1050 <lstat@plt>
cmp    $0xffffffff,%eax
je     11a3 <send_file+0x33>
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
mov    %eax,%r15d
jmp    11df <send_file+0x6f>
lea    0xe56(%rip),%rsi
mov    $0x1,%edi
mov    %rbx,%rdx
call   1060 <err@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
mov    %eax,%r15d
lea    0xe37(%rip),%rsi
mov    $0x1,%edi
mov    %rbx,%rdx
call   1060 <err@plt>
mov    (%rsp),%rbp
test   %rbp,%rbp
jle    121c <send_file+0xac>
mov    %r14d,%edi
mov    %rbp,%rsi
call   1080 <txsize@plt>
mov    %r14d,%edi
mov    %r15d,%esi
xor    %edx,%edx
mov    %rbp,%rcx
call   1070 <sendfile@plt>
test   %rax,%rax
jns    121c <send_file+0xac>
lea    0xe02(%rip),%rsi
mov    $0x1,%edi
mov    %rbx,%rdx
call   1060 <err@plt>
mov    %r15d,%edi
call   1040 <close@plt>
lea    0xdf2(%rip),%rdi
mov    %rbx,%rsi
mov    %rbp,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <printf@plt>

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [9 x i8] c"lstat %s\00"
@O_RDONLY = global i32 0
@.str.1 = [8 x i8] c"open %s\00"
@.str.2 = [12 x i8] c"sendfile %s\00"
@.str.3 = [21 x i8] c"transmitting %s %zu\0A\00"
define void @send_file(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.stat
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
store i32 %0, i32* %3
store i8* %1, i8** %4
%9 = load i8*, i8** %4
%10 = call i32 @lstat(i8* %9, %struct.stat* %5)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = call i32 @err(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %14)
br label %16
16: ; preds = %13, %2
%17 = load i8*, i8** %4
%18 = load i32, i32* @O_RDONLY
%19 = call i32 @open(i8* %17, i32 %18)
store i32 %19, i32* %7
%20 = load i32, i32* %6
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %16
%23 = load i8*, i8** %4
%24 = call i32 @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %23)
br label %25
25: ; preds = %22, %16
%26 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%27 = load i64, i64* %26
%28 = icmp sgt i64 %27, 0
br i1 %28, label %29, label %45
29: ; preds = %25
%30 = load i32, i32* %3
%31 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%32 = load i64, i64* %31
%33 = call i32 @txsize(i32 %30, i64 %32)
%34 = load i32, i32* %3
%35 = load i32, i32* %7
%36 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%37 = load i64, i64* %36
%38 = call i64 @sendfile(i32 %34, i32 %35, i32* null, i64 %37)
store i64 %38, i64* %8
%39 = load i64, i64* %8
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %29
%42 = load i8*, i8** %4
%43 = call i32 @err(i32 1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %42)
br label %44
44: ; preds = %41, %29
br label %45
45: ; preds = %44, %25
%46 = load i32, i32* %7
%47 = call i32 @close(i32 %46)
%48 = load i8*, i8** %4
%49 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%50 = load i64, i64* %49
%51 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %48, i64 %50)
ret void
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [9 x i8] c"lstat %s\00"
@O_RDONLY = global i32 0
@.str.1 = [8 x i8] c"open %s\00"
@.str.2 = [12 x i8] c"sendfile %s\00"
@.str.3 = [21 x i8] c"transmitting %s %zu\0A\00"
define void @send_file(i32 %0, i8* %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i32 @lstat(i8* %1, %struct.stat* %3)
%6 = icmp eq i32 %5, -1
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = load i32, i32* @O_RDONLY
%9 = tail call i32 @open(i8* %1, i32 %8)
br label %15
10: ; preds = %2
%11 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %1)
%12 = load i32, i32* @O_RDONLY
%13 = tail call i32 @open(i8* %1, i32 %12)
%14 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %15
15: ; preds = %7, %10
%16 = phi i32 [ %13, %10 ], [ %9, %7 ]
%17 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%18 = load i64, i64* %17
%19 = icmp sgt i64 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %15
%21 = tail call i32 @txsize(i32 %0, i64 %18)
%22 = tail call i64 @sendfile(i32 %0, i32 %16, i32* null, i64 %18)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %1)
br label %26
26: ; preds = %20, %24, %15
%27 = tail call i32 @close(i32 %16)
%28 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %1, i64 %18)
ret void
}