#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 */
scalar_t__ read(int, char *, size_t);
int /*<<< orphan*/ warn(char *);
ssize_t readn(int fd, void *vptr, size_t n) {
  size_t remaining;
  ssize_t received;
  char *ptr;
  remaining = n;
  ptr = vptr;
  while (remaining > 0) {
    received = read(fd, ptr, remaining);
    if (received <= 0) {
      if (received < 0)
        warn("read");
      return received;
    }
    remaining -= received;
    ptr += received;
  }
  return n;
}


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    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x28(%rbp)
jbe    11b1 <readn+0x91>
mov    -0xc(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <read@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jg     1191 <readn+0x71>
cmpq   $0x0,-0x30(%rbp)
jge    1184 <readn+0x64>
lea    0xe81(%rip),%rdi
call   1030 <warn@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11b9 <readn+0x99>
mov    -0x30(%rbp),%rcx
mov    -0x28(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    1143 <readn+0x23>
mov    -0x20(%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   %r12
push   %rbx
test   %rdx,%rdx
je     1162 <readn+0x42>
mov    %rdx,%r14
mov    %rsi,%r12
mov    %edi,%r15d
mov    %rdx,%rbp
nopl   0x0(%rax)
mov    %r15d,%edi
mov    %r12,%rsi
mov    %rbp,%rdx
call   1040 <read@plt>
mov    %rax,%rbx
test   %rax,%rax
jle    1160 <readn+0x40>
add    %rbx,%r12
sub    %rbx,%rbp
jne    1140 <readn+0x20>
jmp    1165 <readn+0x45>
js     1171 <readn+0x51>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe88(%rip),%rdi
call   1030 <warn@plt>
mov    %rbx,%r14
jmp    1165 <readn+0x45>

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

LLVM IR (-O2):
@.str = [5 x i8] c"read\00"
define i64 @readn(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 @warn(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 [ %8, %12 ], [ 0, %10 ], [ 0, %3 ], [ %2, %14 ]
ret i64 %19
}