#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 int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ error_exit(int, char *);
unsigned int fwrite(char const *, int, int, int /*<<< orphan*/ *);
void write_data_to_file(FILE *fp, const char *data, unsigned length) {
  unsigned rv;
  while (length) {
    rv = fwrite(data, 1, (length > 65536) ? 65536 : length, fp);
    if (!rv)
      error_exit(1, "could not write output data");
    data += rv;
    length -= rv;
  }
}


x86 assembly (clang, -O0):
<write_data_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11b8 <write_data_to_file+0x98>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x10000,-0x14(%rbp)
jbe    115f <write_data_to_file+0x3f>
mov    $0x10000,%eax
mov    %eax,-0x24(%rbp)
jmp    1165 <write_data_to_file+0x45>
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%edx
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fwrite@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    1198 <write_data_to_file+0x78>
mov    $0x1,%edi
lea    0xe6d(%rip),%rsi
call   1030 <error_exit@plt>
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,%ecx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
jmp    1133 <write_data_to_file+0x13>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_data_to_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %edx,%edx
je     117c <write_data_to_file+0x5c>
mov    %edx,%ebx
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xec5(%rip),%r12
jmp    1149 <write_data_to_file+0x29>
nopl   (%rax)
mov    %ebp,%eax
add    %rax,%r14
sub    %ebp,%ebx
je     117c <write_data_to_file+0x5c>
cmp    $0x10000,%ebx
mov    $0x10000,%edx
cmovb  %ebx,%edx
mov    %r14,%rdi
mov    $0x1,%esi
mov    %r15,%rcx
call   1040 <fwrite@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1140 <write_data_to_file+0x20>
mov    $0x1,%edi
mov    %r12,%rsi
call   1030 <error_exit@plt>
jmp    1140 <write_data_to_file+0x20>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [28 x i8] c"could not write output data\00"
define void @write_data_to_file(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 ne i32 %9, 0
br i1 %10, label %11, label %34
11: ; preds = %8
%12 = load i8*, i8** %5
%13 = load i32, i32* %6
%14 = icmp ugt i32 %13, 65536
br i1 %14, label %15, label %16
15: ; preds = %11
br label %18
16: ; preds = %11
%17 = load i32, i32* %6
br label %18
18: ; preds = %16, %15
%19 = phi i32 [ 65536, %15 ], [ %17, %16 ]
%20 = load i32*, i32** %4
%21 = call i32 @fwrite(i8* %12, i32 1, i32 %19, i32* %20)
store i32 %21, i32* %7
%22 = load i32, i32* %7
%23 = icmp ne i32 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %18
%25 = call i32 @error_exit(i32 1, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %26
26: ; preds = %24, %18
%27 = load i32, i32* %7
%28 = load i8*, i8** %5
%29 = zext 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 = %8
ret void
}

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