#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_file(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)
        nwritten = 0;
      else
        return (-1);
    }
    nleft -= nwritten;
    ptr += nwritten;
  }
  return (n);
}


x86 assembly (clang, -O0):
<writen_file>:
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    11b6 <writen_file+0xa6>
mov    -0x8(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jg     1191 <writen_file+0x81>
mov    0x2e7f(%rip),%rax
mov    (%rax),%rax
mov    0x2e6d(%rip),%rcx
cmp    (%rcx),%rax
jne    1180 <writen_file+0x70>
movl   $0x0,-0x24(%rbp)
jmp    118c <writen_file+0x7c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bd <writen_file+0xad>
jmp    1191 <writen_file+0x81>
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_file+0x23>
mov    -0x18(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writen_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
test   %rdx,%rdx
je     116e <writen_file+0x5e>
mov    %rsi,%rbx
mov    %edi,%r15d
mov    0x2eb0(%rip),%r12
mov    0x2ea1(%rip),%r13
mov    %r14,%rbp
jmp    114a <writen_file+0x3a>
nopl   0x0(%rax)
mov    %eax,%eax
add    %rax,%rbx
sub    %rax,%rbp
je     116e <writen_file+0x5e>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <write@plt>
test   %eax,%eax
jg     1140 <writen_file+0x30>
mov    (%r12),%rcx
xor    %eax,%eax
cmp    0x0(%r13),%rcx
je     1140 <writen_file+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_file(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 = %29, %3
%14 = load i64, i64* %8
%15 = icmp ugt i64 %14, 0
br i1 %15, label %16, label %38
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 %29
22: ; preds = %16
%23 = load i64, i64* @errno
%24 = load i64, i64* @EINTR
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %22
store i32 0, i32* %9
br label %28
27: ; preds = %22
store i32 -1, i32* %4
br label %41
28: ; preds = %26
br label %29
29: ; preds = %28, %16
%30 = load i32, i32* %9
%31 = sext i32 %30 to i64
%32 = load i64, i64* %8
%33 = sub i64 %32, %31
store i64 %33, i64* %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 %13
38: ; preds = %13
%39 = load i64, i64* %7
%40 = trunc i64 %39 to i32
store i32 %40, i32* %4
br label %41
41: ; preds = %38, %27
%42 = load i32, i32* %4
ret i32 %42
}

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