#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;
scalar_t__ errno;
int read(int, void *, size_t);
int fd_read(int fd, void *buf, size_t n) {
  int nread;
  do {
    nread = read(fd, buf, n);
  } while (nread == -1 && errno == EINTR);
  if (nread == -1)
    return -2;
  if (nread == 0)
    return -3;
  return nread;
}


x86 assembly (clang, -O0):
<fd_read>:
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)
xor    %eax,%eax
cmpl   $0xffffffff,-0x1c(%rbp)
mov    %al,-0x1d(%rbp)
jne    115f <fd_read+0x4f>
mov    0x2e94(%rip),%rax
mov    (%rax),%rax
mov    0x2e82(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    1123 <fd_read+0x13>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1180 <fd_read+0x70>
movl   $0xfffffffe,-0x4(%rbp)
jmp    119c <fd_read+0x8c>
cmpl   $0x0,-0x1c(%rbp)
jne    1196 <fd_read+0x86>
movl   $0xfffffffd,-0x4(%rbp)
jmp    119c <fd_read+0x8c>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fd_read>:
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>
cmp    $0xffffffff,%eax
jne    114b <fd_read+0x3b>
mov    (%r12),%rcx
cmp    %rcx,(%r15)
je     1130 <fd_read+0x20>
test   %eax,%eax
mov    $0xfffffffd,%ecx
cmovne %eax,%ecx
cmp    $0xffffffff,%eax
mov    $0xfffffffe,%eax
cmovne %ecx,%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 @fd_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
br label %9
9: ; preds = %21, %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
br label %14
14: ; preds = %9
%15 = load i32, i32* %8
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = load i64, i64* @errno
%19 = load i64, i64* @EINTR
%20 = icmp eq i64 %18, %19
br label %21
21: ; preds = %17, %14
%22 = phi i1 [ false, %14 ], [ %20, %17 ]
br i1 %22, label %9, label %23
23: ; preds = %21
%24 = load i32, i32* %8
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %27
26: ; preds = %23
store i32 -2, i32* %4
br label %33
27: ; preds = %23
%28 = load i32, i32* %8
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %27
store i32 -3, i32* %4
br label %33
31: ; preds = %27
%32 = load i32, i32* %8
store i32 %32, i32* %4
br label %33
33: ; preds = %31, %30, %26
%34 = load i32, i32* %4
ret i32 %34
}

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