#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_METHOD_ALL;
int /*<<< orphan*/ *ENGINE_by_id(char *);
int /*<<< orphan*/ ENGINE_finish(int /*<<< orphan*/ *);
int /*<<< orphan*/ ENGINE_free(int /*<<< orphan*/ *);
unsigned int ENGINE_get_table_flags();
int /*<<< orphan*/ ENGINE_init(int /*<<< orphan*/ *);
int /*<<< orphan*/ ENGINE_set_default(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ OpenSSL_add_all_algorithms();
int /*<<< orphan*/ errx(int, char *);
int /*<<< orphan*/ list_engines();
int /*<<< orphan*/ printf(char *, unsigned int);
int /*<<< orphan*/ register_engines();
int /*<<< orphan*/ test_rand(int /*<<< orphan*/ *);
int main(void) {
  ENGINE *eg;
  unsigned flags;
  register_engines();
  list_engines();
  eg = ENGINE_by_id("tpm2tss");
  if (!eg)
    errx(1, "Failed to get TPM2 Engine");
  if (!ENGINE_init(eg))
    errx(1, "Failed to init engine");
  ENGINE_set_default(eg, ENGINE_METHOD_ALL);
  OpenSSL_add_all_algorithms();
  flags = ENGINE_get_table_flags();
  printf("Engine Flags: %X\n", flags);
  test_rand(eg);
  ENGINE_finish(eg);
  ENGINE_free(eg);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10d0 <register_engines@plt>
mov    $0x0,%al
call   1040 <list_engines@plt>
lea    0xe1c(%rip),%rdi
call   10e0 <ENGINE_by_id@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1209 <main+0x49>
mov    $0x1,%edi
lea    0xe04(%rip),%rsi
call   1070 <errx@plt>
mov    -0x10(%rbp),%rdi
call   1060 <ENGINE_init@plt>
cmp    $0x0,%eax
jne    122c <main+0x6c>
mov    $0x1,%edi
lea    0xdfb(%rip),%rsi
call   1070 <errx@plt>
mov    -0x10(%rbp),%rdi
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
call   1050 <ENGINE_set_default@plt>
mov    $0x0,%al
call   1090 <OpenSSL_add_all_algorithms@plt>
mov    $0x0,%al
call   10a0 <ENGINE_get_table_flags@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xddf(%rip),%rdi
call   1080 <printf@plt>
mov    -0x10(%rbp),%rdi
call   10b0 <test_rand@plt>
mov    -0x10(%rbp),%rdi
call   10c0 <ENGINE_finish@plt>
mov    -0x10(%rbp),%rdi
call   1030 <ENGINE_free@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   10d0 <register_engines@plt>
xor    %eax,%eax
call   1040 <list_engines@plt>
lea    0xe2a(%rip),%rdi
call   10e0 <ENGINE_by_id@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11f4 <main+0x34>
lea    0xe1e(%rip),%rsi
mov    $0x1,%edi
call   1070 <errx@plt>
mov    %rbx,%rdi
call   1060 <ENGINE_init@plt>
test   %eax,%eax
jne    1211 <main+0x51>
lea    0xe1b(%rip),%rsi
mov    $0x1,%edi
call   1070 <errx@plt>
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1050 <ENGINE_set_default@plt>
xor    %eax,%eax
call   1090 <OpenSSL_add_all_algorithms@plt>
xor    %eax,%eax
call   10a0 <ENGINE_get_table_flags@plt>
lea    0xe01(%rip),%rdi
mov    %eax,%esi
call   1080 <printf@plt>
mov    %rbx,%rdi
call   10b0 <test_rand@plt>
mov    %rbx,%rdi
call   10c0 <ENGINE_finish@plt>
mov    %rbx,%rdi
call   1030 <ENGINE_free@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [8 x i8] c"tpm2tss\00"
@.str.1 = [26 x i8] c"Failed to get TPM2 Engine\00"
@.str.2 = [22 x i8] c"Failed to init engine\00"
@ENGINE_METHOD_ALL = global i32 0
@.str.3 = [18 x i8] c"Engine Flags: %X\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
store i32 0, i32* %1
%4 = call i32 (...) @register_engines()
%5 = call i32 (...) @list_engines()
%6 = call i32* @ENGINE_by_id(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i32* %6, i32** %2
%7 = load i32*, i32** %2
%8 = icmp ne i32* %7, null
br i1 %8, label %11, label %9
9: ; preds = %0
%10 = call i32 @errx(i32 1, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %11
11: ; preds = %9, %0
%12 = load i32*, i32** %2
%13 = call i32 @ENGINE_init(i32* %12)
%14 = icmp ne i32 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %11
%16 = call i32 @errx(i32 1, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %15, %11
%18 = load i32*, i32** %2
%19 = load i32, i32* @ENGINE_METHOD_ALL
%20 = call i32 @ENGINE_set_default(i32* %18, i32 %19)
%21 = call i32 (...) @OpenSSL_add_all_algorithms()
%22 = call i32 (...) @ENGINE_get_table_flags()
store i32 %22, i32* %3
%23 = load i32, i32* %3
%24 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %23)
%25 = load i32*, i32** %2
%26 = call i32 @test_rand(i32* %25)
%27 = load i32*, i32** %2
%28 = call i32 @ENGINE_finish(i32* %27)
%29 = load i32*, i32** %2
%30 = call i32 @ENGINE_free(i32* %29)
ret i32 0
}

LLVM IR (-O2):
@.str = [8 x i8] c"tpm2tss\00"
@.str.1 = [26 x i8] c"Failed to get TPM2 Engine\00"
@.str.2 = [22 x i8] c"Failed to init engine\00"
@ENGINE_METHOD_ALL = global i32 0
@.str.3 = [18 x i8] c"Engine Flags: %X\0A\00"
define i32 @main() {
%1 = tail call i32 (...) @register_engines()
%2 = tail call i32 (...) @list_engines()
%3 = tail call i32* @ENGINE_by_id(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %7
7: ; preds = %5, %0
%8 = tail call i32 @ENGINE_init(i32* %3)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %12
12: ; preds = %10, %7
%13 = load i32, i32* @ENGINE_METHOD_ALL
%14 = tail call i32 @ENGINE_set_default(i32* %3, i32 %13)
%15 = tail call i32 (...) @OpenSSL_add_all_algorithms()
%16 = tail call i32 (...) @ENGINE_get_table_flags()
%17 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %16)
%18 = tail call i32 @test_rand(i32* %3)
%19 = tail call i32 @ENGINE_finish(i32* %3)
%20 = tail call i32 @ENGINE_free(i32* %3)
ret i32 0
}