#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*/ errx(int, char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
void save_file(char *img, char *file, int size) {
  FILE *img_file;
  img_file = fopen(file, "a+");
  if (img_file != NULL) {
    fwrite(img, 1, size, img_file);
  } else {
    errx(1, "It was not possible to open the image file");
  }
  fclose(img_file);
}


x86 assembly (clang, -O0):
<save_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea2(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     118c <save_file+0x4c>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1060 <fwrite@plt>
jmp    119d <save_file+0x5d>
mov    $0x1,%edi
lea    0xe6b(%rip),%rsi
call   1040 <errx@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_file>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %rsi,%rax
mov    %rdi,%rbp
lea    0xeac(%rip),%rsi
mov    %rax,%rdi
call   1050 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1179 <save_file+0x39>
mov    %rbp,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %rbx,%rcx
call   1060 <fwrite@plt>
jmp    118a <save_file+0x4a>
lea    0xe83(%rip),%rsi
mov    $0x1,%edi
call   1040 <errx@plt>
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <fclose@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"a+\00"
@.str.1 = [43 x i8] c"It was not possible to open the image file\00"
define void @save_file(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%8 = load i8*, i8** %5
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %7
%10 = load i32*, i32** %7
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %17
12: ; preds = %3
%13 = load i8*, i8** %4
%14 = load i32, i32* %6
%15 = load i32*, i32** %7
%16 = call i32 @fwrite(i8* %13, i32 1, i32 %14, i32* %15)
br label %19
17: ; preds = %3
%18 = call i32 @errx(i32 1, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %12
%20 = load i32*, i32** %7
%21 = call i32 @fclose(i32* %20)
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"a+\00"
@.str.1 = [43 x i8] c"It was not possible to open the image file\00"
define void @save_file(i8* nocapture %0, i8* nocapture %1, i32 %2) {
%4 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %8, label %6
6: ; preds = %3
%7 = tail call i32 @fwrite(i8* %0, i32 1, i32 %2, i32* %4)
br label %10
8: ; preds = %3
%9 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
br label %10
10: ; preds = %8, %6
%11 = tail call i32 @fclose(i32* %4)
ret void
}