#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 scalar_t__ ssize_t;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
scalar_t__ write(int, char const *, size_t);
ssize_t writen_ni(int fd, const void *buff, size_t n) {
  size_t nl;
  ssize_t nw;
  const char *p;
  p = buff;
  nl = n;
  while (nl > 0) {
    do {
      nw = write(fd, p, nl);
    } while (nw < 0 && errno == EINTR);
    if (nw <= 0) {
      perror("Error writing to fd");
      break;
    }
    nl -= nw;
    p += nw;
  }
  return n - nl;
}


x86 assembly (clang, -O0):
<writen_ni>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%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    11d8 <writen_ni+0xb8>
jmp    1153 <writen_ni+0x33>
mov    -0x4(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x28(%rbp)
mov    %al,-0x31(%rbp)
jge    1191 <writen_ni+0x71>
mov    0x2e62(%rip),%rax
mov    (%rax),%rax
mov    0x2e50(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    1153 <writen_ni+0x33>
cmpq   $0x0,-0x28(%rbp)
jg     11b8 <writen_ni+0x98>
lea    0xe52(%rip),%rdi
call   1040 <perror@plt>
jmp    11d8 <writen_ni+0xb8>
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1143 <writen_ni+0x23>
mov    -0x18(%rbp),%rax
sub    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writen_ni>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
test   %rdx,%rdx
je     1179 <writen_ni+0x59>
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2ea1(%rip),%r12
mov    0x2e92(%rip),%r13
mov    %r14,%r15
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1030 <write@plt>
test   %rax,%rax
jns    116c <writen_ni+0x4c>
mov    0x0(%r13),%rcx
cmp    %rcx,(%r12)
je     1150 <writen_ni+0x30>
test   %rax,%rax
jle    1191 <writen_ni+0x71>
add    %rax,%rbx
sub    %rax,%r15
jne    1150 <writen_ni+0x30>
xor    %r15d,%r15d
sub    %r15,%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe68(%rip),%rdi
call   1040 <perror@plt>
jmp    117c <writen_ni+0x5c>

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@.str = [20 x i8] c"Error writing to fd\00"
define i64 @writen_ni(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%10 = load i8*, i8** %5
store i8* %10, i8** %9
%11 = load i64, i64* %6
store i64 %11, i64* %7
br label %12
12: ; preds = %35, %3
%13 = load i64, i64* %7
%14 = icmp ugt i64 %13, 0
br i1 %14, label %15, label %42
15: ; preds = %12
br label %16
16: ; preds = %28, %15
%17 = load i32, i32* %4
%18 = load i8*, i8** %9
%19 = load i64, i64* %7
%20 = call i64 @write(i32 %17, i8* %18, i64 %19)
store i64 %20, i64* %8
br label %21
21: ; preds = %16
%22 = load i64, i64* %8
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %21
%25 = load i64, i64* @errno
%26 = load i64, i64* @EINTR
%27 = icmp eq i64 %25, %26
br label %28
28: ; preds = %24, %21
%29 = phi i1 [ false, %21 ], [ %27, %24 ]
br i1 %29, label %16, label %30
30: ; preds = %28
%31 = load i64, i64* %8
%32 = icmp sle i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %42
35: ; preds = %30
%36 = load i64, i64* %8
%37 = load i64, i64* %7
%38 = sub i64 %37, %36
store i64 %38, i64* %7
%39 = load i64, i64* %8
%40 = load i8*, i8** %9
%41 = getelementptr inbounds i8, i8* %40, i64 %39
store i8* %41, i8** %9
br label %12
42: ; preds = %33, %12
%43 = load i64, i64* %6
%44 = load i64, i64* %7
%45 = sub i64 %43, %44
ret i64 %45
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@.str = [20 x i8] c"Error writing to fd\00"
define i64 @writen_ni(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 i8* [ %21, %19 ], [ %1, %3 ]
%7 = phi i64 [ %20, %19 ], [ %2, %3 ]
br label %8
8: ; preds = %5, %8
%9 = tail call i64 @write(i32 %0, i8* %6, i64 %7)
%10 = icmp slt i64 %9, 0
%11 = load i64, i64* @errno
%12 = load i64, i64* @EINTR
%13 = icmp eq i64 %11, %12
%14 = select i1 %10, i1 %13, i1 false
br i1 %14, label %8, label %15
15: ; preds = %8
%16 = icmp slt i64 %9, 1
br i1 %16, label %17, label %19
17: ; preds = %15
%18 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %23
19: ; preds = %15
%20 = sub i64 %7, %9
%21 = getelementptr inbounds i8, i8* %6, i64 %9
%22 = icmp eq i64 %20, 0
br i1 %22, label %23, label %5
23: ; preds = %19, %3, %17
%24 = phi i64 [ %7, %17 ], [ 0, %3 ], [ 0, %19 ]
%25 = sub i64 %2, %24
ret i64 %25
}