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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
size_t read(int, void *, size_t);
ssize_t my_read(int fd, void *buf, size_t length) {
  ssize_t done = length;
  while (done > 0) {
    done = read(fd, buf, length);
    if (done != length)
      if (errno == EINTR)
        done = length;
      else {
        perror("fail to read");
        return -1;
      }
    else
      break;
  }
  return done;
}


x86 assembly (clang, -O0):
<my_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jbe    11b7 <my_read+0x97>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <read@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     11ad <my_read+0x8d>
mov    0x2e71(%rip),%rax
mov    (%rax),%rax
mov    0x2e5f(%rip),%rcx
cmp    (%rcx),%rax
jne    118f <my_read+0x6f>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    11a8 <my_read+0x88>
lea    0xe6a(%rip),%rdi
call   1040 <perror@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11bf <my_read+0x9f>
jmp    11b2 <my_read+0x92>
jmp    11b7 <my_read+0x97>
jmp    113b <my_read+0x1b>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_read>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %rdx,%rdx
je     1180 <my_read+0x60>
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x2ea4(%rip),%r15
mov    0x2e95(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebp,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <read@plt>
cmp    %rbx,%rax
je     1182 <my_read+0x62>
mov    (%r15),%rax
cmp    (%r12),%rax
je     1150 <my_read+0x30>
lea    0xe8e(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffffffffffff,%rbx
jmp    1182 <my_read+0x62>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@.str = [13 x i8] c"fail to read\00"
define i64 @my_read(i32 %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%9 = load i64, i64* %7
store i64 %9, i64* %8
br label %10
10: ; preds = %31, %3
%11 = load i64, i64* %8
%12 = icmp ugt i64 %11, 0
br i1 %12, label %13, label %32
13: ; preds = %10
%14 = load i32, i32* %5
%15 = load i8*, i8** %6
%16 = load i64, i64* %7
%17 = call i64 @read(i32 %14, i8* %15, i64 %16)
store i64 %17, i64* %8
%18 = load i64, i64* %8
%19 = load i64, i64* %7
%20 = icmp ne i64 %18, %19
br i1 %20, label %21, label %30
21: ; preds = %13
%22 = load i64, i64* @errno
%23 = load i64, i64* @EINTR
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = load i64, i64* %7
store i64 %26, i64* %8
br label %29
27: ; preds = %21
%28 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i64 -1, i64* %4
br label %34
29: ; preds = %25
br label %31
30: ; preds = %13
br label %32
31: ; preds = %29
br label %10
32: ; preds = %30, %10
%33 = load i64, i64* %8
store i64 %33, i64* %4
br label %34
34: ; preds = %32, %27
%35 = load i64, i64* %4
ret i64 %35
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@.str = [13 x i8] c"fail to read\00"
define i64 @my_read(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %14, label %5
5: ; preds = %3, %8
%6 = tail call i64 @read(i32 %0, i8* %1, i64 %2)
%7 = icmp eq i64 %6, %2
br i1 %7, label %14, label %8
8: ; preds = %5
%9 = load i64, i64* @errno
%10 = load i64, i64* @EINTR
%11 = icmp eq i64 %9, %10
br i1 %11, label %5, label %12
12: ; preds = %8
%13 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %5, %3, %12
%15 = phi i64 [ -1, %12 ], [ 0, %3 ], [ %2, %5 ]
ret i64 %15
}