#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 write(int, char *, int);
int writen(int fd, char *ptr, int nbytes) {
  int nleft, nwritten;
  char *tmpptr;
  nleft = nbytes;
  tmpptr = ptr;
  while (nleft > 0) {
    nwritten = write(fd, ptr, nleft);
    if (nwritten <= 0) {
      if (errno == EINTR)
        nwritten = 0;
      else {
        perror("probleme  dans write\n");
        return (-1);
      }
    }
    nleft -= nwritten;
    ptr += nwritten;
  }
  return (nbytes);
}


x86 assembly (clang, -O0):
<writen>:
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,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    11ca <writen+0xaa>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     11a9 <writen+0x89>
mov    0x2e73(%rip),%rax
mov    (%rax),%rax
mov    0x2e61(%rip),%rcx
cmp    (%rcx),%rax
jne    118c <writen+0x6c>
movl   $0x0,-0x1c(%rbp)
jmp    11a4 <writen+0x84>
lea    0xe6d(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d0 <writen+0xb0>
jmp    11a9 <writen+0x89>
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1140 <writen+0x20>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writen>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
test   %edx,%edx
jle    1188 <writen+0x68>
mov    %rsi,%rbx
mov    %edi,%r15d
mov    0x2ea1(%rip),%r12
mov    0x2e92(%rip),%r13
mov    %r14d,%ebp
jmp    1159 <writen+0x39>
nopl   0x0(%rax,%rax,1)
mov    %eax,%ecx
add    %rcx,%rbx
sub    %eax,%ebp
jle    1188 <writen+0x68>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
test   %eax,%eax
jg     1150 <writen+0x30>
mov    (%r12),%rcx
xor    %eax,%eax
cmp    0x0(%r13),%rcx
je     1150 <writen+0x30>
lea    0xe83(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%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
@.str = [22 x i8] c"probleme dans write\0A\00"
define i32 @writen(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 i8*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load i32, i32* %7
store i32 %11, i32* %8
%12 = load i8*, i8** %6
store i8* %12, i8** %10
br label %13
13: ; preds = %31, %3
%14 = load i32, i32* %8
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %39
16: ; preds = %13
%17 = load i32, i32* %5
%18 = load i8*, i8** %6
%19 = load i32, i32* %8
%20 = call i32 @write(i32 %17, i8* %18, i32 %19)
store i32 %20, i32* %9
%21 = load i32, i32* %9
%22 = icmp sle 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* %9
br label %30
28: ; preds = %23
%29 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %41
30: ; preds = %27
br label %31
31: ; preds = %30, %16
%32 = load i32, i32* %9
%33 = load i32, i32* %8
%34 = sub nsw i32 %33, %32
store i32 %34, i32* %8
%35 = load i32, i32* %9
%36 = load i8*, i8** %6
%37 = sext i32 %35 to i64
%38 = getelementptr inbounds i8, i8* %36, i64 %37
store i8* %38, i8** %6
br label %13
39: ; preds = %13
%40 = load i32, i32* %7
store i32 %40, i32* %4
br label %41
41: ; preds = %39, %28
%42 = load i32, i32* %4
ret i32 %42
}

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