#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 */
scalar_t__ EINTR;
scalar_t__ errno;
int read(int, char *, size_t);
int readBuffer(int fd, void *buffer, size_t len) {
  size_t nleft;
  int nread;
  char *ptr;
  ptr = buffer;
  nleft = len;
  while (nleft > 0) {
    if ((nread = read(fd, ptr, nleft)) <= 0) {
      if (nread < 0 && errno == EINTR)
        nread = 0;
      else
        return -1;
    }
    nleft -= nread;
    ptr += nread;
  }
  return len;
}


x86 assembly (clang, -O0):
<readBuffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jbe    11c0 <readBuffer+0xb0>
mov    -0x8(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <read@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jg     119b <readBuffer+0x8b>
cmpl   $0x0,-0x24(%rbp)
jge    118a <readBuffer+0x7a>
mov    0x2e75(%rip),%rax
mov    (%rax),%rax
mov    0x2e63(%rip),%rcx
cmp    (%rcx),%rax
jne    118a <readBuffer+0x7a>
movl   $0x0,-0x24(%rbp)
jmp    1196 <readBuffer+0x86>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c7 <readBuffer+0xb7>
jmp    119b <readBuffer+0x8b>
movslq -0x24(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x24(%rbp),%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
jmp    1133 <readBuffer+0x23>
mov    -0x18(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readBuffer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
test   %rdx,%rdx
je     1173 <readBuffer+0x63>
mov    %rsi,%rbx
mov    %edi,%r15d
mov    0x2eb0(%rip),%r12
mov    0x2ea1(%rip),%r13
mov    %r14,%rbp
jmp    114a <readBuffer+0x3a>
nopl   0x0(%rax)
mov    %ecx,%eax
add    %rax,%rbx
sub    %rax,%rbp
je     1173 <readBuffer+0x63>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <read@plt>
mov    %eax,%ecx
test   %eax,%eax
jg     1140 <readBuffer+0x30>
mov    $0xffffffff,%eax
jns    1176 <readBuffer+0x66>
mov    (%r12),%rdx
xor    %ecx,%ecx
cmp    0x0(%r13),%rdx
je     1140 <readBuffer+0x30>
jmp    1176 <readBuffer+0x66>
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 @readBuffer(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
%10 = alloca i8*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i8*, i8** %6
store i8* %11, i8** %10
%12 = load i64, i64* %7
store i64 %12, i64* %8
br label %13
13: ; preds = %32, %3
%14 = load i64, i64* %8
%15 = icmp ugt i64 %14, 0
br i1 %15, label %16, label %41
16: ; preds = %13
%17 = load i32, i32* %5
%18 = load i8*, i8** %10
%19 = load i64, i64* %8
%20 = call i32 @read(i32 %17, i8* %18, i64 %19)
store i32 %20, i32* %9
%21 = icmp sle i32 %20, 0
br i1 %21, label %22, label %32
22: ; preds = %16
%23 = load i32, i32* %9
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %22
%26 = load i64, i64* @errno
%27 = load i64, i64* @EINTR
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %30
29: ; preds = %25
store i32 0, i32* %9
br label %31
30: ; preds = %25, %22
store i32 -1, i32* %4
br label %44
31: ; preds = %29
br label %32
32: ; preds = %31, %16
%33 = load i32, i32* %9
%34 = sext i32 %33 to i64
%35 = load i64, i64* %8
%36 = sub i64 %35, %34
store i64 %36, i64* %8
%37 = load i32, i32* %9
%38 = load i8*, i8** %10
%39 = sext i32 %37 to i64
%40 = getelementptr inbounds i8, i8* %38, i64 %39
store i8* %40, i8** %10
br label %13
41: ; preds = %13
%42 = load i64, i64* %7
%43 = trunc i64 %42 to i32
store i32 %43, i32* %4
br label %44
44: ; preds = %41, %30
%45 = load i32, i32* %4
ret i32 %45
}

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