#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 /*<<< orphan*/ uint8_t;
typedef scalar_t__ ssize_t;

/* Variables and functions */
scalar_t__ write(int, int /*<<< orphan*/ const *, size_t);
bool writeUntilDone(int fd, const uint8_t *buffer, size_t bytesToWrite) {
  size_t curWritten = 0;
  while (curWritten < bytesToWrite) {
    ssize_t writeResult =
        write(fd, buffer + curWritten, bytesToWrite - curWritten);
    if (writeResult < 0) {
      return (false);
    } else if (writeResult == 0) {
      continue;
    }
    curWritten += (size_t)writeResult;
  }
  return (true);
}


x86 assembly (clang, -O0):
<writeUntilDone>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1199 <writeUntilDone+0x89>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x18(%rbp),%rdx
sub    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    1173 <writeUntilDone+0x63>
movl   $0x0,-0x4(%rbp)
jmp    11a0 <writeUntilDone+0x90>
cmpq   $0x0,-0x28(%rbp)
jne    1183 <writeUntilDone+0x73>
jmp    112b <writeUntilDone+0x1b>
jmp    1188 <writeUntilDone+0x78>
mov    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    112b <writeUntilDone+0x1b>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeUntilDone>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    $0x1,%r14d
test   %rdx,%rdx
je     115d <writeUntilDone+0x4d>
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %edi,%r12d
xor    %ebp,%ebp
xchg   %ax,%ax
lea    (%r15,%rbp,4),%rsi
mov    %rbx,%rdx
sub    %rbp,%rdx
mov    %r12d,%edi
call   1030 <write@plt>
mov    $0x0,%ecx
test   %rax,%rax
cmovns %rax,%rcx
js     115a <writeUntilDone+0x4a>
add    %rcx,%rbp
cmp    %rbx,%rbp
jb     1130 <writeUntilDone+0x20>
jmp    115d <writeUntilDone+0x4d>
xor    %r14d,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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