#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, unsigned char *, size_t);
ssize_t zed_file_read_n(int fd, void *buf, size_t n) {
  unsigned char *p;
  size_t n_left;
  ssize_t n_read;
  p = buf;
  n_left = n;
  while (n_left > 0) {
    if ((n_read = read(fd, p, n_left)) < 0) {
      if (errno == EINTR)
        continue;
      else
        return (-1);
    } else if (n_read == 0) {
      break;
    }
    n_left -= n_read;
    p += n_read;
  }
  return (n - n_left);
}


x86 assembly (clang, -O0):
<zed_file_read_n>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jbe    11be <zed_file_read_n+0xae>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <read@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    1185 <zed_file_read_n+0x75>
mov    0x2e7f(%rip),%rax
mov    (%rax),%rax
mov    0x2e6d(%rip),%rcx
cmp    (%rcx),%rax
jne    1179 <zed_file_read_n+0x69>
jmp    1133 <zed_file_read_n+0x23>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c9 <zed_file_read_n+0xb9>
cmpl   $0x0,-0x2c(%rbp)
jne    1194 <zed_file_read_n+0x84>
jmp    11be <zed_file_read_n+0xae>
jmp    1199 <zed_file_read_n+0x89>
movslq -0x2c(%rbp),%rcx
mov    -0x28(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    1133 <zed_file_read_n+0x23>
mov    -0x18(%rbp),%rax
sub    -0x28(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<zed_file_read_n>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
test   %rdx,%rdx
je     1169 <zed_file_read_n+0x59>
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2eb1(%rip),%r12
mov    0x2ea2(%rip),%r13
mov    %r14,%r15
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1030 <read@plt>
test   %eax,%eax
jns    115d <zed_file_read_n+0x4d>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     1140 <zed_file_read_n+0x30>
jmp    1171 <zed_file_read_n+0x61>
je     116c <zed_file_read_n+0x5c>
mov    %eax,%eax
add    %rax,%rbx
sub    %rax,%r15
jne    1140 <zed_file_read_n+0x30>
xor    %r15d,%r15d
sub    %r15d,%r14d
jmp    1177 <zed_file_read_n+0x67>
mov    $0xffffffff,%r14d
mov    %r14d,%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
define i32 @zed_file_read_n(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i8*, i8** %6
store i8* %11, i8** %8
%12 = load i64, i64* %7
store i64 %12, i64* %9
br label %13
13: ; preds = %33, %26, %3
%14 = load i64, i64* %9
%15 = icmp ugt i64 %14, 0
br i1 %15, label %16, label %42
16: ; preds = %13
%17 = load i32, i32* %5
%18 = load i8*, i8** %8
%19 = load i64, i64* %9
%20 = call i32 @read(i32 %17, i8* %18, i64 %19)
store i32 %20, i32* %10
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %16
%23 = load i64, i64* @errno
%24 = load i64, i64* @EINTR
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %22
br label %13
27: ; preds = %22
store i32 -1, i32* %4
br label %47
28: ; preds = %16
%29 = load i32, i32* %10
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %28
br label %42
32: ; preds = %28
br label %33
33: ; preds = %32
%34 = load i32, i32* %10
%35 = sext i32 %34 to i64
%36 = load i64, i64* %9
%37 = sub i64 %36, %35
store i64 %37, i64* %9
%38 = load i32, i32* %10
%39 = load i8*, i8** %8
%40 = sext i32 %38 to i64
%41 = getelementptr inbounds i8, i8* %39, i64 %40
store i8* %41, i8** %8
br label %13
42: ; preds = %31, %13
%43 = load i64, i64* %7
%44 = load i64, i64* %9
%45 = sub i64 %43, %44
%46 = trunc i64 %45 to i32
store i32 %46, i32* %4
br label %47
47: ; preds = %42, %27
%48 = load i32, i32* %4
ret i32 %48
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
define i32 @zed_file_read_n(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %22, label %5
5: ; preds = %3, %17
%6 = phi i64 [ %19, %17 ], [ %2, %3 ]
%7 = phi i8* [ %20, %17 ], [ %1, %3 ]
br label %8
8: ; preds = %5, %11
%9 = tail call i32 @read(i32 %0, i8* %7, i64 %6)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %8
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINTR
%14 = icmp eq i64 %12, %13
br i1 %14, label %8, label %26
15: ; preds = %8
%16 = icmp eq i32 %9, 0
br i1 %16, label %22, label %17
17: ; preds = %15
%18 = zext i32 %9 to i64
%19 = sub i64 %6, %18
%20 = getelementptr inbounds i8, i8* %7, i64 %18
%21 = icmp eq i64 %19, 0
br i1 %21, label %22, label %5
22: ; preds = %15, %17, %3
%23 = phi i64 [ 0, %3 ], [ 0, %17 ], [ %6, %15 ]
%24 = sub i64 %2, %23
%25 = trunc i64 %24 to i32
br label %26
26: ; preds = %11, %22
%27 = phi i32 [ %25, %22 ], [ -1, %11 ]
ret i32 %27
}