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

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
int /*<<< orphan*/ STDOUT_FILENO;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int write(int /*<<< orphan*/, int /*<<< orphan*/ const *, size_t);
void write_all_stdout(const uint8_t *outbuf, size_t size, const char *argv0) {
  size_t written = 0;
  do {
    ssize_t w = write(STDOUT_FILENO, outbuf, size - written);
    if (w == -1) {
      if (errno == EAGAIN || errno == EINTR)
        continue;
      else {
        fprintf(stderr, "%s: could not write stdout: %s\n", argv0,
                strerror(errno));
        exit(1);
      }
    } else
      written += w;
  } while (written < size);
}


x86 assembly (clang, -O0):
<write_all_stdout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2e8d(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
sub    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11ff <write_all_stdout+0xbf>
mov    0x2e46(%rip),%rax
mov    (%rax),%rax
mov    0x2e24(%rip),%rcx
cmp    (%rcx),%rax
je     11b7 <write_all_stdout+0x77>
mov    0x2e2c(%rip),%rax
mov    (%rax),%rax
mov    0x2e1a(%rip),%rcx
cmp    (%rcx),%rax
jne    11bc <write_all_stdout+0x7c>
jmp    1210 <write_all_stdout+0xd0>
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2df9(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xe10(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
movslq -0x24(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    1210 <write_all_stdout+0xd0>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jb     115c <write_all_stdout+0x1c>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_all_stdout>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,(%rsp)
mov    %rsi,%r12
mov    %rdi,%r15
xor    %ebx,%ebx
mov    0x2e92(%rip),%r13
mov    0x2e6b(%rip),%rbp
mov    0x2e4c(%rip),%r14
jmp    117a <write_all_stdout+0x3a>
xchg   %ax,%ax
cltq
add    %rax,%rbx
cmp    %r12,%rbx
jae    11d3 <write_all_stdout+0x93>
mov    0x0(%r13),%edi
mov    %r12,%rdx
sub    %rbx,%rdx
mov    %r15,%rsi
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1170 <write_all_stdout+0x30>
mov    0x0(%rbp),%rdi
cmp    (%r14),%rdi
je     1175 <write_all_stdout+0x35>
mov    0x2e27(%rip),%rax
cmp    (%rax),%rdi
je     1175 <write_all_stdout+0x35>
mov    0x2e2b(%rip),%rax
mov    (%rax),%ebx
call   1060 <strerror@plt>
lea    0xe45(%rip),%rsi
mov    %ebx,%edi
mov    (%rsp),%rdx
mov    %rax,%rcx
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [32 x i8] c"%s: could not write stdout: %s\0A\00"
define void @write_all_stdout(i32* nocapture %0, i64 %1, i8* %2) {
br label %4
4: ; preds = %25, %3
%5 = phi i64 [ 0, %3 ], [ %26, %25 ]
%6 = load i32, i32* @STDOUT_FILENO
%7 = sub i64 %1, %5
%8 = tail call i32 @write(i32 %6, i32* %0, i64 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %22
10: ; preds = %4
%11 = load i64, i64* @errno
%12 = load i64, i64* @EAGAIN
%13 = icmp eq i64 %11, %12
%14 = load i64, i64* @EINTR
%15 = icmp eq i64 %11, %14
%16 = select i1 %13, i1 true, i1 %15
br i1 %16, label %25, label %17
17: ; preds = %10
%18 = load i32, i32* @stderr
%19 = tail call i8* @strerror(i64 %11)
%20 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %2, i8* %19)
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %4
%23 = sext i32 %8 to i64
%24 = add i64 %5, %23
br label %25
25: ; preds = %10, %22
%26 = phi i64 [ %24, %22 ], [ %5, %10 ]
%27 = icmp ult i64 %26, %1
br i1 %27, label %4, label %28
28: ; preds = %25
ret void
}