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


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    11c9 <writen+0xb9>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    1185 <writen+0x75>
mov    0x2e7f(%rip),%rax
mov    (%rax),%rax
mov    0x2e6d(%rip),%rcx
cmp    (%rcx),%rax
jne    1179 <writen+0x69>
jmp    1133 <writen+0x23>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c9 <writen+0xb9>
cmpl   $0x0,-0x24(%rbp)
jne    119f <writen+0x8f>
mov    -0x18(%rbp),%rax
sub    -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    11c9 <writen+0xb9>
jmp    11a4 <writen+0x94>
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+0x23>
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     1170 <writen+0x60>
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2eb1(%rip),%r12
mov    0x2ea2(%rip),%r13
mov    %rdx,%r15
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <write@plt>
test   %eax,%eax
jns    115d <writen+0x4d>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     1140 <writen+0x30>
jmp    1168 <writen+0x58>
je     1182 <writen+0x72>
mov    %eax,%eax
sub    %rax,%r15
jne    1140 <writen+0x30>
jmp    1170 <writen+0x60>
mov    $0xffffffff,%r14d
jmp    1170 <writen+0x60>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
sub    %r15d,%r14d
jmp    1170 <writen+0x60>

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 i32
%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 = %37, %26, %3
%14 = load i64, i64* %8
%15 = icmp ugt i64 %14, 0
br i1 %15, label %16, label %46
16: ; preds = %13
%17 = load i32, i32* %5
%18 = load i8*, i8** %6
%19 = load i64, i64* %7
%20 = call i32 @write(i32 %17, i8* %18, i64 %19)
store i32 %20, i32* %9
%21 = icmp slt i32 %20, 0
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 %46
28: ; preds = %16
%29 = load i32, i32* %9
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %28
%32 = load i64, i64* %7
%33 = load i64, i64* %8
%34 = sub i64 %32, %33
%35 = trunc i64 %34 to i32
store i32 %35, i32* %4
br label %46
36: ; preds = %28
br label %37
37: ; preds = %36
%38 = load i32, i32* %9
%39 = sext i32 %38 to i64
%40 = load i64, i64* %8
%41 = sub i64 %40, %39
store i64 %41, i64* %8
%42 = load i32, i32* %9
%43 = load i8*, i8** %10
%44 = sext i32 %42 to i64
%45 = getelementptr inbounds i8, i8* %43, i64 %44
store i8* %45, i8** %10
br label %13
46: ; preds = %27, %31, %13
%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 %23, label %5
5: ; preds = %3, %19
%6 = phi i64 [ %21, %19 ], [ %2, %3 ]
br label %7
7: ; preds = %5, %10
%8 = tail call i32 @write(i32 %0, i8* %1, i64 %2)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %7
%11 = load i64, i64* @errno
%12 = load i64, i64* @EINTR
%13 = icmp eq i64 %11, %12
br i1 %13, label %7, label %23
14: ; preds = %7
%15 = icmp eq i32 %8, 0
br i1 %15, label %16, label %19
16: ; preds = %14
%17 = sub i64 %2, %6
%18 = trunc i64 %17 to i32
br label %23
19: ; preds = %14
%20 = zext i32 %8 to i64
%21 = sub i64 %6, %20
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %5
23: ; preds = %19, %10, %3, %16
%24 = phi i32 [ %18, %16 ], [ undef, %3 ], [ -1, %10 ], [ undef, %19 ]
ret i32 %24
}