#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;
};
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*/ fstat(int, struct stat *);
int /*<<< orphan*/ fsync(int);
int /*<<< orphan*/ memset(char *, int, int);
int open(char *, int /*<<< orphan*/);
int sendfile(int, int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ unlink(char *);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int fd, read_fd;
  int write_fd;
  struct stat stat_buf;
  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;
  fstat(read_fd, &stat_buf);
  write_fd = creat("foobar2", S_IREAD | S_IWRITE | S_IRWXO);
  ret = sendfile(write_fd, read_fd, &offset, stat_buf.st_size);
  close(read_fd);
  close(write_fd);
  unlink("foobar");
  unlink("foobar2");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    -0x40(%rbp),%rdi
mov    $0x5,%esi
mov    $0x16,%edx
call   1050 <memset@plt>
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
mov    0x2e0f(%rip),%rax
or     (%rax),%esi
lea    0xe2e(%rip),%rdi
call   10a0 <creat@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x40(%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    0x2dde(%rip),%rax
mov    (%rax),%esi
lea    0xdf5(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1229 <main+0x99>
movl   $0x1,-0x4(%rbp)
jmp    12a3 <main+0x113>
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   10b0 <fstat@plt>
mov    0x2d8c(%rip),%rax
mov    (%rax),%esi
mov    0x2d93(%rip),%rax
or     (%rax),%esi
mov    0x2da2(%rip),%rax
or     (%rax),%esi
lea    0xdb0(%rip),%rdi
call   10a0 <creat@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%ecx
lea    -0x1c(%rbp),%rdx
call   1070 <sendfile@plt>
mov    %eax,-0x44(%rbp)
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
lea    0xd75(%rip),%rdi
call   1030 <unlink@plt>
lea    0xd70(%rip),%rdi
call   1030 <unlink@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%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    0x2e06(%rip),%r15
mov    0x2e0f(%rip),%r12
mov    (%r12),%esi
or     (%r15),%esi
lea    0xe29(%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    0x2ddb(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1090 <open@plt>
test   %eax,%eax
js     127a <main+0xea>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   10b0 <fstat@plt>
mov    (%r12),%esi
or     (%r15),%esi
mov    0x2dc1(%rip),%rax
or     (%rax),%esi
lea    0xdcf(%rip),%r14
mov    %r14,%rdi
call   10a0 <creat@plt>
mov    %eax,%ebp
mov    0x8(%rsp),%ecx
lea    0xc(%rsp),%rdx
mov    %eax,%edi
mov    %ebx,%esi
call   1070 <sendfile@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %ebp,%edi
call   1060 <close@plt>
lea    0xd97(%rip),%rdi
call   1030 <unlink@plt>
mov    %r14,%rdi
call   1030 <unlink@plt>
xor    %eax,%eax
jmp    127f <main+0xef>
mov    $0x1,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.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 %struct.stat
%6 = alloca i32
%7 = alloca [22 x i8]
%8 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %6
%9 = getelementptr inbounds [22 x i8], [22 x i8]* %7, i64 0, i64 0
%10 = call i32 @memset(i8* %9, i32 5, i32 22)
%11 = load i32, i32* @S_IREAD
%12 = load i32, i32* @S_IWRITE
%13 = or i32 %11, %12
%14 = call i32 @creat(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %13)
store i32 %14, i32* %2
%15 = load i32, i32* %2
%16 = getelementptr inbounds [22 x i8], [22 x i8]* %7, i64 0, i64 0
%17 = call i32 @write(i32 %15, i8* %16, i32 22)
%18 = load i32, i32* %2
%19 = call i32 @fsync(i32 %18)
%20 = load i32, i32* %2
%21 = call i32 @close(i32 %20)
%22 = load i32, i32* @O_RDONLY
%23 = call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %22)
store i32 %23, i32* %3
%24 = load i32, i32* %3
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %0
store i32 1, i32* %1
br label %47
27: ; preds = %0
%28 = load i32, i32* %3
%29 = call i32 @fstat(i32 %28, %struct.stat* %5)
%30 = load i32, i32* @S_IREAD
%31 = load i32, i32* @S_IWRITE
%32 = or i32 %30, %31
%33 = load i32, i32* @S_IRWXO
%34 = or i32 %32, %33
%35 = call i32 @creat(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %34)
store i32 %35, i32* %4
%36 = load i32, i32* %4
%37 = load i32, i32* %3
%38 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%39 = load i32, i32* %38
%40 = call i32 @sendfile(i32 %36, i32 %37, i32* %6, i32 %39)
store i32 %40, i32* %8
%41 = load i32, i32* %3
%42 = call i32 @close(i32 %41)
%43 = load i32, i32* %4
%44 = call i32 @close(i32 %43)
%45 = call i32 @unlink(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%46 = 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 %47
47: ; preds = %27, %26
%48 = load i32, i32* %1
ret i32 %48
}

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