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


x86 assembly (clang, -O0):
<write_n>:
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    11a7 <write_n+0x97>
mov    -0x8(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jge    1187 <write_n+0x77>
mov    0x2e7d(%rip),%rax
mov    (%rax),%rax
mov    0x2e6b(%rip),%rcx
cmp    (%rcx),%rax
jne    117b <write_n+0x6b>
jmp    1133 <write_n+0x23>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ae <write_n+0x9e>
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    1133 <write_n+0x23>
mov    -0x18(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_n>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
test   %rdx,%rdx
je     116f <write_n+0x5f>
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
jns    115f <write_n+0x4f>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     1140 <write_n+0x30>
jmp    1169 <write_n+0x59>
add    %rax,%rbx
sub    %rax,%rbp
jne    1140 <write_n+0x30>
jmp    116f <write_n+0x5f>
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 @write_n(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 i8*, i8** %6
store i8* %11, i8** %10
%12 = load i64, i64* %7
store i64 %12, i64* %8
br label %13
13: ; preds = %28, %26, %3
%14 = load i64, i64* %8
%15 = icmp ugt i64 %14, 0
br i1 %15, label %16, label %35
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 slt i64 %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 %38
28: ; preds = %16
%29 = load i64, i64* %9
%30 = load i64, i64* %8
%31 = sub i64 %30, %29
store i64 %31, i64* %8
%32 = load i64, i64* %9
%33 = load i8*, i8** %10
%34 = getelementptr inbounds i8, i8* %33, i64 %32
store i8* %34, i8** %10
br label %13
35: ; preds = %13
%36 = load i64, i64* %7
%37 = trunc i64 %36 to i32
store i32 %37, i32* %4
br label %38
38: ; preds = %35, %27
%39 = load i32, i32* %4
ret i32 %39
}

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