#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 */

/* Variables and functions */
int read_bytes(char *, int);
int read_exact_bytes(void *buffer, int size) {
  int result = 0;
  while (result < size) {
    int t = read_bytes((char *)buffer + result, size - result);
    if (t == -1)
      return result;
    result += t;
  }
  return result;
}


x86 assembly (clang, -O0):
<read_exact_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    116e <read_exact_bytes+0x5e>
mov    -0x10(%rbp),%rdi
movslq -0x18(%rbp),%rax
add    %rax,%rdi
mov    -0x14(%rbp),%esi
sub    -0x18(%rbp),%esi
call   1030 <read_bytes@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1160 <read_exact_bytes+0x50>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1174 <read_exact_bytes+0x64>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1126 <read_exact_bytes+0x16>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_exact_bytes>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
xor    %ecx,%ecx
nopl   0x0(%rax,%rax,1)
mov    %ecx,%ebx
cmp    %ebp,%ecx
jge    113d <read_exact_bytes+0x2d>
movslq %ebx,%rdi
add    %r14,%rdi
mov    %ebp,%esi
sub    %ebx,%esi
call   1030 <read_bytes@plt>
lea    (%rax,%rbx,1),%ecx
cmp    $0xffffffff,%eax
jne    1120 <read_exact_bytes+0x10>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @read_exact_bytes(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %6
br label %8
8: ; preds = %25, %2
%9 = load i32, i32* %6
%10 = load i32, i32* %5
%11 = icmp slt i32 %9, %10
br i1 %11, label %12, label %29
12: ; preds = %8
%13 = load i8*, i8** %4
%14 = load i32, i32* %6
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %13, i64 %15
%17 = load i32, i32* %5
%18 = load i32, i32* %6
%19 = sub nsw i32 %17, %18
%20 = call i32 @read_bytes(i8* %16, i32 %19)
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %12
%24 = load i32, i32* %6
store i32 %24, i32* %3
br label %31
25: ; preds = %12
%26 = load i32, i32* %7
%27 = load i32, i32* %6
%28 = add nsw i32 %27, %26
store i32 %28, i32* %6
br label %8
29: ; preds = %8
%30 = load i32, i32* %6
store i32 %30, i32* %3
br label %31
31: ; preds = %29, %23
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
define i32 @read_exact_bytes(i8* %0, i32 %1) {
br label %3
3: ; preds = %6, %2
%4 = phi i32 [ 0, %2 ], [ %12, %6 ]
%5 = icmp slt i32 %4, %1
br i1 %5, label %6, label %13
6: ; preds = %3
%7 = sext i32 %4 to i64
%8 = getelementptr inbounds i8, i8* %0, i64 %7
%9 = sub nsw i32 %1, %4
%10 = tail call i32 @read_bytes(i8* %8, i32 %9)
%11 = icmp eq i32 %10, -1
%12 = add nsw i32 %10, %4
br i1 %11, label %13, label %3
13: ; preds = %3, %6
ret i32 %4
}