#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__ int32_t;

/* Variables and functions */
scalar_t__ write(scalar_t__, char const *, scalar_t__);
int write_file(int32_t fd, const char *buf, int32_t count) {
  int32_t write_size = 0;
  while (count > 0) {
    write_size = write(fd, buf, count);
    if (write_size < 0) {
      return -1;
    }
    buf += write_size;
    count -= write_size;
  }
  return 0;
}


x86 assembly (clang, -O0):
<write_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
jle    1183 <write_file+0x73>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    1163 <write_file+0x53>
movl   $0xffffffff,-0x4(%rbp)
jmp    118a <write_file+0x7a>
mov    -0x28(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    112c <write_file+0x1c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

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

LLVM IR (-O0):
define i32 @write_file(i64 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
store i64 %0, i64* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i64 0, i64* %8
br label %9
9: ; preds = %20, %3
%10 = load i64, i64* %7
%11 = icmp sgt i64 %10, 0
br i1 %11, label %12, label %27
12: ; preds = %9
%13 = load i64, i64* %5
%14 = load i8*, i8** %6
%15 = load i64, i64* %7
%16 = call i64 @write(i64 %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 %20
19: ; preds = %12
store i32 -1, i32* %4
br label %28
20: ; preds = %12
%21 = load i64, i64* %8
%22 = load i8*, i8** %6
%23 = getelementptr inbounds i8, i8* %22, i64 %21
store i8* %23, i8** %6
%24 = load i64, i64* %8
%25 = load i64, i64* %7
%26 = sub nsw i64 %25, %24
store i64 %26, i64* %7
br label %9
27: ; preds = %9
store i32 0, i32* %4
br label %28
28: ; preds = %27, %19
%29 = load i32, i32* %4
ret i32 %29
}

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