#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 */
int /*<<< orphan*/ printf(char *);
scalar_t__ safe_write(int, void const *, size_t);
ssize_t full_write(int fd, const void *buf, size_t len) {
  ssize_t cc;
  ssize_t total;
  total = 0;
  while (len) {
    cc = safe_write(fd, buf, len);
    if (cc < 0) {
      if (total) {
        return total;
      }
      printf("[full_write] safe_write() fail\n");
      return cc;
    }
    total += cc;
    buf = ((const char *)buf) + cc;
    len -= cc;
  }
  return total;
}


x86 assembly (clang, -O0):
<full_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11c2 <full_write+0xa2>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <safe_write@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    1196 <full_write+0x76>
cmpq   $0x0,-0x30(%rbp)
je     117d <full_write+0x5d>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ca <full_write+0xaa>
lea    0xe7c(%rip),%rdi
call   1030 <printf@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ca <full_write+0xaa>
mov    -0x28(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    113b <full_write+0x1b>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<full_write>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %rdx,%rdx
je     1163 <full_write+0x43>
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %edi,%r12d
xor    %r14d,%r14d
nopl   0x0(%rax)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1040 <safe_write@plt>
mov    %rax,%rbp
test   %rax,%rax
js     1168 <full_write+0x48>
add    %rbp,%r14
add    %rbp,%rbx
sub    %rbp,%r15
jne    1140 <full_write+0x20>
jmp    117c <full_write+0x5c>
xor    %r14d,%r14d
jmp    117c <full_write+0x5c>
test   %r14,%r14
jne    117c <full_write+0x5c>
lea    0xe8c(%rip),%rdi
call   1030 <puts@plt>
mov    %rbp,%r14
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [32 x i8] c"[full_write] safe_write() fail\0A\00"
define i64 @full_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
store i64 0, i64* %9
br label %10
10: ; preds = %28, %3
%11 = load i64, i64* %7
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %38
13: ; preds = %10
%14 = load i32, i32* %5
%15 = load i8*, i8** %6
%16 = load i64, i64* %7
%17 = call i64 @safe_write(i32 %14, i8* %15, i64 %16)
store i64 %17, i64* %8
%18 = load i64, i64* %8
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %28
20: ; preds = %13
%21 = load i64, i64* %9
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = load i64, i64* %9
store i64 %24, i64* %4
br label %40
25: ; preds = %20
%26 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%27 = load i64, i64* %8
store i64 %27, i64* %4
br label %40
28: ; preds = %13
%29 = load i64, i64* %8
%30 = load i64, i64* %9
%31 = add nsw i64 %30, %29
store i64 %31, i64* %9
%32 = load i8*, i8** %6
%33 = load i64, i64* %8
%34 = getelementptr inbounds i8, i8* %32, i64 %33
store i8* %34, i8** %6
%35 = load i64, i64* %8
%36 = load i64, i64* %7
%37 = sub i64 %36, %35
store i64 %37, i64* %7
br label %10
38: ; preds = %10
%39 = load i64, i64* %9
store i64 %39, i64* %4
br label %40
40: ; preds = %38, %25, %23
%41 = load i64, i64* %4
ret i64 %41
}

LLVM IR (-O2):
@str = [31 x i8] c"[full_write] safe_write() fail\00"
define i64 @full_write(i32 %0, i8* %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %20, label %5
5: ; preds = %3, %15
%6 = phi i64 [ %16, %15 ], [ 0, %3 ]
%7 = phi i64 [ %18, %15 ], [ %2, %3 ]
%8 = phi i8* [ %17, %15 ], [ %1, %3 ]
%9 = tail call i64 @safe_write(i32 %0, i8* %8, i64 %7)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %5
%12 = icmp eq i64 %6, 0
br i1 %12, label %13, label %20
13: ; preds = %11
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
br label %20
15: ; preds = %5
%16 = add nsw i64 %9, %6
%17 = getelementptr inbounds i8, i8* %8, i64 %9
%18 = sub i64 %7, %9
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %5
20: ; preds = %15, %3, %11, %13
%21 = phi i64 [ %9, %13 ], [ %6, %11 ], [ 0, %3 ], [ %16, %15 ]
ret i64 %21
}