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

/* Variables and functions */
int /*<<< orphan*/ *EVP_PKEY_CTX_new(int /*<<< orphan*/ *,
                                     int /*<<< orphan*/ *);
scalar_t__ EVP_PKEY_CTX_set_rsa_padding(int /*<<< orphan*/ *,
                                        int /*<<< orphan*/);
scalar_t__ EVP_PKEY_decrypt(int /*<<< orphan*/ *, unsigned char *, size_t *,
                            unsigned char *, size_t);
scalar_t__ EVP_PKEY_decrypt_init(int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_PKEY_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ RSA_NO_PADDING;
int /*<<< orphan*/ *get_evp_pkey(char *);
unsigned char *malloc(size_t);
int /*<<< orphan*/ printf(char *, ...);
size_t evp_decrypt(unsigned char *in, size_t inlen, unsigned char **out) {
  size_t outlen = 0;
  EVP_PKEY_CTX *ctx;
  EVP_PKEY *pkey = NULL;
  do {
    if (!out)
      break;
    if (!(pkey = get_evp_pkey("client.key"))) {
      printf("Failed to load private key.\n");
      break;
    }
    ctx = EVP_PKEY_CTX_new(pkey, NULL);
    if (!ctx) {
      printf("fail to create pkey ctx \n");
      break;
    }
    if (EVP_PKEY_decrypt_init(ctx) <= 0) {
      printf("decrypt init fail.\n");
      break;
    }
    if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING) <= 0) {
      printf("Failed to set rsa padding \n");
      break;
    }
    if (EVP_PKEY_decrypt(ctx, NULL, &outlen, in, inlen) <= 0) {
      printf("Failed to determine buffer length \n");
      break;
    }
    *out = malloc(outlen);
    if (!*out) {
      printf("fail to allocate mem.\n");
      break;
    }
    if (EVP_PKEY_decrypt(ctx, *out, &outlen, in, inlen) <= 0) {
      printf("Fail to decrypt \n");
      break;
    }
    for (int i = 0; i < outlen; i++) {
      printf("%02x ", (*out)[i]);
    }
  } while (0);
  EVP_PKEY_free(pkey);
  return outlen;
}


