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

/* Variables and functions */
int /*<<< orphan*/ *EVP_aes_128_cbc();
int /*<<< orphan*/ O_RDONLY;
int PEM_write_RSAPrivateKey(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                            int /*<<< orphan*/ const *, int /*<<< orphan*/ *,
                            int /*<<< orphan*/, int /*<<< orphan*/ *,
                            int /*<<< orphan*/ *);
int PEM_write_RSAPublicKey(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ RAND_add(char *, int, int);
int /*<<< orphan*/ RSA_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *RSA_generate_key(int, int, int /*<<< orphan*/ *,
                                     int /*<<< orphan*/ *);
int /*<<< orphan*/ bzero(char *, int);
scalar_t__ close(int);
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int generate_rsa(char *private_file, char *public_file, pem_password_cb *cb) {
  RSA *rsa = NULL;
  FILE *priv = NULL;
  FILE *pub = NULL;
  const EVP_CIPHER *enc;
  int ret, num_read = 0;
  int return_value = 0;
  int dev_random = -1;
  char seed[32];
  dev_random = open("/dev/random", O_RDONLY);
  if (dev_random < 0) {
    return_value = -1;
    goto out;
  }
  printf("Getting random data, this may take a while\n");
  while (num_read < 32) {
    ret = read(dev_random, seed + num_read, sizeof(seed) - num_read);
    if (ret <= 0) {
      return_value = -1;
      goto out;
    } else {
      num_read += ret;
    }
  }
  RAND_add(seed, sizeof(seed), sizeof(seed));
  bzero(seed, sizeof(seed));
  enc = EVP_aes_128_cbc();
  if (!enc) {
    fprintf(stderr, "AES not available\n");
    return_value = -1;
    goto out;
  }
  priv = fopen(private_file, "w");
  if (!priv) {
    fprintf(stderr, "Could not open %s for writing\n", private_file);
    return_value = -1;
    goto out;
  }
  pub = fopen(public_file, "w");
  if (!pub) {
    fprintf(stderr, "Could not open %s for writing\n", public_file);
    return_value = -1;
    goto out;
  }
  rsa = RSA_generate_key(2048, 65537, NULL, NULL);
  if (!rsa) {
    fprintf(stderr, "Could not generate RSA key\n");
    return_value = -1;
    goto out;
  }
  if (!cb) {
    ret = PEM_write_RSAPrivateKey(priv, rsa, NULL, NULL, 0, NULL, NULL);
  } else {
    ret = PEM_write_RSAPrivateKey(priv, rsa, enc, NULL, 0, cb, NULL);
  }
  if (!ret) {
    fprintf(stderr, "Error writing private key\n");
    return_value = -1;
    goto out;
  }
  ret = PEM_write_RSAPublicKey(pub, rsa);
  if (!ret) {
    fprintf(stderr, "Error writing public key\n");
    return_value = -1;
    goto out;
  }
out:
  if (dev_random != -1 && close(dev_random))
    return_value = -1;
  if (pub && fclose(pub))
    return_value = -1;
  if (priv && fclose(priv))
    return_value = -1;
  if (rsa)
    RSA_free(rsa);
  return return_value;
}


x86 assembly (clang, -O0):
<generate_rsa>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
movl   $0xffffffff,-0x48(%rbp)
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
lea    0xdcc(%rip),%rdi
call   10d0 <open@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0x0,-0x48(%rbp)
jge    1252 <generate_rsa+0x72>
movl   $0xffffffff,-0x44(%rbp)
jmp    14a7 <generate_rsa+0x2c7>
lea    0xdb3(%rip),%rdi
call   1060 <printf@plt>
cmpl   $0x20,-0x40(%rbp)
jge    12b0 <generate_rsa+0xd0>
mov    -0x48(%rbp),%edi
lea    -0x70(%rbp),%rsi
movslq -0x40(%rbp),%rax
add    %rax,%rsi
movslq -0x40(%rbp),%rcx
mov    $0x20,%eax
sub    %rcx,%rax
mov    %eax,%edx
call   1080 <read@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jg     12a2 <generate_rsa+0xc2>
movl   $0xffffffff,-0x44(%rbp)
jmp    14a7 <generate_rsa+0x2c7>
mov    -0x3c(%rbp),%eax
add    -0x40(%rbp),%eax
mov    %eax,-0x40(%rbp)
jmp    125e <generate_rsa+0x7e>
lea    -0x70(%rbp),%rdi
mov    $0x20,%edx
mov    %edx,%esi
call   1090 <RAND_add@plt>
lea    -0x70(%rbp),%rdi
mov    $0x20,%esi
call   10f0 <bzero@plt>
mov    $0x0,%al
call   10c0 <EVP_aes_128_cbc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1307 <generate_rsa+0x127>
mov    0x2ced(%rip),%rax
mov    (%rax),%edi
lea    0xd44(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
movl   $0xffffffff,-0x44(%rbp)
jmp    14a7 <generate_rsa+0x2c7>
mov    -0x8(%rbp),%rdi
lea    0xd39(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    134d <generate_rsa+0x16d>
mov    0x2cab(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xd13(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
movl   $0xffffffff,-0x44(%rbp)
jmp    14a7 <generate_rsa+0x2c7>
mov    -0x10(%rbp),%rdi
lea    0xcf3(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1393 <generate_rsa+0x1b3>
mov    0x2c65(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xccd(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
movl   $0xffffffff,-0x44(%rbp)
jmp    14a7 <generate_rsa+0x2c7>
mov    $0x800,%edi
mov    $0x10001,%esi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1100 <RSA_generate_key@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    13db <generate_rsa+0x1fb>
mov    0x2c19(%rip),%rax
mov    (%rax),%edi
lea    0xca4(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
movl   $0xffffffff,-0x44(%rbp)
jmp    14a7 <generate_rsa+0x2c7>
cmpq   $0x0,-0x18(%rbp)
jne    1411 <generate_rsa+0x231>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r9d
xor    %r8d,%r8d
mov    %r9,%rdx
mov    %r9,%rcx
movq   $0x0,(%rsp)
call   1030 <PEM_write_RSAPrivateKey@plt>
mov    %eax,-0x3c(%rbp)
jmp    1438 <generate_rsa+0x258>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x18(%rbp),%r9
xor    %eax,%eax
mov    %eax,%ecx
xor    %r8d,%r8d
movq   $0x0,(%rsp)
call   1030 <PEM_write_RSAPrivateKey@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jne    1465 <generate_rsa+0x285>
mov    0x2b8f(%rip),%rax
mov    (%rax),%edi
lea    0xc36(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
movl   $0xffffffff,-0x44(%rbp)
jmp    14a7 <generate_rsa+0x2c7>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   10a0 <PEM_write_RSAPublicKey@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jne    14a2 <generate_rsa+0x2c2>
mov    0x2b52(%rip),%rax
mov    (%rax),%edi
lea    0xc14(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
movl   $0xffffffff,-0x44(%rbp)
jmp    14a7 <generate_rsa+0x2c7>
jmp    14a7 <generate_rsa+0x2c7>
cmpl   $0xffffffff,-0x48(%rbp)
je     14ca <generate_rsa+0x2ea>
mov    -0x48(%rbp),%edi
call   1070 <close@plt>
cmp    $0x0,%rax
je     14ca <generate_rsa+0x2ea>
movl   $0xffffffff,-0x44(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     14ef <generate_rsa+0x30f>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
cmp    $0x0,%rax
je     14ef <generate_rsa+0x30f>
movl   $0xffffffff,-0x44(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1514 <generate_rsa+0x334>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
cmp    $0x0,%rax
je     1514 <generate_rsa+0x334>
movl   $0xffffffff,-0x44(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1528 <generate_rsa+0x348>
mov    -0x20(%rbp),%rdi
call   1050 <RSA_free@plt>
mov    -0x44(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate_rsa>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r12
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
lea    0xdf9(%rip),%rdi
call   10d0 <open@plt>
mov    %eax,%ebp
mov    $0xffffffff,%r13d
xor    %r14d,%r14d
test   %eax,%eax
js     1356 <generate_rsa+0x176>
mov    %rbx,0x8(%rsp)
lea    0xe66(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movslq %ebx,%rax
lea    (%rsp,%rax,1),%rsi
add    $0x10,%rsi
mov    $0x20,%edx
sub    %eax,%edx
mov    %ebp,%edi
call   1080 <read@plt>
test   %eax,%eax
jle    1356 <generate_rsa+0x176>
add    %eax,%ebx
cmp    $0x1f,%ebx
jle    1240 <generate_rsa+0x60>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x20,%esi
mov    $0x20,%edx
call   1090 <RAND_add@plt>
mov    %r14,%rdi
mov    $0x20,%esi
call   10f0 <bzero@plt>
xor    %eax,%eax
call   10c0 <EVP_aes_128_cbc@plt>
test   %rax,%rax
je     131d <generate_rsa+0x13d>
mov    %rax,%rbx
lea    0xd79(%rip),%rsi
mov    %r12,%rdi
call   10e0 <fopen@plt>
test   %rax,%rax
je     1339 <generate_rsa+0x159>
mov    %rax,%r14
lea    0xd5e(%rip),%rsi
mov    %r15,%rdi
call   10e0 <fopen@plt>
test   %rax,%rax
je     13b9 <generate_rsa+0x1d9>
mov    %rax,%r12
mov    $0x800,%edi
mov    $0x10001,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1100 <RSA_generate_key@plt>
test   %rax,%rax
je     13d8 <generate_rsa+0x1f8>
mov    %rax,%r15
mov    0x8(%rsp),%r9
test   %r9,%r9
je     13ed <generate_rsa+0x20d>
movq   $0x0,(%rsp)
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
jmp    1405 <generate_rsa+0x225>
mov    0x2cb4(%rip),%rax
mov    (%rax),%edi
lea    0xcdf(%rip),%rsi
xor    %r14d,%r14d
xor    %eax,%eax
call   10b0 <fprintf@plt>
jmp    1356 <generate_rsa+0x176>
mov    0x2c98(%rip),%rax
mov    (%rax),%edi
lea    0xcd8(%rip),%rsi
xor    %r14d,%r14d
mov    %r12,%rdx
xor    %eax,%eax
call   10b0 <fprintf@plt>
xor    %r12d,%r12d
xor    %r15d,%r15d
cmp    $0xffffffff,%ebp
je     1370 <generate_rsa+0x190>
mov    %ebp,%edi
call   1070 <close@plt>
neg    %rax
sbb    %eax,%eax
or     %eax,%r13d
test   %r12,%r12
je     1385 <generate_rsa+0x1a5>
mov    %r12,%rdi
call   1050 <fclose@plt>
neg    %rax
sbb    %eax,%eax
or     %eax,%r13d
test   %r14,%r14
je     139a <generate_rsa+0x1ba>
mov    %r14,%rdi
call   1050 <fclose@plt>
neg    %rax
sbb    %eax,%eax
or     %eax,%r13d
test   %r15,%r15
je     13a7 <generate_rsa+0x1c7>
mov    %r15,%rdi
call   1060 <RSA_free@plt>
mov    %r13d,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c18(%rip),%rax
mov    (%rax),%edi
lea    0xc58(%rip),%rsi
xor    %r12d,%r12d
mov    %r15,%rdx
xor    %eax,%eax
call   10b0 <fprintf@plt>
jmp    1359 <generate_rsa+0x179>
mov    0x2bf9(%rip),%rax
mov    (%rax),%edi
lea    0xc58(%rip),%rsi
xor    %r15d,%r15d
jmp    1447 <generate_rsa+0x267>
movq   $0x0,(%rsp)
mov    %r14,%rdi
mov    %r15,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1040 <PEM_write_RSAPrivateKey@plt>
test   %eax,%eax
je     1425 <generate_rsa+0x245>
mov    %r12,%rdi
mov    %r15,%rsi
call   10a0 <PEM_write_RSAPublicKey@plt>
test   %eax,%eax
je     1437 <generate_rsa+0x257>
xor    %r13d,%r13d
jmp    135c <generate_rsa+0x17c>
mov    0x2bac(%rip),%rax
mov    (%rax),%edi
lea    0xc27(%rip),%rsi
jmp    1447 <generate_rsa+0x267>
mov    0x2b9a(%rip),%rax
mov    (%rax),%edi
lea    0xc30(%rip),%rsi
xor    %eax,%eax
call   10b0 <fprintf@plt>
jmp    135c <generate_rsa+0x17c>

LLVM IR (-O0):
@.str = [12 x i8] c"/dev/random\00"
@O_RDONLY = global i32 0
@.str.1 = [44 x i8] c"Getting random data, this may take a while\0A\00"
@stderr = global i32 0
@.str.2 = [19 x i8] c"AES not available\0A\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [31 x i8] c"Could not open %s for writing\0A\00"
@.str.5 = [28 x i8] c"Could not generate RSA key\0A\00"
@.str.6 = [27 x i8] c"Error writing private key\0A\00"
@.str.7 = [26 x i8] c"Error writing public key\0A\00"
define i32 @generate_rsa(i8* %0, i8* %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca [32 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32* %2, i32** %6
store i32* null, i32** %7
store i32* null, i32** %8
store i32* null, i32** %9
store i32 0, i32* %12
store i32 0, i32* %13
store i32 -1, i32* %14
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %16)
store i32 %17, i32* %14
%18 = load i32, i32* %14
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %3
store i32 -1, i32* %13
br label %110
21: ; preds = %3
%22 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %44, %21
%24 = load i32, i32* %12
%25 = icmp slt i32 %24, 32
br i1 %25, label %26, label %45
26: ; preds = %23
%27 = load i32, i32* %14
%28 = getelementptr inbounds [32 x i8], [32 x i8]* %15, i64 0, i64 0
%29 = load i32, i32* %12
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
%32 = load i32, i32* %12
%33 = sext i32 %32 to i64
%34 = sub i64 32, %33
%35 = trunc i64 %34 to i32
%36 = call i32 @read(i32 %27, i8* %31, i32 %35)
store i32 %36, i32* %11
%37 = load i32, i32* %11
%38 = icmp sle i32 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %26
store i32 -1, i32* %13
br label %110
40: ; preds = %26
%41 = load i32, i32* %11
%42 = load i32, i32* %12
%43 = add nsw i32 %42, %41
store i32 %43, i32* %12
br label %44
44: ; preds = %40
br label %23
45: ; preds = %23
%46 = getelementptr inbounds [32 x i8], [32 x i8]* %15, i64 0, i64 0
%47 = call i32 @RAND_add(i8* %46, i32 32, i32 32)
%48 = getelementptr inbounds [32 x i8], [32 x i8]* %15, i64 0, i64 0
%49 = call i32 @bzero(i8* %48, i32 32)
%50 = call i32* (...) @EVP_aes_128_cbc()
store i32* %50, i32** %10
%51 = load i32*, i32** %10
%52 = icmp ne i32* %51, null
br i1 %52, label %56, label %53
53: ; preds = %45
%54 = load i32, i32* @stderr
%55 = call i32 (i32, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %13
br label %110
56: ; preds = %45
%57 = load i8*, i8** %4
%58 = call i32* @fopen(i8* %57, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %58, i32** %8
%59 = load i32*, i32** %8
%60 = icmp ne i32* %59, null
br i1 %60, label %65, label %61
61: ; preds = %56
%62 = load i32, i32* @stderr
%63 = load i8*, i8** %4
%64 = call i32 (i32, i8*, ...) @fprintf(i32 %62, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %63)
store i32 -1, i32* %13
br label %110
65: ; preds = %56
%66 = load i8*, i8** %5
%67 = call i32* @fopen(i8* %66, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %67, i32** %9
%68 = load i32*, i32** %9
%69 = icmp ne i32* %68, null
br i1 %69, label %74, label %70
70: ; preds = %65
%71 = load i32, i32* @stderr
%72 = load i8*, i8** %5
%73 = call i32 (i32, i8*, ...) @fprintf(i32 %71, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %72)
store i32 -1, i32* %13
br label %110
74: ; preds = %65
%75 = call i32* @RSA_generate_key(i32 2048, i32 65537, i32* null, i32* null)
store i32* %75, i32** %7
%76 = load i32*, i32** %7
%77 = icmp ne i32* %76, null
br i1 %77, label %81, label %78
78: ; preds = %74
%79 = load i32, i32* @stderr
%80 = call i32 (i32, i8*, ...) @fprintf(i32 %79, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %13
br label %110
81: ; preds = %74
%82 = load i32*, i32** %6
%83 = icmp ne i32* %82, null
br i1 %83, label %88, label %84
84: ; preds = %81
%85 = load i32*, i32** %8
%86 = load i32*, i32** %7
%87 = call i32 @PEM_write_RSAPrivateKey(i32* %85, i32* %86, i32* null, i32* null, i32 0, i32* null, i32* null)
store i32 %87, i32* %11
br label %94
88: ; preds = %81
%89 = load i32*, i32** %8
%90 = load i32*, i32** %7
%91 = load i32*, i32** %10
%92 = load i32*, i32** %6
%93 = call i32 @PEM_write_RSAPrivateKey(i32* %89, i32* %90, i32* %91, i32* null, i32 0, i32* %92, i32* null)
store i32 %93, i32* %11
br label %94
94: ; preds = %88, %84
%95 = load i32, i32* %11
%96 = icmp ne i32 %95, 0
br i1 %96, label %100, label %97
97: ; preds = %94
%98 = load i32, i32* @stderr
%99 = call i32 (i32, i8*, ...) @fprintf(i32 %98, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %13
br label %110
100: ; preds = %94
%101 = load i32*, i32** %9
%102 = load i32*, i32** %7
%103 = call i32 @PEM_write_RSAPublicKey(i32* %101, i32* %102)
store i32 %103, i32* %11
%104 = load i32, i32* %11
%105 = icmp ne i32 %104, 0
br i1 %105, label %109, label %106
106: ; preds = %100
%107 = load i32, i32* @stderr
%108 = call i32 (i32, i8*, ...) @fprintf(i32 %107, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0))
store i32 -1, i32* %13
br label %110
109: ; preds = %100
br label %110
110: ; preds = %109, %106, %97, %78, %70, %61, %53, %39, %20
%111 = load i32, i32* %14
%112 = icmp ne i32 %111, -1
br i1 %112, label %113, label %118
113: ; preds = %110
%114 = load i32, i32* %14
%115 = call i64 @close(i32 %114)
%116 = icmp ne i64 %115, 0
br i1 %116, label %117, label %118
117: ; preds = %113
store i32 -1, i32* %13
br label %118
118: ; preds = %117, %113, %110
%119 = load i32*, i32** %9
%120 = icmp ne i32* %119, null
br i1 %120, label %121, label %126
121: ; preds = %118
%122 = load i32*, i32** %9
%123 = call i64 @fclose(i32* %122)
%124 = icmp ne i64 %123, 0
br i1 %124, label %125, label %126
125: ; preds = %121
store i32 -1, i32* %13
br label %126
126: ; preds = %125, %121, %118
%127 = load i32*, i32** %8
%128 = icmp ne i32* %127, null
br i1 %128, label %129, label %134
129: ; preds = %126
%130 = load i32*, i32** %8
%131 = call i64 @fclose(i32* %130)
%132 = icmp ne i64 %131, 0
br i1 %132, label %133, label %134
133: ; preds = %129
store i32 -1, i32* %13
br label %134
134: ; preds = %133, %129, %126
%135 = load i32*, i32** %7
%136 = icmp ne i32* %135, null
br i1 %136, label %137, label %140
137: ; preds = %134
%138 = load i32*, i32** %7
%139 = call i32 @RSA_free(i32* %138)
br label %140
140: ; preds = %137, %134
%141 = load i32, i32* %13
ret i32 %141
}

LLVM IR (-O2):
@.str = [12 x i8] c"/dev/random\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.2 = [19 x i8] c"AES not available\0A\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [31 x i8] c"Could not open %s for writing\0A\00"
@.str.5 = [28 x i8] c"Could not generate RSA key\0A\00"
@.str.6 = [27 x i8] c"Error writing private key\0A\00"
@.str.7 = [26 x i8] c"Error writing public key\0A\00"
@str = [43 x i8] c"Getting random data, this may take a while\00"
define i32 @generate_rsa(i8* %0, i8* %1, i32* %2) {
%4 = alloca [32 x i8]
%5 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %65, label %9
9: ; preds = %3
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str, i64 0, i64 0))
br label %13
11: ; preds = %13
%12 = icmp slt i32 %20, 32
br i1 %12, label %13, label %21
13: ; preds = %9, %11
%14 = phi i32 [ 0, %9 ], [ %20, %11 ]
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 %15
%17 = sub i32 32, %14
%18 = call i32 @read(i32 %7, i8* %16, i32 %17)
%19 = icmp slt i32 %18, 1
%20 = add nsw i32 %18, %14
br i1 %19, label %65, label %11
21: ; preds = %11
%22 = call i32 @RAND_add(i8* %5, i32 32, i32 32)
%23 = call i32 @bzero(i8* %5, i32 32)
%24 = call i32* (...) @EVP_aes_128_cbc()
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %65
29: ; preds = %21
%30 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i32, i32* @stderr
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %0)
br label %65
35: ; preds = %29
%36 = call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%37 = icmp eq i32* %36, null
br i1 %37, label %38, label %41
38: ; preds = %35
%39 = load i32, i32* @stderr
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %1)
br label %65
41: ; preds = %35
%42 = call i32* @RSA_generate_key(i32 2048, i32 65537, i32* null, i32* null)
%43 = icmp eq i32* %42, null
br i1 %43, label %44, label %47
44: ; preds = %41
%45 = load i32, i32* @stderr
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0))
br label %65
47: ; preds = %41
%48 = icmp eq i32* %2, null
br i1 %48, label %49, label %51
49: ; preds = %47
%50 = call i32 @PEM_write_RSAPrivateKey(i32* %30, i32* %42, i32* null, i32* null, i32 0, i32* null, i32* null)
br label %53
51: ; preds = %47
%52 = call i32 @PEM_write_RSAPrivateKey(i32* %30, i32* %42, i32* %24, i32* null, i32 0, i32* %2, i32* null)
br label %53
53: ; preds = %51, %49
%54 = phi i32 [ %52, %51 ], [ %50, %49 ]
%55 = icmp eq i32 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %53
%57 = load i32, i32* @stderr
%58 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0))
br label %65
59: ; preds = %53
%60 = call i32 @PEM_write_RSAPublicKey(i32* %36, i32* %42)
%61 = icmp eq i32 %60, 0
br i1 %61, label %62, label %65
62: ; preds = %59
%63 = load i32, i32* @stderr
%64 = call i32 (i32, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0))
br label %65
65: ; preds = %13, %3, %59, %62, %56, %44, %38, %32, %26
%66 = phi i32* [ %30, %59 ], [ %30, %62 ], [ %30, %56 ], [ %30, %44 ], [ %30, %38 ], [ null, %32 ], [ null, %26 ], [ null, %3 ], [ null, %13 ]
%67 = phi i32* [ %36, %59 ], [ %36, %62 ], [ %36, %56 ], [ %36, %44 ], [ null, %38 ], [ null, %32 ], [ null, %26 ], [ null, %3 ], [ null, %13 ]
%68 = phi i32* [ %42, %59 ], [ %42, %62 ], [ %42, %56 ], [ null, %44 ], [ null, %38 ], [ null, %32 ], [ null, %26 ], [ null, %3 ], [ null, %13 ]
%69 = phi i32 [ 0, %59 ], [ -1, %62 ], [ -1, %56 ], [ -1, %44 ], [ -1, %38 ], [ -1, %32 ], [ -1, %26 ], [ -1, %3 ], [ -1, %13 ]
%70 = icmp eq i32 %7, -1
br i1 %70, label %75, label %71
71: ; preds = %65
%72 = call i64 @close(i32 %7)
%73 = icmp eq i64 %72, 0
%74 = select i1 %73, i32 %69, i32 -1
br label %75
75: ; preds = %71, %65
%76 = phi i32 [ %69, %65 ], [ %74, %71 ]
%77 = icmp eq i32* %67, null
br i1 %77, label %82, label %78
78: ; preds = %75
%79 = call i64 @fclose(i32* %67)
%80 = icmp eq i64 %79, 0
%81 = select i1 %80, i32 %76, i32 -1
br label %82
82: ; preds = %78, %75
%83 = phi i32 [ %76, %75 ], [ %81, %78 ]
%84 = icmp eq i32* %66, null
br i1 %84, label %89, label %85
85: ; preds = %82
%86 = call i64 @fclose(i32* %66)
%87 = icmp eq i64 %86, 0
%88 = select i1 %87, i32 %83, i32 -1
br label %89
89: ; preds = %85, %82
%90 = phi i32 [ %83, %82 ], [ %88, %85 ]
%91 = icmp eq i32* %68, null
br i1 %91, label %94, label %92
92: ; preds = %89
%93 = call i32 @RSA_free(i32* %68)
br label %94
94: ; preds = %92, %89
ret i32 %90
}