#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__ check(int /*<<< orphan*/, char *);
int /*<<< orphan*/ read(int, void *, size_t);
void readn(int fd, void *buf, size_t len) {
  ssize_t read_cnt;
  while ((read_cnt = check(read(fd, buf, len), "readn")) > 0) {
    buf += read_cnt;
    len -= read_cnt;
  }
}


x86 assembly (clang, -O0):
<readn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <read@plt>
mov    %eax,%edi
lea    0xeb4(%rip),%rsi
call   1040 <check@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jle    117f <readn+0x5f>
mov    -0x20(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    1133 <readn+0x13>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readn>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
call   1030 <read@plt>
lea    0xec5(%rip),%rsi
mov    %eax,%edi
call   1040 <check@plt>
test   %rax,%rax
jle    1172 <readn+0x52>
lea    0xeb2(%rip),%r15
xchg   %ax,%ax
add    %rax,%rbx
sub    %rax,%r14
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <read@plt>
mov    %eax,%edi
mov    %r15,%rsi
call   1040 <check@plt>
test   %rax,%rax
jg     1150 <readn+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"readn\00"
define void @readn(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
br label %8
8: ; preds = %15, %3
%9 = load i32, i32* %4
%10 = load i8*, i8** %5
%11 = load i64, i64* %6
%12 = call i32 @read(i32 %9, i8* %10, i64 %11)
%13 = call i64 @check(i32 %12, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i64 %13, i64* %7
%14 = icmp sgt i64 %13, 0
br i1 %14, label %15, label %22
15: ; preds = %8
%16 = load i64, i64* %7
%17 = load i8*, i8** %5
%18 = getelementptr i8, i8* %17, i64 %16
store i8* %18, i8** %5
%19 = load i64, i64* %7
%20 = load i64, i64* %6
%21 = sub i64 %20, %19
store i64 %21, i64* %6
br label %8
22: ; preds = %8
ret void
}

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