#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__ EAGAIN;
scalar_t__ EWOULDBLOCK;
scalar_t__ errno;
int write(int, char *, size_t);
ssize_t write_n(int fd, void *buffer, size_t count) {
  char *ptr = buffer;
  size_t left = count;
  while (left) {
    ssize_t written = write(fd, ptr, left);
    if (written == 0)
      return 0;
    else if (written == -1) {
      if (errno == EAGAIN || errno == EWOULDBLOCK)
        continue;
      return -1;
    }
    ptr += written;
    left -= written;
  }
  return count;
}


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,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11e0 <write_n+0xd0>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    1167 <write_n+0x57>
movl   $0x0,-0x4(%rbp)
jmp    11e7 <write_n+0xd7>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11b6 <write_n+0xa6>
mov    0x2e68(%rip),%rax
mov    (%rax),%rax
mov    0x2e46(%rip),%rcx
cmp    (%rcx),%rax
je     11a5 <write_n+0x95>
mov    0x2e4e(%rip),%rax
mov    (%rax),%rax
mov    0x2e3c(%rip),%rcx
cmp    (%rcx),%rax
jne    11aa <write_n+0x9a>
jmp    1133 <write_n+0x23>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e7 <write_n+0xd7>
jmp    11bb <write_n+0xab>
mov    -0x2c(%rbp),%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
movslq -0x2c(%rbp),%rcx
mov    -0x28(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x28(%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     1185 <write_n+0x75>
mov    %rsi,%rbx
mov    %edi,%r15d
mov    0x2eb0(%rip),%r12
mov    0x2e91(%rip),%r13
mov    %r14,%rbp
jmp    1151 <write_n+0x41>
nopl   0x0(%rax)
test   %eax,%eax
je     1182 <write_n+0x72>
cltq
add    %rax,%rbx
sub    %rax,%rbp
test   %rbp,%rbp
je     1185 <write_n+0x75>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1140 <write_n+0x30>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     114c <write_n+0x3c>
mov    0x2e63(%rip),%rcx
cmp    (%rcx),%rax
je     114c <write_n+0x3c>
mov    $0xffffffff,%r14d
jmp    1185 <write_n+0x75>
mov    %eax,%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
@EAGAIN = global i64 0
@EWOULDBLOCK = 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 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 = %38, %35, %3
%14 = load i64, i64* %9
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %47
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 = load i32, i32* %10
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %16
store i32 0, i32* %4
br label %50
24: ; preds = %16
%25 = load i32, i32* %10
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %37
27: ; preds = %24
%28 = load i64, i64* @errno
%29 = load i64, i64* @EAGAIN
%30 = icmp eq i64 %28, %29
br i1 %30, label %35, label %31
31: ; preds = %27
%32 = load i64, i64* @errno
%33 = load i64, i64* @EWOULDBLOCK
%34 = icmp eq i64 %32, %33
br i1 %34, label %35, label %36
35: ; preds = %31, %27
br label %13
36: ; preds = %31
store i32 -1, i32* %4
br label %50
37: ; preds = %24
br label %38
38: ; preds = %37
%39 = load i32, i32* %10
%40 = load i8*, i8** %8
%41 = sext i32 %39 to i64
%42 = getelementptr inbounds i8, i8* %40, i64 %41
store i8* %42, i8** %8
%43 = load i32, i32* %10
%44 = sext i32 %43 to i64
%45 = load i64, i64* %9
%46 = sub i64 %45, %44
store i64 %46, i64* %9
br label %13
47: ; preds = %13
%48 = load i64, i64* %7
%49 = trunc i64 %48 to i32
store i32 %49, i32* %4
br label %50
50: ; preds = %47, %36, %23
%51 = load i32, i32* %4
ret i32 %51
}

LLVM IR (-O2):
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
define i32 @write_n(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %24, label %5
5: ; preds = %3, %20
%6 = phi i64 [ %22, %20 ], [ %2, %3 ]
%7 = phi i8* [ %21, %20 ], [ %1, %3 ]
%8 = tail call i32 @write(i32 %0, i8* %7, i64 %6)
switch i32 %8, label %16 [
i32 0, label %26
i32 -1, label %9
]
9: ; preds = %5
%10 = load i64, i64* @errno
%11 = load i64, i64* @EAGAIN
%12 = icmp eq i64 %10, %11
%13 = load i64, i64* @EWOULDBLOCK
%14 = icmp eq i64 %10, %13
%15 = select i1 %12, i1 true, i1 %14
br i1 %15, label %20, label %26
16: ; preds = %5
%17 = sext i32 %8 to i64
%18 = getelementptr inbounds i8, i8* %7, i64 %17
%19 = sub i64 %6, %17
br label %20
20: ; preds = %9, %16
%21 = phi i8* [ %18, %16 ], [ %7, %9 ]
%22 = phi i64 [ %19, %16 ], [ %6, %9 ]
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %5
24: ; preds = %20, %3
%25 = trunc i64 %2 to i32
br label %26
26: ; preds = %9, %5, %24
%27 = phi i32 [ %25, %24 ], [ %8, %5 ], [ -1, %9 ]
ret i32 %27
}