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

/* Variables and functions */
int /*<<< orphan*/ exit_on_bad_errno();
int write(int const, char const *const, size_t const);
void retrying_write(const int fd, const char *const buf, const size_t buf_sz) {
  size_t written = 0;
  while (written < buf_sz) {
    const ssize_t sz = write(fd, buf + written, buf_sz - written);
    if (sz == -1) {
      exit_on_bad_errno();
      continue;
    }
    written += sz;
  }
}


x86 assembly (clang, -O0):
<retrying_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    118b <retrying_write+0x6b>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    117a <retrying_write+0x5a>
mov    $0x0,%al
call   1040 <exit_on_bad_errno@plt>
jmp    113b <retrying_write+0x1b>
movslq -0x24(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    113b <retrying_write+0x1b>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<retrying_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdx,%rdx
je     116d <retrying_write+0x4d>
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebp,%ebp
jmp    114a <retrying_write+0x2a>
nopl   0x0(%rax)
cltq
add    %rax,%rbp
cmp    %rbx,%rbp
jae    116d <retrying_write+0x4d>
lea    (%r14,%rbp,1),%rsi
mov    %rbx,%rdx
sub    %rbp,%rdx
mov    %r15d,%edi
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1140 <retrying_write+0x20>
xor    %eax,%eax
call   1040 <exit_on_bad_errno@plt>
cmp    %rbx,%rbp
jb     114a <retrying_write+0x2a>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define void @retrying_write(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %19, label %5
5: ; preds = %3, %16
%6 = phi i64 [ %17, %16 ], [ 0, %3 ]
%7 = getelementptr inbounds i8, i8* %1, i64 %6
%8 = sub i64 %2, %6
%9 = tail call i32 @write(i32 %0, i8* %7, i64 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %5
%12 = tail call i32 (...) @exit_on_bad_errno()
br label %16
13: ; preds = %5
%14 = sext i32 %9 to i64
%15 = add i64 %6, %14
br label %16
16: ; preds = %13, %11
%17 = phi i64 [ %6, %11 ], [ %15, %13 ]
%18 = icmp ult i64 %17, %2
br i1 %18, label %5, label %19
19: ; preds = %16, %3
ret void
}