#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*/ ENGINE;

/* Variables and functions */
int /*<<< orphan*/ *ENGINE_by_id(char *);
int /*<<< orphan*/ ENGINE_cleanup();
int /*<<< orphan*/ ENGINE_ctrl_cmd_string(int /*<<< orphan*/ *, char *,
                                          char const *, int /*<<< orphan*/);
int /*<<< orphan*/ ENGINE_finish(int /*<<< orphan*/ *);
int /*<<< orphan*/ ENGINE_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ ENGINE_init(int /*<<< orphan*/ *);
int /*<<< orphan*/ ENGINE_load_builtin_engines();
int /*<<< orphan*/ ENGINE_set_default_RSA(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *getenv(char *);
int /*<<< orphan*/ stderr;
int rsa_engine_init(ENGINE **pe) {
  ENGINE *e;
  int ret;
  const char *key_pass = getenv("KBUILD_SIGN_PIN");
  ENGINE_load_builtin_engines();
  e = ENGINE_by_id("pkcs11");
  if (!e) {
    fprintf(stderr, "Engine isn't available\n");
    ret = -1;
    goto err_engine_by_id;
  }
  if (!ENGINE_init(e)) {
    fprintf(stderr, "Couldn't initialize engine\n");
    ret = -1;
    goto err_engine_init;
  }
  if (key_pass) {
    if (!ENGINE_ctrl_cmd_string(e, "PIN", key_pass, 0)) {
      fprintf(stderr, "Cannot set PKCS#11 PIN\n");
      goto err_set_rsa;
    }
  }
  if (!ENGINE_set_default_RSA(e)) {
    fprintf(stderr, "Couldn't set engine as default for RSA\n");
    ret = -1;
    goto err_set_rsa;
  }
  *pe = e;
  return 0;
err_set_rsa:
  ENGINE_finish(e);
err_engine_init:
  ENGINE_free(e);
err_engine_by_id:
  ENGINE_cleanup();
  return ret;
}


x86 assembly (clang, -O0):
<rsa_engine_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe4d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   10b0 <ENGINE_load_builtin_engines@plt>
lea    0xe46(%rip),%rdi
call   10c0 <ENGINE_by_id@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ff <rsa_engine_init+0x5f>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
lea    0xe29(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x1c(%rbp)
jmp    12d7 <rsa_engine_init+0x137>
mov    -0x18(%rbp),%rdi
call   1050 <ENGINE_init@plt>
cmp    $0x0,%eax
jne    1232 <rsa_engine_init+0x92>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
lea    0xe0e(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x1c(%rbp)
jmp    12ce <rsa_engine_init+0x12e>
cmpq   $0x0,-0x28(%rbp)
je     127b <rsa_engine_init+0xdb>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rdx
lea    0xdc0(%rip),%rsi
xor    %ecx,%ecx
call   1070 <ENGINE_ctrl_cmd_string@plt>
cmp    $0x0,%eax
jne    1276 <rsa_engine_init+0xd6>
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
lea    0xddf(%rip),%rsi
call   1080 <fprintf@plt>
jmp    12c5 <rsa_engine_init+0x125>
jmp    127b <rsa_engine_init+0xdb>
mov    -0x18(%rbp),%rdi
call   1090 <ENGINE_set_default_RSA@plt>
cmp    $0x0,%eax
jne    12ae <rsa_engine_init+0x10e>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
lea    0xdc6(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x1c(%rbp)
jmp    12c5 <rsa_engine_init+0x125>
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    12e4 <rsa_engine_init+0x144>
mov    -0x18(%rbp),%rdi
call   10a0 <ENGINE_finish@plt>
mov    -0x18(%rbp),%rdi
call   1040 <ENGINE_free@plt>
mov    $0x0,%al
call   1060 <ENGINE_cleanup@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rsa_engine_init>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
lea    0xe51(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r15
xor    %eax,%eax
call   10b0 <ENGINE_load_builtin_engines@plt>
lea    0xe4b(%rip),%rdi
call   10c0 <ENGINE_by_id@plt>
test   %rax,%rax
je     120e <rsa_engine_init+0x6e>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <ENGINE_init@plt>
test   %eax,%eax
je     1225 <rsa_engine_init+0x85>
test   %r15,%r15
je     11fb <rsa_engine_init+0x5b>
lea    0xe22(%rip),%rsi
mov    %rbx,%rdi
mov    %r15,%rdx
xor    %ecx,%ecx
call   1070 <ENGINE_ctrl_cmd_string@plt>
test   %eax,%eax
je     1245 <rsa_engine_init+0xa5>
mov    %rbx,%rdi
call   1090 <ENGINE_set_default_RSA@plt>
test   %eax,%eax
je     123c <rsa_engine_init+0x9c>
mov    %rbx,(%r14)
xor    %eax,%eax
jmp    1276 <rsa_engine_init+0xd6>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
lea    0xdf9(%rip),%rsi
call   1080 <fprintf@plt>
jmp    126a <rsa_engine_init+0xca>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
lea    0xdfa(%rip),%rsi
call   1080 <fprintf@plt>
jmp    1262 <rsa_engine_init+0xc2>
lea    0xe20(%rip),%rsi
jmp    124c <rsa_engine_init+0xac>
lea    0xdff(%rip),%rsi
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
call   1080 <fprintf@plt>
mov    %rbx,%rdi
call   10a0 <ENGINE_finish@plt>
mov    %rbx,%rdi
call   1040 <ENGINE_free@plt>
xor    %eax,%eax
call   1060 <ENGINE_cleanup@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [16 x i8] c"KBUILD_SIGN_PIN\00"
@.str.1 = [7 x i8] c"pkcs11\00"
@stderr = global i32 0
@.str.2 = [24 x i8] c"Engine isn't available\0A\00"
@.str.3 = [28 x i8] c"Couldn't initialize engine\0A\00"
@.str.4 = [4 x i8] c"PIN\00"
@.str.5 = [24 x i8] c"Cannot set PKCS PIN\0A\00"
@.str.6 = [40 x i8] c"Couldn't set engine as default for RSA\0A\00"
define i32 @rsa_engine_init(i32** %0) {
%2 = alloca i32
%3 = alloca i32**
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i8*
store i32** %0, i32*** %3
%7 = call i8* @getenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i8* %7, i8** %6
%8 = call i32 (...) @ENGINE_load_builtin_engines()
%9 = call i32* @ENGINE_by_id(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %4
%10 = load i32*, i32** %4
%11 = icmp ne i32* %10, null
br i1 %11, label %15, label %12
12: ; preds = %1
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %5
br label %50
15: ; preds = %1
%16 = load i32*, i32** %4
%17 = call i32 @ENGINE_init(i32* %16)
%18 = icmp ne i32 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %15
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %5
br label %47
22: ; preds = %15
%23 = load i8*, i8** %6
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %34
25: ; preds = %22
%26 = load i32*, i32** %4
%27 = load i8*, i8** %6
%28 = call i32 @ENGINE_ctrl_cmd_string(i32* %26, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %27, i32 0)
%29 = icmp ne i32 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %25
%31 = load i32, i32* @stderr
%32 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
br label %44
33: ; preds = %25
br label %34
34: ; preds = %33, %22
%35 = load i32*, i32** %4
%36 = call i32 @ENGINE_set_default_RSA(i32* %35)
%37 = icmp ne i32 %36, 0
br i1 %37, label %41, label %38
38: ; preds = %34
%39 = load i32, i32* @stderr
%40 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %5
br label %44
41: ; preds = %34
%42 = load i32*, i32** %4
%43 = load i32**, i32*** %3
store i32* %42, i32** %43
store i32 0, i32* %2
br label %53
44: ; preds = %38, %30
%45 = load i32*, i32** %4
%46 = call i32 @ENGINE_finish(i32* %45)
br label %47
47: ; preds = %44, %19
%48 = load i32*, i32** %4
%49 = call i32 @ENGINE_free(i32* %48)
br label %50
50: ; preds = %47, %12
%51 = call i32 (...) @ENGINE_cleanup()
%52 = load i32, i32* %5
store i32 %52, i32* %2
br label %53
53: ; preds = %50, %41
%54 = load i32, i32* %2
ret i32 %54
}

LLVM IR (-O2):
@.str = [16 x i8] c"KBUILD_SIGN_PIN\00"
@.str.1 = [7 x i8] c"pkcs11\00"
@stderr = global i32 0
@.str.2 = [24 x i8] c"Engine isn't available\0A\00"
@.str.3 = [28 x i8] c"Couldn't initialize engine\0A\00"
@.str.4 = [4 x i8] c"PIN\00"
@.str.5 = [24 x i8] c"Cannot set PKCS PIN\0A\00"
@.str.6 = [40 x i8] c"Couldn't set engine as default for RSA\0A\00"
define i32 @rsa_engine_init(i32** nocapture writeonly %0) {
%2 = tail call i8* @getenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%3 = tail call i32 (...) @ENGINE_load_builtin_engines()
%4 = tail call i32* @ENGINE_by_id(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %31
9: ; preds = %1
%10 = tail call i32 @ENGINE_init(i32* %4)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %29
15: ; preds = %9
%16 = icmp eq i8* %2, null
br i1 %16, label %20, label %17
17: ; preds = %15
%18 = tail call i32 @ENGINE_ctrl_cmd_string(i32* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %2, i32 0)
%19 = icmp eq i32 %18, 0
br i1 %19, label %24, label %20
20: ; preds = %17, %15
%21 = tail call i32 @ENGINE_set_default_RSA(i32* %4)
%22 = icmp eq i32 %21, 0
br i1 %22, label %24, label %23
23: ; preds = %20
store i32* %4, i32** %0
br label %33
24: ; preds = %20, %17
%25 = phi i8* [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), %17 ], [ getelementptr inbounds ([40 x i8], [40 x i8]* @.str.6, i64 0, i64 0), %20 ]
%26 = load i32, i32* @stderr
%27 = tail call i32 @fprintf(i32 %26, i8* %25)
%28 = tail call i32 @ENGINE_finish(i32* %4)
br label %29
29: ; preds = %24, %12
%30 = tail call i32 @ENGINE_free(i32* %4)
br label %31
31: ; preds = %29, %6
%32 = tail call i32 (...) @ENGINE_cleanup()
br label %33
33: ; preds = %31, %23
%34 = phi i32 [ 0, %23 ], [ -1, %31 ]
ret i32 %34
}