#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*/ BIO;

/* Variables and functions */
int /*<<< orphan*/ BIO_free_all(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BIO_new(int /*<<< orphan*/);
size_t BIO_pending(int /*<<< orphan*/ *);
int /*<<< orphan*/ BIO_read(int /*<<< orphan*/ *, char *, size_t);
int /*<<< orphan*/ BIO_s_mem();
int /*<<< orphan*/ ERR_error_string(int /*<<< orphan*/, char *);
int /*<<< orphan*/ ERR_get_error();
int /*<<< orphan*/ ERR_load_crypto_strings();
int /*<<< orphan*/
PEM_write_bio_RSAPrivateKey(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                            int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                            int /*<<< orphan*/, int /*<<< orphan*/ *,
                            int /*<<< orphan*/ *);
int /*<<< orphan*/ PEM_write_bio_RSAPublicKey(int /*<<< orphan*/ *,
                                              int /*<<< orphan*/ *);
int /*<<< orphan*/ RSA_PKCS1_OAEP_PADDING;
int /*<<< orphan*/ RSA_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *RSA_generate_key(int, int, int /*<<< orphan*/ *,
                                     int /*<<< orphan*/ *);
int RSA_private_decrypt(int, unsigned char *, unsigned char *,
                        int /*<<< orphan*/ *, int /*<<< orphan*/);
int RSA_public_encrypt(int, unsigned char *, unsigned char *,
                       int /*<<< orphan*/ *, int /*<<< orphan*/);
int RSA_size(int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int strlen(char *);
int main(void) {
  size_t pri_len;
  size_t pub_len;
  char *pri_key;
  char *pub_key;
  char msg[2048 / 8];
  char *encrypt = NULL;
  char *decrypt = NULL;
  char *err;
  printf("Generating RSA (%d bits) keypair...", 2048);
  fflush(stdout);
  RSA *keypair = RSA_generate_key(2048, 3, NULL, NULL);
  BIO *pri = BIO_new(BIO_s_mem());
  BIO *pub = BIO_new(BIO_s_mem());
  PEM_write_bio_RSAPrivateKey(pri, keypair, NULL, NULL, 0, NULL, NULL);
  PEM_write_bio_RSAPublicKey(pub, keypair);
  pri_len = BIO_pending(pri);
  pub_len = BIO_pending(pub);
  pri_key = malloc(pri_len + 1);
  pub_key = malloc(pub_len + 1);
  BIO_read(pri, pri_key, pri_len);
  BIO_read(pub, pub_key, pub_len);
  pri_key[pri_len] = '\0';
  pub_key[pub_len] = '\0';
  printf("\n%s\n%s\n", pri_key, pub_key);
  printf("done.\n");
  printf("Message to encrypt: ");
  fgets(msg, 2048 - 1, stdin);
  msg[strlen(msg) - 1] = '\0';
  encrypt = malloc(RSA_size(keypair));
  int encrypt_len;
  err = malloc(130);
  if ((encrypt_len = RSA_public_encrypt(strlen(msg) + 1, (unsigned char *)msg,
                                        (unsigned char *)encrypt, keypair,
                                        RSA_PKCS1_OAEP_PADDING)) == -1) {
    ERR_load_crypto_strings();
    ERR_error_string(ERR_get_error(), err);
    fprintf(stderr, "Error encrypting message: %s\n", err);
    goto free_stuff;
  }
  FILE *out = fopen("out.bin", "w");
  fwrite(encrypt, sizeof(*encrypt), RSA_size(keypair), out);
  fclose(out);
  printf("Encrypted message written to file.\n");
  free(encrypt);
  encrypt = NULL;
  printf("Reading back encrypted message and attempting decryption...\n");
  encrypt = malloc(RSA_size(keypair));
  out = fopen("out.bin", "r");
  fread(encrypt, sizeof(*encrypt), RSA_size(keypair), out);
  fclose(out);
  decrypt = malloc(encrypt_len);
  if (RSA_private_decrypt(encrypt_len, (unsigned char *)encrypt,
                          (unsigned char *)decrypt, keypair,
                          RSA_PKCS1_OAEP_PADDING) == -1) {
    ERR_load_crypto_strings();
    ERR_error_string(ERR_get_error(), err);
    fprintf(stderr, "Error decrypting message: %s\n", err);
    goto free_stuff;
  }
  printf("Decrypted message: %s\n", decrypt);
free_stuff:
  RSA_free(keypair);
  BIO_free_all(pub);
  BIO_free_all(pri);
  free(pri_key);
  free(pub_key);
  free(encrypt);
  free(decrypt);
  free(err);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x190,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x138(%rbp)
movq   $0x0,-0x140(%rbp)
lea    0xd31(%rip),%rdi
mov    $0x800,%esi
mov    $0x0,%al
call   10c0 <printf@plt>
mov    0x2ce6(%rip),%rax
mov    (%rax),%edi
call   1110 <fflush@plt>
mov    $0x800,%edi
mov    $0x3,%esi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   11c0 <RSA_generate_key@plt>
mov    %rax,-0x150(%rbp)
mov    $0x0,%al
call   11b0 <BIO_s_mem@plt>
mov    %eax,%edi
call   1120 <BIO_new@plt>
mov    %rax,-0x158(%rbp)
mov    $0x0,%al
call   11b0 <BIO_s_mem@plt>
mov    %eax,%edi
call   1120 <BIO_new@plt>
mov    %rax,-0x160(%rbp)
mov    -0x158(%rbp),%rdi
mov    -0x150(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r9d
xor    %r8d,%r8d
mov    %r9,%rdx
mov    %r9,%rcx
movq   $0x0,(%rsp)
call   1140 <PEM_write_bio_RSAPrivateKey@plt>
mov    -0x160(%rbp),%rdi
mov    -0x150(%rbp),%rsi
call   1070 <PEM_write_bio_RSAPublicKey@plt>
mov    -0x158(%rbp),%rdi
call   10e0 <BIO_pending@plt>
mov    %rax,-0x10(%rbp)
mov    -0x160(%rbp),%rdi
call   10e0 <BIO_pending@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %eax,%edi
call   1100 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %eax,%edi
call   1100 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x158(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1090 <BIO_read@plt>
mov    -0x160(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1090 <BIO_read@plt>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xc23(%rip),%rdi
mov    $0x0,%al
call   10c0 <printf@plt>
lea    0xc1d(%rip),%rdi
mov    $0x0,%al
call   10c0 <printf@plt>
lea    0xc16(%rip),%rdi
mov    $0x0,%al
call   10c0 <printf@plt>
lea    -0x130(%rbp),%rdi
mov    0x2bb6(%rip),%rax
mov    (%rax),%edx
mov    $0x7ff,%esi
call   10d0 <fgets@plt>
lea    -0x130(%rbp),%rdi
call   10a0 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0x130(%rbp,%rax,1)
mov    -0x150(%rbp),%rdi
call   1060 <RSA_size@plt>
mov    %eax,%edi
call   1100 <malloc@plt>
mov    %rax,-0x138(%rbp)
mov    $0x82,%edi
call   1100 <malloc@plt>
mov    %rax,-0x148(%rbp)
lea    -0x130(%rbp),%rdi
call   10a0 <strlen@plt>
mov    %eax,%edi
add    $0x1,%edi
lea    -0x130(%rbp),%rsi
mov    -0x138(%rbp),%rdx
mov    -0x150(%rbp),%rcx
mov    0x2b31(%rip),%rax
mov    (%rax),%r8d
call   1040 <RSA_public_encrypt@plt>
mov    %eax,-0x164(%rbp)
cmp    $0xffffffff,%eax
jne    1503 <main+0x263>
mov    $0x0,%al
call   1180 <ERR_load_crypto_strings@plt>
mov    $0x0,%al
call   1160 <ERR_get_error@plt>
mov    %eax,%edi
mov    -0x148(%rbp),%rsi
call   1130 <ERR_error_string@plt>
mov    0x2ae7(%rip),%rax
mov    (%rax),%edi
mov    -0x148(%rbp),%rdx
lea    0xb4f(%rip),%rsi
call   10f0 <fprintf@plt>
jmp    169b <main+0x3fb>
lea    0xb5c(%rip),%rdi
lea    0xb5d(%rip),%rsi
call   1150 <fopen@plt>
mov    %rax,-0x170(%rbp)
mov    -0x138(%rbp),%rax
mov    %rax,-0x180(%rbp)
mov    -0x150(%rbp),%rdi
call   1060 <RSA_size@plt>
mov    -0x180(%rbp),%rdi
mov    %eax,%edx
mov    -0x170(%rbp),%rcx
mov    $0x1,%esi
call   1190 <fwrite@plt>
mov    -0x170(%rbp),%rdi
call   1080 <fclose@plt>
lea    0xb0c(%rip),%rdi
mov    $0x0,%al
call   10c0 <printf@plt>
mov    -0x138(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x138(%rbp)
lea    0xb0b(%rip),%rdi
mov    $0x0,%al
call   10c0 <printf@plt>
mov    -0x150(%rbp),%rdi
call   1060 <RSA_size@plt>
mov    %eax,%edi
call   1100 <malloc@plt>
mov    %rax,-0x138(%rbp)
lea    0xab5(%rip),%rdi
lea    0xb19(%rip),%rsi
call   1150 <fopen@plt>
mov    %rax,-0x170(%rbp)
mov    -0x138(%rbp),%rax
mov    %rax,-0x178(%rbp)
mov    -0x150(%rbp),%rdi
call   1060 <RSA_size@plt>
mov    -0x178(%rbp),%rdi
mov    %eax,%edx
mov    -0x170(%rbp),%rcx
mov    $0x1,%esi
call   1050 <fread@plt>
mov    -0x170(%rbp),%rdi
call   1080 <fclose@plt>
mov    -0x164(%rbp),%edi
call   1100 <malloc@plt>
mov    %rax,-0x140(%rbp)
mov    -0x164(%rbp),%edi
mov    -0x138(%rbp),%rsi
mov    -0x140(%rbp),%rdx
mov    -0x150(%rbp),%rcx
mov    0x29a8(%rip),%rax
mov    (%rax),%r8d
call   11a0 <RSA_private_decrypt@plt>
cmp    $0xffffffff,%eax
jne    1686 <main+0x3e6>
mov    $0x0,%al
call   1180 <ERR_load_crypto_strings@plt>
mov    $0x0,%al
call   1160 <ERR_get_error@plt>
mov    %eax,%edi
mov    -0x148(%rbp),%rsi
call   1130 <ERR_error_string@plt>
mov    0x2964(%rip),%rax
mov    (%rax),%edi
mov    -0x148(%rbp),%rdx
lea    0xa57(%rip),%rsi
call   10f0 <fprintf@plt>
jmp    169b <main+0x3fb>
mov    -0x140(%rbp),%rsi
lea    0xa5d(%rip),%rdi
mov    $0x0,%al
call   10c0 <printf@plt>
mov    -0x150(%rbp),%rdi
call   10b0 <RSA_free@plt>
mov    -0x160(%rbp),%rdi
call   1170 <BIO_free_all@plt>
mov    -0x158(%rbp),%rdi
call   1170 <BIO_free_all@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x138(%rbp),%rdi
call   1030 <free@plt>
mov    -0x140(%rbp),%rdi
call   1030 <free@plt>
mov    -0x148(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x190,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x148,%rsp
lea    0xd38(%rip),%rdi
mov    $0x800,%esi
xor    %eax,%eax
call   10d0 <printf@plt>
mov    0x2ced(%rip),%rax
mov    (%rax),%edi
call   1120 <fflush@plt>
mov    $0x800,%edi
mov    $0x3,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   11d0 <RSA_generate_key@plt>
mov    %rax,%rbx
mov    %rax,0x30(%rsp)
xor    %eax,%eax
call   11c0 <BIO_s_mem@plt>
mov    %eax,%edi
call   1130 <BIO_new@plt>
mov    %rax,%r14
xor    %eax,%eax
call   11c0 <BIO_s_mem@plt>
mov    %eax,%edi
call   1130 <BIO_new@plt>
mov    %rax,%r15
movq   $0x0,(%rsp)
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1150 <PEM_write_bio_RSAPrivateKey@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1080 <PEM_write_bio_RSAPublicKey@plt>
mov    %r14,%rdi
call   10f0 <BIO_pending@plt>
mov    %rax,%r13
mov    %r15,%rdi
call   10f0 <BIO_pending@plt>
mov    %rax,%rbp
lea    0x1(%r13),%edi
call   1110 <malloc@plt>
mov    %rax,%rbx
lea    0x1(%rbp),%edi
call   1110 <malloc@plt>
mov    %rax,%r12
mov    %r14,0x20(%rsp)
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r13,%rdx
call   10a0 <BIO_read@plt>
mov    %r15,0x18(%rsp)
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbp,%rdx
call   10a0 <BIO_read@plt>
movb   $0x0,(%rbx,%r13,1)
movb   $0x0,(%r12,%rbp,1)
lea    0xc79(%rip),%rdi
mov    %rbx,0x10(%rsp)
mov    %rbx,%rsi
mov    %r12,0x38(%rsp)
mov    %r12,%rdx
mov    0x30(%rsp),%r12
xor    %eax,%eax
call   10d0 <printf@plt>
lea    0xcd2(%rip),%rdi
call   1050 <puts@plt>
lea    0xc52(%rip),%rdi
xor    %eax,%eax
call   10d0 <printf@plt>
mov    0x2c00(%rip),%rax
mov    (%rax),%edx
lea    0x40(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x7ff,%esi
call   10e0 <fgets@plt>
mov    %rbx,%rdi
call   10b0 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    %r12,%rdi
call   1070 <RSA_size@plt>
mov    %eax,%edi
call   1110 <malloc@plt>
mov    %rax,%r13
mov    $0x82,%edi
call   1110 <malloc@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   10b0 <strlen@plt>
lea    0x1(%rax),%edi
mov    0x2ba4(%rip),%rax
mov    (%rax),%r8d
mov    %rbx,%rsi
mov    %r13,%rdx
mov    %r12,%rcx
call   1040 <RSA_public_encrypt@plt>
cmp    $0xffffffff,%eax
je     153d <main+0x28d>
mov    %eax,%r15d
lea    0xbff(%rip),%rbp
lea    0xc00(%rip),%rsi
mov    %rbp,%rdi
call   1160 <fopen@plt>
mov    %rax,%rbx
mov    %r12,%rdi
call   1070 <RSA_size@plt>
mov    %r13,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %rbx,%rcx
call   11a0 <fwrite@plt>
mov    %rbx,%rdi
call   1090 <fclose@plt>
lea    0xc0b(%rip),%rdi
call   1050 <puts@plt>
mov    %r13,%rdi
call   1030 <free@plt>
lea    0xc1a(%rip),%rdi
call   1050 <puts@plt>
mov    %r12,%rdi
call   1070 <RSA_size@plt>
mov    %eax,%edi
call   1110 <malloc@plt>
mov    %rax,%r13
lea    0xb9c(%rip),%rsi
mov    %rbp,%rdi
call   1160 <fopen@plt>
mov    %rax,%rbx
mov    %r12,%rdi
call   1070 <RSA_size@plt>
mov    %r13,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %rbx,%rcx
call   1060 <fread@plt>
mov    %rbx,%rdi
call   1090 <fclose@plt>
mov    %r15d,%edi
call   1110 <malloc@plt>
mov    %rax,%rbx
mov    0x2ad4(%rip),%rax
mov    (%rax),%r8d
mov    %r15d,%edi
mov    %r13,%rsi
mov    %rbx,%rdx
mov    %r12,%rcx
call   11b0 <RSA_private_decrypt@plt>
cmp    $0xffffffff,%eax
mov    %rbx,0x28(%rsp)
je     156e <main+0x2be>
lea    0xb58(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   10d0 <printf@plt>
jmp    159e <main+0x2ee>
xor    %eax,%eax
mov    %rax,0x28(%rsp)
xor    %eax,%eax
call   1190 <ERR_load_crypto_strings@plt>
xor    %eax,%eax
call   1170 <ERR_get_error@plt>
mov    %eax,%edi
mov    %r14,%rsi
call   1140 <ERR_error_string@plt>
mov    0x2a6d(%rip),%rax
mov    (%rax),%edi
lea    0xad5(%rip),%rsi
jmp    1596 <main+0x2e6>
xor    %eax,%eax
call   1190 <ERR_load_crypto_strings@plt>
xor    %eax,%eax
call   1170 <ERR_get_error@plt>
mov    %eax,%edi
mov    %r14,%rsi
call   1140 <ERR_error_string@plt>
mov    0x2a43(%rip),%rax
mov    (%rax),%edi
lea    0xad5(%rip),%rsi
mov    %r14,%rdx
call   1100 <fprintf@plt>
mov    0x20(%rsp),%rbx
mov    0x18(%rsp),%rbp
mov    0x10(%rsp),%r15
mov    %r12,%rdi
call   10c0 <RSA_free@plt>
mov    %rbp,%rdi
call   1180 <BIO_free_all@plt>
mov    %rbx,%rdi
call   1180 <BIO_free_all@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    0x38(%rsp),%rdi
call   1030 <free@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    0x28(%rsp),%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x148,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [36 x i8] c"Generating RSA (%d bits) keypair...\00"
@stdout = global i32 0
@.str.1 = [8 x i8] c"\0A%s\0A%s\0A\00"
@.str.2 = [7 x i8] c"done.\0A\00"
@.str.3 = [21 x i8] c"Message to encrypt: \00"
@stdin = global i32 0
@RSA_PKCS1_OAEP_PADDING = global i32 0
@stderr = global i32 0
@.str.4 = [30 x i8] c"Error encrypting message: %s\0A\00"
@.str.5 = [8 x i8] c"out.bin\00"
@.str.6 = [2 x i8] c"w\00"
@.str.7 = [36 x i8] c"Encrypted message written to file.\0A\00"
@.str.8 = [61 x i8] c"Reading back encrypted message and attempting decryption...\0A\00"
@.str.9 = [2 x i8] c"r\00"
@.str.10 = [30 x i8] c"Error decrypting message: %s\0A\00"
@.str.11 = [23 x i8] c"Decrypted message: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca [256 x i8]
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32*
%13 = alloca i32
%14 = alloca i32*
store i32 0, i32* %1
store i8* null, i8** %7
store i8* null, i8** %8
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 2048)
%16 = load i32, i32* @stdout
%17 = call i32 @fflush(i32 %16)
%18 = call i32* @RSA_generate_key(i32 2048, i32 3, i32* null, i32* null)
store i32* %18, i32** %10
%19 = call i32 (...) @BIO_s_mem()
%20 = call i32* @BIO_new(i32 %19)
store i32* %20, i32** %11
%21 = call i32 (...) @BIO_s_mem()
%22 = call i32* @BIO_new(i32 %21)
store i32* %22, i32** %12
%23 = load i32*, i32** %11
%24 = load i32*, i32** %10
%25 = call i32 @PEM_write_bio_RSAPrivateKey(i32* %23, i32* %24, i32* null, i32* null, i32 0, i32* null, i32* null)
%26 = load i32*, i32** %12
%27 = load i32*, i32** %10
%28 = call i32 @PEM_write_bio_RSAPublicKey(i32* %26, i32* %27)
%29 = load i32*, i32** %11
%30 = call i64 @BIO_pending(i32* %29)
store i64 %30, i64* %2
%31 = load i32*, i32** %12
%32 = call i64 @BIO_pending(i32* %31)
store i64 %32, i64* %3
%33 = load i64, i64* %2
%34 = add i64 %33, 1
%35 = trunc i64 %34 to i32
%36 = call i8* @malloc(i32 %35)
store i8* %36, i8** %4
%37 = load i64, i64* %3
%38 = add i64 %37, 1
%39 = trunc i64 %38 to i32
%40 = call i8* @malloc(i32 %39)
store i8* %40, i8** %5
%41 = load i32*, i32** %11
%42 = load i8*, i8** %4
%43 = load i64, i64* %2
%44 = call i32 @BIO_read(i32* %41, i8* %42, i64 %43)
%45 = load i32*, i32** %12
%46 = load i8*, i8** %5
%47 = load i64, i64* %3
%48 = call i32 @BIO_read(i32* %45, i8* %46, i64 %47)
%49 = load i8*, i8** %4
%50 = load i64, i64* %2
%51 = getelementptr inbounds i8, i8* %49, i64 %50
store i8 0, i8* %51
%52 = load i8*, i8** %5
%53 = load i64, i64* %3
%54 = getelementptr inbounds i8, i8* %52, i64 %53
store i8 0, i8* %54
%55 = load i8*, i8** %4
%56 = load i8*, i8** %5
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %55, i8* %56)
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%60 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%61 = load i32, i32* @stdin
%62 = call i32 @fgets(i8* %60, i32 2047, i32 %61)
%63 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%64 = call i32 @strlen(i8* %63)
%65 = sub nsw i32 %64, 1
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 %66
store i8 0, i8* %67
%68 = load i32*, i32** %10
%69 = call i32 @RSA_size(i32* %68)
%70 = call i8* @malloc(i32 %69)
store i8* %70, i8** %7
%71 = call i8* @malloc(i32 130)
store i8* %71, i8** %9
%72 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%73 = call i32 @strlen(i8* %72)
%74 = add nsw i32 %73, 1
%75 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%76 = load i8*, i8** %7
%77 = load i32*, i32** %10
%78 = load i32, i32* @RSA_PKCS1_OAEP_PADDING
%79 = call i32 @RSA_public_encrypt(i32 %74, i8* %75, i8* %76, i32* %77, i32 %78)
store i32 %79, i32* %13
%80 = icmp eq i32 %79, -1
br i1 %80, label %81, label %89
81: ; preds = %0
%82 = call i32 (...) @ERR_load_crypto_strings()
%83 = call i32 (...) @ERR_get_error()
%84 = load i8*, i8** %9
%85 = call i32 @ERR_error_string(i32 %83, i8* %84)
%86 = load i32, i32* @stderr
%87 = load i8*, i8** %9
%88 = call i32 @fprintf(i32 %86, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i8* %87)
br label %133
89: ; preds = %0
%90 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
store i32* %90, i32** %14
%91 = load i8*, i8** %7
%92 = load i32*, i32** %10
%93 = call i32 @RSA_size(i32* %92)
%94 = load i32*, i32** %14
%95 = call i32 @fwrite(i8* %91, i32 1, i32 %93, i32* %94)
%96 = load i32*, i32** %14
%97 = call i32 @fclose(i32* %96)
%98 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.7, i64 0, i64 0))
%99 = load i8*, i8** %7
%100 = call i32 @free(i8* %99)
store i8* null, i8** %7
%101 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.8, i64 0, i64 0))
%102 = load i32*, i32** %10
%103 = call i32 @RSA_size(i32* %102)
%104 = call i8* @malloc(i32 %103)
store i8* %104, i8** %7
%105 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0))
store i32* %105, i32** %14
%106 = load i8*, i8** %7
%107 = load i32*, i32** %10
%108 = call i32 @RSA_size(i32* %107)
%109 = load i32*, i32** %14
%110 = call i32 @fread(i8* %106, i32 1, i32 %108, i32* %109)
%111 = load i32*, i32** %14
%112 = call i32 @fclose(i32* %111)
%113 = load i32, i32* %13
%114 = call i8* @malloc(i32 %113)
store i8* %114, i8** %8
%115 = load i32, i32* %13
%116 = load i8*, i8** %7
%117 = load i8*, i8** %8
%118 = load i32*, i32** %10
%119 = load i32, i32* @RSA_PKCS1_OAEP_PADDING
%120 = call i32 @RSA_private_decrypt(i32 %115, i8* %116, i8* %117, i32* %118, i32 %119)
%121 = icmp eq i32 %120, -1
br i1 %121, label %122, label %130
122: ; preds = %89
%123 = call i32 (...) @ERR_load_crypto_strings()
%124 = call i32 (...) @ERR_get_error()
%125 = load i8*, i8** %9
%126 = call i32 @ERR_error_string(i32 %124, i8* %125)
%127 = load i32, i32* @stderr
%128 = load i8*, i8** %9
%129 = call i32 @fprintf(i32 %127, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.10, i64 0, i64 0), i8* %128)
br label %133
130: ; preds = %89
%131 = load i8*, i8** %8
%132 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.11, i64 0, i64 0), i8* %131)
br label %133
133: ; preds = %130, %122, %81
%134 = load i32*, i32** %10
%135 = call i32 @RSA_free(i32* %134)
%136 = load i32*, i32** %12
%137 = call i32 @BIO_free_all(i32* %136)
%138 = load i32*, i32** %11
%139 = call i32 @BIO_free_all(i32* %138)
%140 = load i8*, i8** %4
%141 = call i32 @free(i8* %140)
%142 = load i8*, i8** %5
%143 = call i32 @free(i8* %142)
%144 = load i8*, i8** %7
%145 = call i32 @free(i8* %144)
%146 = load i8*, i8** %8
%147 = call i32 @free(i8* %146)
%148 = load i8*, i8** %9
%149 = call i32 @free(i8* %148)
ret i32 0
}

