#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 {
  int /*<<< orphan*/ st_size;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ exit(int);
scalar_t__ fstat(int, struct stat *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int sendfile(int, int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main() {
  int fd = open("/etc/passwd", O_RDONLY);
  if (fd < 0) {
    perror("open");
    exit(1);
  }
  struct stat buf;
  if (fstat(fd, &buf) != 0) {
    perror("fstat");
    exit(1);
  }
  int ret = sendfile(1, fd, NULL, buf.st_size);
  perror("sendfile");
  printf("return: %d\n", ret);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
lea    0xe81(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11a7 <main+0x47>
lea    0xe74(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1080 <fstat@plt>
cmp    $0x0,%rax
je     11d3 <main+0x73>
lea    0xe4d(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%ecx
mov    $0x1,%edi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <sendfile@plt>
mov    %eax,-0x14(%rbp)
lea    0xe26(%rip),%rdi
call   1060 <perror@plt>
mov    -0x14(%rbp),%esi
lea    0xe20(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
lea    0xe8b(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     11c7 <main+0x67>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1080 <fstat@plt>
test   %rax,%rax
jne    11d0 <main+0x70>
mov    0x8(%rsp),%ecx
mov    $0x1,%edi
mov    %ebx,%esi
xor    %edx,%edx
call   1040 <sendfile@plt>
mov    %eax,%ebx
lea    0xe6b(%rip),%rdi
call   1060 <perror@plt>
lea    0xe68(%rip),%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe3e(%rip),%rdi
jmp    11d7 <main+0x77>
lea    0xe3a(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [12 x i8] c"/etc/passwd\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [6 x i8] c"fstat\00"
@.str.3 = [9 x i8] c"sendfile\00"
@.str.4 = [12 x i8] c"return: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.stat
%4 = alloca i32
store i32 0, i32* %1
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = load i32, i32* %2
%14 = call i64 @fstat(i32 %13, %struct.stat* %3)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %12
%20 = load i32, i32* %2
%21 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%22 = load i32, i32* %21
%23 = call i32 @sendfile(i32 1, i32 %20, i32* null, i32 %22)
store i32 %23, i32* %4
%24 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%25 = load i32, i32* %4
%26 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 %25)
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [12 x i8] c"/etc/passwd\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [6 x i8] c"fstat\00"
@.str.3 = [9 x i8] c"sendfile\00"
@.str.4 = [12 x i8] c"return: %d\0A\00"
define i32 @main() {
%1 = alloca %struct.stat
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = bitcast %struct.stat* %1 to i8*
%10 = call i64 @fstat(i32 %3, %struct.stat* %1)
%11 = icmp eq i64 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %8
%13 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %8
%16 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%17 = load i32, i32* %16
%18 = tail call i32 @sendfile(i32 1, i32 %3, i32* null, i32 %17)
%19 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%20 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 %18)
ret i32 0
}