#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 */
scalar_t__ fclose(int /*<<< orphan*/ *);
scalar_t__ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char const *, ...);
void save_image(const char *filename, unsigned char *data, int len) {
  FILE *fp = fopen(filename, "wb");
  if (fp == NULL) {
    printf("ERROR opening file %s\n", filename);
    return;
  }
  int written = fwrite(data, len, 1, fp);
  if (written != 1) {
    printf("ERROR writing to file %s. Wrote %d bytes of %d total\n", filename,
           written, len);
    return;
  }
  if (fflush(fp) != 0) {
    printf("ERROR flushing file %s\n", filename);
    return;
  }
  if (fclose(fp) != 0) {
    printf("ERROR closing file %s\n", filename);
    return;
  }
}


x86 assembly (clang, -O0):
<save_image>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe92(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1199 <save_image+0x49>
mov    -0x8(%rbp),%rsi
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1227 <save_image+0xd7>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   1070 <fwrite@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x1,-0x24(%rbp)
je     11d8 <save_image+0x88>
mov    -0x8(%rbp),%rsi
mov    -0x24(%rbp),%edx
mov    -0x14(%rbp),%ecx
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1227 <save_image+0xd7>
mov    -0x20(%rbp),%rdi
call   1050 <fflush@plt>
cmp    $0x0,%rax
je     1202 <save_image+0xb2>
mov    -0x8(%rbp),%rsi
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1227 <save_image+0xd7>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
cmp    $0x0,%rax
je     1227 <save_image+0xd7>
mov    -0x8(%rbp),%rsi
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_image>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %rdi,%r15
lea    0xe99(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11a2 <save_image+0x52>
mov    %rax,%rbp
mov    %rbx,%rdi
mov    %r14d,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1070 <fwrite@plt>
cmp    $0x1,%eax
jne    11bd <save_image+0x6d>
mov    %rbp,%rdi
call   1050 <fflush@plt>
test   %rax,%rax
je     11dd <save_image+0x8d>
lea    0xeb0(%rip),%rdi
jmp    11a9 <save_image+0x59>
lea    0xe5a(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <printf@plt>
lea    0xe56(%rip),%rdi
mov    %r15,%rsi
mov    %eax,%edx
mov    %r14d,%ecx
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <printf@plt>
mov    %rbp,%rdi
call   1030 <fclose@plt>
test   %rax,%rax
je     11f3 <save_image+0xa3>
lea    0xe77(%rip),%rdi
jmp    11a9 <save_image+0x59>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@.str.1 = [23 x i8] c"ERROR opening file %s\0A\00"
@.str.2 = [54 x i8] c"ERROR writing to file %s. Wrote %d bytes of %d total\0A\00"
@.str.3 = [24 x i8] c"ERROR flushing file %s\0A\00"
@.str.4 = [23 x i8] c"ERROR closing file %s\0A\00"
define void @save_image(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = load i8*, i8** %4
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %7
%11 = load i32*, i32** %7
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = load i8*, i8** %4
%15 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %14)
br label %42
16: ; preds = %3
%17 = load i8*, i8** %5
%18 = load i32, i32* %6
%19 = load i32*, i32** %7
%20 = call i32 @fwrite(i8* %17, i32 %18, i32 1, i32* %19)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = icmp ne i32 %21, 1
br i1 %22, label %23, label %28
23: ; preds = %16
%24 = load i8*, i8** %4
%25 = load i32, i32* %8
%26 = load i32, i32* %6
%27 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.2, i64 0, i64 0), i8* %24, i32 %25, i32 %26)
br label %42
28: ; preds = %16
%29 = load i32*, i32** %7
%30 = call i64 @fflush(i32* %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = load i8*, i8** %4
%34 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %33)
br label %42
35: ; preds = %28
%36 = load i32*, i32** %7
%37 = call i64 @fclose(i32* %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %35
%40 = load i8*, i8** %4
%41 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8* %40)
br label %42
42: ; preds = %13, %23, %32, %39, %35
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@.str.1 = [23 x i8] c"ERROR opening file %s\0A\00"
@.str.2 = [54 x i8] c"ERROR writing to file %s. Wrote %d bytes of %d total\0A\00"
@.str.3 = [24 x i8] c"ERROR flushing file %s\0A\00"
@.str.4 = [23 x i8] c"ERROR closing file %s\0A\00"
define void @save_image(i8* %0, i8* nocapture %1, i32 %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %23
8: ; preds = %3
%9 = tail call i32 @fwrite(i8* %1, i32 %2, i32 1, i32* %4)
%10 = icmp eq i32 %9, 1
br i1 %10, label %13, label %11
11: ; preds = %8
%12 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %9, i32 %2)
br label %23
13: ; preds = %8
%14 = tail call i64 @fflush(i32* %4)
%15 = icmp eq i64 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %23
18: ; preds = %13
%19 = tail call i64 @fclose(i32* %4)
%20 = icmp eq i64 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %18
%22 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8* %0)
br label %23
23: ; preds = %11, %16, %21, %18, %6
ret void
}