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


x86 assembly (clang, -O0):
<do_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    11a2 <do_write+0x82>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
add    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    -0x28(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jge    1189 <do_write+0x69>
lea    0xe89(%rip),%rdi
call   1040 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    11aa <do_write+0x8a>
mov    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    113b <do_write+0x1b>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdx,%rdx
je     116c <do_write+0x4c>
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
lea    (%r15,%rbx,1),%rsi
mov    %r14,%rdx
sub    %rbx,%rdx
mov    %ebp,%edi
call   1030 <write@plt>
test   %rax,%rax
js     1160 <do_write+0x40>
add    %rax,%rbx
cmp    %r14,%rbx
jb     1140 <do_write+0x20>
jmp    116e <do_write+0x4e>
lea    0xe99(%rip),%rdi
call   1040 <perror@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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