#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*/ uint8_t;
typedef int /*<<< orphan*/ tmp;

/* Variables and functions */
scalar_t__ read_bytes(int /*<<< orphan*/ *, unsigned int);
int consume_bytes(unsigned int n) {
  uint8_t tmp[512];
  unsigned int len;
  while (n > 0) {
    len = sizeof(tmp);
    if (len > n)
      len = n;
    if (read_bytes(tmp, len) == 0)
      return 0;
    n -= len;
  }
  return 1;
}


x86 assembly (clang, -O0):
<consume_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x820,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jbe    1185 <consume_bytes+0x75>
movl   $0x800,-0x814(%rbp)
mov    -0x814(%rbp),%eax
cmp    -0x8(%rbp),%eax
jbe    114a <consume_bytes+0x3a>
mov    -0x8(%rbp),%eax
mov    %eax,-0x814(%rbp)
lea    -0x810(%rbp),%rdi
mov    -0x814(%rbp),%esi
call   1030 <read_bytes@plt>
cmp    $0x0,%rax
jne    1172 <consume_bytes+0x62>
movl   $0x0,-0x4(%rbp)
jmp    118c <consume_bytes+0x7c>
mov    -0x814(%rbp),%ecx
mov    -0x8(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x8(%rbp)
jmp    111e <consume_bytes+0xe>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x820,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<consume_bytes>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x808,%rsp
mov    $0x1,%r14d
test   %edi,%edi
je     1156 <consume_bytes+0x46>
mov    %edi,%ebp
mov    %rsp,%r15
nopl   0x0(%rax)
cmp    $0x800,%ebp
mov    $0x800,%ebx
cmovb  %ebp,%ebx
mov    %r15,%rdi
mov    %ebx,%esi
call   1030 <read_bytes@plt>
test   %rax,%rax
je     1153 <consume_bytes+0x43>
sub    %ebx,%ebp
jne    1130 <consume_bytes+0x20>
jmp    1156 <consume_bytes+0x46>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x808,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @consume_bytes(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [512 x i32]
%5 = alloca i32
store i32 %0, i32* %3
br label %6
6: ; preds = %21, %1
%7 = load i32, i32* %3
%8 = icmp ugt i32 %7, 0
br i1 %8, label %9, label %25
9: ; preds = %6
store i32 2048, i32* %5
%10 = load i32, i32* %5
%11 = load i32, i32* %3
%12 = icmp ugt i32 %10, %11
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = load i32, i32* %3
store i32 %14, i32* %5
br label %15
15: ; preds = %13, %9
%16 = getelementptr inbounds [512 x i32], [512 x i32]* %4, i64 0, i64 0
%17 = load i32, i32* %5
%18 = call i64 @read_bytes(i32* %16, i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %15
store i32 0, i32* %2
br label %26
21: ; preds = %15
%22 = load i32, i32* %5
%23 = load i32, i32* %3
%24 = sub i32 %23, %22
store i32 %24, i32* %3
br label %6
25: ; preds = %6
store i32 1, i32* %2
br label %26
26: ; preds = %25, %20
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
define i32 @consume_bytes(i32 %0) {
%2 = alloca [512 x i32]
%3 = bitcast [512 x i32]* %2 to i8*
%4 = icmp eq i32 %0, 0
br i1 %4, label %16, label %5
5: ; preds = %1
%6 = getelementptr inbounds [512 x i32], [512 x i32]* %2, i64 0, i64 0
br label %9
7: ; preds = %9
%8 = icmp eq i32 %15, 0
br i1 %8, label %16, label %9
9: ; preds = %5, %7
%10 = phi i32 [ %0, %5 ], [ %15, %7 ]
%11 = icmp ult i32 %10, 2048
%12 = select i1 %11, i32 %10, i32 2048
%13 = call i64 @read_bytes(i32* %6, i32 %12)
%14 = icmp eq i64 %13, 0
%15 = sub i32 %10, %12
br i1 %14, label %16, label %7
16: ; preds = %9, %7, %1
%17 = phi i32 [ 1, %1 ], [ 1, %7 ], [ 0, %9 ]
ret i32 %17
}