#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 */
int /*<<< orphan*/ assert(int);
size_t write(int, void const *, size_t);
ssize_t write_all(int fd, const void *buffer, size_t count) {
  size_t left_to_write = count;
  while (left_to_write > 0) {
    size_t written = write(fd, buffer, count);
    if (written == -1)
      return -1;
    else
      left_to_write -= written;
  }
  assert(left_to_write == 0);
  return count;
}


x86 assembly (clang, -O0):
<write_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jbe    1185 <write_all+0x65>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    1171 <write_all+0x51>
movl   $0xffffffff,-0x4(%rbp)
jmp    119e <write_all+0x7e>
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    113b <write_all+0x1b>
cmpq   $0x0,-0x20(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x18(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
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
mov    %rdx,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
test   %rbx,%rbx
je     1163 <write_all+0x43>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1030 <write@plt>
sub    %rax,%rbx
cmp    $0xffffffffffffffff,%rax
jne    1140 <write_all+0x20>
mov    $0xffffffff,%r14d
jmp    116d <write_all+0x4d>
mov    $0x1,%edi
call   1040 <assert@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @write_all(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i64, i64* %7
store i64 %10, i64* %8
br label %11
11: ; preds = %26, %3
%12 = load i64, i64* %8
%13 = icmp ugt i64 %12, 0
br i1 %13, label %14, label %27
14: ; preds = %11
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = load i64, i64* %7
%18 = call i64 @write(i32 %15, i8* %16, i64 %17)
store i64 %18, i64* %9
%19 = load i64, i64* %9
%20 = icmp eq i64 %19, -1
br i1 %20, label %21, label %22
21: ; preds = %14
store i32 -1, i32* %4
br label %34
22: ; preds = %14
%23 = load i64, i64* %9
%24 = load i64, i64* %8
%25 = sub i64 %24, %23
store i64 %25, i64* %8
br label %26
26: ; preds = %22
br label %11
27: ; preds = %11
%28 = load i64, i64* %8
%29 = icmp eq i64 %28, 0
%30 = zext i1 %29 to i32
%31 = call i32 @assert(i32 %30)
%32 = load i64, i64* %7
%33 = trunc i64 %32 to i32
store i32 %33, i32* %4
br label %34
34: ; preds = %27, %21
%35 = load i32, i32* %4
ret i32 %35
}

LLVM IR (-O2):
define i32 @write_all(i32 %0, i8* nocapture %1, i64 %2) {
br label %4
4: ; preds = %7, %3
%5 = phi i64 [ %2, %3 ], [ %10, %7 ]
%6 = icmp eq i64 %5, 0
br i1 %6, label %11, label %7
7: ; preds = %4
%8 = tail call i64 @write(i32 %0, i8* %1, i64 %2)
%9 = icmp eq i64 %8, -1
%10 = sub i64 %5, %8
br i1 %9, label %14, label %4
11: ; preds = %4
%12 = tail call i32 @assert(i32 1)
%13 = trunc i64 %2 to i32
br label %14
14: ; preds = %7, %11
%15 = phi i32 [ %13, %11 ], [ -1, %7 ]
ret i32 %15
}