#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;
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
int read(int, void *, size_t);
ssize_t my_read(int fd, void *buffer, size_t length) {
  ssize_t done = length;
  while (done > 0) {
    done = read(fd, buffer, length);
    if (done == -1)
      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    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    11ae <my_read+0x8e>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11a4 <my_read+0x84>
mov    0x2e78(%rip),%rax
mov    (%rax),%rax
mov    0x2e66(%rip),%rcx
cmp    (%rcx),%rax
jne    1187 <my_read+0x67>
mov    -0x18(%rbp),%rax
mov    %eax,-0x1c(%rbp)
jmp    119f <my_read+0x7f>
lea    0xe72(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b4 <my_read+0x94>
jmp    11a9 <my_read+0x89>
jmp    11ae <my_read+0x8e>
jmp    113a <my_read+0x1a>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_read>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
test   %r14d,%r14d
jle    1182 <my_read+0x62>
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2ea4(%rip),%r15
mov    0x2e95(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <read@plt>
cmp    $0xffffffff,%eax
jne    117f <my_read+0x5f>
mov    (%r15),%rax
cmp    (%r12),%rax
je     1150 <my_read+0x30>
lea    0xe8e(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%r14d
jmp    1182 <my_read+0x62>
mov    %eax,%r14d
mov    %r14d,%eax
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 i32 @my_read(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%9 = load i64, i64* %7
%10 = trunc i64 %9 to i32
store i32 %10, i32* %8
br label %11
11: ; preds = %32, %3
%12 = load i32, i32* %8
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %33
14: ; preds = %11
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = load i64, i64* %7
%18 = call i32 @read(i32 %15, i8* %16, i64 %17)
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %31
21: ; preds = %14
%22 = load i64, i64* @errno
%23 = load i64, i64* @EINTR
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = load i64, i64* %7
%27 = trunc i64 %26 to i32
store i32 %27, i32* %8
br label %30
28: ; preds = %21
%29 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %35
30: ; preds = %25
br label %32
31: ; preds = %14
br label %33
32: ; preds = %30
br label %11
33: ; preds = %31, %11
%34 = load i32, i32* %8
store i32 %34, i32* %4
br label %35
35: ; preds = %33, %28
%36 = load i32, i32* %4
ret i32 %36
}

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