#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;
int /*<<< orphan*/ debug_print(int /*<<< orphan*/ *, int, char *, int, int,
                               ...);
scalar_t__ errno;
int read(int, void *, int);
int read_fd(int fd, void *buf, int bytes) {
  int left = bytes, ret;
  while (left > 0) {
    if (((ret = read(fd, buf, left)) < 0) && (errno == EINTR)) {
      debug_print(NULL, 2,
                  "read_fd: read fd %d failed (ret = %d, errno = %d)\n", fd,
                  ret, errno);
      continue;
    }
    if (ret < 0) {
      debug_print(NULL, 2, "read_fd: read fd %d failed (ret = %d)\n", fd, ret);
      return ret;
    }
    left -= ret;
    buf += ret;
  }
  return 0;
}


x86 assembly (clang, -O0):
<read_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    11f8 <read_fd+0xd8>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11a4 <read_fd+0x84>
mov    0x2e7c(%rip),%rax
mov    (%rax),%rax
mov    0x2e6a(%rip),%rcx
cmp    (%rcx),%rax
jne    11a4 <read_fd+0x84>
mov    -0x8(%rbp),%ecx
mov    -0x1c(%rbp),%r8d
mov    0x2e5b(%rip),%rax
mov    (%rax),%r9
xor    %eax,%eax
mov    %eax,%edi
mov    $0x2,%esi
lea    0xe68(%rip),%rdx
mov    $0x0,%al
call   1040 <debug_print@plt>
jmp    1138 <read_fd+0x18>
cmpl   $0x0,-0x1c(%rbp)
jge    11d7 <read_fd+0xb7>
mov    -0x8(%rbp),%ecx
mov    -0x1c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x2,%esi
lea    0xe6e(%rip),%rdx
mov    $0x0,%al
call   1040 <debug_print@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ff <read_fd+0xdf>
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1138 <read_fd+0x18>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_fd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
jle    11a9 <read_fd+0x89>
mov    %edx,%ebp
mov    %rsi,%r14
mov    %edi,%r12d
mov    0x2e9a(%rip),%r13
lea    0xebb(%rip),%r15
jmp    1150 <read_fd+0x30>
mov    %ebx,%eax
add    %rax,%r14
sub    %ebx,%ebp
jle    11a9 <read_fd+0x89>
mov    %r12d,%edi
mov    %r14,%rsi
mov    %ebp,%edx
call   1030 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1147 <read_fd+0x27>
mov    0x2e76(%rip),%rax
mov    (%rax),%r9
cmp    0x0(%r13),%r9
jne    118c <read_fd+0x6c>
xor    %edi,%edi
mov    $0x2,%esi
mov    %r15,%rdx
mov    %r12d,%ecx
mov    %ebx,%r8d
xor    %eax,%eax
call   1040 <debug_print@plt>
jmp    1150 <read_fd+0x30>
lea    0xea0(%rip),%rdx
xor    %edi,%edi
mov    $0x2,%esi
mov    %r12d,%ecx
mov    %ebx,%r8d
xor    %eax,%eax
call   1040 <debug_print@plt>
jmp    11ab <read_fd+0x8b>
xor    %ebx,%ebx
mov    %ebx,%eax
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
@.str = [51 x i8] c"read_fd: read fd %d failed (ret = %d, errno = %d)\0A\00"
@.str.1 = [39 x i8] c"read_fd: read fd %d failed (ret = %d)\0A\00"
define i32 @read_fd(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i32, i32* %7
store i32 %10, i32* %8
br label %11
11: ; preds = %37, %24, %3
%12 = load i32, i32* %8
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %45
14: ; preds = %11
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = load i32, i32* %8
%18 = call i32 @read(i32 %15, i8* %16, i32 %17)
store i32 %18, i32* %9
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %29
20: ; preds = %14
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINTR
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %29
24: ; preds = %20
%25 = load i32, i32* %5
%26 = load i32, i32* %9
%27 = load i64, i64* @errno
%28 = call i32 (i32*, i32, i8*, i32, i32, ...) @debug_print(i32* null, i32 2, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i32 %25, i32 %26, i64 %27)
br label %11
29: ; preds = %20, %14
%30 = load i32, i32* %9
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %29
%33 = load i32, i32* %5
%34 = load i32, i32* %9
%35 = call i32 (i32*, i32, i8*, i32, i32, ...) @debug_print(i32* null, i32 2, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %33, i32 %34)
%36 = load i32, i32* %9
store i32 %36, i32* %4
br label %46
37: ; preds = %29
%38 = load i32, i32* %9
%39 = load i32, i32* %8
%40 = sub nsw i32 %39, %38
store i32 %40, i32* %8
%41 = load i32, i32* %9
%42 = load i8*, i8** %6
%43 = sext i32 %41 to i64
%44 = getelementptr i8, i8* %42, i64 %43
store i8* %44, i8** %6
br label %11
45: ; preds = %11
store i32 0, i32* %4
br label %46
46: ; preds = %45, %32
%47 = load i32, i32* %4
ret i32 %47
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@.str = [51 x i8] c"read_fd: read fd %d failed (ret = %d, errno = %d)\0A\00"
@.str.1 = [39 x i8] c"read_fd: read fd %d failed (ret = %d)\0A\00"
define i32 @read_fd(i32 %0, i8* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %27
5: ; preds = %3, %21
%6 = phi i32 [ %23, %21 ], [ %2, %3 ]
%7 = phi i8* [ %25, %21 ], [ %1, %3 ]
%8 = tail call i32 @read(i32 %0, i8* %7, i32 %6)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %21
10: ; preds = %5, %15
%11 = phi i32 [ %17, %15 ], [ %8, %5 ]
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINTR
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %19
15: ; preds = %10
%16 = tail call i32 (i32*, i32, i8*, i32, i32, ...) @debug_print(i32* null, i32 2, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %11, i64 %12)
%17 = tail call i32 @read(i32 %0, i8* %7, i32 %6)
%18 = icmp slt i32 %17, 0
br i1 %18, label %10, label %21
19: ; preds = %10
%20 = tail call i32 (i32*, i32, i8*, i32, i32, ...) @debug_print(i32* null, i32 2, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %0, i32 %11)
br label %27
21: ; preds = %15, %5
%22 = phi i32 [ %8, %5 ], [ %17, %15 ]
%23 = sub nsw i32 %6, %22
%24 = zext i32 %22 to i64
%25 = getelementptr i8, i8* %7, i64 %24
%26 = icmp sgt i32 %23, 0
br i1 %26, label %5, label %27
27: ; preds = %21, %3, %19
%28 = phi i32 [ %11, %19 ], [ 0, %3 ], [ 0, %21 ]
ret i32 %28
}