#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 */
int EINTR;
int errno;
int read(int, void *, size_t);
ssize_t zread(int fd, void *buf, size_t count) {
  ssize_t ret;
  int ec;
  for (;;) {
    if ((ret = read(fd, buf, count)) < 0) {
      ec = errno;
      if (ec == EINTR) {
        continue;
      }
      return -1;
    }
    return ret;
  }
  return -1;
}


x86 assembly (clang, -O0):
<zread>:
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    $0x0,%eax
jge    116e <zread+0x5e>
mov    0x2e9a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    0x2e83(%rip),%rcx
cmp    (%rcx),%eax
jne    1162 <zread+0x52>
jmp    1123 <zread+0x13>
movl   $0xffffffff,-0x4(%rbp)
jmp    1174 <zread+0x64>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<zread>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2eb9(%rip),%r15
mov    0x2eaa(%rip),%r12
xchg   %ax,%ax
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <read@plt>
test   %eax,%eax
jns    114f <zread+0x3f>
mov    (%r15),%eax
cmp    (%r12),%eax
je     1130 <zread+0x20>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@EINTR = global i32 0
define i32 @zread(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
br label %10
10: ; preds = %21, %3
%11 = load i32, i32* %5
%12 = load i8*, i8** %6
%13 = load i64, i64* %7
%14 = call i32 @read(i32 %11, i8* %12, i64 %13)
store i32 %14, i32* %8
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %23
16: ; preds = %10
%17 = load i32, i32* @errno
store i32 %17, i32* %9
%18 = load i32, i32* %9
%19 = load i32, i32* @EINTR
%20 = icmp eq i32 %18, %19
br i1 %20, label %21, label %22
21: ; preds = %16
br label %10
22: ; preds = %16
store i32 -1, i32* %4
br label %25
23: ; preds = %10
%24 = load i32, i32* %8
store i32 %24, i32* %4
br label %25
25: ; preds = %23, %22
%26 = load i32, i32* %4
ret i32 %26
}

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