#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_ctrl_cmd_string(int /*<<< orphan*/ *, char *,
                                          char const *, int);
int /*<<< orphan*/ ENGINE_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ ENGINE_load_builtin_engines();
int /*<<< orphan*/ ENGINE_register_all_complete();
char *getenv(char *);
int /*<<< orphan*/ printf(char *, ...);
void register_engines(void) {
  ENGINE *eg;
  const char *val;
  ENGINE_load_builtin_engines();
  eg = ENGINE_by_id("dynamic");
  if (!eg)
    goto out;
  val = getenv("OPENSSL_TPM_SO");
  if (val) {
    printf("OPENSSL_TPM_SO = %s\n", val);
    ENGINE_ctrl_cmd_string(eg, "SO_PATH", val, 0);
    ENGINE_ctrl_cmd_string(eg, "ID", "tpm2tss", 0);
    ENGINE_ctrl_cmd_string(eg, "LIST_ADD", "2", 2);
    if (!ENGINE_ctrl_cmd_string(eg, "LOAD", NULL, 0))
      printf("Failed to load TPM engine\n");
  }
  ENGINE_free(eg);
out:
  ENGINE_register_all_complete();
}


x86 assembly (clang, -O0):
<register_engines>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1080 <ENGINE_load_builtin_engines@plt>
lea    0xe7a(%rip),%rdi
call   1090 <ENGINE_by_id@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    119f <register_engines+0x2f>
jmp    1252 <register_engines+0xe2>
lea    0xe62(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1249 <register_engines+0xd9>
mov    -0x10(%rbp),%rsi
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe51(%rip),%rsi
xor    %ecx,%ecx
call   1050 <ENGINE_ctrl_cmd_string@plt>
mov    -0x8(%rbp),%rdi
lea    0xe47(%rip),%rsi
lea    0xe43(%rip),%rdx
xor    %ecx,%ecx
call   1050 <ENGINE_ctrl_cmd_string@plt>
mov    -0x8(%rbp),%rdi
lea    0xe39(%rip),%rsi
lea    0xe3b(%rip),%rdx
mov    $0x2,%ecx
call   1050 <ENGINE_ctrl_cmd_string@plt>
mov    -0x8(%rbp),%rdi
lea    0xe28(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <ENGINE_ctrl_cmd_string@plt>
cmp    $0x0,%eax
jne    1244 <register_engines+0xd4>
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1249 <register_engines+0xd9>
mov    -0x8(%rbp),%rdi
call   1040 <ENGINE_free@plt>
mov    $0x0,%al
call   1070 <ENGINE_register_all_complete@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<register_engines>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1090 <ENGINE_load_builtin_engines@plt>
lea    0xe6e(%rip),%rdi
call   10a0 <ENGINE_by_id@plt>
test   %rax,%rax
je     123a <register_engines+0xba>
mov    %rax,%r14
lea    0xe5e(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1232 <register_engines+0xb2>
mov    %rax,%rbx
lea    0xe59(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xe5d(%rip),%rsi
mov    %r14,%rdi
mov    %rbx,%rdx
xor    %ecx,%ecx
call   1060 <ENGINE_ctrl_cmd_string@plt>
lea    0xe51(%rip),%rsi
lea    0xe4d(%rip),%rdx
mov    %r14,%rdi
xor    %ecx,%ecx
call   1060 <ENGINE_ctrl_cmd_string@plt>
lea    0xe44(%rip),%rsi
lea    0xe46(%rip),%rdx
mov    %r14,%rdi
mov    $0x2,%ecx
call   1060 <ENGINE_ctrl_cmd_string@plt>
lea    0xe34(%rip),%rsi
mov    %r14,%rdi
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <ENGINE_ctrl_cmd_string@plt>
test   %eax,%eax
jne    1232 <register_engines+0xb2>
lea    0xe22(%rip),%rdi
call   1050 <puts@plt>
mov    %r14,%rdi
call   1040 <ENGINE_free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1080 <ENGINE_register_all_complete@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"dynamic\00"
@.str.1 = [15 x i8] c"OPENSSL_TPM_SO\00"
@.str.2 = [21 x i8] c"OPENSSL_TPM_SO = %s\0A\00"
@.str.3 = [8 x i8] c"SO_PATH\00"
@.str.4 = [3 x i8] c"ID\00"
@.str.5 = [8 x i8] c"tpm2tss\00"
@.str.6 = [9 x i8] c"LIST_ADD\00"
@.str.7 = [2 x i8] c"2\00"
@.str.8 = [5 x i8] c"LOAD\00"
@.str.9 = [27 x i8] c"Failed to load TPM engine\0A\00"
define void @register_engines() {
%1 = alloca i32*
%2 = alloca i8*
%3 = call i32 (...) @ENGINE_load_builtin_engines()
%4 = call i32* @ENGINE_by_id(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i32* %4, i32** %1
%5 = load i32*, i32** %1
%6 = icmp ne i32* %5, null
br i1 %6, label %8, label %7
7: ; preds = %0
br label %31
8: ; preds = %0
%9 = call i8* @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i8* %9, i8** %2
%10 = load i8*, i8** %2
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %28
12: ; preds = %8
%13 = load i8*, i8** %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %13)
%15 = load i32*, i32** %1
%16 = load i8*, i8** %2
%17 = call i32 @ENGINE_ctrl_cmd_string(i32* %15, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %16, i32 0)
%18 = load i32*, i32** %1
%19 = call i32 @ENGINE_ctrl_cmd_string(i32* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i32 0)
%20 = load i32*, i32** %1
%21 = call i32 @ENGINE_ctrl_cmd_string(i32* %20, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0), i32 2)
%22 = load i32*, i32** %1
%23 = call i32 @ENGINE_ctrl_cmd_string(i32* %22, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0), i8* null, i32 0)
%24 = icmp ne i32 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %12
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.9, i64 0, i64 0))
br label %27
27: ; preds = %25, %12
br label %28
28: ; preds = %27, %8
%29 = load i32*, i32** %1
%30 = call i32 @ENGINE_free(i32* %29)
br label %31
31: ; preds = %28, %7
%32 = call i32 (...) @ENGINE_register_all_complete()
ret void
}

