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

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
int /*<<< orphan*/ SEEK_SET;
scalar_t__ errno;
scalar_t__ fstat(int, struct stat *);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ sendfile(int, int, int /*<<< orphan*/ *, size_t);
int pdf2laser_sendfile(int out_fd, int in_fd) {
  lseek(in_fd, 0, SEEK_SET);
  struct stat file_stat;
  if (fstat(in_fd, &file_stat)) {
    perror("Error stating file");
    return -1;
  }
  ssize_t bs = 0;
  size_t bytes_sent = 0;
  size_t count = file_stat.st_size;
  while (bytes_sent < count) {
    if ((bs = sendfile(out_fd, in_fd, NULL, count - bytes_sent)) <= 0) {
      if (errno == EINTR || errno == EAGAIN)
        continue;
      perror("sendfile failed");
      return errno;
    }
    bytes_sent += bs;
  }
  return 0;
}


x86 assembly (clang, -O0):
<pdf2laser_sendfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2e80(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1060 <fstat@plt>
cmp    $0x0,%rax
je     118f <pdf2laser_sendfile+0x4f>
lea    0xe82(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1249 <pdf2laser_sendfile+0x109>
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    1242 <pdf2laser_sendfile+0x102>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x30(%rbp),%rcx
sub    -0x28(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%edx
call   1040 <sendfile@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jg     1231 <pdf2laser_sendfile+0xf1>
mov    0x2dff(%rip),%rax
mov    (%rax),%rax
mov    0x2de5(%rip),%rcx
cmp    (%rcx),%rax
je     120e <pdf2laser_sendfile+0xce>
mov    0x2de5(%rip),%rax
mov    (%rax),%rax
mov    0x2dbb(%rip),%rcx
cmp    (%rcx),%rax
jne    1213 <pdf2laser_sendfile+0xd3>
jmp    11a7 <pdf2laser_sendfile+0x67>
lea    0xdf9(%rip),%rdi
call   1050 <perror@plt>
mov    0x2dba(%rip),%rax
mov    (%rax),%rax
mov    %eax,-0x4(%rbp)
jmp    1249 <pdf2laser_sendfile+0x109>
mov    -0x20(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    11a7 <pdf2laser_sendfile+0x67>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pdf2laser_sendfile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %edi,%ebp
mov    0x2e7e(%rip),%rax
mov    (%rax),%edx
xor    %ebx,%ebx
mov    %esi,%edi
xor    %esi,%esi
call   1030 <lseek@plt>
lea    0x10(%rsp),%rsi
mov    %r14d,%edi
call   1060 <fstat@plt>
test   %rax,%rax
jne    1209 <pdf2laser_sendfile+0xc9>
mov    0x10(%rsp),%rax
test   %rax,%rax
je     121a <pdf2laser_sendfile+0xda>
xor    %ecx,%ecx
mov    0x2e4c(%rip),%r12
mov    0x2e35(%rip),%r15
mov    0x2e1e(%rip),%r13
mov    %rax,%rbx
mov    %rax,(%rsp)
jmp    11c1 <pdf2laser_sendfile+0x81>
nopl   0x0(%rax,%rax,1)
mov    0x8(%rsp),%rcx
add    %rax,%rcx
mov    (%rsp),%rbx
sub    %rcx,%rbx
jbe    1205 <pdf2laser_sendfile+0xc5>
mov    %rcx,0x8(%rsp)
cs nopw 0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r14d,%esi
xor    %edx,%edx
mov    %rbx,%rcx
call   1040 <sendfile@plt>
test   %rax,%rax
jg     11b0 <pdf2laser_sendfile+0x70>
mov    (%r12),%rax
cmp    (%r15),%rax
je     11d0 <pdf2laser_sendfile+0x90>
cmp    0x0(%r13),%rax
je     11d0 <pdf2laser_sendfile+0x90>
lea    0xe19(%rip),%rdi
call   1050 <perror@plt>
mov    (%r12),%ebx
jmp    121a <pdf2laser_sendfile+0xda>
xor    %ebx,%ebx
jmp    121a <pdf2laser_sendfile+0xda>
lea    0xdf0(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@SEEK_SET = global i32 0
@.str = [19 x i8] c"Error stating file\00"
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@.str.1 = [16 x i8] c"sendfile failed\00"
define i32 @pdf2laser_sendfile(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.stat
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i32 %0, i32* %4
store i32 %1, i32* %5
%10 = load i32, i32* %5
%11 = load i32, i32* @SEEK_SET
%12 = call i32 @lseek(i32 %10, i32 0, i32 %11)
%13 = load i32, i32* %5
%14 = call i64 @fstat(i32 %13, %struct.stat* %6)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %51
18: ; preds = %2
store i64 0, i64* %7
store i64 0, i64* %8
%19 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%20 = load i64, i64* %19
store i64 %20, i64* %9
br label %21
21: ; preds = %46, %41, %18
%22 = load i64, i64* %8
%23 = load i64, i64* %9
%24 = icmp ult i64 %22, %23
br i1 %24, label %25, label %50
25: ; preds = %21
%26 = load i32, i32* %4
%27 = load i32, i32* %5
%28 = load i64, i64* %9
%29 = load i64, i64* %8
%30 = sub i64 %28, %29
%31 = call i64 @sendfile(i32 %26, i32 %27, i32* null, i64 %30)
store i64 %31, i64* %7
%32 = icmp sle i64 %31, 0
br i1 %32, label %33, label %46
33: ; preds = %25
%34 = load i64, i64* @errno
%35 = load i64, i64* @EINTR
%36 = icmp eq i64 %34, %35
br i1 %36, label %41, label %37
37: ; preds = %33
%38 = load i64, i64* @errno
%39 = load i64, i64* @EAGAIN
%40 = icmp eq i64 %38, %39
br i1 %40, label %41, label %42
41: ; preds = %37, %33
br label %21
42: ; preds = %37
%43 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%44 = load i64, i64* @errno
%45 = trunc i64 %44 to i32
store i32 %45, i32* %3
br label %51
46: ; preds = %25
%47 = load i64, i64* %7
%48 = load i64, i64* %8
%49 = add i64 %48, %47
store i64 %49, i64* %8
br label %21
50: ; preds = %21
store i32 0, i32* %3
br label %51
51: ; preds = %50, %42, %16
%52 = load i32, i32* %3
ret i32 %52
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@SEEK_SET = global i32 0
@.str = [19 x i8] c"Error stating file\00"
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@.str.1 = [16 x i8] c"sendfile failed\00"
define i32 @pdf2laser_sendfile(i32 %0, i32 %1) {
%3 = alloca %struct.stat
%4 = load i32, i32* @SEEK_SET
%5 = tail call i32 @lseek(i32 %1, i32 0, i32 %4)
%6 = bitcast %struct.stat* %3 to i8*
%7 = call i64 @fstat(i32 %1, %struct.stat* %3)
%8 = icmp eq i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %36
11: ; preds = %2
%12 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%13 = load i64, i64* %12
%14 = icmp eq i64 %13, 0
br i1 %14, label %36, label %15
15: ; preds = %11, %32
%16 = phi i64 [ %35, %32 ], [ %13, %11 ]
%17 = phi i64 [ %33, %32 ], [ 0, %11 ]
br label %18
18: ; preds = %15, %21
%19 = tail call i64 @sendfile(i32 %0, i32 %1, i32* null, i64 %16)
%20 = icmp slt i64 %19, 1
br i1 %20, label %21, label %32
21: ; preds = %18
%22 = load i64, i64* @errno
%23 = load i64, i64* @EINTR
%24 = icmp eq i64 %22, %23
%25 = load i64, i64* @EAGAIN
%26 = icmp eq i64 %22, %25
%27 = select i1 %24, i1 true, i1 %26
br i1 %27, label %18, label %28
28: ; preds = %21
%29 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%30 = load i64, i64* @errno
%31 = trunc i64 %30 to i32
br label %36
32: ; preds = %18
%33 = add i64 %19, %17
%34 = icmp ugt i64 %13, %33
%35 = sub i64 %13, %33
br i1 %34, label %15, label %36
36: ; preds = %32, %11, %28, %9
%37 = phi i32 [ -1, %9 ], [ %31, %28 ], [ 0, %11 ], [ 0, %32 ]
ret i32 %37
}