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

/* Variables and functions */
int /*<<< orphan*/ fatal(char *);
int write(int, char const *, size_t);
void write2(int fd, const char *buf, size_t len) {
  while (len > 0) {
    ssize_t count = write(fd, buf, len);
    if (count == -1) {
      fatal("write() failed");
    }
    len -= count;
    buf += count;
  }
}


x86 assembly (clang, -O0):
<write2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    118c <write2+0x6c>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1167 <write2+0x47>
lea    0xe9e(%rip),%rdi
call   1040 <fatal@plt>
movslq -0x1c(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1133 <write2+0x13>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write2>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %rdx,%rdx
je     116a <write2+0x4a>
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%r12d
lea    0xec3(%rip),%r15
jmp    114b <write2+0x2b>
nop
movslq %ebp,%rax
add    %rax,%rbx
sub    %rax,%r14
je     116a <write2+0x4a>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <write@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1140 <write2+0x20>
mov    %r15,%rdi
call   1040 <fatal@plt>
jmp    1140 <write2+0x20>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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