#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*/ exit(int);
int /*<<< orphan*/ perror(char *);
scalar_t__ write(int, void const *, size_t);
ssize_t do_write(int fd, const void *buf, size_t len) {
  while (len) {
    ssize_t wrote = write(fd, buf, len);
    if (wrote < 0) {
      perror("write");
      exit(1);
    }
    buf += wrote;
    len -= wrote;
  }
  return len;
}


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     11a3 <do_write+0x73>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    1183 <do_write+0x53>
lea    0xe8c(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x20(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    1143 <do_write+0x13>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_write>:
push   %rbp
push   %r14
push   %rbx
test   %rdx,%rdx
je     116a <do_write+0x3a>
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   1030 <write@plt>
test   %rax,%rax
js     1171 <do_write+0x41>
add    %rax,%rbx
sub    %rax,%r14
jne    1150 <do_write+0x20>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe88(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"write\00"
define i64 @do_write(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 = %21, %3
%9 = load i64, i64* %6
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %28
11: ; preds = %8
%12 = load i32, i32* %4
%13 = load i8*, i8** %5
%14 = load i64, i64* %6
%15 = call i64 @write(i32 %12, i8* %13, i64 %14)
store i64 %15, i64* %7
%16 = load i64, i64* %7
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %11
%19 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %11
%22 = load i64, i64* %7
%23 = load i8*, i8** %5
%24 = getelementptr i8, i8* %23, i64 %22
store i8* %24, i8** %5
%25 = load i64, i64* %7
%26 = load i64, i64* %6
%27 = sub i64 %26, %25
store i64 %27, i64* %6
br label %8
28: ; preds = %8
%29 = load i64, i64* %6
ret i64 %29
}

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