LLVM IR (-O2):
@.str = [8 x i8] c"dynamic\00"
@.str.1 = [15 x i8] c"OPENSSL_TPM_SO\00"
@.str.2 = [21 x i8] c"OPENSSL_TPM_SO = %s\0A\00"
@.str.3 = [8 x i8] c"SO_PATH\00"
@.str.4 = [3 x i8] c"ID\00"
@.str.5 = [8 x i8] c"tpm2tss\00"
@.str.6 = [9 x i8] c"LIST_ADD\00"
@.str.7 = [2 x i8] c"2\00"
@.str.8 = [5 x i8] c"LOAD\00"
@str = [26 x i8] c"Failed to load TPM engine\00"
define void @register_engines() {
%1 = tail call i32 (...) @ENGINE_load_builtin_engines()
%2 = tail call i32* @ENGINE_by_id(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %18, label %4
4: ; preds = %0
%5 = tail call i8* @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i8* %5, null
br i1 %6, label %16, label %7
7: ; preds = %4
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %5)
%9 = tail call i32 @ENGINE_ctrl_cmd_string(i32* %2, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %5, i32 0)
%10 = tail call i32 @ENGINE_ctrl_cmd_string(i32* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i32 0)
%11 = tail call i32 @ENGINE_ctrl_cmd_string(i32* %2, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0), i32 2)
%12 = tail call i32 @ENGINE_ctrl_cmd_string(i32* %2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0), i8* null, i32 0)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %7
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
br label %16
16: ; preds = %7, %14, %4
%17 = tail call i32 @ENGINE_free(i32* %2)
br label %18
18: ; preds = %0, %16
%19 = tail call i32 (...) @ENGINE_register_all_complete()
ret void
}