#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 read(int, void *, size_t);
int xread(int fd, void *buf, size_t len) {
  ssize_t ret;
  while ((ret = read(fd, buf, len)) == -1) {
    if (errno == EINTR) {
      buf += ret;
      continue;
    }
    return -1;
  }
  return ret;
}


x86 assembly (clang, -O0):
<xread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    117b <xread+0x6b>
mov    0x2e9a(%rip),%rax
mov    (%rax),%rax
mov    0x2e88(%rip),%rcx
cmp    (%rcx),%rax
jne    116f <xread+0x5f>
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1123 <xread+0x13>
movl   $0xffffffff,-0x4(%rbp)
jmp    1181 <xread+0x71>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xread>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
call   1030 <read@plt>
cmp    $0xffffffff,%eax
jne    1166 <xread+0x56>
add    $0xffffffffffffffff,%rbx
mov    0x2eab(%rip),%r15
mov    0x2e9c(%rip),%r12
nopl   0x0(%rax)
mov    (%r15),%rax
cmp    (%r12),%rax
jne    1161 <xread+0x51>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <read@plt>
add    $0xffffffffffffffff,%rbx
cmp    $0xffffffff,%eax
je     1140 <xread+0x30>
jmp    1166 <xread+0x56>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
define i32 @xread(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
br label %9
9: ; preds = %19, %3
%10 = load i32, i32* %5
%11 = load i8*, i8** %6
%12 = load i64, i64* %7
%13 = call i32 @read(i32 %10, i8* %11, i64 %12)
store i32 %13, i32* %8
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %25
15: ; preds = %9
%16 = load i64, i64* @errno
%17 = load i64, i64* @EINTR
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %24
19: ; preds = %15
%20 = load i32, i32* %8
%21 = load i8*, i8** %6
%22 = sext i32 %20 to i64
%23 = getelementptr i8, i8* %21, i64 %22
store i8* %23, i8** %6
br label %9
24: ; preds = %15
store i32 -1, i32* %4
br label %27
25: ; preds = %9
%26 = load i32, i32* %8
store i32 %26, i32* %4
br label %27
27: ; preds = %25, %24
%28 = load i32, i32* %4
ret i32 %28
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
define i32 @xread(i32 %0, i8* nocapture %1, i64 %2) {
%4 = tail call i32 @read(i32 %0, i8* %1, i64 %2)
%5 = icmp eq i32 %4, -1
br i1 %5, label %9, label %15
6: ; preds = %9
%7 = tail call i32 @read(i32 %0, i8* %14, i64 %2)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %15
9: ; preds = %3, %6
%10 = phi i8* [ %14, %6 ], [ %1, %3 ]
%11 = load i64, i64* @errno
%12 = load i64, i64* @EINTR
%13 = icmp eq i64 %11, %12
%14 = getelementptr i8, i8* %10, i64 -1
br i1 %13, label %6, label %15
15: ; preds = %9, %6, %3
%16 = phi i32 [ %4, %3 ], [ %7, %6 ], [ -1, %9 ]
ret i32 %16
}