#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 size_t ssize_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
size_t write(int, char const *, size_t);
void write_all(int fd, const char *c, size_t n) {
  do {
    ssize_t written = write(fd, c, n);
    n -= written;
    if (written == -1) {
      perror("write()");
      exit(-1);
    }
  } while (n > 0);
}


x86 assembly (clang, -O0):
<write_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    1187 <write_all+0x57>
lea    0xe88(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>
jmp    118c <write_all+0x5c>
cmpq   $0x0,-0x18(%rbp)
ja     1143 <write_all+0x13>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_all>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%ebp
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
cmp    $0xffffffffffffffff,%rax
je     115d <write_all+0x2d>
sub    %rax,%rbx
jne    1140 <write_all+0x10>
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe9c(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%edi
call   1050 <exit@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 i64
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
br label %8
8: ; preds = %22, %3
%9 = load i32, i32* %4
%10 = load i8*, i8** %5
%11 = load i64, i64* %6
%12 = call i64 @write(i32 %9, i8* %10, i64 %11)
store i64 %12, i64* %7
%13 = load i64, i64* %7
%14 = load i64, i64* %6
%15 = sub i64 %14, %13
store i64 %15, i64* %6
%16 = load i64, i64* %7
%17 = icmp eq i64 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %8
%19 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 -1)
unreachable
21: ; preds = %8
br label %22
22: ; preds = %21
%23 = load i64, i64* %6
%24 = icmp ugt i64 %23, 0
br i1 %24, label %8, label %25
25: ; preds = %22
ret void
}

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