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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int read(int, int /*<<< orphan*/ *, size_t);
bool read_bytes(int fd, uint8_t *buf, size_t bytes_needed) {
  while (bytes_needed > 0) {
    ssize_t bytes_read;
    bytes_read = read(fd, buf, bytes_needed);
    if (bytes_read == -1 && errno == EINTR)
      continue;
    else if (bytes_read <= 0)
      return false;
    bytes_needed -= bytes_read;
    buf += bytes_read;
  }
  return true;
}


x86 assembly (clang, -O0):
<read_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    11ae <read_bytes+0x9e>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    116a <read_bytes+0x5a>
mov    0x2e8e(%rip),%rax
mov    (%rax),%rax
mov    0x2e7c(%rip),%rcx
cmp    (%rcx),%rax
jne    116a <read_bytes+0x5a>
jmp    1123 <read_bytes+0x13>
cmpl   $0x0,-0x1c(%rbp)
jg     1180 <read_bytes+0x70>
movl   $0x0,-0x4(%rbp)
jmp    11b5 <read_bytes+0xa5>
jmp    1185 <read_bytes+0x75>
movslq -0x1c(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1123 <read_bytes+0x13>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_bytes>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x1,%r14d
test   %rdx,%rdx
je     117a <read_bytes+0x6a>
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %edi,%r15d
mov    0x2eaa(%rip),%r12
mov    0x2e9b(%rip),%r13
jmp    114f <read_bytes+0x3f>
nop
mov    (%r12),%rax
cmp    0x0(%r13),%rax
jne    1177 <read_bytes+0x67>
test   %rbx,%rbx
je     117a <read_bytes+0x6a>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1030 <read@plt>
cmp    $0xffffffff,%eax
je     1140 <read_bytes+0x30>
test   %eax,%eax
jle    1177 <read_bytes+0x67>
mov    %eax,%eax
sub    %rax,%rbx
lea    0x0(%rbp,%rax,4),%rbp
test   %rbx,%rbx
jne    114f <read_bytes+0x3f>
jmp    117a <read_bytes+0x6a>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
define i32 @read_bytes(i32 %0, i32* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %24, label %5
5: ; preds = %3, %20
%6 = phi i64 [ %22, %20 ], [ %2, %3 ]
%7 = phi i32* [ %21, %20 ], [ %1, %3 ]
%8 = tail call i32 @read(i32 %0, i32* %7, i64 %6)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %14
10: ; preds = %5
%11 = load i64, i64* @errno
%12 = load i64, i64* @EINTR
%13 = icmp eq i64 %11, %12
br i1 %13, label %20, label %24
14: ; preds = %5
%15 = icmp slt i32 %8, 1
br i1 %15, label %24, label %16
16: ; preds = %14
%17 = zext i32 %8 to i64
%18 = sub i64 %6, %17
%19 = getelementptr inbounds i32, i32* %7, i64 %17
br label %20
20: ; preds = %10, %16
%21 = phi i32* [ %19, %16 ], [ %7, %10 ]
%22 = phi i64 [ %18, %16 ], [ %6, %10 ]
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %5
24: ; preds = %20, %14, %10, %3
%25 = phi i32 [ 1, %3 ], [ 0, %10 ], [ 0, %14 ], [ 1, %20 ]
ret i32 %25
}