#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, unsigned char *, int);
void dowrite(int fd, unsigned char *buf, size_t len) {
  ssize_t r;
  while (len > 0) {
    r = write(fd, buf, len > 65536 ? 65536 : len);
    if (r < 0) {
      perror("write");
      exit(1);
    }
    buf += r;
    len -= r;
  }
}


x86 assembly (clang, -O0):
<dowrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    11d7 <dowrite+0xa7>
mov    -0x4(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x10000,-0x18(%rbp)
jbe    1178 <dowrite+0x48>
mov    $0x10000,%eax
mov    %rax,-0x38(%rbp)
jmp    1180 <dowrite+0x50>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rsi
mov    -0x2c(%rbp),%edi
mov    -0x38(%rbp),%rax
mov    %eax,%edx
call   1030 <write@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11b7 <dowrite+0x87>
lea    0xe58(%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 <dowrite+0x13>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dowrite>:
push   %rbp
push   %r14
push   %rbx
test   %rdx,%rdx
je     1176 <dowrite+0x46>
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmp    $0x10000,%rbx
mov    $0x10000,%edx
cmovb  %ebx,%edx
mov    %ebp,%edi
mov    %r14,%rsi
call   1030 <write@plt>
test   %rax,%rax
js     117b <dowrite+0x4b>
add    %rax,%r14
sub    %rax,%rbx
jne    1150 <dowrite+0x20>
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe7e(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

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

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