#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;
long read(int, unsigned char *, size_t);
long retrying_read(int fd, void *buf, size_t len) {
  long amount_read, r;
  amount_read = 0;
  while (amount_read < ((long)len)) {
    r = read(fd, ((unsigned char *)buf) + amount_read, len - amount_read);
    if (r > 0) {
      amount_read += r;
    } else if (!r) {
      break;
    } else if (errno != EINTR) {
      amount_read = -1;
      break;
    }
  }
  return (amount_read);
}


x86 assembly (clang, -O0):
<retrying_read>:
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    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jge    11b7 <retrying_read+0xa7>
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    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jle    1171 <retrying_read+0x61>
mov    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    11b2 <retrying_read+0xa2>
cmpq   $0x0,-0x28(%rbp)
jne    1181 <retrying_read+0x71>
jmp    11b7 <retrying_read+0xa7>
mov    0x2e58(%rip),%rax
mov    (%rax),%rax
mov    0x2e46(%rip),%rcx
cmp    (%rcx),%rax
je     11a8 <retrying_read+0x98>
movq   $0xffffffffffffffff,-0x20(%rbp)
ff
jmp    11b7 <retrying_read+0xa7>
jmp    11ad <retrying_read+0x9d>
jmp    11b2 <retrying_read+0xa2>
jmp    112b <retrying_read+0x1b>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<retrying_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
jle    1173 <retrying_read+0x63>
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebp
xor    %ebx,%ebx
mov    0x2eaf(%rip),%r12
mov    0x2ea0(%rip),%r13
jmp    1148 <retrying_read+0x38>
nopw   0x0(%rax,%rax,1)
add    %rax,%rbx
cmp    %r14,%rbx
jge    1175 <retrying_read+0x65>
lea    (%r15,%rbx,1),%rsi
mov    %r14,%rdx
sub    %rbx,%rdx
mov    %ebp,%edi
call   1030 <read@plt>
test   %rax,%rax
jg     1140 <retrying_read+0x30>
je     1175 <retrying_read+0x65>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     1143 <retrying_read+0x33>
mov    $0xffffffffffffffff,%rbx
jmp    1175 <retrying_read+0x65>
xor    %ebx,%ebx
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 @retrying_read(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
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 = %39, %3
%10 = load i64, i64* %7
%11 = load i64, i64* %6
%12 = icmp slt i64 %10, %11
br i1 %12, label %13, label %40
13: ; preds = %9
%14 = load i32, i32* %4
%15 = load i8*, i8** %5
%16 = load i64, i64* %7
%17 = getelementptr inbounds i8, i8* %15, i64 %16
%18 = load i64, i64* %6
%19 = load i64, i64* %7
%20 = sub i64 %18, %19
%21 = call i64 @read(i32 %14, i8* %17, i64 %20)
store i64 %21, i64* %8
%22 = load i64, i64* %8
%23 = icmp sgt i64 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %13
%25 = load i64, i64* %8
%26 = load i64, i64* %7
%27 = add nsw i64 %26, %25
store i64 %27, i64* %7
br label %39
28: ; preds = %13
%29 = load i64, i64* %8
%30 = icmp ne i64 %29, 0
br i1 %30, label %32, label %31
31: ; preds = %28
br label %40
32: ; preds = %28
%33 = load i64, i64* @errno
%34 = load i64, i64* @EINTR
%35 = icmp ne i64 %33, %34
br i1 %35, label %36, label %37
36: ; preds = %32
store i64 -1, i64* %7
br label %40
37: ; preds = %32
br label %38
38: ; preds = %37
br label %39
39: ; preds = %38, %24
br label %9
40: ; preds = %36, %31, %9
%41 = load i64, i64* %7
ret i64 %41
}

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