#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 */

/* Variables and functions */
int write(int, char const *, size_t);
int full_write(int desc, const char *ptr, size_t len) {
  int total_written;
  total_written = 0;
  while (len > 0) {
    int written = write(desc, ptr, len);
    if (written < 0) {
      return written;
    }
    total_written += written;
    ptr += written;
    len -= written;
  }
  return total_written;
}


x86 assembly (clang, -O0):
<full_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    118b <full_write+0x7b>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    115d <full_write+0x4d>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1191 <full_write+0x81>
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
movslq -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    112a <full_write+0x1a>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<full_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdx,%rdx
je     1151 <full_write+0x41>
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %edi,%r15d
xor    %r14d,%r14d
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
test   %eax,%eax
js     1156 <full_write+0x46>
add    %eax,%r14d
mov    %eax,%eax
add    %rax,%rbp
sub    %rax,%rbx
jne    1130 <full_write+0x20>
jmp    1159 <full_write+0x49>
xor    %r14d,%r14d
jmp    1159 <full_write+0x49>
mov    %eax,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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