#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*/ u_int8_t;
typedef int ssize_t;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ cli_fatal(char *, int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ strerror(scalar_t__);
int write(int, int /*<<< orphan*/ const *, size_t);
void cli_file_write(int fd, const void *buf, size_t len) {
  ssize_t r;
  const u_int8_t *d;
  size_t written;
  d = buf;
  written = 0;
  while (written != len) {
    r = write(fd, d + written, len - written);
    if (r == -1) {
      if (errno == EINTR)
        continue;
      cli_fatal("cli_file_write: %s", strerror(errno));
    }
    written += r;
  }
}


x86 assembly (clang, -O0):
<cli_file_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     11da <cli_file_write+0xaa>
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x18(%rbp),%rdx
sub    -0x30(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11c9 <cli_file_write+0x99>
mov    0x2e4c(%rip),%rax
mov    (%rax),%rax
mov    0x2e3a(%rip),%rcx
cmp    (%rcx),%rax
jne    11ac <cli_file_write+0x7c>
jmp    1153 <cli_file_write+0x23>
mov    0x2e2d(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    %eax,%esi
lea    0xe3c(%rip),%rdi
call   1040 <cli_fatal@plt>
movslq -0x1c(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1153 <cli_file_write+0x23>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cli_file_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,(%rsp)
mov    %rdx,0x8(%rsp)
test   %rdx,%rdx
je     11d4 <cli_file_write+0xa4>
mov    %edi,%r13d
xor    %ecx,%ecx
mov    0x2e84(%rip),%r12
mov    0x2e75(%rip),%r14
mov    0x8(%rsp),%rbx
mov    (%rsp),%rbp
jmp    118d <cli_file_write+0x5d>
xchg   %ax,%ax
movslq %r15d,%rax
mov    0x10(%rsp),%rcx
add    %rax,%rcx
mov    0x8(%rsp),%rbx
mov    (%rsp),%rax
lea    (%rax,%rcx,4),%rbp
sub    %rcx,%rbx
je     11d4 <cli_file_write+0xa4>
mov    %rcx,0x10(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r13d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
jne    1170 <cli_file_write+0x40>
mov    (%r12),%rdi
cmp    (%r14),%rdi
je     11a0 <cli_file_write+0x70>
call   1050 <strerror@plt>
lea    0xe35(%rip),%rdi
mov    %eax,%esi
call   1040 <cli_fatal@plt>
jmp    1170 <cli_file_write+0x40>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@.str = [19 x i8] c"cli_file_write: %s\00"
define void @cli_file_write(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%10 = load i8*, i8** %5
%11 = bitcast i8* %10 to i32*
store i32* %11, i32** %8
store i64 0, i64* %9
br label %12
12: ; preds = %36, %31, %3
%13 = load i64, i64* %9
%14 = load i64, i64* %6
%15 = icmp ne i64 %13, %14
br i1 %15, label %16, label %41
16: ; preds = %12
%17 = load i32, i32* %4
%18 = load i32*, i32** %8
%19 = load i64, i64* %9
%20 = getelementptr inbounds i32, i32* %18, i64 %19
%21 = load i64, i64* %6
%22 = load i64, i64* %9
%23 = sub i64 %21, %22
%24 = call i32 @write(i32 %17, i32* %20, i64 %23)
store i32 %24, i32* %7
%25 = load i32, i32* %7
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %36
27: ; preds = %16
%28 = load i64, i64* @errno
%29 = load i64, i64* @EINTR
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %32
31: ; preds = %27
br label %12
32: ; preds = %27
%33 = load i64, i64* @errno
%34 = call i32 @strerror(i64 %33)
%35 = call i32 @cli_fatal(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %34)
br label %36
36: ; preds = %32, %16
%37 = load i32, i32* %7
%38 = sext i32 %37 to i64
%39 = load i64, i64* %9
%40 = add i64 %39, %38
store i64 %40, i64* %9
br label %12
41: ; preds = %12
ret void
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@.str = [19 x i8] c"cli_file_write: %s\00"
define void @cli_file_write(i32 %0, i8* nocapture %1, i64 %2) {
%4 = bitcast i8* %1 to i32*
%5 = icmp eq i64 %2, 0
br i1 %5, label %26, label %6
6: ; preds = %3, %20
%7 = phi i64 [ %25, %20 ], [ %2, %3 ]
%8 = phi i32* [ %24, %20 ], [ %4, %3 ]
%9 = phi i64 [ %22, %20 ], [ 0, %3 ]
br label %10
10: ; preds = %6, %13
%11 = tail call i32 @write(i32 %0, i32* %8, i64 %7)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %20
13: ; preds = %10
%14 = load i64, i64* @errno
%15 = load i64, i64* @EINTR
%16 = icmp eq i64 %14, %15
br i1 %16, label %10, label %17
17: ; preds = %13
%18 = tail call i32 @strerror(i64 %14)
%19 = tail call i32 @cli_fatal(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %18)
br label %20
20: ; preds = %10, %17
%21 = sext i32 %11 to i64
%22 = add i64 %9, %21
%23 = icmp eq i64 %22, %2
%24 = getelementptr inbounds i32, i32* %4, i64 %22
%25 = sub i64 %2, %22
br i1 %23, label %26, label %6
26: ; preds = %20, %3
ret void
}