#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 */

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int read(int, void *, int);
int readn(int fd, void *buf, int n) {
  int readed = 0, left = n, bytes;
  while (left > 0) {
    bytes = read(fd, buf + readed, left);
    if (bytes == -1 && errno == EINTR)
      continue;
    if (bytes <= 0)
      return bytes;
    readed += bytes;
    left -= bytes;
  }
  return n;
}


x86 assembly (clang, -O0):
<readn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    11a9 <readn+0x99>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x1c(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    117b <readn+0x6b>
mov    0x2e7d(%rip),%rax
mov    (%rax),%rax
mov    0x2e6b(%rip),%rcx
cmp    (%rcx),%rax
jne    117b <readn+0x6b>
jmp    112f <readn+0x1f>
cmpl   $0x0,-0x20(%rbp)
jg     1190 <readn+0x80>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11af <readn+0x9f>
mov    -0x20(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
jmp    112f <readn+0x1f>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readn>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%eax
test   %edx,%edx
jle    118d <readn+0x7d>
mov    %edi,%r12d
xor    %r13d,%r13d
mov    0x2eaf(%rip),%r14
mov    0x2ea0(%rip),%r15
mov    %rsi,%rbp
mov    %eax,0xc(%rsp)
mov    %eax,%ebx
mov    %rsi,0x10(%rsp)
cs nopw 0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1030 <read@plt>
cmp    $0xffffffff,%eax
jne    116c <readn+0x5c>
mov    (%r14),%rax
cmp    (%r15),%rax
je     1150 <readn+0x40>
jmp    1188 <readn+0x78>
test   %eax,%eax
mov    0x10(%rsp),%rsi
jle    118d <readn+0x7d>
add    %eax,%r13d
movslq %r13d,%rbp
add    %rsi,%rbp
sub    %eax,%ebx
jg     1150 <readn+0x40>
mov    0xc(%rsp),%eax
jmp    118d <readn+0x7d>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
define i32 @readn(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %8
%11 = load i32, i32* %7
store i32 %11, i32* %9
br label %12
12: ; preds = %35, %29, %3
%13 = load i32, i32* %9
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %42
15: ; preds = %12
%16 = load i32, i32* %5
%17 = load i8*, i8** %6
%18 = load i32, i32* %8
%19 = sext i32 %18 to i64
%20 = getelementptr i8, i8* %17, i64 %19
%21 = load i32, i32* %9
%22 = call i32 @read(i32 %16, i8* %20, i32 %21)
store i32 %22, i32* %10
%23 = load i32, i32* %10
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %30
25: ; preds = %15
%26 = load i64, i64* @errno
%27 = load i64, i64* @EINTR
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %30
29: ; preds = %25
br label %12
30: ; preds = %25, %15
%31 = load i32, i32* %10
%32 = icmp sle i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = load i32, i32* %10
store i32 %34, i32* %4
br label %44
35: ; preds = %30
%36 = load i32, i32* %10
%37 = load i32, i32* %8
%38 = add nsw i32 %37, %36
store i32 %38, i32* %8
%39 = load i32, i32* %10
%40 = load i32, i32* %9
%41 = sub nsw i32 %40, %39
store i32 %41, i32* %9
br label %12
42: ; preds = %12
%43 = load i32, i32* %7
store i32 %43, i32* %4
br label %44
44: ; preds = %42, %33
%45 = load i32, i32* %4
ret i32 %45
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
define i32 @readn(i32 %0, i8* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %24
5: ; preds = %3, %18
%6 = phi i8* [ %23, %18 ], [ %1, %3 ]
%7 = phi i32 [ %20, %18 ], [ %2, %3 ]
%8 = phi i32 [ %19, %18 ], [ 0, %3 ]
br label %9
9: ; preds = %5, %12
%10 = tail call i32 @read(i32 %0, i8* %6, i32 %7)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %9
%13 = load i64, i64* @errno
%14 = load i64, i64* @EINTR
%15 = icmp eq i64 %13, %14
br i1 %15, label %9, label %24
16: ; preds = %9
%17 = icmp slt i32 %10, 1
br i1 %17, label %24, label %18
18: ; preds = %16
%19 = add nsw i32 %10, %8
%20 = sub nsw i32 %7, %10
%21 = icmp sgt i32 %20, 0
%22 = sext i32 %19 to i64
%23 = getelementptr i8, i8* %1, i64 %22
br i1 %21, label %5, label %24
24: ; preds = %18, %16, %12, %3
%25 = phi i32 [ %2, %3 ], [ -1, %12 ], [ %10, %16 ], [ %2, %18 ]
ret i32 %25
}