#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*/ abort();
int /*<<< orphan*/ perror(char *);
scalar_t__ write(int, char const *, size_t);
void write_all(int fd, void const *buf, size_t len) {
  char const *ptr = buf;
  while (len > 0) {
    ssize_t l = write(fd, ptr, len);
    if (l == 0)
      abort();
    else if (l > 0) {
      ptr += l;
      len -= l;
    } else {
      perror("write()");
      abort();
    }
  }
}


x86 assembly (clang, -O0):
<write_all>:
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,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    11c4 <write_all+0x94>
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <write@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    117c <write_all+0x4c>
mov    $0x0,%al
call   1030 <abort@plt>
cmpq   $0x0,-0x28(%rbp)
jle    11a7 <write_all+0x77>
mov    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    11ba <write_all+0x8a>
lea    0xe52(%rip),%rdi
call   1050 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    11bf <write_all+0x8f>
jmp    114b <write_all+0x1b>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_all>:
push   %rbp
push   %r14
push   %rbx
test   %rdx,%rdx
je     116c <write_all+0x3c>
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1040 <write@plt>
test   %rax,%rax
je     1171 <write_all+0x41>
jle    1178 <write_all+0x48>
add    %rax,%rbx
sub    %rax,%r14
jne    1150 <write_all+0x20>
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %eax,%eax
call   1030 <abort@plt>
lea    0xe81(%rip),%rdi
call   1050 <perror@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"write()\00"
define void @write_all(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%9 = load i8*, i8** %5
store i8* %9, i8** %7
br label %10
10: ; preds = %36, %3
%11 = load i64, i64* %6
%12 = icmp ugt i64 %11, 0
br i1 %12, label %13, label %37
13: ; preds = %10
%14 = load i32, i32* %4
%15 = load i8*, i8** %7
%16 = load i64, i64* %6
%17 = call i64 @write(i32 %14, i8* %15, i64 %16)
store i64 %17, i64* %8
%18 = load i64, i64* %8
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %13
%21 = call i32 (...) @abort()
unreachable
22: ; preds = %13
%23 = load i64, i64* %8
%24 = icmp sgt i64 %23, 0
br i1 %24, label %25, label %32
25: ; preds = %22
%26 = load i64, i64* %8
%27 = load i8*, i8** %7
%28 = getelementptr inbounds i8, i8* %27, i64 %26
store i8* %28, i8** %7
%29 = load i64, i64* %8
%30 = load i64, i64* %6
%31 = sub i64 %30, %29
store i64 %31, i64* %6
br label %35
32: ; preds = %22
%33 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%34 = call i32 (...) @abort()
unreachable
35: ; preds = %25
br label %36
36: ; preds = %35
br label %10
37: ; preds = %10
ret void
}

LLVM IR (-O2):
@.str = [8 x i8] c"write()\00"
define void @write_all(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %21, label %5
5: ; preds = %3, %14
%6 = phi i64 [ %16, %14 ], [ %2, %3 ]
%7 = phi i8* [ %15, %14 ], [ %1, %3 ]
%8 = tail call i64 @write(i32 %0, i8* %7, i64 %6)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %5
%11 = tail call i32 (...) @abort()
unreachable
12: ; preds = %5
%13 = icmp sgt i64 %8, 0
br i1 %13, label %14, label %18
14: ; preds = %12
%15 = getelementptr inbounds i8, i8* %7, i64 %8
%16 = sub i64 %6, %8
%17 = icmp eq i64 %16, 0
br i1 %17, label %21, label %5
18: ; preds = %12
%19 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%20 = tail call i32 (...) @abort()
unreachable
21: ; preds = %14, %3
ret void
}