#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 OS_Write(int, char *, int);
int write_it_all(int fd, char *buf, int len) {
  int wrote;
  while (len) {
    wrote = OS_Write(fd, buf, len);
    if (wrote < 0)
      return wrote;
    len -= wrote;
    buf += wrote;
  }
  return len;
}


x86 assembly (clang, -O0):
<write_it_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1174 <write_it_all+0x64>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <OS_Write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1153 <write_it_all+0x43>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    117a <write_it_all+0x6a>
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1122 <write_it_all+0x12>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_it_all>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %r14d,%r14d
test   %edx,%edx
je     114f <write_it_all+0x3f>
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r15d
cs nopw 0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <OS_Write@plt>
test   %eax,%eax
js     114c <write_it_all+0x3c>
mov    %eax,%ecx
add    %rcx,%rbx
sub    %eax,%ebp
jne    1130 <write_it_all+0x20>
jmp    114f <write_it_all+0x3f>
mov    %eax,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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