#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, void const *, size_t);
int write_complete(int fd, const void *buf, size_t len) {
  size_t orig_len = len;
  for (;;) {
    int result = write(fd, buf, len);
    if (result == -1) {
      return -1;
    }
    if (result == 0) {
      return 0;
    }
    len -= result;
    buf = ((char *)buf) + result;
    if (len == 0) {
      return orig_len;
    }
  }
}


x86 assembly (clang, -O0):
<write_complete>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1154 <write_complete+0x44>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a4 <write_complete+0x94>
cmpl   $0x0,-0x24(%rbp)
jne    116a <write_complete+0x5a>
movl   $0x0,-0x4(%rbp)
jmp    11a4 <write_complete+0x94>
movslq -0x24(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    119f <write_complete+0x8f>
mov    -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    11a4 <write_complete+0x94>
jmp    112b <write_complete+0x1b>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_complete>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%r15d
mov    %rdx,%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <write@plt>
lea    0x1(%rax),%ecx
cmp    $0x2,%ecx
jb     1152 <write_complete+0x42>
cltq
add    %rax,%rbx
sub    %rax,%rbp
jne    1130 <write_complete+0x20>
jmp    1155 <write_complete+0x45>
mov    %eax,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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