#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*/ perror(char *);
scalar_t__ read(int, char *, size_t);
ssize_t readn_block(int fd, void *vptr, size_t n) {
  size_t nleft = 0;
  ssize_t nread = 0;
  char *ptr = vptr;
  nleft = n;
  while (nleft > 0) {
    if ((nread = read(fd, ptr, nleft)) <= 0) {
      if (nread < 0) {
        perror("read");
      }
      break;
    }
    nleft -= nread;
    ptr += nread;
  }
  return (n - nleft);
}


x86 assembly (clang, -O0):
<readn_block>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jbe    11b8 <readn_block+0x98>
mov    -0x4(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <read@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jg     1198 <readn_block+0x78>
cmpq   $0x0,-0x28(%rbp)
jge    1193 <readn_block+0x73>
lea    0xe72(%rip),%rdi
call   1040 <perror@plt>
jmp    11b8 <readn_block+0x98>
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1153 <readn_block+0x33>
mov    -0x18(%rbp),%rax
sub    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readn_block>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
test   %rdx,%rdx
je     115a <readn_block+0x3a>
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %r14,%r15
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1030 <read@plt>
test   %rax,%rax
jle    115f <readn_block+0x3f>
add    %rax,%rbx
sub    %rax,%r15
jne    1140 <readn_block+0x20>
xor    %r15d,%r15d
jmp    1161 <readn_block+0x41>
js     1172 <readn_block+0x52>
sub    %r15,%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe87(%rip),%rdi
call   1040 <perror@plt>
jmp    1161 <readn_block+0x41>

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

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