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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int write(int, char const *, size_t);
ssize_t mb_writeall(int fd, const char *buf, size_t size,
                    bool continue_on_error) {
  size_t sz = size;
  ssize_t ret;
  for (;;) {
    if ((ret = write(fd, buf, sz)) == -1) {
      perror("mb_writeall:write");
      if (!continue_on_error)
        exit(EXIT_FAILURE);
    }
    if (ret < sz) {
      sz -= ret;
      buf += ret;
    } else {
      break;
    }
  }
  return size;
}


x86 assembly (clang, -O0):
<mb_writeall>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
jne    1193 <mb_writeall+0x63>
lea    0xe8f(%rip),%rdi
call   1040 <perror@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    118e <mb_writeall+0x5e>
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
jmp    1193 <mb_writeall+0x63>
movslq -0x2c(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    11c6 <mb_writeall+0x96>
movslq -0x2c(%rbp),%rcx
mov    -0x28(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    11cb <mb_writeall+0x9b>
jmp    11d0 <mb_writeall+0xa0>
jmp    114e <mb_writeall+0x1e>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mb_writeall>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %rsi,%rbp
mov    %edi,%r13d
mov    %rdx,%rbx
nopw   0x0(%rax,%rax,1)
mov    %r13d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
je     1173 <mb_writeall+0x43>
movslq %r12d,%rax
sub    %rax,%rbx
jbe    1192 <mb_writeall+0x62>
add    %rax,%rbp
jmp    1150 <mb_writeall+0x20>
lea    0xe86(%rip),%rdi
call   1040 <perror@plt>
test   %r14d,%r14d
jne    1166 <mb_writeall+0x36>
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"mb_writeall:write\00"
@EXIT_FAILURE = global i32 0
define i32 @mb_writeall(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i64
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 %3, i32* %8
%11 = load i64, i64* %7
store i64 %11, i64* %9
br label %12
12: ; preds = %41, %4
%13 = load i32, i32* %5
%14 = load i8*, i8** %6
%15 = load i64, i64* %9
%16 = call i32 @write(i32 %13, i8* %14, i64 %15)
store i32 %16, i32* %10
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %26
18: ; preds = %12
%19 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%20 = load i32, i32* %8
%21 = icmp ne i32 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %18
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %18
br label %26
26: ; preds = %25, %12
%27 = load i32, i32* %10
%28 = sext i32 %27 to i64
%29 = load i64, i64* %9
%30 = icmp ult i64 %28, %29
br i1 %30, label %31, label %40
31: ; preds = %26
%32 = load i32, i32* %10
%33 = sext i32 %32 to i64
%34 = load i64, i64* %9
%35 = sub i64 %34, %33
store i64 %35, i64* %9
%36 = load i32, i32* %10
%37 = load i8*, i8** %6
%38 = sext i32 %36 to i64
%39 = getelementptr inbounds i8, i8* %37, i64 %38
store i8* %39, i8** %6
br label %41
40: ; preds = %26
br label %42
41: ; preds = %31
br label %12
42: ; preds = %40
%43 = load i64, i64* %7
%44 = trunc i64 %43 to i32
ret i32 %44
}

LLVM IR (-O2):
@.str = [18 x i8] c"mb_writeall:write\00"
@EXIT_FAILURE = global i32 0
define i32 @mb_writeall(i32 %0, i8* nocapture %1, i64 %2, i32 %3) {
%5 = icmp eq i32 %3, 0
br label %6
6: ; preds = %19, %4
%7 = phi i64 [ %2, %4 ], [ %20, %19 ]
%8 = phi i8* [ %1, %4 ], [ %21, %19 ]
%9 = tail call i32 @write(i32 %0, i8* %8, i64 %7)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %16
11: ; preds = %6
%12 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br i1 %5, label %13, label %16
13: ; preds = %11
%14 = load i32, i32* @EXIT_FAILURE
%15 = tail call i32 @exit(i32 %14)
unreachable
16: ; preds = %11, %6
%17 = sext i32 %9 to i64
%18 = icmp ugt i64 %7, %17
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = sub i64 %7, %17
%21 = getelementptr inbounds i8, i8* %8, i64 %17
br label %6
22: ; preds = %16
%23 = trunc i64 %2 to i32
ret i32 %23
}