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

/* Variables and functions */
scalar_t__ EVP_aes_192_cbc();
int PEM_write_PrivateKey(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                         int /*<<< orphan*/ *, unsigned char *, int,
                         int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int S_IRUSR;
int S_IWUSR;
int creat(char const *, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int strlen(char *);
int pkey_to_file(EVP_PKEY *key, const char *fname, unsigned char *passphrase) {
  int fd;
  FILE *fp = NULL;
  int result = 0;
  if ((fd = creat(fname, S_IRUSR | S_IWUSR)) != 0 &&
      (fp = fdopen(fd, "w")) != NULL) {
    EVP_CIPHER *cipher = NULL;
    int pp_len = 0;
    if (passphrase != NULL && (pp_len = strlen((char *)passphrase)) != 0)
      cipher = (EVP_CIPHER *)EVP_aes_192_cbc();
    result =
        PEM_write_PrivateKey(fp, key, cipher, passphrase, pp_len, NULL, NULL);
    fclose(fp);
  }
  return result;
}


x86 assembly (clang, -O0):
<pkey_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
mov    0x2e51(%rip),%rax
or     (%rax),%esi
call   1080 <creat@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
je     1233 <pkey_to_file+0xd3>
mov    -0x1c(%rbp),%edi
lea    0xe4c(%rip),%rsi
call   1060 <fdopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1233 <pkey_to_file+0xd3>
movq   $0x0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1201 <pkey_to_file+0xa1>
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0x0,%eax
je     1201 <pkey_to_file+0xa1>
mov    $0x0,%al
call   1040 <EVP_aes_192_cbc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x3c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%r9d
movq   $0x0,(%rsp)
call   1070 <PEM_write_PrivateKey@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x2c(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pkey_to_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%rax
mov    %rdi,%r14
mov    0x2e61(%rip),%rcx
mov    0x2e6a(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
mov    %rax,%rdi
call   1080 <creat@plt>
test   %eax,%eax
je     11c3 <pkey_to_file+0x63>
lea    0xe6b(%rip),%rsi
mov    %eax,%edi
call   1060 <fdopen@plt>
test   %rax,%rax
je     11c3 <pkey_to_file+0x63>
mov    %rax,%rbx
test   %r15,%r15
je     11c7 <pkey_to_file+0x67>
mov    %r15,%rdi
call   1050 <strlen@plt>
test   %eax,%eax
je     11c7 <pkey_to_file+0x67>
mov    %eax,%ebp
xor    %eax,%eax
call   1040 <EVP_aes_192_cbc@plt>
mov    %rax,%rdx
jmp    11cb <pkey_to_file+0x6b>
xor    %eax,%eax
jmp    11f3 <pkey_to_file+0x93>
xor    %edx,%edx
xor    %ebp,%ebp
movq   $0x0,(%rsp)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rcx
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1070 <PEM_write_PrivateKey@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str = [2 x i8] c"w\00"
define i32 @pkey_to_file(i32* %0, i8* %1, i8* %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32
store i32* %0, i32** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
store i32* null, i32** %8
store i32 0, i32* %9
%12 = load i8*, i8** %5
%13 = load i32, i32* @S_IRUSR
%14 = load i32, i32* @S_IWUSR
%15 = or i32 %13, %14
%16 = call i32 @creat(i8* %12, i32 %15)
store i32 %16, i32* %7
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %41
18: ; preds = %3
%19 = load i32, i32* %7
%20 = call i32* @fdopen(i32 %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %20, i32** %8
%21 = icmp ne i32* %20, null
br i1 %21, label %22, label %41
22: ; preds = %18
store i32* null, i32** %10
store i32 0, i32* %11
%23 = load i8*, i8** %6
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %32
25: ; preds = %22
%26 = load i8*, i8** %6
%27 = call i32 @strlen(i8* %26)
store i32 %27, i32* %11
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = call i64 (...) @EVP_aes_192_cbc()
%31 = inttoptr i64 %30 to i32*
store i32* %31, i32** %10
br label %32
32: ; preds = %29, %25, %22
%33 = load i32*, i32** %8
%34 = load i32*, i32** %4
%35 = load i32*, i32** %10
%36 = load i8*, i8** %6
%37 = load i32, i32* %11
%38 = call i32 @PEM_write_PrivateKey(i32* %33, i32* %34, i32* %35, i8* %36, i32 %37, i32* null, i32* null)
store i32 %38, i32* %9
%39 = load i32*, i32** %8
%40 = call i32 @fclose(i32* %39)
br label %41
41: ; preds = %32, %18, %3
%42 = load i32, i32* %9
ret i32 %42
}

LLVM IR (-O2):
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str = [2 x i8] c"w\00"
define i32 @pkey_to_file(i32* %0, i8* %1, i8* %2) {
%4 = load i32, i32* @S_IRUSR
%5 = load i32, i32* @S_IWUSR
%6 = or i32 %5, %4
%7 = tail call i32 @creat(i8* %1, i32 %6)
%8 = icmp eq i32 %7, 0
br i1 %8, label %25, label %9
9: ; preds = %3
%10 = tail call i32* @fdopen(i32 %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %25, label %12
12: ; preds = %9
%13 = icmp eq i8* %2, null
br i1 %13, label %20, label %14
14: ; preds = %12
%15 = tail call i32 @strlen(i8* %2)
%16 = icmp eq i32 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %14
%18 = tail call i64 (...) @EVP_aes_192_cbc()
%19 = inttoptr i64 %18 to i32*
br label %20
20: ; preds = %17, %14, %12
%21 = phi i32* [ %19, %17 ], [ null, %14 ], [ null, %12 ]
%22 = phi i32 [ %15, %17 ], [ 0, %14 ], [ 0, %12 ]
%23 = tail call i32 @PEM_write_PrivateKey(i32* %10, i32* %0, i32* %21, i8* %2, i32 %22, i32* null, i32* null)
%24 = tail call i32 @fclose(i32* %10)
br label %25
25: ; preds = %20, %9, %3
%26 = phi i32 [ %23, %20 ], [ 0, %9 ], [ 0, %3 ]
ret i32 %26
}