#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 *, 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("dithered.bin", "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)
lea    0xe96(%rip),%rdi
lea    0xe9c(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    119c <save_image+0x4c>
mov    -0x8(%rbp),%rsi
lea    0xe80(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    122a <save_image+0xda>
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     11db <save_image+0x8b>
mov    -0x8(%rbp),%rsi
mov    -0x24(%rbp),%edx
mov    -0x14(%rbp),%ecx
lea    0xe58(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    122a <save_image+0xda>
mov    -0x20(%rbp),%rdi
call   1050 <fflush@plt>
cmp    $0x0,%rax
je     1205 <save_image+0xb5>
mov    -0x8(%rbp),%rsi
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    122a <save_image+0xda>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
cmp    $0x0,%rax
je     122a <save_image+0xda>
mov    -0x8(%rbp),%rsi
lea    0xe52(%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,%r15d
mov    %rsi,%rbp
mov    %rdi,%r14
lea    0xe99(%rip),%rdi
lea    0xe9f(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11a9 <save_image+0x59>
mov    %rax,%rbx
mov    %rbp,%rdi
mov    %r15d,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1070 <fwrite@plt>
cmp    $0x1,%eax
jne    11c4 <save_image+0x74>
mov    %rbx,%rdi
call   1050 <fflush@plt>
test   %rax,%rax
je     11e4 <save_image+0x94>
lea    0xeb6(%rip),%rdi
jmp    11b0 <save_image+0x60>
lea    0xe60(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <printf@plt>
lea    0xe5c(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
mov    %r15d,%ecx
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
test   %rax,%rax
je     11fa <save_image+0xaa>
lea    0xe7d(%rip),%rdi
jmp    11b0 <save_image+0x60>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [13 x i8] c"dithered.bin\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [23 x i8] c"ERROR opening file %s\0A\00"
@.str.3 = [54 x i8] c"ERROR writing to file %s. Wrote %d bytes of %d total\0A\00"
@.str.4 = [24 x i8] c"ERROR flushing file %s\0A\00"
@.str.5 = [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* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, 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.2, 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.3, 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.4, 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.5, i64 0, i64 0), i8* %0)
br label %23
23: ; preds = %11, %16, %21, %18, %6
ret void
}