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

/* Variables and functions */
int /*<<< orphan*/ *EVP_PKEY_get1_RSA(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EVP_PKEY_new();
int /*<<< orphan*/ OpenSSL_add_all_algorithms();
int /*<<< orphan*/ PEM_read_PrivateKey(int /*<<< orphan*/ *,
                                       int /*<<< orphan*/ **,
                                       int /*<<< orphan*/ *,
                                       int /*<<< orphan*/ *);
int /*<<< orphan*/ PEM_write_PrivateKey(int /*<<< orphan*/,
                                        int /*<<< orphan*/ *,
                                        int /*<<< orphan*/ *,
                                        int /*<<< orphan*/ *,
                                        int /*<<< orphan*/, int /*<<< orphan*/,
                                        int /*<<< orphan*/ *);
scalar_t__ RSA_check_key(int /*<<< orphan*/ *);
int /*<<< orphan*/ RSA_print_fp(int /*<<< orphan*/, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stdout;
int main(int argc, char **argv) {
  EVP_PKEY *privkey;
  FILE *fp;
  RSA *rsakey;
  OpenSSL_add_all_algorithms();
  privkey = EVP_PKEY_new();
  fp = fopen(argv[1], "r");
  PEM_read_PrivateKey(fp, &privkey, NULL, NULL);
  fclose(fp);
  rsakey = EVP_PKEY_get1_RSA(privkey);
  if (RSA_check_key(rsakey)) {
    printf("RSA key is valid.\n");
  } else {
    printf("Error validating RSA key.\n");
  }
  RSA_print_fp(stdout, rsakey, 3);
  PEM_write_PrivateKey(stdout, privkey, NULL, NULL, 0, 0, NULL);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1060 <OpenSSL_add_all_algorithms@plt>
mov    $0x0,%al
call   1080 <EVP_PKEY_new@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe19(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   10c0 <PEM_read_PrivateKey@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1070 <EVP_PKEY_get1_RSA@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <RSA_check_key@plt>
cmp    $0x0,%rax
je     123e <main+0x8e>
lea    0xdce(%rip),%rdi
call   1050 <printf@plt>
jmp    124a <main+0x9a>
lea    0xdd0(%rip),%rdi
call   1050 <printf@plt>
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rsi
mov    $0x3,%edx
call   1090 <RSA_print_fp@plt>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
xor    %r9d,%r9d
mov    %rcx,%rdx
mov    %r9d,%r8d
movq   $0x0,(%rsp)
call   10b0 <PEM_write_PrivateKey@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
xor    %eax,%eax
call   1060 <OpenSSL_add_all_algorithms@plt>
xor    %eax,%eax
call   1080 <EVP_PKEY_new@plt>
mov    %rax,0x10(%rsp)
mov    0x8(%rbx),%rdi
lea    0xe28(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%rbx
lea    0x10(%rsp),%rsi
mov    %rax,%rdi
xor    %edx,%edx
xor    %ecx,%ecx
call   10c0 <PEM_read_PrivateKey@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    0x10(%rsp),%rdi
call   1070 <EVP_PKEY_get1_RSA@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1030 <RSA_check_key@plt>
test   %rax,%rax
lea    0xdea(%rip),%rax
lea    0xdfd(%rip),%rdi
cmove  %rax,%rdi
call   1040 <puts@plt>
mov    0x2db1(%rip),%r14
mov    (%r14),%edi
mov    %rbx,%rsi
mov    $0x3,%edx
call   1090 <RSA_print_fp@plt>
mov    (%r14),%edi
mov    0x10(%rsp),%rsi
movq   $0x0,(%rsp)
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   10b0 <PEM_write_PrivateKey@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [19 x i8] c"RSA key is valid.\0A\00"
@.str.2 = [27 x i8] c"Error validating RSA key.\0A\00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32 (...) @OpenSSL_add_all_algorithms()
%10 = call i32* (...) @EVP_PKEY_new()
store i32* %10, i32** %6
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 1
%13 = load i8*, i8** %12
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %7
%15 = load i32*, i32** %7
%16 = call i32 @PEM_read_PrivateKey(i32* %15, i32** %6, i32* null, i32* null)
%17 = load i32*, i32** %7
%18 = call i32 @fclose(i32* %17)
%19 = load i32*, i32** %6
%20 = call i32* @EVP_PKEY_get1_RSA(i32* %19)
store i32* %20, i32** %8
%21 = load i32*, i32** %8
%22 = call i64 @RSA_check_key(i32* %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %2
%25 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %28
26: ; preds = %2
%27 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %26, %24
%29 = load i32, i32* @stdout
%30 = load i32*, i32** %8
%31 = call i32 @RSA_print_fp(i32 %29, i32* %30, i32 3)
%32 = load i32, i32* @stdout
%33 = load i32*, i32** %6
%34 = call i32 @PEM_write_PrivateKey(i32 %32, i32* %33, i32* null, i32* null, i32 0, i32 0, i32* null)
%35 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stdout = global i32 0
@str = [26 x i8] c"Error validating RSA key.\00"
@str.3 = [18 x i8] c"RSA key is valid.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32*
%4 = bitcast i32** %3 to i8*
%5 = tail call i32 (...) @OpenSSL_add_all_algorithms()
%6 = tail call i32* (...) @EVP_PKEY_new()
store i32* %6, i32** %3
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @PEM_read_PrivateKey(i32* %9, i32** %3, i32* null, i32* null)
%11 = call i32 @fclose(i32* %9)
%12 = load i32*, i32** %3
%13 = call i32* @EVP_PKEY_get1_RSA(i32* %12)
%14 = call i64 @RSA_check_key(i32* %13)
%15 = icmp eq i64 %14, 0
%16 = select i1 %15, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @str.3, i64 0, i64 0)
%17 = call i32 @puts(i8* dereferenceable(1) %16)
%18 = load i32, i32* @stdout
%19 = call i32 @RSA_print_fp(i32 %18, i32* %13, i32 3)
%20 = load i32, i32* @stdout
%21 = load i32*, i32** %3
%22 = call i32 @PEM_write_PrivateKey(i32 %20, i32* %21, i32* null, i32* null, i32 0, i32 0, i32* null)
%23 = call i32 @exit(i32 0)
unreachable
}