#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 */

/* Variables and functions */
int /*<<< orphan*/ error(char *);
size_t write(int, char *, size_t);
void write_all(int fd, char *buffer, size_t bytes) {
  size_t n = 0;
  while (n < bytes) {
    size_t next = write(fd, buffer + n, bytes - n);
    if (next <= 0)
      error("write failed");
    n += next;
  }
}


x86 assembly (clang, -O0):
<write_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    118d <write_all+0x6d>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
ja     117c <write_all+0x5c>
lea    0xe89(%rip),%rdi
call   1040 <error@plt>
mov    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    113b <write_all+0x1b>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_all>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     117c <write_all+0x5c>
mov    %rdx,%r13
mov    %rsi,%r14
mov    %edi,%r12d
xor    %ebx,%ebx
lea    0xebe(%rip),%r15
jmp    1158 <write_all+0x38>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    %rbp,%rbx
cmp    %r13,%rbx
jae    117c <write_all+0x5c>
lea    (%r14,%rbx,1),%rsi
mov    %r13,%rdx
sub    %rbx,%rdx
mov    %r12d,%edi
call   1030 <write@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    1150 <write_all+0x30>
mov    %r15,%rdi
call   1040 <error@plt>
jmp    1150 <write_all+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"write failed\00"
define void @write_all(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i64 0, i64* %7
br label %9
9: ; preds = %26, %3
%10 = load i64, i64* %7
%11 = load i64, i64* %6
%12 = icmp ult i64 %10, %11
br i1 %12, label %13, label %30
13: ; preds = %9
%14 = load i32, i32* %4
%15 = load i8*, i8** %5
%16 = load i64, i64* %7
%17 = getelementptr inbounds i8, i8* %15, i64 %16
%18 = load i64, i64* %6
%19 = load i64, i64* %7
%20 = sub i64 %18, %19
%21 = call i64 @write(i32 %14, i8* %17, i64 %20)
store i64 %21, i64* %8
%22 = load i64, i64* %8
%23 = icmp ule i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %13
%25 = call i32 @error(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %26
26: ; preds = %24, %13
%27 = load i64, i64* %8
%28 = load i64, i64* %7
%29 = add i64 %28, %27
store i64 %29, i64* %7
br label %9
30: ; preds = %9
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"write failed\00"
define void @write_all(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %16, label %5
5: ; preds = %3, %13
%6 = phi i64 [ %14, %13 ], [ 0, %3 ]
%7 = getelementptr inbounds i8, i8* %1, i64 %6
%8 = sub i64 %2, %6
%9 = tail call i64 @write(i32 %0, i8* %7, i64 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %5
%12 = tail call i32 @error(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %5
%14 = add i64 %9, %6
%15 = icmp ult i64 %14, %2
br i1 %15, label %5, label %16
16: ; preds = %13, %3
ret void
}