#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__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ errno;
int read(int, char *, size_t);
int c_read(int fd, char *buf, size_t start, size_t n) {
  int i;
  buf += start;
  for (;;) {
    i = read(fd, buf, n);
    if (i >= 0)
      return i;
    if (errno != EAGAIN && errno != EINTR)
      return -1;
  }
}


x86 assembly (clang, -O0):
<c_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     115b <c_read+0x4b>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11a0 <c_read+0x90>
mov    0x2e7e(%rip),%rax
mov    (%rax),%rax
mov    0x2e5c(%rip),%rcx
cmp    (%rcx),%rax
je     119b <c_read+0x8b>
mov    0x2e64(%rip),%rax
mov    (%rax),%rax
mov    0x2e52(%rip),%rcx
cmp    (%rcx),%rax
je     119b <c_read+0x8b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a0 <c_read+0x90>
jmp    1133 <c_read+0x23>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<c_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
add    %rdx,%rbx
mov    0x2eb3(%rip),%r15
mov    0x2e94(%rip),%r12
mov    0x2e9d(%rip),%r13
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <read@plt>
test   %eax,%eax
jns    1165 <c_read+0x55>
mov    (%r15),%rax
cmp    (%r12),%rax
je     1140 <c_read+0x30>
cmp    0x0(%r13),%rax
je     1140 <c_read+0x30>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EAGAIN = global i64 0
@EINTR = global i64 0
define i32 @c_read(i32 %0, i8* %1, i64 %2, i64 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i64 %3, i64* %9
%11 = load i64, i64* %8
%12 = load i8*, i8** %7
%13 = getelementptr inbounds i8, i8* %12, i64 %11
store i8* %13, i8** %7
br label %14
14: ; preds = %32, %4
%15 = load i32, i32* %6
%16 = load i8*, i8** %7
%17 = load i64, i64* %9
%18 = call i32 @read(i32 %15, i8* %16, i64 %17)
store i32 %18, i32* %10
%19 = load i32, i32* %10
%20 = icmp sge i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = load i32, i32* %10
store i32 %22, i32* %5
br label %33
23: ; preds = %14
%24 = load i64, i64* @errno
%25 = load i64, i64* @EAGAIN
%26 = icmp ne i64 %24, %25
br i1 %26, label %27, label %32
27: ; preds = %23
%28 = load i64, i64* @errno
%29 = load i64, i64* @EINTR
%30 = icmp ne i64 %28, %29
br i1 %30, label %31, label %32
31: ; preds = %27
store i32 -1, i32* %5
br label %33
32: ; preds = %27, %23
br label %14
33: ; preds = %31, %21
%34 = load i32, i32* %5
ret i32 %34
}

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