#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*/ *fopen(char *, char *);
size_t fwrite(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *);
int pi_write(FILE **file, char *filedata, size_t filesize, char *filename) {
  int rc = 0;
  *file = fopen(filename, "wb");
  if (!*file) {
    printf("Could not create %s\n", filename);
    rc = 1;
    goto cleanup;
  }
  if (fwrite(filedata, 1, filesize, *file) != filesize) {
    printf("Could not write %s\n", filename);
    rc = 1;
    goto cleanup;
  }
cleanup:
  return rc;
}


x86 assembly (clang, -O0):
<pi_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xea6(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1193 <pi_write+0x63>
mov    -0x20(%rbp),%rsi
lea    0xe81(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x24(%rbp)
jmp    11d7 <pi_write+0xa7>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
mov    $0x1,%esi
call   1050 <fwrite@plt>
cmp    -0x18(%rbp),%rax
je     11d2 <pi_write+0xa2>
mov    -0x20(%rbp),%rsi
lea    0xe57(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x24(%rbp)
jmp    11d7 <pi_write+0xa7>
jmp    11d7 <pi_write+0xa7>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pi_write>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0xeb5(%rip),%rsi
mov    %rcx,%rdi
call   1040 <fopen@plt>
mov    %rax,(%rbx)
test   %rax,%rax
je     1181 <pi_write+0x51>
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %rax,%rcx
call   1050 <fwrite@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %r12,%rcx
je     1195 <pi_write+0x65>
lea    0xe99(%rip),%rdi
jmp    1188 <pi_write+0x58>
lea    0xe7b(%rip),%rdi
mov    %r14,%rsi
call   1030 <printf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@.str.1 = [21 x i8] c"Could not create %s\0A\00"
@.str.2 = [20 x i8] c"Could not write %s\0A\00"
define i32 @pi_write(i32** %0, i8* %1, i64 %2, i8* %3) {
%5 = alloca i32**
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32
store i32** %0, i32*** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i8* %3, i8** %8
store i32 0, i32* %9
%10 = load i8*, i8** %8
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%12 = load i32**, i32*** %5
store i32* %11, i32** %12
%13 = load i32**, i32*** %5
%14 = load i32*, i32** %13
%15 = icmp ne i32* %14, null
br i1 %15, label %19, label %16
16: ; preds = %4
%17 = load i8*, i8** %8
%18 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %17)
store i32 1, i32* %9
br label %31
19: ; preds = %4
%20 = load i8*, i8** %6
%21 = load i64, i64* %7
%22 = load i32**, i32*** %5
%23 = load i32*, i32** %22
%24 = call i64 @fwrite(i8* %20, i32 1, i64 %21, i32* %23)
%25 = load i64, i64* %7
%26 = icmp ne i64 %24, %25
br i1 %26, label %27, label %30
27: ; preds = %19
%28 = load i8*, i8** %8
%29 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %28)
store i32 1, i32* %9
br label %31
30: ; preds = %19
br label %31
31: ; preds = %30, %27, %16
%32 = load i32, i32* %9
ret i32 %32
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@.str.1 = [21 x i8] c"Could not create %s\0A\00"
@.str.2 = [20 x i8] c"Could not write %s\0A\00"
define i32 @pi_write(i32** nocapture writeonly %0, i8* nocapture %1, i64 %2, i8* %3) {
%5 = tail call i32* @fopen(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %5, i32** %0
%6 = icmp eq i32* %5, null
br i1 %6, label %10, label %7
7: ; preds = %4
%8 = tail call i64 @fwrite(i8* %1, i32 1, i64 %2, i32* %5)
%9 = icmp eq i64 %8, %2
br i1 %9, label %13, label %10
10: ; preds = %7, %4
%11 = phi i8* [ getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), %4 ], [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), %7 ]
%12 = tail call i32 @printf(i8* %11, i8* %3)
br label %13
13: ; preds = %10, %7
%14 = phi i32 [ 0, %7 ], [ 1, %10 ]
ret i32 %14
}