#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*/ 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_X509(int /*<<< orphan*/ *, void *);
int /*<<< orphan*/ printf(char *);
int write_public_key(void *pub_key, char *name) {
  BIO *out = BIO_new(BIO_s_file());
  if (BIO_write_filename(out, name) <= 0 ||
      PEM_write_bio_X509(out, pub_key) == 0) {
    printf("failed to write public key\n");
    return 0;
  }
  BIO_free_all(out);
  return 1;
}


x86 assembly (clang, -O0):
<write_public_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    $0x0,%al
call   1070 <BIO_s_file@plt>
mov    %eax,%edi
call   1050 <BIO_new@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <BIO_write_filename@plt>
cmp    $0x0,%rax
jle    11b0 <write_public_key+0x50>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <PEM_write_bio_X509@plt>
cmp    $0x0,%rax
jne    11c8 <write_public_key+0x68>
lea    0xe49(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11d8 <write_public_key+0x78>
mov    -0x20(%rbp),%rdi
call   1060 <BIO_free_all@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_public_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   1050 <BIO_new@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %rbp,%rsi
call   1080 <BIO_write_filename@plt>
test   %rax,%rax
jle    11b1 <write_public_key+0x51>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <PEM_write_bio_X509@plt>
test   %rax,%rax
je     11b1 <write_public_key+0x51>
mov    %rbx,%rdi
call   1060 <BIO_free_all@plt>
mov    $0x1,%r15d
jmp    11bd <write_public_key+0x5d>
lea    0xe48(%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 = [28 x i8] c"failed to write public key\0A\00"
define i32 @write_public_key(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
store i8* %0, i8** %4
store i8* %1, i8** %5
%7 = call i32 (...) @BIO_s_file()
%8 = call i32* @BIO_new(i32 %7)
store i32* %8, i32** %6
%9 = load i32*, i32** %6
%10 = load i8*, i8** %5
%11 = call i64 @BIO_write_filename(i32* %9, i8* %10)
%12 = icmp sle i64 %11, 0
br i1 %12, label %18, label %13
13: ; preds = %2
%14 = load i32*, i32** %6
%15 = load i8*, i8** %4
%16 = call i64 @PEM_write_bio_X509(i32* %14, i8* %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %13, %2
%19 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %23
20: ; preds = %13
%21 = load i32*, i32** %6
%22 = call i32 @BIO_free_all(i32* %21)
store i32 1, i32* %3
br label %23
23: ; preds = %20, %18
%24 = load i32, i32* %3
ret i32 %24
}

LLVM IR (-O2):
@str = [27 x i8] c"failed to write public key\00"
define i32 @write_public_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_X509(i32* %4, i8* %0)
%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 ([27 x i8], [27 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
}