#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 */
int /*<<< orphan*/ perror(char *);
scalar_t__ write(int const, int /*<<< orphan*/ const *, size_t);
int write_all(const int fd, const uint8_t *vec, size_t count) {
  ssize_t rc = 0;
  while (count > 0) {
    rc = write(fd, vec, count);
    if (rc < 0) {
      perror("write");
      return -1;
    }
    count -= rc;
    vec += rc;
  }
  return 0;
}


x86 assembly (clang, -O0):
<write_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    11a1 <write_all+0x81>
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    117d <write_all+0x5d>
lea    0xe94(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a8 <write_all+0x88>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
shl    $0x2,%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    113b <write_all+0x1b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_all>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %r14d,%r14d
test   %rdx,%rdx
je     115d <write_all+0x3d>
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     116b <write_all+0x4b>
lea    0x0(%rbp,%rax,4),%rbp
sub    %rax,%rbx
jne    1140 <write_all+0x20>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe8e(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%r14d
jmp    115d <write_all+0x3d>

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

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