#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*/ perror(char *);
scalar_t__ write(int const, char const *, scalar_t__ const);
ssize_t write_all(const int fd, const char *buf, const ssize_t bytes_to_write) {
  ssize_t total_bytes = 0;
  ssize_t nw = 0;
  do {
    nw = write(fd, buf + total_bytes, bytes_to_write - total_bytes);
    if (nw < 0)
      perror("Could not write buffer");
    else
      total_bytes += nw;
  } while ((nw >= 0) && (total_bytes < bytes_to_write));
  return total_bytes;
}


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)
movq   $0x0,-0x28(%rbp)
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)
jge    117b <write_all+0x5b>
lea    0xe8f(%rip),%rdi
call   1040 <perror@plt>
jmp    1187 <write_all+0x67>
mov    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    118c <write_all+0x6c>
xor    %eax,%eax
cmpq   $0x0,-0x28(%rbp)
mov    %al,-0x29(%rbp)
jl     11aa <write_all+0x8a>
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
setl   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    1143 <write_all+0x23>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_all>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    (%r15,%rbx,1),%rsi
mov    %r14,%rdx
sub    %rbx,%rdx
mov    %ebp,%edi
call   1030 <write@plt>
test   %rax,%rax
js     116c <write_all+0x4c>
add    %rax,%rbx
cmp    %r14,%rbx
jl     1140 <write_all+0x20>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe8d(%rip),%rdi
call   1040 <perror@plt>
jmp    115e <write_all+0x3e>

LLVM IR (-O0):
@.str = [23 x i8] c"Could not write buffer\00"
define i64 @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
store i64 0, i64* %8
br label %9
9: ; preds = %34, %3
%10 = load i32, i32* %4
%11 = load i8*, i8** %5
%12 = load i64, i64* %7
%13 = getelementptr inbounds i8, i8* %11, i64 %12
%14 = load i64, i64* %6
%15 = load i64, i64* %7
%16 = sub nsw i64 %14, %15
%17 = call i64 @write(i32 %10, i8* %13, i64 %16)
store i64 %17, i64* %8
%18 = load i64, i64* %8
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %9
%21 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %26
22: ; preds = %9
%23 = load i64, i64* %8
%24 = load i64, i64* %7
%25 = add nsw i64 %24, %23
store i64 %25, i64* %7
br label %26
26: ; preds = %22, %20
br label %27
27: ; preds = %26
%28 = load i64, i64* %8
%29 = icmp sge i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %27
%31 = load i64, i64* %7
%32 = load i64, i64* %6
%33 = icmp slt i64 %31, %32
br label %34
34: ; preds = %30, %27
%35 = phi i1 [ false, %27 ], [ %33, %30 ]
br i1 %35, label %9, label %36
36: ; preds = %34
%37 = load i64, i64* %7
ret i64 %37
}

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