#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 */
typedef int /*<<< orphan*/ off_t;
typedef int /*<<< orphan*/ buff;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int S_IREAD;
int S_IRWXO;
int S_IWRITE;
int /*<<< orphan*/ close(int);
int creat(char *, int);
int /*<<< orphan*/ fsync(int);
int /*<<< orphan*/ memset(char *, int, int);
int open(char *, int /*<<< orphan*/);
int sendfile(int, int, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ unlink(char *);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int fd, read_fd;
  int write_fd;
  off_t offset = 0;
  char buff[22];
  int ret;
  memset(buff, 5, sizeof(buff));
  fd = creat("foobar", S_IREAD | S_IWRITE);
  write(fd, buff, sizeof(buff));
  fsync(fd);
  close(fd);
  read_fd = open("foobar", O_RDONLY);
  if (read_fd < 0)
    return 1;
  write_fd = creat("foobar2", S_IREAD | S_IWRITE | S_IRWXO);
  ret = sendfile(write_fd, read_fd, &offset, sizeof(buff));
  sendfile(-1, read_fd, &offset, sizeof(buff));
  sendfile(write_fd, -1, &offset, sizeof(buff));
  sendfile(write_fd, read_fd, (off_t *)-1, sizeof(buff));
  sendfile(write_fd, read_fd, &offset, -1);
  close(read_fd);
  close(write_fd);
  unlink("foobar");
  unlink("foobar2");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x14(%rbp)
lea    -0x30(%rbp),%rdi
mov    $0x5,%esi
mov    $0x16,%edx
call   1050 <memset@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
mov    0x2e1f(%rip),%rax
or     (%rax),%esi
lea    0xe3e(%rip),%rdi
call   10a0 <creat@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x16,%edx
call   1040 <write@plt>
mov    -0x8(%rbp),%edi
call   1080 <fsync@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
lea    0xe05(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1219 <main+0x99>
movl   $0x1,-0x4(%rbp)
jmp    12e0 <main+0x160>
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
mov    0x2daf(%rip),%rax
or     (%rax),%esi
mov    0x2dbe(%rip),%rax
or     (%rax),%esi
lea    0xdcc(%rip),%rdi
call   10a0 <creat@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
mov    -0xc(%rbp),%esi
lea    -0x14(%rbp),%rdx
mov    $0x16,%ecx
call   1070 <sendfile@plt>
mov    %eax,-0x34(%rbp)
mov    -0xc(%rbp),%esi
mov    $0xffffffff,%edi
lea    -0x14(%rbp),%rdx
mov    $0x16,%ecx
call   1070 <sendfile@plt>
mov    -0x10(%rbp),%edi
mov    $0xffffffff,%esi
lea    -0x14(%rbp),%rdx
mov    $0x16,%ecx
call   1070 <sendfile@plt>
mov    -0x10(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    $0xffffffffffffffff,%rdx
mov    $0x16,%ecx
call   1070 <sendfile@plt>
mov    -0x10(%rbp),%edi
mov    -0xc(%rbp),%esi
lea    -0x14(%rbp),%rdx
mov    $0xffffffff,%ecx
call   1070 <sendfile@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
lea    0xd38(%rip),%rdi
call   1030 <unlink@plt>
lea    0xd33(%rip),%rdi
call   1030 <unlink@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
movl   $0x0,0xc(%rsp)
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x5,%esi
mov    $0x16,%edx
call   1050 <memset@plt>
mov    0x2e16(%rip),%r15
mov    0x2e1f(%rip),%r12
mov    (%r12),%esi
or     (%r15),%esi
lea    0xe39(%rip),%r14
mov    %r14,%rdi
call   10a0 <creat@plt>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x16,%edx
call   1040 <write@plt>
mov    %ebp,%edi
call   1080 <fsync@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1090 <open@plt>
test   %eax,%eax
js     12b4 <main+0x134>
mov    %eax,%ebp
mov    (%r12),%esi
or     (%r15),%esi
mov    0x2dd9(%rip),%rax
or     (%rax),%esi
lea    0xde7(%rip),%r14
mov    %r14,%rdi
call   10a0 <creat@plt>
mov    %eax,%ebx
lea    0xc(%rsp),%r15
mov    %eax,%edi
mov    %ebp,%esi
mov    %r15,%rdx
mov    $0x16,%ecx
call   1070 <sendfile@plt>
mov    $0xffffffff,%edi
mov    %ebp,%esi
mov    %r15,%rdx
mov    $0x16,%ecx
call   1070 <sendfile@plt>
mov    %ebx,%edi
mov    $0xffffffff,%esi
mov    %r15,%rdx
mov    $0x16,%ecx
call   1070 <sendfile@plt>
mov    %ebx,%edi
mov    %ebp,%esi
mov    $0xffffffffffffffff,%rdx
mov    $0x16,%ecx
call   1070 <sendfile@plt>
mov    %ebx,%edi
mov    %ebp,%esi
mov    %r15,%rdx
mov    $0xffffffff,%ecx
call   1070 <sendfile@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %ebx,%edi
call   1060 <close@plt>
lea    0xd5d(%rip),%rdi
call   1030 <unlink@plt>
mov    %r14,%rdi
call   1030 <unlink@plt>
xor    %eax,%eax
jmp    12b9 <main+0x139>
mov    $0x1,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"foobar\00"
@S_IREAD = global i32 0
@S_IWRITE = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [8 x i8] c"foobar2\00"
@S_IRWXO = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [22 x i8]
%7 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %5
%8 = getelementptr inbounds [22 x i8], [22 x i8]* %6, i64 0, i64 0
%9 = call i32 @memset(i8* %8, i32 5, i32 22)
%10 = load i32, i32* @S_IREAD
%11 = load i32, i32* @S_IWRITE
%12 = or i32 %10, %11
%13 = call i32 @creat(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 %13, i32* %2
%14 = load i32, i32* %2
%15 = getelementptr inbounds [22 x i8], [22 x i8]* %6, i64 0, i64 0
%16 = call i32 @write(i32 %14, i8* %15, i32 22)
%17 = load i32, i32* %2
%18 = call i32 @fsync(i32 %17)
%19 = load i32, i32* %2
%20 = call i32 @close(i32 %19)
%21 = load i32, i32* @O_RDONLY
%22 = call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %21)
store i32 %22, i32* %3
%23 = load i32, i32* %3
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %0
store i32 1, i32* %1
br label %52
26: ; preds = %0
%27 = load i32, i32* @S_IREAD
%28 = load i32, i32* @S_IWRITE
%29 = or i32 %27, %28
%30 = load i32, i32* @S_IRWXO
%31 = or i32 %29, %30
%32 = call i32 @creat(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %31)
store i32 %32, i32* %4
%33 = load i32, i32* %4
%34 = load i32, i32* %3
%35 = call i32 @sendfile(i32 %33, i32 %34, i32* %5, i32 22)
store i32 %35, i32* %7
%36 = load i32, i32* %3
%37 = call i32 @sendfile(i32 -1, i32 %36, i32* %5, i32 22)
%38 = load i32, i32* %4
%39 = call i32 @sendfile(i32 %38, i32 -1, i32* %5, i32 22)
%40 = load i32, i32* %4
%41 = load i32, i32* %3
%42 = call i32 @sendfile(i32 %40, i32 %41, i32* inttoptr (i64 -1 to i32*), i32 22)
%43 = load i32, i32* %4
%44 = load i32, i32* %3
%45 = call i32 @sendfile(i32 %43, i32 %44, i32* %5, i32 -1)
%46 = load i32, i32* %3
%47 = call i32 @close(i32 %46)
%48 = load i32, i32* %4
%49 = call i32 @close(i32 %48)
%50 = call i32 @unlink(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%51 = call i32 @unlink(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %1
br label %52
52: ; preds = %26, %25
%53 = load i32, i32* %1
ret i32 %53
}

LLVM IR (-O2):
@.str = [7 x i8] c"foobar\00"
@S_IREAD = global i32 0
@S_IWRITE = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [8 x i8] c"foobar2\00"
@S_IRWXO = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca [22 x i8]
%3 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%4 = getelementptr inbounds [22 x i8], [22 x i8]* %2, i64 0, i64 0
%5 = call i32 @memset(i8* %4, i32 5, i32 22)
%6 = load i32, i32* @S_IREAD
%7 = load i32, i32* @S_IWRITE
%8 = or i32 %7, %6
%9 = call i32 @creat(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = call i32 @write(i32 %9, i8* %4, i32 22)
%11 = call i32 @fsync(i32 %9)
%12 = call i32 @close(i32 %9)
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %32, label %16
16: ; preds = %0
%17 = load i32, i32* @S_IREAD
%18 = load i32, i32* @S_IWRITE
%19 = or i32 %18, %17
%20 = load i32, i32* @S_IRWXO
%21 = or i32 %19, %20
%22 = call i32 @creat(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = call i32 @sendfile(i32 %22, i32 %14, i32* %1, i32 22)
%24 = call i32 @sendfile(i32 -1, i32 %14, i32* %1, i32 22)
%25 = call i32 @sendfile(i32 %22, i32 -1, i32* %1, i32 22)
%26 = call i32 @sendfile(i32 %22, i32 %14, i32* inttoptr (i64 -1 to i32*), i32 22)
%27 = call i32 @sendfile(i32 %22, i32 %14, i32* %1, i32 -1)
%28 = call i32 @close(i32 %14)
%29 = call i32 @close(i32 %22)
%30 = call i32 @unlink(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%31 = call i32 @unlink(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %32
32: ; preds = %0, %16
%33 = phi i32 [ 0, %16 ], [ 1, %0 ]
ret i32 %33
}