#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 */

/* Variables and functions */
int /*<<< orphan*/ VINK_set_error(char *, size_t, char const *, ...);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int write(int, char *, size_t);
int write_all(int fd, void *buf, size_t total, const char *path) {
  char *cbuf = buf;
  size_t offset = 0;
  int ret;
  while (offset < total) {
    ret = write(fd, cbuf, total - offset);
    if (ret == -1) {
      VINK_set_error("Error writing %zu bytes to '%s': %s", total - offset,
                     path, strerror(errno));
      return -1;
    }
    if (ret == 0) {
      VINK_set_error("Error writing %zu bytes to '%s': write returned 0",
                     total - offset, path);
      return -1;
    }
    offset += ret;
  }
  return 0;
}


x86 assembly (clang, -O0):
<write_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    120d <write_all+0xdd>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x30(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    11cc <write_all+0x9c>
mov    -0x18(%rbp),%rax
sub    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x48(%rbp),%rsi
mov    -0x40(%rbp),%rdx
mov    %eax,%ecx
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1040 <VINK_set_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1214 <write_all+0xe4>
cmpl   $0x0,-0x34(%rbp)
jne    11fc <write_all+0xcc>
mov    -0x18(%rbp),%rsi
sub    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xe3b(%rip),%rdi
mov    $0x0,%al
call   1040 <VINK_set_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1214 <write_all+0xe4>
movslq -0x34(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1157 <write_all+0x27>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_all>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     1177 <write_all+0x47>
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
mov    %edi,%r13d
xor    %ebp,%ebp
xchg   %ax,%ax
mov    %r15,%rbx
sub    %rbp,%rbx
mov    %r13d,%edi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
test   %eax,%eax
je     117b <write_all+0x4b>
cmp    $0xffffffff,%eax
je     1191 <write_all+0x61>
cltq
add    %rax,%rbp
cmp    %r15,%rbp
jb     1150 <write_all+0x20>
xor    %eax,%eax
jmp    11ba <write_all+0x8a>
lea    0xea2(%rip),%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <VINK_set_error@plt>
jmp    11b5 <write_all+0x85>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe5a(%rip),%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1040 <VINK_set_error@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [36 x i8] c"Error writing %zu bytes to '%s': %s\00"
@errno = global i32 0
@.str.1 = [50 x i8] c"Error writing %zu bytes to '%s': write returned 0\00"
define i32 @write_all(i32 %0, i8* %1, i64 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i8* %3, i8** %9
%13 = load i8*, i8** %7
store i8* %13, i8** %10
store i64 0, i64* %11
br label %14
14: ; preds = %44, %4
%15 = load i64, i64* %11
%16 = load i64, i64* %8
%17 = icmp ult i64 %15, %16
br i1 %17, label %18, label %49
18: ; preds = %14
%19 = load i32, i32* %6
%20 = load i8*, i8** %10
%21 = load i64, i64* %8
%22 = load i64, i64* %11
%23 = sub i64 %21, %22
%24 = call i32 @write(i32 %19, i8* %20, i64 %23)
store i32 %24, i32* %12
%25 = load i32, i32* %12
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %35
27: ; preds = %18
%28 = load i64, i64* %8
%29 = load i64, i64* %11
%30 = sub i64 %28, %29
%31 = load i8*, i8** %9
%32 = load i32, i32* @errno
%33 = call i32 @strerror(i32 %32)
%34 = call i32 (i8*, i64, i8*, ...) @VINK_set_error(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i64 %30, i8* %31, i32 %33)
store i32 -1, i32* %5
br label %50
35: ; preds = %18
%36 = load i32, i32* %12
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %44
38: ; preds = %35
%39 = load i64, i64* %8
%40 = load i64, i64* %11
%41 = sub i64 %39, %40
%42 = load i8*, i8** %9
%43 = call i32 (i8*, i64, i8*, ...) @VINK_set_error(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), i64 %41, i8* %42)
store i32 -1, i32* %5
br label %50
44: ; preds = %35
%45 = load i32, i32* %12
%46 = sext i32 %45 to i64
%47 = load i64, i64* %11
%48 = add i64 %47, %46
store i64 %48, i64* %11
br label %14
49: ; preds = %14
store i32 0, i32* %5
br label %50
50: ; preds = %49, %38, %27
%51 = load i32, i32* %5
ret i32 %51
}

LLVM IR (-O2):
@.str = [36 x i8] c"Error writing %zu bytes to '%s': %s\00"
@errno = global i32 0
@.str.1 = [50 x i8] c"Error writing %zu bytes to '%s': write returned 0\00"
define i32 @write_all(i32 %0, i8* nocapture %1, i64 %2, i8* %3) {
%5 = icmp eq i64 %2, 0
br i1 %5, label %20, label %6
6: ; preds = %4, %16
%7 = phi i64 [ %18, %16 ], [ 0, %4 ]
%8 = sub i64 %2, %7
%9 = tail call i32 @write(i32 %0, i8* %1, i64 %8)
switch i32 %9, label %16 [
i32 -1, label %10
i32 0, label %14
]
10: ; preds = %6
%11 = load i32, i32* @errno
%12 = tail call i32 @strerror(i32 %11)
%13 = tail call i32 (i8*, i64, i8*, ...) @VINK_set_error(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i64 %8, i8* %3, i32 %12)
br label %20
14: ; preds = %6
%15 = tail call i32 (i8*, i64, i8*, ...) @VINK_set_error(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), i64 %8, i8* %3)
br label %20
16: ; preds = %6
%17 = sext i32 %9 to i64
%18 = add i64 %7, %17
%19 = icmp ult i64 %18, %2
br i1 %19, label %6, label %20
20: ; preds = %16, %4, %14, %10
%21 = phi i32 [ -1, %10 ], [ -1, %14 ], [ 0, %4 ], [ 0, %16 ]
ret i32 %21
}