#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 */
scalar_t__ write(int, void const *, scalar_t__);
int writeall(int fd, const void *buf, ssize_t count) {
  ssize_t rv;
  while (count > 0) {
    rv = write(fd, buf, count);
    if (rv < 0)
      return rv;
    count -= rv;
    buf += rv;
  }
  return 0;
}


x86 assembly (clang, -O0):
<writeall>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jle    1179 <writeall+0x69>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    1159 <writeall+0x49>
mov    -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    1180 <writeall+0x70>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    1123 <writeall+0x13>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeall>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %r14d,%r14d
test   %rdx,%rdx
jle    1150 <writeall+0x40>
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %edi,%r15d
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
test   %rax,%rax
js     114d <writeall+0x3d>
add    %rax,%rbp
sub    %rax,%rbx
jg     1130 <writeall+0x20>
jmp    1150 <writeall+0x40>
mov    %eax,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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