#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 /*<<< orphan*/ die(char *);
int write(int, void *, size_t);
void do_write(int fd, void *buf, size_t size) {
  while (size) {
    int ret = write(fd, buf, size);
    if (ret < 0)
      die("failed to write");
    size -= ret;
    buf += ret;
  }
}


x86 assembly (clang, -O0):
<do_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     118c <do_write+0x6c>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1167 <do_write+0x47>
lea    0xe9e(%rip),%rdi
call   1040 <die@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 <do_write+0x13>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_write>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %rdx,%rdx
je     1169 <do_write+0x49>
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%r12d
lea    0xec3(%rip),%r15
jmp    114b <do_write+0x2b>
nop
movslq %ebp,%rax
add    %rax,%rbx
sub    %rax,%r14
je     1169 <do_write+0x49>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <write@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1140 <do_write+0x20>
mov    %r15,%rdi
call   1040 <die@plt>
jmp    1140 <do_write+0x20>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"failed to write\00"
define void @do_write(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 ne 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 slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %11
%19 = call i32 @die(i8* getelementptr inbounds ([16 x i8], [16 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 i8, i8* %26, i64 %27
store i8* %28, i8** %5
br label %8
29: ; preds = %8
ret void
}

LLVM IR (-O2):
@.str = [16 x i8] c"failed to write\00"
define void @do_write(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 slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %5
%11 = tail call i32 @die(i8* getelementptr inbounds ([16 x i8], [16 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 i8, i8* %6, i64 %13
%16 = icmp eq i64 %14, 0
br i1 %16, label %17, label %5
17: ; preds = %12, %3
ret void
}