#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fwrite(void *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *);
int save_file(char *name, void *buf, int size) {
  FILE *fp;
  fp = fopen(name, "wb");
  if (fp == NULL) {
    printf("Open file %s failed!\n", name);
    return -1;
  }
  fwrite(buf, size, 1, fp);
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<save_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea2(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    118e <save_file+0x4e>
mov    -0x10(%rbp),%rsi
lea    0xe86(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b3 <save_file+0x73>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1060 <fwrite@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbp
lea    0xea9(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1183 <save_file+0x43>
mov    %rax,%rbx
mov    %r15,%rdi
mov    %r14d,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1060 <fwrite@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    1197 <save_file+0x57>
lea    0xe79(%rip),%rdi
mov    %rbp,%rsi
call   1040 <printf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@.str.1 = [22 x i8] c"Open file %s failed!\0A\00"
define i32 @save_file(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i8*, i8** %5
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %8
%11 = load i32*, i32** %8
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = load i8*, i8** %5
%15 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %14)
store i32 -1, i32* %4
br label %23
16: ; preds = %3
%17 = load i8*, i8** %6
%18 = load i32, i32* %7
%19 = load i32*, i32** %8
%20 = call i32 @fwrite(i8* %17, i32 %18, i32 1, i32* %19)
%21 = load i32*, i32** %8
%22 = call i32 @fclose(i32* %21)
store i32 0, i32* %4
br label %23
23: ; preds = %16, %13
%24 = load i32, i32* %4
ret i32 %24
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@.str.1 = [22 x i8] c"Open file %s failed!\0A\00"
define i32 @save_file(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 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %11
8: ; preds = %3
%9 = tail call i32 @fwrite(i8* %1, i32 %2, i32 1, i32* %4)
%10 = tail call i32 @fclose(i32* %4)
br label %11
11: ; preds = %8, %6
%12 = phi i32 [ -1, %6 ], [ 0, %8 ]
ret i32 %12
}