#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 size_t ssize_t;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
size_t read(int, char *, size_t);
ssize_t readn(int fd, void *buffer, size_t n) {
  ssize_t reads;
  size_t total_reads;
  char *buf;
  buf = buffer;
  for (total_reads = 0; total_reads < n;) {
    reads = read(fd, buf, n - total_reads);
    if (reads == 0)
      return total_reads;
    if (reads == -1) {
      if (errno == EINTR)
        continue;
      else
        return -1;
    }
    total_reads += reads;
    buf += reads;
  }
  return total_reads;
}


x86 assembly (clang, -O0):
<readn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    11c5 <readn+0xb5>
mov    -0xc(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    -0x30(%rbp),%rdx
call   1030 <read@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1171 <readn+0x61>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11cd <readn+0xbd>
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    11a8 <readn+0x98>
mov    0x2e5d(%rip),%rax
mov    (%rax),%rax
mov    0x2e4b(%rip),%rcx
cmp    (%rcx),%rax
jne    119b <readn+0x8b>
jmp    1133 <readn+0x23>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11cd <readn+0xbd>
mov    -0x28(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    1133 <readn+0x23>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readn>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     1175 <readn+0x65>
mov    %rsi,%rbx
mov    %edi,%r12d
xor    %r15d,%r15d
mov    0x2eb0(%rip),%r13
mov    0x2ea1(%rip),%r14
mov    %rdx,(%rsp)
mov    %rdx,%rbp
xchg   %ax,%ax
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <read@plt>
cmp    $0xffffffffffffffff,%rax
jne    115f <readn+0x4f>
mov    0x0(%r13),%rax
cmp    (%r14),%rax
je     1140 <readn+0x30>
jmp    117a <readn+0x6a>
test   %rax,%rax
je     1181 <readn+0x71>
add    %rax,%r15
add    %rax,%rbx
mov    (%rsp),%rbp
sub    %r15,%rbp
ja     1140 <readn+0x30>
jmp    1181 <readn+0x71>
xor    %r15d,%r15d
jmp    1181 <readn+0x71>
mov    $0xffffffffffffffff,%r15
mov    %r15,%rax
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 i64 @readn(i32 %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i8*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i8*, i8** %6
store i8* %11, i8** %10
store i64 0, i64* %9
br label %12
12: ; preds = %36, %34, %3
%13 = load i64, i64* %9
%14 = load i64, i64* %7
%15 = icmp ult i64 %13, %14
br i1 %15, label %16, label %43
16: ; preds = %12
%17 = load i32, i32* %5
%18 = load i8*, i8** %10
%19 = load i64, i64* %7
%20 = load i64, i64* %9
%21 = sub i64 %19, %20
%22 = call i64 @read(i32 %17, i8* %18, i64 %21)
store i64 %22, i64* %8
%23 = load i64, i64* %8
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %16
%26 = load i64, i64* %9
store i64 %26, i64* %4
br label %45
27: ; preds = %16
%28 = load i64, i64* %8
%29 = icmp eq i64 %28, -1
br i1 %29, label %30, label %36
30: ; preds = %27
%31 = load i64, i64* @errno
%32 = load i64, i64* @EINTR
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %35
34: ; preds = %30
br label %12
35: ; preds = %30
store i64 -1, i64* %4
br label %45
36: ; preds = %27
%37 = load i64, i64* %8
%38 = load i64, i64* %9
%39 = add i64 %38, %37
store i64 %39, i64* %9
%40 = load i64, i64* %8
%41 = load i8*, i8** %10
%42 = getelementptr inbounds i8, i8* %41, i64 %40
store i8* %42, i8** %10
br label %12
43: ; preds = %12
%44 = load i64, i64* %9
store i64 %44, i64* %4
br label %45
45: ; preds = %43, %35, %25
%46 = load i64, i64* %4
ret i64 %46
}

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