x86 assembly (clang, -O0):
<evp_decrypt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b4 <evp_decrypt+0x34>
jmp    134d <evp_decrypt+0x1cd>
lea    0xe45(%rip),%rdi
call   1080 <get_evp_pkey@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    11e1 <evp_decrypt+0x61>
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    134d <evp_decrypt+0x1cd>
mov    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <EVP_PKEY_CTX_new@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1210 <evp_decrypt+0x90>
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    134d <evp_decrypt+0x1cd>
mov    -0x28(%rbp),%rdi
call   10a0 <EVP_PKEY_decrypt_init@plt>
cmp    $0x0,%rax
jg     1236 <evp_decrypt+0xb6>
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    134d <evp_decrypt+0x1cd>
mov    -0x28(%rbp),%rdi
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
call   1090 <EVP_PKEY_CTX_set_rsa_padding@plt>
cmp    $0x0,%rax
jg     1265 <evp_decrypt+0xe5>
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    134d <evp_decrypt+0x1cd>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    -0x10(%rbp),%r8
xor    %eax,%eax
mov    %eax,%esi
lea    -0x20(%rbp),%rdx
call   1060 <EVP_PKEY_decrypt@plt>
cmp    $0x0,%rax
jg     129b <evp_decrypt+0x11b>
lea    0xde3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    134d <evp_decrypt+0x1cd>
mov    -0x20(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    12cf <evp_decrypt+0x14f>
lea    0xdd3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    134d <evp_decrypt+0x1cd>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
mov    -0x8(%rbp),%rcx
mov    -0x10(%rbp),%r8
lea    -0x20(%rbp),%rdx
call   1060 <EVP_PKEY_decrypt@plt>
cmp    $0x0,%rax
jg     1308 <evp_decrypt+0x188>
lea    0xdb1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    134d <evp_decrypt+0x1cd>
movl   $0x0,-0x34(%rbp)
movslq -0x34(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1348 <evp_decrypt+0x1c8>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movslq -0x34(%rbp),%rcx
movzbl (%rax,%rcx,1),%esi
lea    0xd8c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    130f <evp_decrypt+0x18f>
jmp    134d <evp_decrypt+0x1cd>
mov    -0x30(%rbp),%rdi
call   1050 <EVP_PKEY_free@plt>
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<evp_decrypt>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
movq   $0x0,(%rsp)
test   %rdx,%rdx
je     128e <evp_decrypt+0xfe>
mov    %rdx,%rbp
mov    %rsi,%r15
mov    %rdi,%r12
lea    0xe44(%rip),%rdi
call   1090 <get_evp_pkey@plt>
test   %rax,%rax
je     1293 <evp_decrypt+0x103>
mov    %rax,%r14
mov    %rax,%rdi
xor    %esi,%esi
call   1030 <EVP_PKEY_CTX_new@plt>
test   %rax,%rax
je     12a4 <evp_decrypt+0x114>
mov    %rax,%r13
mov    %rax,%rdi
call   10b0 <EVP_PKEY_decrypt_init@plt>
test   %rax,%rax
jle    12ad <evp_decrypt+0x11d>
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
mov    %r13,%rdi
call   10a0 <EVP_PKEY_CTX_set_rsa_padding@plt>
test   %rax,%rax
jle    12b6 <evp_decrypt+0x126>
mov    %rsp,%rdx
mov    %r13,%rdi
xor    %esi,%esi
mov    %r12,%rcx
mov    %r15,%r8
call   1070 <EVP_PKEY_decrypt@plt>
test   %rax,%rax
jle    12bf <evp_decrypt+0x12f>
mov    (%rsp),%rdi
call   1080 <malloc@plt>
mov    %rax,0x0(%rbp)
test   %rax,%rax
je     12c8 <evp_decrypt+0x138>
mov    %rsp,%rdx
mov    %r13,%rdi
mov    %rax,%rsi
mov    %r12,%rcx
mov    %r15,%r8
call   1070 <EVP_PKEY_decrypt@plt>
test   %rax,%rax
jle    12d1 <evp_decrypt+0x141>
cmpq   $0x0,(%rsp)
je     12dd <evp_decrypt+0x14d>
lea    0xda4(%rip),%r15
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    0x0(%rbp),%rax
movzbl (%rax,%rbx,1),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,(%rsp)
ja     1270 <evp_decrypt+0xe0>
jmp    12dd <evp_decrypt+0x14d>
xor    %r14d,%r14d
jmp    12dd <evp_decrypt+0x14d>
lea    0xd77(%rip),%rdi
call   1040 <puts@plt>
xor    %r14d,%r14d
jmp    12dd <evp_decrypt+0x14d>
lea    0xd82(%rip),%rdi
jmp    12d8 <evp_decrypt+0x148>
lea    0xdf7(%rip),%rdi
jmp    12d8 <evp_decrypt+0x148>
lea    0xdd3(%rip),%rdi
jmp    12d8 <evp_decrypt+0x148>
lea    0xda7(%rip),%rdi
jmp    12d8 <evp_decrypt+0x148>
lea    0xd77(%rip),%rdi
jmp    12d8 <evp_decrypt+0x148>
lea    0xd84(%rip),%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
call   1060 <EVP_PKEY_free@plt>
mov    (%rsp),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"client.key\00"
@.str.1 = [29 x i8] c"Failed to load private key.\0A\00"
@.str.2 = [26 x i8] c"fail to create pkey ctx \0A\00"
@.str.3 = [20 x i8] c"decrypt init fail.\0A\00"
@RSA_NO_PADDING = global i32 0
@.str.4 = [28 x i8] c"Failed to set rsa padding \0A\00"
@.str.5 = [36 x i8] c"Failed to determine buffer length \0A\00"
@.str.6 = [23 x i8] c"fail to allocate mem.\0A\00"
@.str.7 = [18 x i8] c"Fail to decrypt \0A\00"
@.str.8 = [6 x i8] c"%02x \00"
define i64 @evp_decrypt(i8* %0, i64 %1, i8** %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8**
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
store i8** %2, i8*** %6
store i64 0, i64* %7
store i32* null, i32** %9
br label %11
11: ; preds = %3
%12 = load i8**, i8*** %6
%13 = icmp ne i8** %12, null
br i1 %13, label %15, label %14
14: ; preds = %11
br label %86
15: ; preds = %11
%16 = call i32* @get_evp_pkey(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32* %16, i32** %9
%17 = icmp ne i32* %16, null
br i1 %17, label %20, label %18
18: ; preds = %15
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
br label %86
20: ; preds = %15
%21 = load i32*, i32** %9
%22 = call i32* @EVP_PKEY_CTX_new(i32* %21, i32* null)
store i32* %22, i32** %8
%23 = load i32*, i32** %8
%24 = icmp ne i32* %23, null
br i1 %24, label %27, label %25
25: ; preds = %20
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %86
27: ; preds = %20
%28 = load i32*, i32** %8
%29 = call i64 @EVP_PKEY_decrypt_init(i32* %28)
%30 = icmp sle i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %86
33: ; preds = %27
%34 = load i32*, i32** %8
%35 = load i32, i32* @RSA_NO_PADDING
%36 = call i64 @EVP_PKEY_CTX_set_rsa_padding(i32* %34, i32 %35)
%37 = icmp sle i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %33
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
br label %86
40: ; preds = %33
%41 = load i32*, i32** %8
%42 = load i8*, i8** %4
%43 = load i64, i64* %5
%44 = call i64 @EVP_PKEY_decrypt(i32* %41, i8* null, i64* %7, i8* %42, i64 %43)
%45 = icmp sle i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %40
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0))
br label %86
48: ; preds = %40
%49 = load i64, i64* %7
%50 = call i8* @malloc(i64 %49)
%51 = load i8**, i8*** %6
store i8* %50, i8** %51
%52 = load i8**, i8*** %6
%53 = load i8*, i8** %52
%54 = icmp ne i8* %53, null
br i1 %54, label %57, label %55
55: ; preds = %48
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0))
br label %86
57: ; preds = %48
%58 = load i32*, i32** %8
%59 = load i8**, i8*** %6
%60 = load i8*, i8** %59
%61 = load i8*, i8** %4
%62 = load i64, i64* %5
%63 = call i64 @EVP_PKEY_decrypt(i32* %58, i8* %60, i64* %7, i8* %61, i64 %62)
%64 = icmp sle i64 %63, 0
br i1 %64, label %65, label %67
65: ; preds = %57
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0))
br label %86
67: ; preds = %57
store i32 0, i32* %10
br label %68
68: ; preds = %82, %67
%69 = load i32, i32* %10
%70 = sext i32 %69 to i64
%71 = load i64, i64* %7
%72 = icmp ult i64 %70, %71
br i1 %72, label %73, label %85
73: ; preds = %68
%74 = load i8**, i8*** %6
%75 = load i8*, i8** %74
%76 = load i32, i32* %10
%77 = sext i32 %76 to i64
%78 = getelementptr inbounds i8, i8* %75, i64 %77
%79 = load i8, i8* %78
%80 = zext i8 %79 to i32
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0), i32 %80)
br label %82
82: ; preds = %73
%83 = load i32, i32* %10
%84 = add nsw i32 %83, 1
store i32 %84, i32* %10
br label %68
85: ; preds = %68
br label %86
86: ; preds = %85, %65, %55, %46, %38, %31, %25, %18, %14
%87 = load i32*, i32** %9
%88 = call i32 @EVP_PKEY_free(i32* %87)
%89 = load i64, i64* %7
ret i64 %89
}

