#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*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int write(int, char *, int);
void doWrite(int outputFd, char *buf, int leng) {
  ssize_t dataWrite = write(outputFd, buf, leng);
  ssize_t dataMonitor = 0;
  if (dataWrite == -1) {
    perror("Output File");
    close(outputFd);
    exit(EXIT_FAILURE);
  } else if (dataWrite != leng) {
    dataMonitor += dataWrite;
    while (leng != dataMonitor) {
      dataMonitor += write(outputFd, buf + dataMonitor, leng - dataMonitor);
      if (dataWrite == -1) {
        perror("Output File");
        close(outputFd);
        exit(EXIT_FAILURE);
      }
    }
  }
}


x86 assembly (clang, -O0):
<doWrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1197 <doWrite+0x57>
lea    0xe84(%rip),%rdi
call   1050 <perror@plt>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
mov    0x2e50(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     120d <doWrite+0xcd>
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     1208 <doWrite+0xc8>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x1c(%rbp),%edx
call   1030 <write@plt>
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1203 <doWrite+0xc3>
lea    0xe18(%rip),%rdi
call   1050 <perror@plt>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
jmp    11ac <doWrite+0x6c>
jmp    120d <doWrite+0xcd>
jmp    1212 <doWrite+0xd2>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<doWrite>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r15
mov    %edi,%r14d
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     1188 <doWrite+0x48>
nopl   0x0(%rax)
cmp    %ebp,%eax
je     117d <doWrite+0x3d>
movslq %eax,%rbx
lea    (%r15,%rbx,1),%rsi
mov    %ebp,%edx
sub    %ebx,%edx
mov    %r14d,%edi
call   1030 <write@plt>
add    %ebx,%eax
cmp    %ebp,%eax
jne    1164 <doWrite+0x24>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe71(%rip),%rdi
call   1050 <perror@plt>
mov    %r14d,%edi
call   1040 <close@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

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

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