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

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
scalar_t__ write(int, int /*<<< orphan*/ const *, size_t);
int _checked_write(int fd, const uint8_t *buffer, size_t buffer_size) {
  while (buffer_size > 0) {
    ssize_t res = write(fd, buffer, buffer_size);
    if (res <= 0) {
      fprintf(stderr, "fd %d - ", fd);
      perror("write failed");
      return -1;
    }
    buffer_size -= res;
  }
  return 0;
}


x86 assembly (clang, -O0):
<_checked_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    11b1 <_checked_write+0x81>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jg     119d <_checked_write+0x6d>
mov    0x2e6c(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe80(%rip),%rsi
call   1040 <fprintf@plt>
lea    0xe7d(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b8 <_checked_write+0x88>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    1143 <_checked_write+0x13>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_checked_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %r14d,%r14d
test   %rdx,%rdx
je     1168 <_checked_write+0x38>
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %edi,%r15d
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
test   %rax,%rax
jle    1176 <_checked_write+0x46>
sub    %rax,%rbx
jne    1150 <_checked_write+0x20>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e63(%rip),%rax
mov    (%rax),%edi
lea    0xe7a(%rip),%rsi
mov    %r15d,%edx
call   1040 <fprintf@plt>
lea    0xe74(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%r14d
jmp    1168 <_checked_write+0x38>

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

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