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

/* Variables and functions */
int 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 = (char *)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    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jle    1180 <readn+0x60>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
movslq -0x1c(%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   %eax,%eax
jle    1173 <readn+0x53>
lea    0xeb3(%rip),%r15
nopl   (%rax)
mov    %eax,%eax
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   %eax,%eax
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 i32
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 i32 @check(i32 %12, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 %13, i32* %7
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %24
15: ; preds = %8
%16 = load i8*, i8** %5
%17 = load i32, i32* %7
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
store i8* %19, i8** %5
%20 = load i32, i32* %7
%21 = sext i32 %20 to i64
%22 = load i64, i64* %6
%23 = sub i64 %22, %21
store i64 %23, i64* %6
br label %8
24: ; 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 i32 @check(i32 %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %17
7: ; preds = %3, %7
%8 = phi i32 [ %15, %7 ], [ %5, %3 ]
%9 = phi i8* [ %12, %7 ], [ %1, %3 ]
%10 = phi i64 [ %13, %7 ], [ %2, %3 ]
%11 = zext i32 %8 to i64
%12 = getelementptr inbounds i8, i8* %9, i64 %11
%13 = sub i64 %10, %11
%14 = tail call i32 @read(i32 %0, i8* %12, i64 %13)
%15 = tail call i32 @check(i32 %14, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%16 = icmp sgt i32 %15, 0
br i1 %16, label %7, label %17
17: ; preds = %7, %3
ret void
}