#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 scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ perror(char *);
scalar_t__ sendfile(int, int, int /*<<< orphan*/ *, unsigned int);
int do_sendfile(int infd, int outfd, unsigned int count) {
  while (count > 0) {
    ssize_t r;
    r = sendfile(outfd, infd, NULL, count);
    if (r < 0) {
      perror("sendfile");
      return 3;
    }
    count -= r;
  }
  return 0;
}


x86 assembly (clang, -O0):
<do_sendfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jbe    1186 <do_sendfile+0x66>
mov    -0xc(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%ecx
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sendfile@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1174 <do_sendfile+0x54>
lea    0xe9d(%rip),%rdi
call   1040 <perror@plt>
movl   $0x3,-0x4(%rbp)
jmp    118d <do_sendfile+0x6d>
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%eax
sub    %rcx,%rax
mov    %eax,-0x10(%rbp)
jmp    1131 <do_sendfile+0x11>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_sendfile>:
push   %rbp
push   %r14
push   %rbx
test   %edx,%edx
je     1147 <do_sendfile+0x27>
mov    %edx,%ebx
mov    %esi,%r14d
mov    %edi,%ebp
nop
mov    %r14d,%edi
mov    %ebp,%esi
xor    %edx,%edx
mov    %ebx,%ecx
call   1030 <sendfile@plt>
test   %rax,%rax
js     114e <do_sendfile+0x2e>
sub    %eax,%ebx
jne    1130 <do_sendfile+0x10>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xeab(%rip),%rdi
call   1040 <perror@plt>
mov    $0x3,%eax
jmp    1149 <do_sendfile+0x29>

LLVM IR (-O0):
@.str = [9 x i8] c"sendfile\00"
define i32 @do_sendfile(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
br label %9
9: ; preds = %21, %3
%10 = load i32, i32* %7
%11 = icmp ugt i32 %10, 0
br i1 %11, label %12, label %27
12: ; preds = %9
%13 = load i32, i32* %6
%14 = load i32, i32* %5
%15 = load i32, i32* %7
%16 = call i64 @sendfile(i32 %13, i32 %14, i32* null, i32 %15)
store i64 %16, i64* %8
%17 = load i64, i64* %8
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %12
%20 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i32 3, i32* %4
br label %28
21: ; preds = %12
%22 = load i64, i64* %8
%23 = load i32, i32* %7
%24 = zext i32 %23 to i64
%25 = sub nsw i64 %24, %22
%26 = trunc i64 %25 to i32
store i32 %26, i32* %7
br label %9
27: ; preds = %9
store i32 0, i32* %4
br label %28
28: ; preds = %27, %19
%29 = load i32, i32* %4
ret i32 %29
}

LLVM IR (-O2):
@.str = [9 x i8] c"sendfile\00"
define i32 @do_sendfile(i32 %0, i32 %1, i32 %2) {
%4 = icmp eq i32 %2, 0
br i1 %4, label %15, label %7
5: ; preds = %7
%6 = icmp eq i32 %12, 0
br i1 %6, label %15, label %7
7: ; preds = %3, %5
%8 = phi i32 [ %12, %5 ], [ %2, %3 ]
%9 = tail call i64 @sendfile(i32 %1, i32 %0, i32* null, i32 %8)
%10 = icmp slt i64 %9, 0
%11 = trunc i64 %9 to i32
%12 = sub i32 %8, %11
br i1 %10, label %13, label %5
13: ; preds = %7
%14 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %5, %3, %13
%16 = phi i32 [ 3, %13 ], [ 0, %3 ], [ 0, %5 ]
ret i32 %16
}