#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*/ errno;
int /*<<< orphan*/ errorExit(char *, unsigned int const, unsigned int const,
                             int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int write(int const, unsigned char *, unsigned int const);
void safeWrite(int const fd, unsigned char *const buf,
               unsigned int const nbyte) {
  unsigned int numWritten;
  numWritten = 0;
  while (numWritten < nbyte) {
    int const result = write(fd, &buf[numWritten], nbyte - numWritten);
    if (result == -1)
      errorExit("write (of %u bytes (total %u) ) returned "
                "errno %d (%s)",
                nbyte - numWritten, nbyte, errno, strerror(errno));
    numWritten += result;
  }
}


x86 assembly (clang, -O0):
<safeWrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    11c9 <safeWrite+0x99>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%eax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x18(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11bb <safeWrite+0x8b>
mov    -0x14(%rbp),%eax
sub    -0x18(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e50(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
call   1040 <strerror@plt>
mov    -0x28(%rbp),%esi
mov    -0x24(%rbp),%edx
mov    -0x20(%rbp),%ecx
mov    %eax,%r8d
lea    0xe4a(%rip),%rdi
call   1050 <errorExit@plt>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1149 <safeWrite+0x19>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safeWrite>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
je     11bb <safeWrite+0x8b>
mov    %edx,%r13d
mov    %rsi,%r15
mov    %edi,%r12d
xor    %r14d,%r14d
mov    %edi,0x4(%rsp)
jmp    1168 <safeWrite+0x38>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    %ebp,%r14d
cmp    %r13d,%r14d
jae    11bb <safeWrite+0x8b>
mov    %r14d,%esi
add    %r15,%rsi
mov    %r13d,%ebx
sub    %r14d,%ebx
mov    %r12d,%edi
mov    %ebx,%edx
call   1030 <write@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1160 <safeWrite+0x30>
mov    %r15,%r12
mov    0x2e51(%rip),%rax
mov    (%rax),%r15d
mov    %r15d,%edi
call   1040 <strerror@plt>
lea    0xe5f(%rip),%rdi
mov    %ebx,%esi
mov    %r13d,%edx
mov    %r15d,%ecx
mov    %r12,%r15
mov    0x4(%rsp),%r12d
mov    %eax,%r8d
call   1050 <errorExit@plt>
jmp    1160 <safeWrite+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [55 x i8] c"write (of %u bytes (total %u) ) returned errno %d (%s)\00"
@errno = global i32 0
define void @safeWrite(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
store i32 0, i32* %7
br label %9
9: ; preds = %34, %3
%10 = load i32, i32* %7
%11 = load i32, i32* %6
%12 = icmp ult i32 %10, %11
br i1 %12, label %13, label %38
13: ; preds = %9
%14 = load i32, i32* %4
%15 = load i8*, i8** %5
%16 = load i32, i32* %7
%17 = zext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %15, i64 %17
%19 = load i32, i32* %6
%20 = load i32, i32* %7
%21 = sub i32 %19, %20
%22 = call i32 @write(i32 %14, i8* %18, i32 %21)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %34
25: ; preds = %13
%26 = load i32, i32* %6
%27 = load i32, i32* %7
%28 = sub i32 %26, %27
%29 = load i32, i32* %6
%30 = load i32, i32* @errno
%31 = load i32, i32* @errno
%32 = call i32 @strerror(i32 %31)
%33 = call i32 @errorExit(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0), i32 %28, i32 %29, i32 %30, i32 %32)
br label %34
34: ; preds = %25, %13
%35 = load i32, i32* %8
%36 = load i32, i32* %7
%37 = add i32 %36, %35
store i32 %37, i32* %7
br label %9
38: ; preds = %9
ret void
}

LLVM IR (-O2):
@.str = [55 x i8] c"write (of %u bytes (total %u) ) returned errno %d (%s)\00"
@errno = global i32 0
define void @safeWrite(i32 %0, i8* nocapture %1, i32 %2) {
%4 = icmp eq i32 %2, 0
br i1 %4, label %19, label %5
5: ; preds = %3, %16
%6 = phi i32 [ %17, %16 ], [ 0, %3 ]
%7 = zext i32 %6 to i64
%8 = getelementptr inbounds i8, i8* %1, i64 %7
%9 = sub i32 %2, %6
%10 = tail call i32 @write(i32 %0, i8* %8, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %5
%13 = load i32, i32* @errno
%14 = tail call i32 @strerror(i32 %13)
%15 = tail call i32 @errorExit(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0), i32 %9, i32 %2, i32 %13, i32 %14)
br label %16
16: ; preds = %12, %5
%17 = add i32 %10, %6
%18 = icmp ult i32 %17, %2
br i1 %18, label %5, label %19
19: ; preds = %16, %3
ret void
}