#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);
int /*<<< orphan*/ stderr;
scalar_t__ write(int, void const *, size_t);
int write_ensure(int fd, const void *buffer, size_t count) {
  while (count > 0) {
    ssize_t n = write(fd, buffer, count);
    if (n < 0) {
      fprintf(stderr, "fail in writing %d bytes: %d\n", (int)count, (int)n);
      return n;
    }
    count -= n;
    buffer = ((uint8_t *)buffer) + n;
  }
  return 0;
}


x86 assembly (clang, -O0):
<write_ensure>:
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 <write_ensure+0x91>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    118a <write_ensure+0x6a>
mov    0x2e7c(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rax
mov    %eax,%edx
mov    -0x20(%rbp),%rax
mov    %eax,%ecx
lea    0xe87(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    11b8 <write_ensure+0x98>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1133 <write_ensure+0x13>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_ensure>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
xor    %r14d,%r14d
test   %rdx,%rdx
je     117f <write_ensure+0x5f>
mov    %rdx,%r12
mov    %rsi,%rbp
mov    %edi,%r15d
nopl   0x0(%rax)
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %r12,%rdx
call   1030 <write@plt>
mov    %rax,%rbx
test   %rax,%rax
js     1162 <write_ensure+0x42>
lea    0x0(%rbp,%rbx,4),%rbp
sub    %rbx,%r12
jne    1140 <write_ensure+0x20>
jmp    117f <write_ensure+0x5f>
mov    0x2e77(%rip),%rax
mov    (%rax),%edi
lea    0xe8e(%rip),%rsi
mov    %r12d,%edx
mov    %ebx,%ecx
call   1040 <fprintf@plt>
mov    %ebx,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [30 x i8] c"fail in writing %d bytes: %d\0A\00"
define i32 @write_ensure(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
br label %9
9: ; preds = %28, %3
%10 = load i64, i64* %7
%11 = icmp ugt i64 %10, 0
br i1 %11, label %12, label %37
12: ; preds = %9
%13 = load i32, i32* %5
%14 = load i8*, i8** %6
%15 = load i64, i64* %7
%16 = call i64 @write(i32 %13, i8* %14, i64 %15)
store i64 %16, i64* %8
%17 = load i64, i64* %8
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %28
19: ; preds = %12
%20 = load i32, i32* @stderr
%21 = load i64, i64* %7
%22 = trunc i64 %21 to i32
%23 = load i64, i64* %8
%24 = trunc i64 %23 to i32
%25 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %22, i32 %24)
%26 = load i64, i64* %8
%27 = trunc i64 %26 to i32
store i32 %27, i32* %4
br label %38
28: ; preds = %12
%29 = load i64, i64* %8
%30 = load i64, i64* %7
%31 = sub i64 %30, %29
store i64 %31, i64* %7
%32 = load i8*, i8** %6
%33 = bitcast i8* %32 to i32*
%34 = load i64, i64* %8
%35 = getelementptr inbounds i32, i32* %33, i64 %34
%36 = bitcast i32* %35 to i8*
store i8* %36, i8** %6
br label %9
37: ; preds = %9
store i32 0, i32* %4
br label %38
38: ; preds = %37, %19
%39 = load i32, i32* %4
ret i32 %39
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [30 x i8] c"fail in writing %d bytes: %d\0A\00"
define i32 @write_ensure(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %21, label %5
5: ; preds = %3, %15
%6 = phi i64 [ %16, %15 ], [ %2, %3 ]
%7 = phi i8* [ %19, %15 ], [ %1, %3 ]
%8 = tail call i64 @write(i32 %0, i8* %7, i64 %6)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %5
%11 = load i32, i32* @stderr
%12 = trunc i64 %6 to i32
%13 = trunc i64 %8 to i32
%14 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %13)
br label %21
15: ; preds = %5
%16 = sub i64 %6, %8
%17 = bitcast i8* %7 to i32*
%18 = getelementptr inbounds i32, i32* %17, i64 %8
%19 = bitcast i32* %18 to i8*
%20 = icmp eq i64 %16, 0
br i1 %20, label %21, label %5
21: ; preds = %15, %3, %10
%22 = phi i32 [ %13, %10 ], [ 0, %3 ], [ 0, %15 ]
ret i32 %22
}