LLVM IR (-O2):
@.str = [11 x i8] c"client.key\00"
@RSA_NO_PADDING = global i32 0
@.str.8 = [6 x i8] c"%02x \00"
@str = [28 x i8] c"Failed to load private key.\00"
@str.9 = [25 x i8] c"fail to create pkey ctx \00"
@str.10 = [22 x i8] c"fail to allocate mem.\00"
@str.11 = [17 x i8] c"Fail to decrypt \00"
@str.12 = [35 x i8] c"Failed to determine buffer length \00"
@str.13 = [27 x i8] c"Failed to set rsa padding \00"
@str.14 = [19 x i8] c"decrypt init fail.\00"
define i64 @evp_decrypt(i8* %0, i64 %1, i8** %2) {
%4 = alloca i64
%5 = bitcast i64* %4 to i8*
store i64 0, i64* %4
%6 = icmp eq i8** %2, null
br i1 %6, label %57, label %7
7: ; preds = %3
%8 = tail call i32* @get_evp_pkey(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
br label %57
12: ; preds = %7
%13 = tail call i32* @EVP_PKEY_CTX_new(i32* %8, i32* null)
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.9, i64 0, i64 0))
br label %57
17: ; preds = %12
%18 = tail call i64 @EVP_PKEY_decrypt_init(i32* %13)
%19 = icmp slt i64 %18, 1
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.14, i64 0, i64 0))
br label %57
22: ; preds = %17
%23 = load i32, i32* @RSA_NO_PADDING
%24 = tail call i64 @EVP_PKEY_CTX_set_rsa_padding(i32* %13, i32 %23)
%25 = icmp slt i64 %24, 1
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.13, i64 0, i64 0))
br label %57
28: ; preds = %22
%29 = call i64 @EVP_PKEY_decrypt(i32* %13, i8* null, i64* %4, i8* %0, i64 %1)
%30 = icmp slt i64 %29, 1
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.12, i64 0, i64 0))
br label %57
33: ; preds = %28
%34 = load i64, i64* %4
%35 = call i8* @malloc(i64 %34)
store i8* %35, i8** %2
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.10, i64 0, i64 0))
br label %57
39: ; preds = %33
%40 = call i64 @EVP_PKEY_decrypt(i32* %13, i8* %35, i64* %4, i8* %0, i64 %1)
%41 = icmp slt i64 %40, 1
br i1 %41, label %45, label %42
42: ; preds = %39
%43 = load i64, i64* %4
%44 = icmp eq i64 %43, 0
br i1 %44, label %57, label %47
45: ; preds = %39
%46 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.11, i64 0, i64 0))
br label %57
47: ; preds = %42, %47
%48 = phi i64 [ %54, %47 ], [ 0, %42 ]
%49 = load i8*, i8** %2
%50 = getelementptr inbounds i8, i8* %49, i64 %48
%51 = load i8, i8* %50
%52 = zext i8 %51 to i32
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0), i32 %52)
%54 = add nuw i64 %48, 1
%55 = load i64, i64* %4
%56 = icmp ugt i64 %55, %54
br i1 %56, label %47, label %57
57: ; preds = %47, %42, %3, %45, %37, %31, %26, %20, %15, %10
%58 = phi i32* [ %8, %20 ], [ %8, %26 ], [ %8, %31 ], [ %8, %45 ], [ %8, %37 ], [ %8, %15 ], [ null, %10 ], [ null, %3 ], [ %8, %42 ], [ %8, %47 ]
%59 = call i32 @EVP_PKEY_free(i32* %58)
%60 = load i64, i64* %4
ret i64 %60
}