#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, char *, size_t);
ssize_t othello_write_all(int fd, void *buf, size_t count) {
  ssize_t bytes_write;
  char *cursor;
  bytes_write = 0;
  cursor = buf;
  while (count > 0 && (bytes_write = write(fd, cursor, count)) > 0) {
    count -= bytes_write;
    cursor += bytes_write;
  }
  return bytes_write;
}


x86 assembly (clang, -O0):
<othello_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    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x18(%rbp)
mov    %al,-0x29(%rbp)
jbe    1161 <othello_write_all+0x51>
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
setg   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    1171 <othello_write_all+0x61>
jmp    1191 <othello_write_all+0x81>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    1133 <othello_write_all+0x23>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<othello_write_all>:
push   %rbp
push   %r14
push   %rbx
test   %rdx,%rdx
je     114f <othello_write_all+0x3f>
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
test   %rax,%rax
jle    1151 <othello_write_all+0x41>
sub    %rax,%rbx
add    %rax,%r14
test   %rbx,%rbx
jne    1130 <othello_write_all+0x20>
jmp    1151 <othello_write_all+0x41>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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