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

/* Variables and functions */
scalar_t__ EINTR;
int _read(int, char *, size_t);
scalar_t__ errno;
size_t we_read_fully(int fd, char *buffer, size_t len) {
  size_t done;
  ssize_t nread;
  done = 0;
  do {
    nread = _read(fd, buffer + done, len - done);
    if (nread == -1 && errno == EINTR)
      continue;
    if (nread <= 0)
      break;
    done += nread;
  } while (done != len);
  return done;
}


x86 assembly (clang, -O0):
<we_read_fully>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x20(%rbp),%rdx
call   1030 <_read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    116f <we_read_fully+0x5f>
mov    0x2e89(%rip),%rax
mov    (%rax),%rax
mov    0x2e77(%rip),%rcx
cmp    (%rcx),%rax
jne    116f <we_read_fully+0x5f>
jmp    118a <we_read_fully+0x7a>
cmpl   $0x0,-0x24(%rbp)
jg     117e <we_read_fully+0x6e>
jmp    1198 <we_read_fully+0x88>
movslq -0x24(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    112b <we_read_fully+0x1b>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<we_read_fully>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebp
xor    %ebx,%ebx
mov    0x2eb4(%rip),%r12
mov    0x2ea5(%rip),%r13
jmp    114f <we_read_fully+0x3f>
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r12),%rax
cmp    0x0(%r13),%rax
jne    1176 <we_read_fully+0x66>
cmp    %r14,%rbx
je     1173 <we_read_fully+0x63>
lea    (%r15,%rbx,1),%rsi
mov    %r14,%rdx
sub    %rbx,%rdx
mov    %ebp,%edi
call   1030 <_read@plt>
cmp    $0xffffffff,%eax
je     1140 <we_read_fully+0x30>
test   %eax,%eax
jle    1176 <we_read_fully+0x66>
mov    %eax,%eax
add    %rax,%rbx
cmp    %r14,%rbx
jne    114f <we_read_fully+0x3f>
mov    %r14,%rbx
mov    %rbx,%rax
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 i64 @we_read_fully(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i64 0, i64* %7
br label %9
9: ; preds = %34, %3
%10 = load i32, i32* %4
%11 = load i8*, i8** %5
%12 = load i64, i64* %7
%13 = getelementptr inbounds i8, i8* %11, i64 %12
%14 = load i64, i64* %6
%15 = load i64, i64* %7
%16 = sub i64 %14, %15
%17 = call i32 @_read(i32 %10, i8* %13, i64 %16)
store i32 %17, i32* %8
%18 = load i32, i32* %8
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %25
20: ; preds = %9
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINTR
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %25
24: ; preds = %20
br label %34
25: ; preds = %20, %9
%26 = load i32, i32* %8
%27 = icmp sle i32 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %25
br label %38
29: ; preds = %25
%30 = load i32, i32* %8
%31 = sext i32 %30 to i64
%32 = load i64, i64* %7
%33 = add i64 %32, %31
store i64 %33, i64* %7
br label %34
34: ; preds = %29, %24
%35 = load i64, i64* %7
%36 = load i64, i64* %6
%37 = icmp ne i64 %35, %36
br i1 %37, label %9, label %38
38: ; preds = %34, %28
%39 = load i64, i64* %7
ret i64 %39
}

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