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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ read(int, void *, scalar_t__);
ssize_t readn(int rfd, void *buf, size_t size) {
  ssize_t nleft = size;
  ssize_t nread = 0;
  while (nleft > 0) {
    if ((nread = read(rfd, buf, nleft)) == -1) {
      perror("read");
      close(rfd);
      exit(EXIT_FAILURE);
    } else if (nread == 0) {
      break;
    }
    nleft -= nread;
    buf += nread;
  }
  return size - nleft;
}


x86 assembly (clang, -O0):
<readn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
jle    11e3 <readn+0xa3>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <read@plt>
mov    %rax,-0x28(%rbp)
cmp    $0xffffffffffffffff,%rax
jne    11ae <readn+0x6e>
lea    0xe6d(%rip),%rdi
call   1050 <perror@plt>
mov    -0x4(%rbp),%edi
call   1030 <close@plt>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
cmpq   $0x0,-0x28(%rbp)
jne    11be <readn+0x7e>
jmp    11e3 <readn+0xa3>
jmp    11c3 <readn+0x83>
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    1163 <readn+0x23>
mov    -0x18(%rbp),%rax
sub    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readn>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rdx,%rbx
test   %rdx,%rdx
jle    1181 <readn+0x41>
mov    %rsi,%rbp
mov    %edi,%r15d
mov    %r14,%rbx
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1040 <read@plt>
test   %rax,%rax
je     1181 <readn+0x41>
cmp    $0xffffffffffffffff,%rax
je     1192 <readn+0x52>
add    %rax,%rbp
sub    %rax,%rbx
jg     1160 <readn+0x20>
sub    %rbx,%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe67(%rip),%rdi
call   1050 <perror@plt>
mov    %r15d,%edi
call   1030 <close@plt>
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"read\00"
@EXIT_FAILURE = global i32 0
define i64 @readn(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%9 = load i64, i64* %6
store i64 %9, i64* %7
store i64 0, i64* %8
br label %10
10: ; preds = %30, %3
%11 = load i64, i64* %7
%12 = icmp sgt i64 %11, 0
br i1 %12, label %13, label %37
13: ; preds = %10
%14 = load i32, i32* %4
%15 = load i8*, i8** %5
%16 = load i64, i64* %7
%17 = call i64 @read(i32 %14, i8* %15, i64 %16)
store i64 %17, i64* %8
%18 = icmp eq i64 %17, -1
br i1 %18, label %19, label %25
19: ; preds = %13
%20 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%21 = load i32, i32* %4
%22 = call i32 @close(i32 %21)
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %13
%26 = load i64, i64* %8
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %25
br label %37
29: ; preds = %25
br label %30
30: ; preds = %29
%31 = load i64, i64* %8
%32 = load i64, i64* %7
%33 = sub nsw i64 %32, %31
store i64 %33, i64* %7
%34 = load i64, i64* %8
%35 = load i8*, i8** %5
%36 = getelementptr i8, i8* %35, i64 %34
store i8* %36, i8** %5
br label %10
37: ; preds = %28, %10
%38 = load i64, i64* %6
%39 = load i64, i64* %7
%40 = sub i64 %38, %39
ret i64 %40
}

LLVM IR (-O2):
@.str = [5 x i8] c"read\00"
@EXIT_FAILURE = global i32 0
define i64 @readn(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp sgt i64 %2, 0
br i1 %4, label %5, label %18
5: ; preds = %3, %14
%6 = phi i64 [ %15, %14 ], [ %2, %3 ]
%7 = phi i8* [ %16, %14 ], [ %1, %3 ]
%8 = tail call i64 @read(i32 %0, i8* %7, i64 %6)
switch i64 %8, label %14 [
i64 -1, label %9
i64 0, label %18
]
9: ; preds = %5
%10 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @close(i32 %0)
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %5
%15 = sub nsw i64 %6, %8
%16 = getelementptr i8, i8* %7, i64 %8
%17 = icmp sgt i64 %15, 0
br i1 %17, label %5, label %18
18: ; preds = %14, %5, %3
%19 = phi i64 [ %2, %3 ], [ %6, %5 ], [ %15, %14 ]
%20 = sub i64 %2, %19
ret i64 %20
}