#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 errno;
int fwrite(char const *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *);
char *strerror(int);
int WriteFileData(const char *pBuffer, int length, FILE *fout) {
  int num_written;
  while (length) {
    num_written = fwrite(pBuffer, 1, length, fout);
    if (num_written == -1) {
      printf("Error: fwrite failed - %s\n", strerror(errno));
      return errno;
    }
    if (num_written == 0 && length)
      return -1;
    length -= num_written;
    pBuffer += num_written;
  }
  return 0;
}


x86 assembly (clang, -O0):
<WriteFileData>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11de <WriteFileData+0xae>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fwrite@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    119d <WriteFileData+0x6d>
mov    0x2e6a(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    %rax,%rsi
lea    0xe79(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11e5 <WriteFileData+0xb5>
cmpl   $0x0,-0x24(%rbp)
jne    11bd <WriteFileData+0x8d>
cmpl   $0x0,-0x14(%rbp)
je     11bd <WriteFileData+0x8d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e5 <WriteFileData+0xb5>
mov    -0x24(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x24(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1143 <WriteFileData+0x13>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<WriteFileData>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %r14d,%r14d
test   %esi,%esi
je     119f <WriteFileData+0x6f>
mov    %rdx,%r15
mov    %esi,%ebp
mov    %rdi,%rbx
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %r15,%rcx
call   1040 <fwrite@plt>
test   %eax,%eax
je     1177 <WriteFileData+0x47>
cmp    $0xffffffff,%eax
je     117f <WriteFileData+0x4f>
movslq %eax,%rcx
add    %rcx,%rbx
sub    %eax,%ebp
jne    1150 <WriteFileData+0x20>
jmp    119f <WriteFileData+0x6f>
mov    $0xffffffff,%r14d
jmp    119f <WriteFileData+0x6f>
mov    0x2e5a(%rip),%rbx
mov    (%rbx),%edi
call   1050 <strerror@plt>
lea    0xe6c(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
mov    (%rbx),%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [27 x i8] c"Error: fwrite failed - %s\0A\00"
@errno = global i32 0
define i32 @WriteFileData(i8* %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32* %2, i32** %7
br label %9
9: ; preds = %31, %3
%10 = load i32, i32* %6
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %39
12: ; preds = %9
%13 = load i8*, i8** %5
%14 = load i32, i32* %6
%15 = load i32*, i32** %7
%16 = call i32 @fwrite(i8* %13, i32 1, i32 %14, i32* %15)
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %24
19: ; preds = %12
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = load i32, i32* @errno
store i32 %23, i32* %4
br label %40
24: ; preds = %12
%25 = load i32, i32* %8
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %24
%28 = load i32, i32* %6
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %27
store i32 -1, i32* %4
br label %40
31: ; preds = %27, %24
%32 = load i32, i32* %8
%33 = load i32, i32* %6
%34 = sub nsw i32 %33, %32
store i32 %34, i32* %6
%35 = load i32, i32* %8
%36 = load i8*, i8** %5
%37 = sext i32 %35 to i64
%38 = getelementptr inbounds i8, i8* %36, i64 %37
store i8* %38, i8** %5
br label %9
39: ; preds = %9
store i32 0, i32* %4
br label %40
40: ; preds = %39, %30, %19
%41 = load i32, i32* %4
ret i32 %41
}

LLVM IR (-O2):
@.str = [27 x i8] c"Error: fwrite failed - %s\0A\00"
@errno = global i32 0
define i32 @WriteFileData(i8* nocapture %0, i32 %1, i32* nocapture %2) {
%4 = icmp eq i32 %1, 0
br i1 %4, label %19, label %5
5: ; preds = %3, %14
%6 = phi i8* [ %17, %14 ], [ %0, %3 ]
%7 = phi i32 [ %15, %14 ], [ %1, %3 ]
%8 = tail call i32 @fwrite(i8* %6, i32 1, i32 %7, i32* %2)
switch i32 %8, label %14 [
i32 -1, label %9
i32 0, label %19
]
9: ; preds = %5
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %11)
%13 = load i32, i32* @errno
br label %19
14: ; preds = %5
%15 = sub nsw i32 %7, %8
%16 = sext i32 %8 to i64
%17 = getelementptr inbounds i8, i8* %6, i64 %16
%18 = icmp eq i32 %15, 0
br i1 %18, label %19, label %5
19: ; preds = %5, %14, %3, %9
%20 = phi i32 [ %13, %9 ], [ 0, %3 ], [ -1, %5 ], [ 0, %14 ]
ret i32 %20
}