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

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
scalar_t__ write(int, void *, int);
void checked_write(int handle, void *buffer, int nbyte) {
  while (nbyte > 0) {
    ssize_t res = write(handle, buffer, nbyte);
    if (res <= 0) {
      fprintf(stderr, "write to fd %i failed: %s\n", handle, strerror(errno));
      return;
    }
    nbyte -= res;
  }
}


x86 assembly (clang, -O0):
<checked_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11b7 <checked_write+0x87>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jg     11a4 <checked_write+0x74>
mov    0x2e6f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    -0x24(%rbp),%edx
mov    %rax,%rcx
lea    0xe66(%rip),%rsi
call   1040 <fprintf@plt>
jmp    11b7 <checked_write+0x87>
mov    -0x20(%rbp),%rcx
movslq -0x14(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x14(%rbp)
jmp    1142 <checked_write+0x12>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checked_write>:
push   %rbp
push   %r14
push   %rbx
test   %edx,%edx
jle    1158 <checked_write+0x28>
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r14d
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
test   %rax,%rax
jle    115d <checked_write+0x2d>
sub    %eax,%ebp
test   %ebp,%ebp
jg     1140 <checked_write+0x10>
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e7c(%rip),%rax
mov    (%rax),%ebx
mov    0x2e6b(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe85(%rip),%rsi
mov    %ebx,%edi
mov    %r14d,%edx
mov    %rax,%rcx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <fprintf@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [27 x i8] c"write to fd %i failed: %s\0A\00"
@errno = global i32 0
define void @checked_write(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
br label %8
8: ; preds = %24, %3
%9 = load i32, i32* %6
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %30
11: ; preds = %8
%12 = load i32, i32* %4
%13 = load i8*, i8** %5
%14 = load i32, i32* %6
%15 = call i64 @write(i32 %12, i8* %13, i32 %14)
store i64 %15, i64* %7
%16 = load i64, i64* %7
%17 = icmp sle i64 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %11
%19 = load i32, i32* @stderr
%20 = load i32, i32* %4
%21 = load i32, i32* @errno
%22 = call i8* @strerror(i32 %21)
%23 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %20, i8* %22)
br label %30
24: ; preds = %11
%25 = load i64, i64* %7
%26 = load i32, i32* %6
%27 = sext i32 %26 to i64
%28 = sub nsw i64 %27, %25
%29 = trunc i64 %28 to i32
store i32 %29, i32* %6
br label %8
30: ; preds = %18, %8
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [27 x i8] c"write to fd %i failed: %s\0A\00"
@errno = global i32 0
define void @checked_write(i32 %0, i8* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %7, label %18
5: ; preds = %7
%6 = icmp sgt i32 %12, 0
br i1 %6, label %7, label %18
7: ; preds = %3, %5
%8 = phi i32 [ %12, %5 ], [ %2, %3 ]
%9 = tail call i64 @write(i32 %0, i8* %1, i32 %8)
%10 = icmp slt i64 %9, 1
%11 = trunc i64 %9 to i32
%12 = sub i32 %8, %11
br i1 %10, label %13, label %5
13: ; preds = %7
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = tail call i8* @strerror(i32 %15)
%17 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %0, i8* %16)
br label %18
18: ; preds = %5, %3, %13
ret void
}