#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*/
PEM_write_RSAPrivateKey(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                        int /*<<< orphan*/ *, int /*<<< orphan*/ *, int,
                        int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ PEM_write_RSAPublicKey(int /*<<< orphan*/ *,
                                          int /*<<< orphan*/ *);
unsigned long RSA_3;
int RSA_check_key(int /*<<< orphan*/ *);
int /*<<< orphan*/ RSA_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *RSA_generate_key(int, unsigned long, int /*<<< orphan*/ *,
                                     int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int generate_key() {
  RSA *pRsa;
  int bits = 1024, iret;
  unsigned long e = RSA_3;
  FILE *pfprivatekey, *pfpublickey;
  if ((pfpublickey = fopen("rsapublic.key", "wb")) == NULL) {
    printf("\nopen file [ %s ] error!\n\n", "rsapublic.key");
    return 1;
  }
  if ((pfprivatekey = fopen("rsaprivate.key", "wb")) == NULL) {
    printf("\nopen file [ %s ] error!\n\n", "rsaprivate.key");
    return 1;
  }
  pRsa = RSA_generate_key(bits, e, NULL, NULL);
  iret = RSA_check_key(pRsa);
  if (iret != 1) {
    printf("key error! \n");
    return 1;
  }
  PEM_write_RSAPublicKey(pfpublickey, pRsa);
  PEM_write_RSAPrivateKey(pfprivatekey, pRsa, NULL, NULL, bits, NULL, NULL);
  RSA_free(pRsa);
  fclose(pfpublickey);
  fclose(pfprivatekey);
  return 0;
}


x86 assembly (clang, -O0):
<generate_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x400,-0x14(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
lea    0xe5c(%rip),%rdi
lea    0xe63(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    11df <generate_key+0x5f>
lea    0xe4c(%rip),%rdi
lea    0xe34(%rip),%rsi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12bb <generate_key+0x13b>
lea    0xe46(%rip),%rdi
lea    0xe21(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1221 <generate_key+0xa1>
lea    0xe0a(%rip),%rdi
lea    0xe1e(%rip),%rsi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12bb <generate_key+0x13b>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   10a0 <RSA_generate_key@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <RSA_check_key@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x1,-0x18(%rbp)
je     1268 <generate_key+0xe8>
lea    0xde6(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12bb <generate_key+0x13b>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <PEM_write_RSAPublicKey@plt>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%r9d
mov    %r9,%rdx
mov    %r9,%rcx
movq   $0x0,(%rsp)
call   1040 <PEM_write_RSAPrivateKey@plt>
mov    -0x10(%rbp),%rdi
call   1060 <RSA_free@plt>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate_key>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    0x2e49(%rip),%rax
mov    (%rax),%r12
lea    0xe57(%rip),%rdi
lea    0xe5e(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1245 <generate_key+0xb5>
mov    %rax,%rbx
lea    0xe64(%rip),%rdi
lea    0xe3f(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1255 <generate_key+0xc5>
mov    %rax,%r15
xor    %r14d,%r14d
mov    $0x400,%edi
mov    %r12,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   10b0 <RSA_generate_key@plt>
mov    %rax,%r12
mov    %rax,%rdi
call   1030 <RSA_check_key@plt>
cmp    $0x1,%eax
jne    126c <generate_key+0xdc>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1090 <PEM_write_RSAPublicKey@plt>
movq   $0x0,(%rsp)
mov    %r15,%rdi
mov    %r12,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    $0x400,%r8d
xor    %r9d,%r9d
call   1050 <PEM_write_RSAPrivateKey@plt>
mov    %r12,%rdi
call   1070 <RSA_free@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    %r15,%rdi
call   1060 <fclose@plt>
jmp    127e <generate_key+0xee>
lea    0xdc5(%rip),%rdi
lea    0xdad(%rip),%rsi
jmp    1263 <generate_key+0xd3>
lea    0xdb5(%rip),%rdi
lea    0xdc9(%rip),%rsi
xor    %eax,%eax
call   1080 <printf@plt>
jmp    1278 <generate_key+0xe8>
lea    0xdc8(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@RSA_3 = global i64 0
@.str = [14 x i8] c"rsapublic.key\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [27 x i8] c"\0Aopen file [ %s ] error!\0A\0A\00"
@.str.3 = [15 x i8] c"rsaprivate.key\00"
@.str.4 = [13 x i8] c"key error! \0A\00"
define i32 @generate_key() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i32*
store i32 1024, i32* %3
%8 = load i64, i64* @RSA_3
store i64 %8, i64* %5
%9 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %7
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %42
13: ; preds = %0
%14 = call i32* @fopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %14, i32** %6
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %42
18: ; preds = %13
%19 = load i32, i32* %3
%20 = load i64, i64* %5
%21 = call i32* @RSA_generate_key(i32 %19, i64 %20, i32* null, i32* null)
store i32* %21, i32** %2
%22 = load i32*, i32** %2
%23 = call i32 @RSA_check_key(i32* %22)
store i32 %23, i32* %4
%24 = load i32, i32* %4
%25 = icmp ne i32 %24, 1
br i1 %25, label %26, label %28
26: ; preds = %18
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %42
28: ; preds = %18
%29 = load i32*, i32** %7
%30 = load i32*, i32** %2
%31 = call i32 @PEM_write_RSAPublicKey(i32* %29, i32* %30)
%32 = load i32*, i32** %6
%33 = load i32*, i32** %2
%34 = load i32, i32* %3
%35 = call i32 @PEM_write_RSAPrivateKey(i32* %32, i32* %33, i32* null, i32* null, i32 %34, i32* null, i32* null)
%36 = load i32*, i32** %2
%37 = call i32 @RSA_free(i32* %36)
%38 = load i32*, i32** %7
%39 = call i32 @fclose(i32* %38)
%40 = load i32*, i32** %6
%41 = call i32 @fclose(i32* %40)
store i32 0, i32* %1
br label %42
42: ; preds = %28, %26, %16, %11
%43 = load i32, i32* %1
ret i32 %43
}

LLVM IR (-O2):
@RSA_3 = global i64 0
@.str = [14 x i8] c"rsapublic.key\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [27 x i8] c"\0Aopen file [ %s ] error!\0A\0A\00"
@.str.3 = [15 x i8] c"rsaprivate.key\00"
@str = [12 x i8] c"key error! \00"
define i32 @generate_key() {
%1 = load i64, i64* @RSA_3
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %23
6: ; preds = %0
%7 = tail call i32* @fopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %23
11: ; preds = %6
%12 = tail call i32* @RSA_generate_key(i32 1024, i64 %1, i32* null, i32* null)
%13 = tail call i32 @RSA_check_key(i32* %12)
%14 = icmp eq i32 %13, 1
br i1 %14, label %17, label %15
15: ; preds = %11
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %23
17: ; preds = %11
%18 = tail call i32 @PEM_write_RSAPublicKey(i32* %2, i32* %12)
%19 = tail call i32 @PEM_write_RSAPrivateKey(i32* %7, i32* %12, i32* null, i32* null, i32 1024, i32* null, i32* null)
%20 = tail call i32 @RSA_free(i32* %12)
%21 = tail call i32 @fclose(i32* %2)
%22 = tail call i32 @fclose(i32* %7)
br label %23
23: ; preds = %17, %15, %9, %4
%24 = phi i32 [ 1, %4 ], [ 1, %9 ], [ 1, %15 ], [ 0, %17 ]
ret i32 %24
}