#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 *, size_t);
ssize_t writen(int fd, const void *buffer, size_t n) {
  ssize_t writes;
  size_t total_writes;
  const char *buf;
  buf = buffer;
  for (total_writes = 0; total_writes < n;) {
    writes = write(fd, buf, n - total_writes);
    if (writes <= 0) {
      if (writes == -1 && errno == EINTR)
        continue;
      else
        return -1;
    }
    total_writes += writes;
    buf += writes;
  }
  return total_writes;
}


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)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11b9 <writen+0xa9>
mov    -0x8(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x28(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     1197 <writen+0x87>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    118b <writen+0x7b>
mov    0x2e6d(%rip),%rax
mov    (%rax),%rax
mov    0x2e5b(%rip),%rcx
cmp    (%rcx),%rax
jne    118b <writen+0x7b>
jmp    1133 <writen+0x23>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c0 <writen+0xb0>
movslq -0x1c(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
jmp    1133 <writen+0x23>
mov    -0x28(%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
test   %rdx,%rdx
je     117f <writen+0x6f>
mov    %rsi,%rbx
mov    %edi,%r15d
xor    %r12d,%r12d
mov    0x2eb0(%rip),%r13
mov    0x2ea1(%rip),%r14
mov    %rdx,%rbp
mov    %rdx,(%rsp)
xchg   %ax,%ax
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <write@plt>
mov    %eax,%ecx
test   %eax,%eax
jg     1169 <writen+0x59>
mov    $0xffffffff,%eax
cmp    $0xffffffff,%ecx
jne    1181 <writen+0x71>
mov    0x0(%r13),%rcx
cmp    (%r14),%rcx
je     1140 <writen+0x30>
jmp    1181 <writen+0x71>
mov    %ecx,%eax
add    %rax,%r12
add    %rax,%rbx
mov    (%rsp),%rbp
sub    %r12,%rbp
ja     1140 <writen+0x30>
mov    %r12d,%eax
jmp    1181 <writen+0x71>
xor    %eax,%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 i64
%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
store i64 0, i64* %9
br label %12
12: ; preds = %34, %32, %3
%13 = load i64, i64* %9
%14 = load i64, i64* %7
%15 = icmp ult i64 %13, %14
br i1 %15, label %16, label %43
16: ; preds = %12
%17 = load i32, i32* %5
%18 = load i8*, i8** %10
%19 = load i64, i64* %7
%20 = load i64, i64* %9
%21 = sub i64 %19, %20
%22 = call i32 @write(i32 %17, i8* %18, i64 %21)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp sle i32 %23, 0
br i1 %24, label %25, label %34
25: ; preds = %16
%26 = load i32, i32* %8
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %33
28: ; preds = %25
%29 = load i64, i64* @errno
%30 = load i64, i64* @EINTR
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %33
32: ; preds = %28
br label %12
33: ; preds = %28, %25
store i32 -1, i32* %4
br label %46
34: ; preds = %16
%35 = load i32, i32* %8
%36 = sext i32 %35 to i64
%37 = load i64, i64* %9
%38 = add i64 %37, %36
store i64 %38, i64* %9
%39 = load i32, i32* %8
%40 = load i8*, i8** %10
%41 = sext i32 %39 to i64
%42 = getelementptr inbounds i8, i8* %40, i64 %41
store i8* %42, i8** %10
br label %12
43: ; preds = %12
%44 = load i64, i64* %9
%45 = trunc i64 %44 to i32
store i32 %45, i32* %4
br label %46
46: ; preds = %43, %33
%47 = load i32, i32* %4
ret i32 %47
}

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 %26, label %5
5: ; preds = %3, %18
%6 = phi i64 [ %23, %18 ], [ %2, %3 ]
%7 = phi i8* [ %21, %18 ], [ %1, %3 ]
%8 = phi i64 [ %20, %18 ], [ 0, %3 ]
br label %9
9: ; preds = %5, %14
%10 = tail call i32 @write(i32 %0, i8* %7, i64 %6)
%11 = icmp slt i32 %10, 1
br i1 %11, label %12, label %18
12: ; preds = %9
%13 = icmp eq i32 %10, -1
br i1 %13, label %14, label %26
14: ; preds = %12
%15 = load i64, i64* @errno
%16 = load i64, i64* @EINTR
%17 = icmp eq i64 %15, %16
br i1 %17, label %9, label %26
18: ; preds = %9
%19 = zext i32 %10 to i64
%20 = add i64 %8, %19
%21 = getelementptr inbounds i8, i8* %7, i64 %19
%22 = icmp ult i64 %20, %2
%23 = sub i64 %2, %20
br i1 %22, label %5, label %24
24: ; preds = %18
%25 = trunc i64 %20 to i32
br label %26
26: ; preds = %12, %14, %3, %24
%27 = phi i32 [ 0, %3 ], [ %25, %24 ], [ -1, %14 ], [ -1, %12 ]
ret i32 %27
}