#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__ EINTR;
scalar_t__ errno;
int write(int, char const *, int);
ssize_t writen(int fd, const void *vptr, size_t n) {
  ssize_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)
        nwritten = 0;
      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,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    11af <writen+0x9f>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jg     118e <writen+0x7e>
mov    0x2e82(%rip),%rax
mov    (%rax),%rax
mov    0x2e70(%rip),%rcx
cmp    (%rcx),%rax
jne    117d <writen+0x6d>
movl   $0x0,-0x20(%rbp)
jmp    1189 <writen+0x79>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b6 <writen+0xa6>
jmp    118e <writen+0x7e>
mov    -0x20(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    1132 <writen+0x22>
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,%r14
test   %r14d,%r14d
jle    116c <writen+0x5c>
mov    %rsi,%rbx
mov    %edi,%r15d
mov    0x2eb0(%rip),%r12
mov    0x2ea1(%rip),%r13
mov    %r14d,%ebp
jmp    1149 <writen+0x39>
nopl   0x0(%rax)
mov    %eax,%ecx
add    %rcx,%rbx
sub    %eax,%ebp
jle    116c <writen+0x5c>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
test   %eax,%eax
jg     1140 <writen+0x30>
mov    (%r12),%rcx
xor    %eax,%eax
cmp    0x0(%r13),%rcx
je     1140 <writen+0x30>
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
define i32 @writen(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%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
%13 = trunc i64 %12 to i32
store i32 %13, i32* %8
br label %14
14: ; preds = %30, %3
%15 = load i32, i32* %8
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %38
17: ; preds = %14
%18 = load i32, i32* %5
%19 = load i8*, i8** %10
%20 = load i32, i32* %8
%21 = call i32 @write(i32 %18, i8* %19, i32 %20)
store i32 %21, i32* %9
%22 = icmp sle i32 %21, 0
br i1 %22, label %23, label %30
23: ; preds = %17
%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 %29
28: ; preds = %23
store i32 -1, i32* %4
br label %41
29: ; preds = %27
br label %30
30: ; preds = %29, %17
%31 = load i32, i32* %9
%32 = load i32, i32* %8
%33 = sub nsw i32 %32, %31
store i32 %33, i32* %8
%34 = load i32, i32* %9
%35 = load i8*, i8** %10
%36 = sext i32 %34 to i64
%37 = getelementptr inbounds i8, i8* %35, i64 %36
store i8* %37, i8** %10
br label %14
38: ; preds = %14
%39 = load i64, i64* %7
%40 = trunc i64 %39 to i32
store i32 %40, i32* %4
br label %41
41: ; preds = %38, %28
%42 = load i32, i32* %4
ret i32 %42
}

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