#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 write(int, char const *, size_t);
int writen(int fd, const char *buffer, size_t length) {
  while (length) {
    int nwritten = write(fd, buffer, length);
    if (nwritten < 0) {
      if (errno == EINTR)
        continue;
      return -1;
    }
    length -= nwritten;
    buffer += nwritten;
  }
  return 0;
}


x86 assembly (clang, -O0):
<writen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     119b <writen+0x8b>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1176 <writen+0x66>
mov    0x2e8e(%rip),%rax
mov    (%rax),%rax
mov    0x2e7c(%rip),%rcx
cmp    (%rcx),%rax
jne    116a <writen+0x5a>
jmp    1123 <writen+0x13>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a2 <writen+0x92>
movslq -0x1c(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1123 <writen+0x13>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writen>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %r14d,%r14d
test   %rdx,%rdx
je     116f <writen+0x5f>
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %edi,%r15d
mov    0x2ead(%rip),%r12
mov    0x2e9e(%rip),%r13
jmp    114d <writen+0x3d>
nopl   0x0(%rax)
mov    %eax,%eax
sub    %rax,%rbx
add    %rax,%rbp
test   %rbx,%rbx
je     116f <writen+0x5f>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
test   %eax,%eax
jns    1140 <writen+0x30>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     1148 <writen+0x38>
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
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
br label %9
9: ; preds = %25, %23, %3
%10 = load i64, i64* %7
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %34
12: ; preds = %9
%13 = load i32, i32* %5
%14 = load i8*, i8** %6
%15 = load i64, i64* %7
%16 = call i32 @write(i32 %13, i8* %14, i64 %15)
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %12
%20 = load i64, i64* @errno
%21 = load i64, i64* @EINTR
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %24
23: ; preds = %19
br label %9
24: ; preds = %19
store i32 -1, i32* %4
br label %35
25: ; preds = %12
%26 = load i32, i32* %8
%27 = sext i32 %26 to i64
%28 = load i64, i64* %7
%29 = sub i64 %28, %27
store i64 %29, i64* %7
%30 = load i32, i32* %8
%31 = load i8*, i8** %6
%32 = sext i32 %30 to i64
%33 = getelementptr inbounds i8, i8* %31, i64 %32
store i8* %33, i8** %6
br label %9
34: ; preds = %9
store i32 0, i32* %4
br label %35
35: ; preds = %34, %24
%36 = load i32, i32* %4
ret i32 %36
}

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