#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;

/* Variables and functions */
int /*<<< orphan*/ ERR_free_strings();
int /*<<< orphan*/ ERR_load_crypto_strings();
int /*<<< orphan*/ ERR_print_errors_fp(int /*<<< orphan*/);
int /*<<< orphan*/ EVP_aes_256_cbc();
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int PEM_write_RSAPrivateKey(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                            int /*<<< orphan*/, int /*<<< orphan*/ *,
                            int /*<<< orphan*/, int /*<<< orphan*/ *,
                            int /*<<< orphan*/ *);
int PEM_write_RSAPublicKey(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ RAND_load_file(char *, int);
int /*<<< orphan*/ RSA_F4;
int RSA_check_key(int /*<<< orphan*/ *);
int /*<<< orphan*/ RSA_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *RSA_generate_key(int, int /*<<< orphan*/,
                                     int /*<<< orphan*/ *,
                                     int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  int retval;
  FILE *file;
  RSA *key_pair;
  ERR_load_crypto_strings();
  RAND_load_file("/dev/urandom", 1024);
  key_pair = RSA_generate_key(1024, RSA_F4, NULL, NULL);
  if (key_pair == NULL) {
    ERR_print_errors_fp(stderr);
    exit(EXIT_FAILURE);
  }
  retval = RSA_check_key(key_pair);
  if (retval <= 0) {
    if (retval == -1) {
      ERR_print_errors_fp(stderr);
    }
    fprintf(stderr, "Problem with keys. They should be regenerated.\n");
    exit(EXIT_FAILURE);
  }
  file = fopen("public.key", "w");
  if (file == NULL) {
    perror("fopen()");
    exit(EXIT_FAILURE);
  }
  retval = PEM_write_RSAPublicKey(file, key_pair);
  if (!retval) {
    ERR_print_errors_fp(stderr);
    exit(EXIT_FAILURE);
  }
  fclose(file);
  file = fopen("private.key", "w");
  if (file == NULL) {
    perror("fopen()");
    exit(EXIT_FAILURE);
  }
  retval = PEM_write_RSAPrivateKey(file, key_pair, EVP_aes_256_cbc(), NULL, 0,
                                   NULL, NULL);
  if (!retval) {
    ERR_print_errors_fp(stderr);
    exit(EXIT_FAILURE);
  }
  fclose(file);
  RSA_free(key_pair);
  ERR_free_strings();
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   10e0 <ERR_load_crypto_strings@plt>
lea    0xdec(%rip),%rdi
mov    $0x400,%esi
call   1030 <RAND_load_file@plt>
mov    0x2da3(%rip),%rax
mov    (%rax),%esi
mov    $0x400,%edi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1110 <RSA_generate_key@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1263 <main+0x73>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
call   1100 <ERR_print_errors_fp@plt>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
mov    -0x28(%rbp),%rdi
call   1040 <RSA_check_key@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jg     12b4 <main+0xc4>
cmpl   $0xffffffff,-0x14(%rbp)
jne    1291 <main+0xa1>
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
call   1100 <ERR_print_errors_fp@plt>
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
lea    0xd6c(%rip),%rsi
call   10a0 <fprintf@plt>
mov    0x2d2b(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
lea    0xd82(%rip),%rdi
lea    0xd86(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12f0 <main+0x100>
lea    0xd6d(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2cef(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1090 <PEM_write_RSAPublicKey@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1326 <main+0x136>
mov    0x2ccf(%rip),%rax
mov    (%rax),%edi
call   1100 <ERR_print_errors_fp@plt>
mov    0x2cb9(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
mov    -0x20(%rbp),%rdi
call   1070 <fclose@plt>
lea    0xd1c(%rip),%rdi
lea    0xd0b(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    136b <main+0x17b>
lea    0xcf2(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2c74(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1050 <EVP_aes_256_cbc@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
xor    %eax,%eax
mov    %eax,%r9d
xor    %r8d,%r8d
mov    %r9,%rcx
movq   $0x0,(%rsp)
call   1060 <PEM_write_RSAPrivateKey@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    13cd <main+0x1dd>
mov    0x2c28(%rip),%rax
mov    (%rax),%edi
call   1100 <ERR_print_errors_fp@plt>
mov    0x2c12(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
mov    -0x20(%rbp),%rdi
call   1070 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1080 <RSA_free@plt>
mov    $0x0,%al
call   10b0 <ERR_free_strings@plt>
mov    0x2be3(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   10e0 <ERR_load_crypto_strings@plt>
lea    0xdfe(%rip),%rdi
mov    $0x400,%esi
call   1030 <RAND_load_file@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
mov    $0x400,%edi
xor    %edx,%edx
xor    %ecx,%ecx
call   1110 <RSA_generate_key@plt>
test   %rax,%rax
jne    123b <main+0x4b>
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
call   1100 <ERR_print_errors_fp@plt>
jmp    12ca <main+0xda>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <RSA_check_key@plt>
test   %eax,%eax
jg     1274 <main+0x84>
cmp    $0xffffffff,%eax
jne    125d <main+0x6d>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
call   1100 <ERR_print_errors_fp@plt>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
lea    0xda0(%rip),%rsi
call   10a0 <fprintf@plt>
jmp    12ca <main+0xda>
lea    0xdc2(%rip),%rdi
lea    0xdc6(%rip),%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
je     12be <main+0xce>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1090 <PEM_write_RSAPublicKey@plt>
test   %eax,%eax
je     1228 <main+0x38>
mov    %r14,%rdi
call   1070 <fclose@plt>
lea    0xda5(%rip),%rdi
lea    0xd94(%rip),%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
jne    12d8 <main+0xe8>
lea    0xd85(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2d07(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1050 <EVP_aes_256_cbc@plt>
movq   $0x0,(%rsp)
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1060 <PEM_write_RSAPrivateKey@plt>
test   %eax,%eax
je     1228 <main+0x38>
mov    %r14,%rdi
call   1070 <fclose@plt>
mov    %rbx,%rdi
call   1080 <RSA_free@plt>
xor    %eax,%eax
call   10b0 <ERR_free_strings@plt>
mov    0x2cab(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@RSA_F4 = global i32 0
@stderr = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [48 x i8] c"Problem with keys. They should be regenerated.\0A\00"
@.str.2 = [11 x i8] c"public.key\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [8 x i8] c"fopen()\00"
@.str.5 = [12 x i8] c"private.key\00"
@EXIT_SUCCESS = 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 (...) @ERR_load_crypto_strings()
%10 = call i32 @RAND_load_file(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 1024)
%11 = load i32, i32* @RSA_F4
%12 = call i32* @RSA_generate_key(i32 1024, i32 %11, i32* null, i32* null)
store i32* %12, i32** %8
%13 = load i32*, i32** %8
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %20
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = call i32 @ERR_print_errors_fp(i32 %16)
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %2
%21 = load i32*, i32** %8
%22 = call i32 @RSA_check_key(i32* %21)
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = icmp sle i32 %23, 0
br i1 %24, label %25, label %36
25: ; preds = %20
%26 = load i32, i32* %6
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = load i32, i32* @stderr
%30 = call i32 @ERR_print_errors_fp(i32 %29)
br label %31
31: ; preds = %28, %25
%32 = load i32, i32* @stderr
%33 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0))
%34 = load i32, i32* @EXIT_FAILURE
%35 = call i32 @exit(i32 %34)
unreachable
36: ; preds = %20
%37 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %37, i32** %7
%38 = load i32*, i32** %7
%39 = icmp eq i32* %38, null
br i1 %39, label %40, label %44
40: ; preds = %36
%41 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%42 = load i32, i32* @EXIT_FAILURE
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %36
%45 = load i32*, i32** %7
%46 = load i32*, i32** %8
%47 = call i32 @PEM_write_RSAPublicKey(i32* %45, i32* %46)
store i32 %47, i32* %6
%48 = load i32, i32* %6
%49 = icmp ne i32 %48, 0
br i1 %49, label %55, label %50
50: ; preds = %44
%51 = load i32, i32* @stderr
%52 = call i32 @ERR_print_errors_fp(i32 %51)
%53 = load i32, i32* @EXIT_FAILURE
%54 = call i32 @exit(i32 %53)
unreachable
55: ; preds = %44
%56 = load i32*, i32** %7
%57 = call i32 @fclose(i32* %56)
%58 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %58, i32** %7
%59 = load i32*, i32** %7
%60 = icmp eq i32* %59, null
br i1 %60, label %61, label %65
61: ; preds = %55
%62 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%63 = load i32, i32* @EXIT_FAILURE
%64 = call i32 @exit(i32 %63)
unreachable
65: ; preds = %55
%66 = load i32*, i32** %7
%67 = load i32*, i32** %8
%68 = call i32 (...) @EVP_aes_256_cbc()
%69 = call i32 @PEM_write_RSAPrivateKey(i32* %66, i32* %67, i32 %68, i32* null, i32 0, i32* null, i32* null)
store i32 %69, i32* %6
%70 = load i32, i32* %6
%71 = icmp ne i32 %70, 0
br i1 %71, label %77, label %72
72: ; preds = %65
%73 = load i32, i32* @stderr
%74 = call i32 @ERR_print_errors_fp(i32 %73)
%75 = load i32, i32* @EXIT_FAILURE
%76 = call i32 @exit(i32 %75)
unreachable
77: ; preds = %65
%78 = load i32*, i32** %7
%79 = call i32 @fclose(i32* %78)
%80 = load i32*, i32** %8
%81 = call i32 @RSA_free(i32* %80)
%82 = call i32 (...) @ERR_free_strings()
%83 = load i32, i32* @EXIT_SUCCESS
%84 = call i32 @exit(i32 %83)
unreachable
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@RSA_F4 = global i32 0
@stderr = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [48 x i8] c"Problem with keys. They should be regenerated.\0A\00"
@.str.2 = [11 x i8] c"public.key\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [8 x i8] c"fopen()\00"
@.str.5 = [12 x i8] c"private.key\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @ERR_load_crypto_strings()
%4 = tail call i32 @RAND_load_file(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 1024)
%5 = load i32, i32* @RSA_F4
%6 = tail call i32* @RSA_generate_key(i32 1024, i32 %5, i32* null, i32* null)
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %13
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 @ERR_print_errors_fp(i32 %9)
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %2
%14 = tail call i32 @RSA_check_key(i32* %6)
%15 = icmp slt i32 %14, 1
br i1 %15, label %16, label %26
16: ; preds = %13
%17 = icmp eq i32 %14, -1
br i1 %17, label %18, label %21
18: ; preds = %16
%19 = load i32, i32* @stderr
%20 = tail call i32 @ERR_print_errors_fp(i32 %19)
br label %21
21: ; preds = %18, %16
%22 = load i32, i32* @stderr
%23 = tail call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = tail call i32 @exit(i32 %24)
unreachable
26: ; preds = %13
%27 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %33
29: ; preds = %26
%30 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = tail call i32 @exit(i32 %31)
unreachable
33: ; preds = %26
%34 = tail call i32 @PEM_write_RSAPublicKey(i32* %27, i32* %6)
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %33
%37 = load i32, i32* @stderr
%38 = tail call i32 @ERR_print_errors_fp(i32 %37)
%39 = load i32, i32* @EXIT_FAILURE
%40 = tail call i32 @exit(i32 %39)
unreachable
41: ; preds = %33
%42 = tail call i32 @fclose(i32* %27)
%43 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%44 = icmp eq i32* %43, null
br i1 %44, label %45, label %49
45: ; preds = %41
%46 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%47 = load i32, i32* @EXIT_FAILURE
%48 = tail call i32 @exit(i32 %47)
unreachable
49: ; preds = %41
%50 = tail call i32 (...) @EVP_aes_256_cbc()
%51 = tail call i32 @PEM_write_RSAPrivateKey(i32* %43, i32* %6, i32 %50, i32* null, i32 0, i32* null, i32* null)
%52 = icmp eq i32 %51, 0
br i1 %52, label %53, label %58
53: ; preds = %49
%54 = load i32, i32* @stderr
%55 = tail call i32 @ERR_print_errors_fp(i32 %54)
%56 = load i32, i32* @EXIT_FAILURE
%57 = tail call i32 @exit(i32 %56)
unreachable
58: ; preds = %49
%59 = tail call i32 @fclose(i32* %43)
%60 = tail call i32 @RSA_free(i32* %6)
%61 = tail call i32 (...) @ERR_free_strings()
%62 = load i32, i32* @EXIT_SUCCESS
%63 = tail call i32 @exit(i32 %62)
unreachable
}