#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 fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fwrite(void *, size_t, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int rsa_key_save(void *key, size_t size, const char *filename) {
  FILE *file;
  if ((file = fopen(filename, "w")) == NULL) {
    perror("fopen");
    return -1;
  }
  fwrite(key, size, 1, file);
  return fclose(file);
}


x86 assembly (clang, -O0):
<rsa_key_save>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xea1(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    118a <rsa_key_save+0x4a>
lea    0xe89(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ac <rsa_key_save+0x6c>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1060 <fwrite@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rsa_key_save>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xeae(%rip),%rsi
mov    %rdx,%rdi
call   1040 <fopen@plt>
test   %rax,%rax
je     1182 <rsa_key_save+0x42>
mov    %rax,%rbx
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0x1,%edx
mov    %rax,%rcx
call   1060 <fwrite@plt>
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <fclose@plt>
lea    0xe79(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [6 x i8] c"fopen\00"
define i32 @rsa_key_save(i8* nocapture %0, i64 %1, i8* nocapture %2) {
%4 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 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 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %11
8: ; preds = %3
%9 = tail call i32 @fwrite(i8* %0, i64 %1, i32 1, i32* %4)
%10 = tail call i32 @fclose(i32* %4)
br label %11
11: ; preds = %8, %6
%12 = phi i32 [ -1, %6 ], [ %10, %8 ]
ret i32 %12
}