#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 ourfd {
  int dummy;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
unsigned long ourread(struct ourfd *, char *, unsigned long);
int eatBytes(struct ourfd *fd, unsigned long amount) {
  char buf[4096];
  unsigned long bite;
  while (amount) {
    bite = (amount > sizeof(buf)) ? sizeof(buf) : amount;
    if (ourread(fd, buf, bite) != bite)
      return (18 | 0x80000000);
    amount -= bite;
  }
  return 0;
}


x86 assembly (clang, -O0):
<eatBytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11ad <eatBytes+0x9d>
cmpq   $0x1000,-0x18(%rbp)
jbe    114d <eatBytes+0x3d>
mov    $0x1000,%eax
mov    %rax,-0x1030(%rbp)
jmp    1158 <eatBytes+0x48>
mov    -0x18(%rbp),%rax
mov    %rax,-0x1030(%rbp)
mov    -0x1030(%rbp),%rax
mov    %rax,-0x1028(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x1020(%rbp),%rsi
mov    -0x1028(%rbp),%rdx
call   1030 <ourread@plt>
cmp    -0x1028(%rbp),%rax
je     1196 <eatBytes+0x86>
movl   $0x80000012,-0x4(%rbp)
jmp    11b4 <eatBytes+0xa4>
mov    -0x1028(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    1123 <eatBytes+0x13>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<eatBytes>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1000,%rsp
xor    %r14d,%r14d
test   %rsi,%rsi
je     1160 <eatBytes+0x50>
mov    %rsi,%rbx
mov    %rdi,%r12
mov    %rsp,%r15
cmp    $0x1000,%rbx
mov    $0x1000,%ebp
cmovb  %rbx,%rbp
mov    %r12,%rdi
mov    %r15,%rsi
mov    %rbp,%rdx
call   1030 <ourread@plt>
cmp    %rbp,%rax
jne    115a <eatBytes+0x4a>
sub    %rbp,%rbx
jne    1130 <eatBytes+0x20>
jmp    1160 <eatBytes+0x50>
mov    $0x80000012,%r14d
mov    %r14d,%eax
add    $0x1000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ourfd = type { i32 }
define i32 @eatBytes(%struct.ourfd* %0, i64 %1) {
%3 = alloca i32
%4 = alloca %struct.ourfd*
%5 = alloca i64
%6 = alloca [4096 x i8]
%7 = alloca i64
store %struct.ourfd* %0, %struct.ourfd** %4
store i64 %1, i64* %5
br label %8
8: ; preds = %26, %2
%9 = load i64, i64* %5
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %30
11: ; preds = %8
%12 = load i64, i64* %5
%13 = icmp ugt i64 %12, 4096
br i1 %13, label %14, label %15
14: ; preds = %11
br label %17
15: ; preds = %11
%16 = load i64, i64* %5
br label %17
17: ; preds = %15, %14
%18 = phi i64 [ 4096, %14 ], [ %16, %15 ]
store i64 %18, i64* %7
%19 = load %struct.ourfd*, %struct.ourfd** %4
%20 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%21 = load i64, i64* %7
%22 = call i64 @ourread(%struct.ourfd* %19, i8* %20, i64 %21)
%23 = load i64, i64* %7
%24 = icmp ne i64 %22, %23
br i1 %24, label %25, label %26
25: ; preds = %17
store i32 -2147483630, i32* %3
br label %31
26: ; preds = %17
%27 = load i64, i64* %7
%28 = load i64, i64* %5
%29 = sub i64 %28, %27
store i64 %29, i64* %5
br label %8
30: ; preds = %8
store i32 0, i32* %3
br label %31
31: ; preds = %30, %25
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
%struct.ourfd = type { i32 }
define i32 @eatBytes(%struct.ourfd* %0, i64 %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = icmp eq i64 %1, 0
br i1 %5, label %15, label %8
6: ; preds = %8
%7 = icmp eq i64 %14, 0
br i1 %7, label %15, label %8
8: ; preds = %2, %6
%9 = phi i64 [ %14, %6 ], [ %1, %2 ]
%10 = icmp ult i64 %9, 4096
%11 = select i1 %10, i64 %9, i64 4096
%12 = call i64 @ourread(%struct.ourfd* %0, i8* %4, i64 %11)
%13 = icmp eq i64 %12, %11
%14 = sub i64 %9, %11
br i1 %13, label %6, label %15
15: ; preds = %8, %6, %2
%16 = phi i32 [ 0, %2 ], [ 0, %6 ], [ -2147483630, %8 ]
ret i32 %16
}