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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ eembed_system_read(int, unsigned char *, size_t);
scalar_t__ errno;
ssize_t eembed_retrying_read(int fd, void *buf, size_t len) {
  size_t amount_read = 0;
  unsigned char *cbuf = NULL;
  ssize_t r = 0;
  while (amount_read < len) {
    cbuf = ((unsigned char *)buf) + amount_read;
    r = eembed_system_read(fd, cbuf, len - amount_read);
    if (r > 0) {
      amount_read += r;
    } else {
      if (!r) {
        break;
      } else if (errno != EINTR) {
        return -1;
      }
    }
  }
  return amount_read;
}


x86 assembly (clang, -O0):
<eembed_retrying_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    11cf <eembed_retrying_read+0xbf>
mov    -0x18(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    -0x28(%rbp),%rdx
call   1030 <eembed_system_read@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jle    1189 <eembed_retrying_read+0x79>
mov    -0x38(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    11ca <eembed_retrying_read+0xba>
cmpq   $0x0,-0x38(%rbp)
jne    1199 <eembed_retrying_read+0x89>
jmp    11cf <eembed_retrying_read+0xbf>
mov    0x2e40(%rip),%rax
mov    (%rax),%rax
mov    0x2e2e(%rip),%rcx
cmp    (%rcx),%rax
je     11c0 <eembed_retrying_read+0xb0>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11d7 <eembed_retrying_read+0xc7>
jmp    11c5 <eembed_retrying_read+0xb5>
jmp    11ca <eembed_retrying_read+0xba>
jmp    113b <eembed_retrying_read+0x2b>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

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

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
define i64 @eembed_retrying_read(i32 %0, i8* %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %22, label %5
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 @eembed_system_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 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 ult i64 %20, %2
br i1 %21, label %5, label %22
22: ; preds = %15, %13, %19, %3
%23 = phi i64 [ 0, %3 ], [ %20, %19 ], [ %6, %13 ], [ -1, %15 ]
ret i64 %23
}