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


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    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x20(%rbp)
jbe    11bc <writen+0xac>
mov    -0x8(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
cmp    $0xffffffffffffffff,%rax
jne    1187 <writen+0x77>
mov    0x2e7d(%rip),%rax
mov    (%rax),%rax
mov    0x2e6b(%rip),%rcx
cmp    (%rcx),%rax
jne    117b <writen+0x6b>
jmp    1133 <writen+0x23>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c3 <writen+0xb3>
cmpq   $0x0,-0x28(%rbp)
jne    1197 <writen+0x87>
jmp    1133 <writen+0x23>
jmp    119c <writen+0x8c>
mov    -0x28(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%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,%r14
test   %rdx,%rdx
je     1175 <writen+0x65>
mov    %rsi,%rbx
mov    %edi,%r15d
mov    0x2eb0(%rip),%r12
mov    0x2ea1(%rip),%r13
mov    %r14,%rbp
nopw   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <write@plt>
test   %rax,%rax
je     1140 <writen+0x30>
cmp    $0xffffffffffffffff,%rax
jne    1165 <writen+0x55>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     1140 <writen+0x30>
jmp    116f <writen+0x5f>
add    %rax,%rbx
sub    %rax,%rbp
jne    1140 <writen+0x30>
jmp    1175 <writen+0x65>
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 i64
%9 = alloca i64
%10 = alloca i8*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i64, i64* %7
store i64 %11, i64* %8
%12 = load i8*, i8** %6
store i8* %12, i8** %10
br label %13
13: ; preds = %33, %31, %26, %3
%14 = load i64, i64* %8
%15 = icmp ugt i64 %14, 0
br i1 %15, label %16, label %40
16: ; preds = %13
%17 = load i32, i32* %5
%18 = load i8*, i8** %10
%19 = load i64, i64* %8
%20 = call i64 @write(i32 %17, i8* %18, i64 %19)
store i64 %20, i64* %9
%21 = icmp eq i64 %20, -1
br i1 %21, label %22, label %28
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
br label %13
27: ; preds = %22
store i32 -1, i32* %4
br label %43
28: ; preds = %16
%29 = load i64, i64* %9
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %28
br label %13
32: ; preds = %28
br label %33
33: ; preds = %32
%34 = load i64, i64* %9
%35 = load i8*, i8** %10
%36 = getelementptr inbounds i8, i8* %35, i64 %34
store i8* %36, i8** %10
%37 = load i64, i64* %9
%38 = load i64, i64* %8
%39 = sub i64 %38, %37
store i64 %39, i64* %8
br label %13
40: ; preds = %13
%41 = load i64, i64* %7
%42 = trunc i64 %41 to i32
store i32 %42, i32* %4
br label %43
43: ; preds = %40, %27
%44 = load i32, i32* %4
ret i32 %44
}

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