#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 */
scalar_t__ write(int, char const *, size_t);
ssize_t insist_write(int fd, const char *buf, size_t count) {
  ssize_t ret;
  size_t orig_count = count;
  while (count > 0) {
    ret = write(fd, buf, count);
    if (ret < 0)
      return ret;
    buf += ret;
    count -= ret;
  }
  return orig_count;
}


x86 assembly (clang, -O0):
<insist_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x20(%rbp)
jbe    1182 <insist_write+0x72>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    1162 <insist_write+0x52>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    118a <insist_write+0x7a>
mov    -0x28(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    112b <insist_write+0x1b>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<insist_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdx,%rdx
je     114d <insist_write+0x3d>
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%r15d
mov    %rdx,%rbp
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <write@plt>
test   %rax,%rax
js     1152 <insist_write+0x42>
add    %rax,%rbx
sub    %rax,%rbp
jne    1130 <insist_write+0x20>
jmp    1155 <insist_write+0x45>
xor    %r14d,%r14d
jmp    1155 <insist_write+0x45>
mov    %rax,%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i64 @insist_write(i32 %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i64, i64* %7
store i64 %10, i64* %9
br label %11
11: ; preds = %23, %3
%12 = load i64, i64* %7
%13 = icmp ugt i64 %12, 0
br i1 %13, label %14, label %30
14: ; preds = %11
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = load i64, i64* %7
%18 = call i64 @write(i32 %15, i8* %16, i64 %17)
store i64 %18, i64* %8
%19 = load i64, i64* %8
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = load i64, i64* %8
store i64 %22, i64* %4
br label %32
23: ; preds = %14
%24 = load i64, i64* %8
%25 = load i8*, i8** %6
%26 = getelementptr inbounds i8, i8* %25, i64 %24
store i8* %26, i8** %6
%27 = load i64, i64* %8
%28 = load i64, i64* %7
%29 = sub i64 %28, %27
store i64 %29, i64* %7
br label %11
30: ; preds = %11
%31 = load i64, i64* %9
store i64 %31, i64* %4
br label %32
32: ; preds = %30, %21
%33 = load i64, i64* %4
ret i64 %33
}

LLVM IR (-O2):
define i64 @insist_write(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %14, label %5
5: ; preds = %3, %10
%6 = phi i8* [ %11, %10 ], [ %1, %3 ]
%7 = phi i64 [ %12, %10 ], [ %2, %3 ]
%8 = tail call i64 @write(i32 %0, i8* %6, i64 %7)
%9 = icmp slt i64 %8, 0
br i1 %9, label %14, label %10
10: ; preds = %5
%11 = getelementptr inbounds i8, i8* %6, i64 %8
%12 = sub i64 %7, %8
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %5
14: ; preds = %5, %10, %3
%15 = phi i64 [ 0, %3 ], [ %2, %10 ], [ %8, %5 ]
ret i64 %15
}