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

/* Variables and functions */
int /*<<< orphan*/ BIO_printf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ ENGINE_METHOD_RSA;
int /*<<< orphan*/ *ENGINE_by_id(char const *);
int /*<<< orphan*/ ENGINE_finish(int /*<<< orphan*/ *);
int /*<<< orphan*/ ENGINE_free(int /*<<< orphan*/ *);
char const *ENGINE_get_id(int /*<<< orphan*/ *);
int /*<<< orphan*/ ENGINE_init(int /*<<< orphan*/ *);
int /*<<< orphan*/ ENGINE_load_builtin_engines();
int /*<<< orphan*/ ENGINE_set_default(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ ERR_print_errors(int /*<<< orphan*/ *);
int /*<<< orphan*/ *try_load_engine(int /*<<< orphan*/ *, char const *);
ENGINE *setup_engine(BIO *err, const char *engine) {
  ENGINE *e = NULL;
  ENGINE_load_builtin_engines();
  if (engine) {
    if ((e = ENGINE_by_id(engine)) == NULL &&
        (e = try_load_engine(err, engine)) == NULL) {
      BIO_printf(err, "invalid engine \"%s\"\n", engine);
      ERR_print_errors(err);
      return NULL;
    }
    if (!ENGINE_init(e)) {
      ENGINE_free(e);
      return NULL;
    }
    if (!ENGINE_set_default(e, ENGINE_METHOD_RSA)) {
      BIO_printf(err, "can't use that engine\n");
      ERR_print_errors(err);
      ENGINE_free(e);
      return NULL;
    }
    BIO_printf(err, "engine \"%s\" set.\n", ENGINE_get_id(e));
    ENGINE_finish(e);
    ENGINE_free(e);
  }
  return e;
}


x86 assembly (clang, -O0):
<setup_engine>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    $0x0,%al
call   10a0 <ENGINE_load_builtin_engines@plt>
cmpq   $0x0,-0x18(%rbp)
je     12d4 <setup_engine+0x134>
mov    -0x18(%rbp),%rdi
call   10c0 <ENGINE_by_id@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1228 <setup_engine+0x88>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1090 <try_load_engine@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1228 <setup_engine+0x88>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xdf5(%rip),%rsi
mov    $0x0,%al
call   1070 <BIO_printf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <ERR_print_errors@plt>
movq   $0x0,-0x8(%rbp)
jmp    12dc <setup_engine+0x13c>
mov    -0x20(%rbp),%rdi
call   1060 <ENGINE_init@plt>
cmp    $0x0,%eax
jne    1250 <setup_engine+0xb0>
mov    -0x20(%rbp),%rdi
call   1030 <ENGINE_free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12dc <setup_engine+0x13c>
mov    -0x20(%rbp),%rdi
mov    0x2d95(%rip),%rax
mov    (%rax),%esi
call   1040 <ENGINE_set_default@plt>
cmp    $0x0,%eax
jne    129c <setup_engine+0xfc>
mov    -0x10(%rbp),%rdi
lea    0xd9f(%rip),%rsi
mov    $0x0,%al
call   1070 <BIO_printf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <ERR_print_errors@plt>
mov    -0x20(%rbp),%rdi
call   1030 <ENGINE_free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12dc <setup_engine+0x13c>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   10b0 <ENGINE_get_id@plt>
mov    -0x28(%rbp),%rdi
mov    %rax,%rdx
lea    0xd71(%rip),%rsi
mov    $0x0,%al
call   1070 <BIO_printf@plt>
mov    -0x20(%rbp),%rdi
call   1080 <ENGINE_finish@plt>
mov    -0x20(%rbp),%rdi
call   1030 <ENGINE_free@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_engine>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
xor    %r15d,%r15d
xor    %eax,%eax
call   10a0 <ENGINE_load_builtin_engines@plt>
test   %rbx,%rbx
je     1275 <setup_engine+0xd5>
mov    %rbx,%rdi
call   10c0 <ENGINE_by_id@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11e1 <setup_engine+0x41>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1090 <try_load_engine@plt>
mov    %rax,%r15
test   %rax,%rax
je     1256 <setup_engine+0xb6>
mov    %r15,%rdi
call   1060 <ENGINE_init@plt>
test   %eax,%eax
je     1249 <setup_engine+0xa9>
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   1040 <ENGINE_set_default@plt>
test   %eax,%eax
je     1230 <setup_engine+0x90>
mov    %r15,%rdi
call   10b0 <ENGINE_get_id@plt>
lea    0xe1b(%rip),%rsi
mov    %r14,%rdi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <BIO_printf@plt>
mov    %r15,%rdi
call   1080 <ENGINE_finish@plt>
mov    %r15,%rdi
call   1030 <ENGINE_free@plt>
jmp    1275 <setup_engine+0xd5>
lea    0xdde(%rip),%rsi
mov    %r14,%rdi
xor    %eax,%eax
call   1070 <BIO_printf@plt>
mov    %r14,%rdi
call   1050 <ERR_print_errors@plt>
mov    %r15,%rdi
call   1030 <ENGINE_free@plt>
xor    %r15d,%r15d
jmp    1275 <setup_engine+0xd5>
lea    0xda3(%rip),%rsi
xor    %r15d,%r15d
mov    %r14,%rdi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <BIO_printf@plt>
mov    %r14,%rdi
call   1050 <ERR_print_errors@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [21 x i8] c"invalid engine \22%s\22\0A\00"
@ENGINE_METHOD_RSA = global i32 0
@.str.1 = [23 x i8] c"can't use that engine\0A\00"
@.str.2 = [18 x i8] c"engine \22%s\22 set.\0A\00"
define i32* @setup_engine(i32* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32*
store i32* %0, i32** %4
store i8* %1, i8** %5
store i32* null, i32** %6
%7 = call i32 (...) @ENGINE_load_builtin_engines()
%8 = load i8*, i8** %5
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %53
10: ; preds = %2
%11 = load i8*, i8** %5
%12 = call i32* @ENGINE_by_id(i8* %11)
store i32* %12, i32** %6
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %25
14: ; preds = %10
%15 = load i32*, i32** %4
%16 = load i8*, i8** %5
%17 = call i32* @try_load_engine(i32* %15, i8* %16)
store i32* %17, i32** %6
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %25
19: ; preds = %14
%20 = load i32*, i32** %4
%21 = load i8*, i8** %5
%22 = call i32 (i32*, i8*, ...) @BIO_printf(i32* %20, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = load i32*, i32** %4
%24 = call i32 @ERR_print_errors(i32* %23)
store i32* null, i32** %3
br label %55
25: ; preds = %14, %10
%26 = load i32*, i32** %6
%27 = call i32 @ENGINE_init(i32* %26)
%28 = icmp ne i32 %27, 0
br i1 %28, label %32, label %29
29: ; preds = %25
%30 = load i32*, i32** %6
%31 = call i32 @ENGINE_free(i32* %30)
store i32* null, i32** %3
br label %55
32: ; preds = %25
%33 = load i32*, i32** %6
%34 = load i32, i32* @ENGINE_METHOD_RSA
%35 = call i32 @ENGINE_set_default(i32* %33, i32 %34)
%36 = icmp ne i32 %35, 0
br i1 %36, label %44, label %37
37: ; preds = %32
%38 = load i32*, i32** %4
%39 = call i32 (i32*, i8*, ...) @BIO_printf(i32* %38, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%40 = load i32*, i32** %4
%41 = call i32 @ERR_print_errors(i32* %40)
%42 = load i32*, i32** %6
%43 = call i32 @ENGINE_free(i32* %42)
store i32* null, i32** %3
br label %55
44: ; preds = %32
%45 = load i32*, i32** %4
%46 = load i32*, i32** %6
%47 = call i8* @ENGINE_get_id(i32* %46)
%48 = call i32 (i32*, i8*, ...) @BIO_printf(i32* %45, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %47)
%49 = load i32*, i32** %6
%50 = call i32 @ENGINE_finish(i32* %49)
%51 = load i32*, i32** %6
%52 = call i32 @ENGINE_free(i32* %51)
br label %53
53: ; preds = %44, %2
%54 = load i32*, i32** %6
store i32* %54, i32** %3
br label %55
55: ; preds = %53, %37, %29, %19
%56 = load i32*, i32** %3
ret i32* %56
}

LLVM IR (-O2):
@.str = [21 x i8] c"invalid engine \22%s\22\0A\00"
@ENGINE_METHOD_RSA = global i32 0
@.str.1 = [23 x i8] c"can't use that engine\0A\00"
@.str.2 = [18 x i8] c"engine \22%s\22 set.\0A\00"
define i32* @setup_engine(i32* %0, i8* %1) {
%3 = tail call i32 (...) @ENGINE_load_builtin_engines()
%4 = icmp eq i8* %1, null
br i1 %4, label %33, label %5
5: ; preds = %2
%6 = tail call i32* @ENGINE_by_id(i8* %1)
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %14
8: ; preds = %5
%9 = tail call i32* @try_load_engine(i32* %0, i8* %1)
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = tail call i32 (i32*, i8*, ...) @BIO_printf(i32* %0, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %1)
%13 = tail call i32 @ERR_print_errors(i32* %0)
br label %33
14: ; preds = %8, %5
%15 = phi i32* [ %9, %8 ], [ %6, %5 ]
%16 = tail call i32 @ENGINE_init(i32* %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = tail call i32 @ENGINE_free(i32* %15)
br label %33
20: ; preds = %14
%21 = load i32, i32* @ENGINE_METHOD_RSA
%22 = tail call i32 @ENGINE_set_default(i32* %15, i32 %21)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %20
%25 = tail call i32 (i32*, i8*, ...) @BIO_printf(i32* %0, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%26 = tail call i32 @ERR_print_errors(i32* %0)
%27 = tail call i32 @ENGINE_free(i32* %15)
br label %33
28: ; preds = %20
%29 = tail call i8* @ENGINE_get_id(i32* %15)
%30 = tail call i32 (i32*, i8*, ...) @BIO_printf(i32* %0, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %29)
%31 = tail call i32 @ENGINE_finish(i32* %15)
%32 = tail call i32 @ENGINE_free(i32* %15)
br label %33
33: ; preds = %2, %28, %24, %18, %11
%34 = phi i32* [ null, %11 ], [ null, %24 ], [ null, %18 ], [ %15, %28 ], [ null, %2 ]
ret i32* %34
}