#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 /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int readn(int fd, char *ptr, int maxlen) {
  char *tmpptr;
  int nleft, nreadn;
  nleft = maxlen;
  tmpptr = ptr;
  while (nleft > 0) {
    nreadn = read(fd, ptr, nleft);
    if (nreadn < 0) {
      if (errno == EINTR)
        nreadn = 0;
      else {
        perror("readn : probleme  dans read \n");
        return (-1);
      }
    } else if (nreadn == 0) {
      break;
    }
    nleft -= nreadn;
    ptr += nreadn;
  }
  return (maxlen - nleft);
}


x86 assembly (clang, -O0):
<readn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x24(%rbp)
jle    11de <readn+0xbe>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11a9 <readn+0x89>
mov    0x2e73(%rip),%rax
mov    (%rax),%rax
mov    0x2e61(%rip),%rcx
cmp    (%rcx),%rax
jne    118c <readn+0x6c>
movl   $0x0,-0x28(%rbp)
jmp    11a4 <readn+0x84>
lea    0xe6d(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e7 <readn+0xc7>
jmp    11bd <readn+0x9d>
cmpl   $0x0,-0x28(%rbp)
jne    11b8 <readn+0x98>
jmp    11de <readn+0xbe>
jmp    11bd <readn+0x9d>
mov    -0x28(%rbp),%ecx
mov    -0x24(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1140 <readn+0x20>
mov    -0x14(%rbp),%eax
sub    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readn>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %edx,%ebp
test   %edx,%edx
jle    1178 <readn+0x58>
mov    %rsi,%rbx
mov    %edi,%r15d
mov    0x2e9f(%rip),%r12
mov    0x2e90(%rip),%r13
mov    %r14d,%ebp
jmp    1165 <readn+0x45>
nopl   (%rax)
mov    (%r12),%rcx
xor    %eax,%eax
cmp    0x0(%r13),%rcx
jne    118d <readn+0x6d>
mov    %eax,%ecx
add    %rcx,%rbx
sub    %eax,%ebp
jle    1178 <readn+0x58>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <read@plt>
test   %eax,%eax
js     1150 <readn+0x30>
jne    115c <readn+0x3c>
sub    %ebp,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe6c(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%r14d
jmp    117b <readn+0x5b>

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

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@.str = [30 x i8] c"readn : probleme dans read \0A\00"
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 i32 [ %20, %18 ], [ %2, %3 ]
%7 = phi i8* [ %22, %18 ], [ %1, %3 ]
%8 = tail call i32 @read(i32 %0, i8* %7, i32 %6)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %16
10: ; preds = %5
%11 = load i64, i64* @errno
%12 = load i64, i64* @EINTR
%13 = icmp eq i64 %11, %12
br i1 %13, label %18, label %14
14: ; preds = %10
%15 = tail call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %27
16: ; preds = %5
%17 = icmp eq i32 %8, 0
br i1 %17, label %24, label %18
18: ; preds = %10, %16
%19 = phi i32 [ %8, %16 ], [ 0, %10 ]
%20 = sub nsw i32 %6, %19
%21 = zext i32 %19 to i64
%22 = getelementptr inbounds i8, i8* %7, i64 %21
%23 = icmp sgt i32 %20, 0
br i1 %23, label %5, label %24
24: ; preds = %18, %16, %3
%25 = phi i32 [ %2, %3 ], [ %6, %16 ], [ %20, %18 ]
%26 = sub nsw i32 %2, %25
br label %27
27: ; preds = %24, %14
%28 = phi i32 [ -1, %14 ], [ %26, %24 ]
ret i32 %28
}