LLVM IR (-O2):
@.str = [36 x i8] c"Generating RSA (%d bits) keypair...\00"
@stdout = global i32 0
@.str.1 = [8 x i8] c"\0A%s\0A%s\0A\00"
@.str.3 = [21 x i8] c"Message to encrypt: \00"
@stdin = global i32 0
@RSA_PKCS1_OAEP_PADDING = global i32 0
@stderr = global i32 0
@.str.4 = [30 x i8] c"Error encrypting message: %s\0A\00"
@.str.5 = [8 x i8] c"out.bin\00"
@.str.6 = [2 x i8] c"w\00"
@.str.9 = [2 x i8] c"r\00"
@.str.10 = [30 x i8] c"Error decrypting message: %s\0A\00"
@.str.11 = [23 x i8] c"Decrypted message: %s\0A\00"
@str = [6 x i8] c"done.\00"
@str.12 = [35 x i8] c"Encrypted message written to file.\00"
@str.13 = [60 x i8] c"Reading back encrypted message and attempting decryption...\00"
define i32 @main() {
%1 = alloca [256 x i8]
%2 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 2048)
%4 = load i32, i32* @stdout
%5 = tail call i32 @fflush(i32 %4)
%6 = tail call i32* @RSA_generate_key(i32 2048, i32 3, i32* null, i32* null)
%7 = tail call i32 (...) @BIO_s_mem()
%8 = tail call i32* @BIO_new(i32 %7)
%9 = tail call i32 (...) @BIO_s_mem()
%10 = tail call i32* @BIO_new(i32 %9)
%11 = tail call i32 @PEM_write_bio_RSAPrivateKey(i32* %8, i32* %6, i32* null, i32* null, i32 0, i32* null, i32* null)
%12 = tail call i32 @PEM_write_bio_RSAPublicKey(i32* %10, i32* %6)
%13 = tail call i64 @BIO_pending(i32* %8)
%14 = tail call i64 @BIO_pending(i32* %10)
%15 = trunc i64 %13 to i32
%16 = add i32 %15, 1
%17 = tail call i8* @malloc(i32 %16)
%18 = trunc i64 %14 to i32
%19 = add i32 %18, 1
%20 = tail call i8* @malloc(i32 %19)
%21 = tail call i32 @BIO_read(i32* %8, i8* %17, i64 %13)
%22 = tail call i32 @BIO_read(i32* %10, i8* %20, i64 %14)
%23 = getelementptr inbounds i8, i8* %17, i64 %13
store i8 0, i8* %23
%24 = getelementptr inbounds i8, i8* %20, i64 %14
store i8 0, i8* %24
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %17, i8* %20)
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str, i64 0, i64 0))
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%28 = load i32, i32* @stdin
%29 = call i32 @fgets(i8* %2, i32 2047, i32 %28)
%30 = call i32 @strlen(i8* %2)
%31 = add nsw i32 %30, -1
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 %32
store i8 0, i8* %33
%34 = call i32 @RSA_size(i32* %6)
%35 = call i8* @malloc(i32 %34)
%36 = call dereferenceable_or_null(130) i8* @malloc(i32 130)
%37 = call i32 @strlen(i8* %2)
%38 = add nsw i32 %37, 1
%39 = load i32, i32* @RSA_PKCS1_OAEP_PADDING
%40 = call i32 @RSA_public_encrypt(i32 %38, i8* %2, i8* %35, i32* %6, i32 %39)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %48
42: ; preds = %0
%43 = call i32 (...) @ERR_load_crypto_strings()
%44 = call i32 (...) @ERR_get_error()
%45 = call i32 @ERR_error_string(i32 %44, i8* %36)
%46 = load i32, i32* @stderr
%47 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i8* %36)
br label %74
48: ; preds = %0
%49 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%50 = call i32 @RSA_size(i32* %6)
%51 = call i32 @fwrite(i8* %35, i32 1, i32 %50, i32* %49)
%52 = call i32 @fclose(i32* %49)
%53 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.12, i64 0, i64 0))
%54 = call i32 @free(i8* %35)
%55 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([60 x i8], [60 x i8]* @str.13, i64 0, i64 0))
%56 = call i32 @RSA_size(i32* %6)
%57 = call i8* @malloc(i32 %56)
%58 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0))
%59 = call i32 @RSA_size(i32* %6)
%60 = call i32 @fread(i8* %57, i32 1, i32 %59, i32* %58)
%61 = call i32 @fclose(i32* %58)
%62 = call i8* @malloc(i32 %40)
%63 = load i32, i32* @RSA_PKCS1_OAEP_PADDING
%64 = call i32 @RSA_private_decrypt(i32 %40, i8* %57, i8* %62, i32* %6, i32 %63)
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %72
66: ; preds = %48
%67 = call i32 (...) @ERR_load_crypto_strings()
%68 = call i32 (...) @ERR_get_error()
%69 = call i32 @ERR_error_string(i32 %68, i8* %36)
%70 = load i32, i32* @stderr
%71 = call i32 @fprintf(i32 %70, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.10, i64 0, i64 0), i8* %36)
br label %74
72: ; preds = %48
%73 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.11, i64 0, i64 0), i8* %62)
br label %74
74: ; preds = %72, %66, %42
%75 = phi i8* [ null, %42 ], [ %62, %66 ], [ %62, %72 ]
%76 = phi i8* [ %35, %42 ], [ %57, %66 ], [ %57, %72 ]
%77 = call i32 @RSA_free(i32* %6)
%78 = call i32 @BIO_free_all(i32* %10)
%79 = call i32 @BIO_free_all(i32* %8)
%80 = call i32 @free(i8* %17)
%81 = call i32 @free(i8* %20)
%82 = call i32 @free(i8* %76)
%83 = call i32 @free(i8* %75)
%84 = call i32 @free(i8* %36)
ret i32 0
}