#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 */
scalar_t__ ECHILD;
scalar_t__ EINTR;
scalar_t__ errno;
int write(int, char const *, size_t);
ssize_t writen(int fd, const void *vptr, size_t n) {
  size_t nleft;
  ssize_t nwritten;
  const char *ptr;
  ptr = vptr;
  nleft = n;
  while (nleft > 0) {
    if ((nwritten = write(fd, ptr, nleft)) <= 0) {
      if ((errno == EINTR || errno == ECHILD)) {
        nwritten = 0;
        continue;
      } else
        return -1;
    }
    nleft -= nwritten;
    ptr += nwritten;
  }
  return n;
}


x86 assembly (clang, -O0):
<writen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jbe    11cb <writen+0xbb>
mov    -0x8(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jg     11a6 <writen+0x96>
mov    0x2e7f(%rip),%rax
mov    (%rax),%rax
mov    0x2e6d(%rip),%rcx
cmp    (%rcx),%rax
je     118e <writen+0x7e>
mov    0x2e65(%rip),%rax
mov    (%rax),%rax
mov    0x2e4b(%rip),%rcx
cmp    (%rcx),%rax
jne    119a <writen+0x8a>
movl   $0x0,-0x24(%rbp)
jmp    1133 <writen+0x23>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d2 <writen+0xc2>
movslq -0x24(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x24(%rbp),%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
jmp    1133 <writen+0x23>
mov    -0x18(%rbp),%rax
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    %rdx,%rax
test   %rdx,%rdx
je     1188 <writen+0x78>
mov    %rsi,%rbx
mov    %edi,%r15d
mov    0x2eb0(%rip),%r12
mov    0x2ea1(%rip),%r13
mov    0x2e92(%rip),%r14
mov    %rax,%rbp
mov    %rax,(%rsp)
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <write@plt>
test   %eax,%eax
jg     1173 <writen+0x63>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     1150 <writen+0x40>
cmp    (%r14),%rax
je     1150 <writen+0x40>
jmp    1183 <writen+0x73>
mov    %eax,%eax
add    %rax,%rbx
sub    %rax,%rbp
mov    (%rsp),%rax
jne    1150 <writen+0x40>
jmp    1188 <writen+0x78>
mov    $0xffffffff,%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
@ECHILD = global i64 0
define i32 @writen(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
%10 = alloca i8*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i8*, i8** %6
store i8* %11, i8** %10
%12 = load i64, i64* %7
store i64 %12, i64* %8
br label %13
13: ; preds = %32, %30, %3
%14 = load i64, i64* %8
%15 = icmp ugt i64 %14, 0
br i1 %15, label %16, label %41
16: ; preds = %13
%17 = load i32, i32* %5
%18 = load i8*, i8** %10
%19 = load i64, i64* %8
%20 = call i32 @write(i32 %17, i8* %18, i64 %19)
store i32 %20, i32* %9
%21 = icmp sle i32 %20, 0
br i1 %21, label %22, label %32
22: ; preds = %16
%23 = load i64, i64* @errno
%24 = load i64, i64* @EINTR
%25 = icmp eq i64 %23, %24
br i1 %25, label %30, label %26
26: ; preds = %22
%27 = load i64, i64* @errno
%28 = load i64, i64* @ECHILD
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %31
30: ; preds = %26, %22
store i32 0, i32* %9
br label %13
31: ; preds = %26
store i32 -1, i32* %4
br label %44
32: ; preds = %16
%33 = load i32, i32* %9
%34 = sext i32 %33 to i64
%35 = load i64, i64* %8
%36 = sub i64 %35, %34
store i64 %36, i64* %8
%37 = load i32, i32* %9
%38 = load i8*, i8** %10
%39 = sext i32 %37 to i64
%40 = getelementptr inbounds i8, i8* %38, i64 %39
store i8* %40, i8** %10
br label %13
41: ; preds = %13
%42 = load i64, i64* %7
%43 = trunc i64 %42 to i32
store i32 %43, i32* %4
br label %44
44: ; preds = %41, %31
%45 = load i32, i32* %4
ret i32 %45
}

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