#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, unsigned char const *, size_t);
ssize_t fd_write_n(int fd, const void *buf, size_t n) {
  const unsigned char *p;
  size_t nleft;
  ssize_t nwritten;
  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):
<fd_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,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jbe    11aa <fd_write_n+0x9a>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    1185 <fd_write_n+0x75>
mov    0x2e7f(%rip),%rax
mov    (%rax),%rax
mov    0x2e6d(%rip),%rcx
cmp    (%rcx),%rax
jne    1179 <fd_write_n+0x69>
jmp    1133 <fd_write_n+0x23>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b1 <fd_write_n+0xa1>
movslq -0x2c(%rbp),%rcx
mov    -0x28(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    1133 <fd_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):
<fd_write_n>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
test   %rdx,%rdx
je     1170 <fd_write_n+0x60>
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   %eax,%eax
jns    115e <fd_write_n+0x4e>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     1140 <fd_write_n+0x30>
jmp    116a <fd_write_n+0x5a>
mov    %eax,%eax
add    %rax,%rbx
sub    %rax,%rbp
jne    1140 <fd_write_n+0x30>
jmp    1170 <fd_write_n+0x60>
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 @fd_write_n(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i8*, i8** %6
store i8* %11, i8** %8
%12 = load i64, i64* %7
store i64 %12, i64* %9
br label %13
13: ; preds = %28, %26, %3
%14 = load i64, i64* %9
%15 = icmp ugt i64 %14, 0
br i1 %15, label %16, label %37
16: ; preds = %13
%17 = load i32, i32* %5
%18 = load i8*, i8** %8
%19 = load i64, i64* %9
%20 = call i32 @write(i32 %17, i8* %18, i64 %19)
store i32 %20, i32* %10
%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 %40
28: ; preds = %16
%29 = load i32, i32* %10
%30 = sext i32 %29 to i64
%31 = load i64, i64* %9
%32 = sub i64 %31, %30
store i64 %32, i64* %9
%33 = load i32, i32* %10
%34 = load i8*, i8** %8
%35 = sext i32 %33 to i64
%36 = getelementptr inbounds i8, i8* %34, i64 %35
store i8* %36, i8** %8
br label %13
37: ; preds = %13
%38 = load i64, i64* %7
%39 = trunc i64 %38 to i32
store i32 %39, i32* %4
br label %40
40: ; preds = %37, %27
%41 = load i32, i32* %4
ret i32 %41
}

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