#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 */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, scalar_t__, char *);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int write(int, char *, int);
void do_write(int fd, void *data, int len) {
  int done, left = len;
  char *p = data;
  while (left != 0) {
    if ((done = write(fd, p, left)) == -1) {
      if (errno == EINTR)
        continue;
      fprintf(stderr, "Error writing output image: %d (%s).\n", errno,
              strerror(errno));
      exit(1);
    }
    left -= done;
    p += done;
  }
}


x86 assembly (clang, -O0):
<do_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     120e <do_write+0xce>
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    11ed <do_write+0xad>
mov    0x2e4c(%rip),%rax
mov    (%rax),%rax
mov    0x2e3a(%rip),%rcx
cmp    (%rcx),%rax
jne    11a4 <do_write+0x64>
jmp    1160 <do_write+0x20>
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2e21(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e13(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xe22(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x18(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    1160 <do_write+0x20>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
je     1198 <do_write+0x58>
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %edi,%r14d
mov    0x2e79(%rip),%r12
mov    0x2e6a(%rip),%r13
jmp    117a <do_write+0x3a>
nopl   0x0(%rax,%rax,1)
mov    (%r12),%rbp
cmp    0x0(%r13),%rbp
jne    11a7 <do_write+0x67>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     1170 <do_write+0x30>
movslq %eax,%rcx
add    %rcx,%rbx
sub    %eax,%r15d
jne    117a <do_write+0x3a>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e32(%rip),%rax
mov    (%rax),%ebx
mov    %rbp,%rdi
call   1060 <strerror@plt>
lea    0xe41(%rip),%rsi
mov    %ebx,%edi
mov    %rbp,%rdx
mov    %rax,%rcx
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

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

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [38 x i8] c"Error writing output image: %d (%s).\0A\00"
define void @do_write(i32 %0, i8* nocapture %1, i32 %2) {
%4 = icmp eq i32 %2, 0
br i1 %4, label %25, label %5
5: ; preds = %3, %20
%6 = phi i8* [ %23, %20 ], [ %1, %3 ]
%7 = phi i32 [ %21, %20 ], [ %2, %3 ]
br label %8
8: ; preds = %5, %11
%9 = tail call i32 @write(i32 %0, i8* %6, i32 %7)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %20
11: ; preds = %8
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINTR
%14 = icmp eq i64 %12, %13
br i1 %14, label %8, label %15
15: ; preds = %11
%16 = load i32, i32* @stderr
%17 = tail call i8* @strerror(i64 %12)
%18 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i64 %12, i8* %17)
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %8
%21 = sub nsw i32 %7, %9
%22 = sext i32 %9 to i64
%23 = getelementptr inbounds i8, i8* %6, i64 %22
%24 = icmp eq i32 %21, 0
br i1 %24, label %25, label %5
25: ; preds = %20, %3
ret void
}