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

/* Variables and functions */
int EINVAL;
int /*<<< orphan*/ *ENGINE_load_public_key(int /*<<< orphan*/ *, char const *,
                                           int /*<<< orphan*/ *,
                                           int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_PKEY_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EVP_PKEY_get1_RSA(int /*<<< orphan*/ *);
int rsa_err(char *);
int rsa_engine_get_pub_key(const char *key_id, ENGINE *engine, RSA **rsap) {
  EVP_PKEY *key;
  RSA *rsa;
  int ret;
  *rsap = NULL;
  key = ENGINE_load_public_key(engine, key_id, NULL, NULL);
  if (!key)
    return rsa_err("Failure loading public key from engine");
  rsa = EVP_PKEY_get1_RSA(key);
  if (!rsa) {
    rsa_err("Couldn't convert to a RSA style key");
    ret = -EINVAL;
    goto err_rsa;
  }
  EVP_PKEY_free(key);
  *rsap = rsa;
  return 0;
err_rsa:
  EVP_PKEY_free(key);
  return ret;
}


x86 assembly (clang, -O0):
<rsa_engine_get_pub_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1060 <ENGINE_load_public_key@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1196 <rsa_engine_get_pub_key+0x56>
lea    0xe77(%rip),%rdi
call   1040 <rsa_err@plt>
mov    %eax,-0x4(%rbp)
jmp    11fc <rsa_engine_get_pub_key+0xbc>
mov    -0x28(%rbp),%rdi
call   1050 <EVP_PKEY_get1_RSA@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11cd <rsa_engine_get_pub_key+0x8d>
lea    0xe72(%rip),%rdi
call   1040 <rsa_err@plt>
xor    %eax,%eax
mov    0x2e1d(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x34(%rbp)
jmp    11ed <rsa_engine_get_pub_key+0xad>
mov    -0x28(%rbp),%rdi
call   1030 <EVP_PKEY_free@plt>
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11fc <rsa_engine_get_pub_key+0xbc>
mov    -0x28(%rbp),%rdi
call   1030 <EVP_PKEY_free@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rsa_engine_get_pub_key>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rdi,%rax
movq   $0x0,(%rdx)
mov    %rsi,%rdi
mov    %rax,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <ENGINE_load_public_key@plt>
test   %rax,%rax
je     1187 <rsa_engine_get_pub_key+0x47>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <EVP_PKEY_get1_RSA@plt>
test   %rax,%rax
je     1197 <rsa_engine_get_pub_key+0x57>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1030 <EVP_PKEY_free@plt>
mov    %rbp,(%r14)
xor    %ebp,%ebp
jmp    11b6 <rsa_engine_get_pub_key+0x76>
lea    0xe72(%rip),%rdi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <rsa_err@plt>
lea    0xe89(%rip),%rdi
call   1040 <rsa_err@plt>
mov    0x2e36(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
mov    %rbx,%rdi
call   1030 <EVP_PKEY_free@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [39 x i8] c"Failure loading public key from engine\00"
@.str.1 = [36 x i8] c"Couldn't convert to a RSA style key\00"
@EINVAL = global i32 0
define i32 @rsa_engine_get_pub_key(i8* %0, i32* %1, i32** %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32**
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32** %2, i32*** %7
%11 = load i32**, i32*** %7
store i32* null, i32** %11
%12 = load i32*, i32** %6
%13 = load i8*, i8** %5
%14 = call i32* @ENGINE_load_public_key(i32* %12, i8* %13, i32* null, i32* null)
store i32* %14, i32** %8
%15 = load i32*, i32** %8
%16 = icmp ne i32* %15, null
br i1 %16, label %19, label %17
17: ; preds = %3
%18 = call i32 @rsa_err(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
store i32 %18, i32* %4
br label %37
19: ; preds = %3
%20 = load i32*, i32** %8
%21 = call i32* @EVP_PKEY_get1_RSA(i32* %20)
store i32* %21, i32** %9
%22 = load i32*, i32** %9
%23 = icmp ne i32* %22, null
br i1 %23, label %28, label %24
24: ; preds = %19
%25 = call i32 @rsa_err(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%26 = load i32, i32* @EINVAL
%27 = sub nsw i32 0, %26
store i32 %27, i32* %10
br label %33
28: ; preds = %19
%29 = load i32*, i32** %8
%30 = call i32 @EVP_PKEY_free(i32* %29)
%31 = load i32*, i32** %9
%32 = load i32**, i32*** %7
store i32* %31, i32** %32
store i32 0, i32* %4
br label %37
33: ; preds = %24
%34 = load i32*, i32** %8
%35 = call i32 @EVP_PKEY_free(i32* %34)
%36 = load i32, i32* %10
store i32 %36, i32* %4
br label %37
37: ; preds = %33, %28, %17
%38 = load i32, i32* %4
ret i32 %38
}

LLVM IR (-O2):
@.str = [39 x i8] c"Failure loading public key from engine\00"
@.str.1 = [36 x i8] c"Couldn't convert to a RSA style key\00"
@EINVAL = global i32 0
define i32 @rsa_engine_get_pub_key(i8* %0, i32* %1, i32** nocapture writeonly %2) {
store i32* null, i32** %2
%4 = tail call i32* @ENGINE_load_public_key(i32* %1, i8* %0, i32* null, i32* null)
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @rsa_err(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
br label %18
8: ; preds = %3
%9 = tail call i32* @EVP_PKEY_get1_RSA(i32* %4)
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %16
11: ; preds = %8
%12 = tail call i32 @rsa_err(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%13 = load i32, i32* @EINVAL
%14 = sub nsw i32 0, %13
%15 = tail call i32 @EVP_PKEY_free(i32* %4)
br label %18
16: ; preds = %8
%17 = tail call i32 @EVP_PKEY_free(i32* %4)
store i32* %9, i32** %2
br label %18
18: ; preds = %11, %16, %6
%19 = phi i32 [ 0, %16 ], [ %14, %11 ], [ %7, %6 ]
ret i32 %19
}