#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 size_t ssize_t;

/* Variables and functions */
size_t write(int, int /*<<< orphan*/ *, size_t);
ssize_t write_fully(int fd, uint8_t *buf, ssize_t count) {
  ssize_t writtensofar = 0;
  ssize_t writtenthistime;
  while (writtensofar < count) {
    writtenthistime = write(fd, &buf[writtensofar], count - writtensofar);
    if (writtenthistime <= 0)
      return -1;
    writtensofar += writtenthistime;
  }
  return writtensofar;
}


x86 assembly (clang, -O0):
<write_fully>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1185 <write_fully+0x75>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x20(%rbp),%rdx
sub    -0x28(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
ja     1174 <write_fully+0x64>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    118d <write_fully+0x7d>
mov    -0x30(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    112b <write_fully+0x1b>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_fully>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdx,%rdx
je     1150 <write_fully+0x40>
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
lea    (%r15,%rbx,4),%rsi
mov    %r14,%rdx
sub    %rbx,%rdx
mov    %ebp,%edi
call   1030 <write@plt>
test   %rax,%rax
je     1154 <write_fully+0x44>
add    %rax,%rbx
cmp    %r14,%rbx
jb     1130 <write_fully+0x20>
jmp    115b <write_fully+0x4b>
xor    %ebx,%ebx
jmp    115b <write_fully+0x4b>
mov    $0xffffffffffffffff,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i64 @write_fully(i32 %0, i32* %1, i64 %2) {
%4 = alloca i64
%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 = %26, %3
%11 = load i64, i64* %8
%12 = load i64, i64* %7
%13 = icmp ult i64 %11, %12
br i1 %13, label %14, label %30
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 ule i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %14
store i64 -1, i64* %4
br label %32
26: ; preds = %14
%27 = load i64, i64* %9
%28 = load i64, i64* %8
%29 = add i64 %28, %27
store i64 %29, i64* %8
br label %10
30: ; preds = %10
%31 = load i64, i64* %8
store i64 %31, i64* %4
br label %32
32: ; preds = %30, %25
%33 = load i64, i64* %4
ret i64 %33
}

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