#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*/ EVP_CIPHER;
typedef int /*<<< orphan*/ BIO;

/* Variables and functions */
int /*<<< orphan*/ BIO_free_all(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BIO_new(int /*<<< orphan*/);
int /*<<< orphan*/ BIO_s_file();
scalar_t__ BIO_write_filename(int /*<<< orphan*/ *, char *);
scalar_t__ PEM_write_bio_PrivateKey(int /*<<< orphan*/ *, void *,
                                    int /*<<< orphan*/ const *,
                                    int /*<<< orphan*/ *, int /*<<< orphan*/,
                                    int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int write_private_key(void *priv_key, char *name) {
  BIO *out = BIO_new(BIO_s_file());
  const EVP_CIPHER *enc = NULL;
  if (BIO_write_filename(out, name) <= 0 ||
      PEM_write_bio_PrivateKey(out, priv_key, enc, NULL, 0, NULL, NULL) == 0) {
    printf("Failed to write private key\n");
    return 0;
  }
  BIO_free_all(out);
  return 1;
}


x86 assembly (clang, -O0):
<write_private_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    $0x0,%al
call   1070 <BIO_s_file@plt>
mov    %eax,%edi
call   1040 <BIO_new@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <BIO_write_filename@plt>
cmp    $0x0,%rax
jle    11cf <write_private_key+0x6f>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%r9d
xor    %r8d,%r8d
mov    %r9,%rcx
movq   $0x0,(%rsp)
call   1050 <PEM_write_bio_PrivateKey@plt>
cmp    $0x0,%rax
jne    11e7 <write_private_key+0x87>
lea    0xe2a(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11f7 <write_private_key+0x97>
mov    -0x20(%rbp),%rdi
call   1060 <BIO_free_all@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_private_key>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbp
mov    %rdi,%r14
xor    %r15d,%r15d
xor    %eax,%eax
call   1070 <BIO_s_file@plt>
mov    %eax,%edi
call   1040 <BIO_new@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %rbp,%rsi
call   1080 <BIO_write_filename@plt>
test   %rax,%rax
jle    11c3 <write_private_key+0x63>
movq   $0x0,(%rsp)
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1050 <PEM_write_bio_PrivateKey@plt>
test   %rax,%rax
je     11c3 <write_private_key+0x63>
mov    %rbx,%rdi
call   1060 <BIO_free_all@plt>
mov    $0x1,%r15d
jmp    11cf <write_private_key+0x6f>
lea    0xe36(%rip),%rdi
call   1030 <puts@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"Failed to write private key\0A\00"
define i32 @write_private_key(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32*
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = call i32 (...) @BIO_s_file()
%9 = call i32* @BIO_new(i32 %8)
store i32* %9, i32** %6
store i32* null, i32** %7
%10 = load i32*, i32** %6
%11 = load i8*, i8** %5
%12 = call i64 @BIO_write_filename(i32* %10, i8* %11)
%13 = icmp sle i64 %12, 0
br i1 %13, label %20, label %14
14: ; preds = %2
%15 = load i32*, i32** %6
%16 = load i8*, i8** %4
%17 = load i32*, i32** %7
%18 = call i64 @PEM_write_bio_PrivateKey(i32* %15, i8* %16, i32* %17, i32* null, i32 0, i32* null, i32* null)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %14, %2
%21 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %25
22: ; preds = %14
%23 = load i32*, i32** %6
%24 = call i32 @BIO_free_all(i32* %23)
store i32 1, i32* %3
br label %25
25: ; preds = %22, %20
%26 = load i32, i32* %3
ret i32 %26
}

LLVM IR (-O2):
@str = [28 x i8] c"Failed to write private key\00"
define i32 @write_private_key(i8* %0, i8* %1) {
%3 = tail call i32 (...) @BIO_s_file()
%4 = tail call i32* @BIO_new(i32 %3)
%5 = tail call i64 @BIO_write_filename(i32* %4, i8* %1)
%6 = icmp slt i64 %5, 1
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = tail call i64 @PEM_write_bio_PrivateKey(i32* %4, i8* %0, i32* null, i32* null, i32 0, i32* null, i32* null)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %7, %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
br label %14
12: ; preds = %7
%13 = tail call i32 @BIO_free_all(i32* %4)
br label %14
14: ; preds = %12, %10
%15 = phi i32 [ 0, %10 ], [ 1, %12 ]
ret i32 %15
}