#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 */
size_t read(int, char *, size_t);
size_t _read_bytes(int fd, char *buf, const size_t size) {
  size_t bytes_remaining, bytes_read;
  char *ptr;
  bytes_remaining = size;
  ptr = buf;
  while (bytes_remaining > 0) {
    bytes_read = read(fd, ptr, bytes_remaining);
    if (bytes_read <= 0)
      return 0;
    bytes_remaining -= bytes_read;
    ptr += bytes_read;
  }
  return size;
}


x86 assembly (clang, -O0):
<_read_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x28(%rbp)
jbe    118a <_read_bytes+0x7a>
mov    -0xc(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <read@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
ja     116a <_read_bytes+0x5a>
movq   $0x0,-0x8(%rbp)
jmp    1192 <_read_bytes+0x82>
mov    -0x30(%rbp),%rcx
mov    -0x28(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    1133 <_read_bytes+0x23>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_read_bytes>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdx,%rdx
je     114d <_read_bytes+0x3d>
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%r15d
mov    %rdx,%rbp
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <read@plt>
test   %rax,%rax
je     114d <_read_bytes+0x3d>
add    %rax,%rbx
sub    %rax,%rbp
jne    1130 <_read_bytes+0x20>
jmp    1150 <_read_bytes+0x40>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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