#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 write(int, unsigned char *, int);
unsigned int writebuffer(int h_dev, unsigned char *ptr, unsigned int len) {
  int n;
  while (len > 0) {
    n = (len >= 1 << 30) ? 1 << 30 : len;
    n = write(h_dev, ptr, n);
    if (n < 0) {
      break;
    }
    ptr += n;
    len -= n;
  }
  return (len);
}


x86 assembly (clang, -O0):
<writebuffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jbe    1194 <writebuffer+0x84>
cmpl   $0x40000000,-0x14(%rbp)
jb     1146 <writebuffer+0x36>
mov    $0x40000000,%eax
mov    %eax,-0x1c(%rbp)
jmp    114c <writebuffer+0x3c>
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1173 <writebuffer+0x63>
jmp    1194 <writebuffer+0x84>
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
jmp    1122 <writebuffer+0x12>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writebuffer>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %r14d,%r14d
test   %edx,%edx
je     115b <writebuffer+0x4b>
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r15d
cs nopw 0x0(%rax,%rax,1)
cmp    $0x40000000,%ebp
mov    $0x40000000,%edx
cmovb  %ebp,%edx
mov    %r15d,%edi
mov    %rbx,%rsi
call   1030 <write@plt>
test   %eax,%eax
js     1158 <writebuffer+0x48>
mov    %eax,%ecx
add    %rcx,%rbx
sub    %eax,%ebp
jne    1130 <writebuffer+0x20>
jmp    115b <writebuffer+0x4b>
mov    %ebp,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @writebuffer(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
br label %8
8: ; preds = %26, %3
%9 = load i32, i32* %6
%10 = icmp ugt i32 %9, 0
br i1 %10, label %11, label %34
11: ; preds = %8
%12 = load i32, i32* %6
%13 = icmp uge i32 %12, 1073741824
br i1 %13, label %14, label %15
14: ; preds = %11
br label %17
15: ; preds = %11
%16 = load i32, i32* %6
br label %17
17: ; preds = %15, %14
%18 = phi i32 [ 1073741824, %14 ], [ %16, %15 ]
store i32 %18, i32* %7
%19 = load i32, i32* %4
%20 = load i8*, i8** %5
%21 = load i32, i32* %7
%22 = call i32 @write(i32 %19, i8* %20, i32 %21)
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %17
br label %34
26: ; preds = %17
%27 = load i32, i32* %7
%28 = load i8*, i8** %5
%29 = sext i32 %27 to i64
%30 = getelementptr inbounds i8, i8* %28, i64 %29
store i8* %30, i8** %5
%31 = load i32, i32* %7
%32 = load i32, i32* %6
%33 = sub i32 %32, %31
store i32 %33, i32* %6
br label %8
34: ; preds = %25, %8
%35 = load i32, i32* %6
ret i32 %35
}

LLVM IR (-O2):
define i32 @writebuffer(i32 %0, i8* nocapture %1, i32 %2) {
%4 = icmp eq i32 %2, 0
br i1 %4, label %17, label %5
5: ; preds = %3, %12
%6 = phi i32 [ %15, %12 ], [ %2, %3 ]
%7 = phi i8* [ %14, %12 ], [ %1, %3 ]
%8 = icmp ult i32 %6, 1073741824
%9 = select i1 %8, i32 %6, i32 1073741824
%10 = tail call i32 @write(i32 %0, i8* %7, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %17, label %12
12: ; preds = %5
%13 = zext i32 %10 to i64
%14 = getelementptr inbounds i8, i8* %7, i64 %13
%15 = sub i32 %6, %10
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %5
17: ; preds = %12, %5, %3
%18 = phi i32 [ 0, %3 ], [ %6, %5 ], [ 0, %12 ]
ret i32 %18
}