#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 */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
scalar_t__ write(int, char const *, size_t);
void write_all(int ofd, const char *buf, size_t nbyte, const char *filename) {
  size_t rem_bytes, bytes_written;
  ssize_t write_result;
  bytes_written = 0;
  rem_bytes = nbyte;
  while (rem_bytes) {
    write_result = write(ofd, &buf[bytes_written], rem_bytes);
    if (write_result < 0) {
      perror("ERROR: write ");
      fprintf(stderr, "ERROR: Couldn't write file \"%s\" \n", filename);
      exit(EXIT_FAILURE);
    }
    rem_bytes -= write_result;
    bytes_written += write_result;
  }
  return;
}


x86 assembly (clang, -O0):
<write_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11e8 <write_all+0xa8>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jge    11c8 <write_all+0x88>
lea    0xe64(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xe59(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x38(%rbp),%rcx
mov    -0x28(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x38(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1167 <write_all+0x27>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_all>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %rdx,%rdx
je     117c <write_all+0x3c>
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %edi,%r12d
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
lea    (%r15,%rbp,1),%rsi
mov    %r12d,%edi
mov    %rbx,%rdx
call   1030 <write@plt>
test   %rax,%rax
js     1185 <write_all+0x45>
add    %rax,%rbp
sub    %rax,%rbx
jne    1160 <write_all+0x20>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe74(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
lea    0xe6d(%rip),%rsi
mov    %r14,%rdx
call   1040 <fprintf@plt>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"ERROR: write \00"
@stderr = global i32 0
@.str.1 = [34 x i8] c"ERROR: Couldn't write file \22%s\22 \0A\00"
@EXIT_FAILURE = global i32 0
define void @write_all(i32 %0, i8* %1, i64 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i8* %3, i8** %8
store i64 0, i64* %10
%12 = load i64, i64* %7
store i64 %12, i64* %9
br label %13
13: ; preds = %32, %4
%14 = load i64, i64* %9
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %39
16: ; preds = %13
%17 = load i32, i32* %5
%18 = load i8*, i8** %6
%19 = load i64, i64* %10
%20 = getelementptr inbounds i8, i8* %18, i64 %19
%21 = load i64, i64* %9
%22 = call i64 @write(i32 %17, i8* %20, i64 %21)
store i64 %22, i64* %11
%23 = load i64, i64* %11
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %32
25: ; preds = %16
%26 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%27 = load i32, i32* @stderr
%28 = load i8*, i8** %8
%29 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %28)
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %16
%33 = load i64, i64* %11
%34 = load i64, i64* %9
%35 = sub i64 %34, %33
store i64 %35, i64* %9
%36 = load i64, i64* %11
%37 = load i64, i64* %10
%38 = add i64 %37, %36
store i64 %38, i64* %10
br label %13
39: ; preds = %13
ret void
}

LLVM IR (-O2):
@.str = [14 x i8] c"ERROR: write \00"
@stderr = global i32 0
@.str.1 = [34 x i8] c"ERROR: Couldn't write file \22%s\22 \0A\00"
@EXIT_FAILURE = global i32 0
define void @write_all(i32 %0, i8* nocapture %1, i64 %2, i8* %3) {
%5 = icmp eq i64 %2, 0
br i1 %5, label %22, label %6
6: ; preds = %4, %18
%7 = phi i64 [ %19, %18 ], [ %2, %4 ]
%8 = phi i64 [ %20, %18 ], [ 0, %4 ]
%9 = getelementptr inbounds i8, i8* %1, i64 %8
%10 = tail call i64 @write(i32 %0, i8* %9, i64 %7)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %6
%13 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %3)
%16 = load i32, i32* @EXIT_FAILURE
%17 = tail call i32 @exit(i32 %16)
unreachable
18: ; preds = %6
%19 = sub i64 %7, %10
%20 = add i64 %10, %8
%21 = icmp eq i64 %19, 0
br i1 %21, label %22, label %6
22: ; preds = %18, %4
ret void
}