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

/* Variables and functions */
int /*<<< orphan*/ RSA_F4;
int /*<<< orphan*/ RSA_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *RSA_generate_key(int, int /*<<< orphan*/,
                                     int /*<<< orphan*/ *,
                                     int /*<<< orphan*/ *);
int /*<<< orphan*/ debug(int, char *, ...);
int i2d_RSAPrivateKey(int /*<<< orphan*/ *, int /*<<< orphan*/ **);
int i2d_RSAPublicKey(int /*<<< orphan*/ *, int /*<<< orphan*/ **);
int /*<<< orphan*/ ssl_strerr();
RSA *genrsa(int key_bits, u_char **pub, u_int *pub_len, u_char **priv,
            u_int *priv_len) {
  RSA *rsa = 0;
  int len;
  rsa = RSA_generate_key(key_bits, RSA_F4, NULL, NULL);
  if (!rsa) {
    debug(2, "RSA key generation failed");
    goto error;
  }
  if (priv) {
    *priv = 0;
    len = i2d_RSAPrivateKey(rsa, priv);
    if (priv_len)
      *priv_len = len;
    if (len <= 0) {
      debug(2, "Cannot dump RSA public key: %s", ssl_strerr());
      goto error;
    }
  }
  if (pub) {
    *pub = 0;
    len = i2d_RSAPublicKey(rsa, pub);
    if (pub_len)
      *pub_len = len;
    if (len <= 0) {
      debug(2, "Cannot dump RSA public key: %s", ssl_strerr());
      goto error;
    }
  }
  return rsa;
error:
  if (rsa)
    RSA_free(rsa);
  return 0;
}


x86 assembly (clang, -O0):
<genrsa>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1070 <RSA_generate_key@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11c2 <genrsa+0x62>
mov    $0x2,%edi
lea    0xe4a(%rip),%rsi
mov    $0x0,%al
call   1040 <debug@plt>
jmp    12a3 <genrsa+0x143>
cmpq   $0x0,-0x28(%rbp)
je     122c <genrsa+0xcc>
mov    -0x28(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1080 <i2d_RSAPrivateKey@plt>
mov    %eax,-0x3c(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11fc <genrsa+0x9c>
mov    -0x3c(%rbp),%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,(%rax)
cmpl   $0x0,-0x3c(%rbp)
jg     1227 <genrsa+0xc7>
mov    $0x0,%al
call   1060 <ssl_strerr@plt>
mov    %eax,%edx
mov    $0x2,%edi
lea    0xdff(%rip),%rsi
mov    $0x0,%al
call   1040 <debug@plt>
jmp    12a3 <genrsa+0x143>
jmp    122c <genrsa+0xcc>
cmpq   $0x0,-0x18(%rbp)
je     1296 <genrsa+0x136>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <i2d_RSAPublicKey@plt>
mov    %eax,-0x3c(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1266 <genrsa+0x106>
mov    -0x3c(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
cmpl   $0x0,-0x3c(%rbp)
jg     1291 <genrsa+0x131>
mov    $0x0,%al
call   1060 <ssl_strerr@plt>
mov    %eax,%edx
mov    $0x2,%edi
lea    0xd95(%rip),%rsi
mov    $0x0,%al
call   1040 <debug@plt>
jmp    12a3 <genrsa+0x143>
jmp    1296 <genrsa+0x136>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12bf <genrsa+0x15f>
cmpq   $0x0,-0x38(%rbp)
je     12b7 <genrsa+0x157>
mov    -0x38(%rbp),%rdi
call   1050 <RSA_free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<genrsa>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %r8,%r12
mov    %rcx,%rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <RSA_generate_key@plt>
test   %rax,%rax
je     11ff <genrsa+0x9f>
mov    %rax,%r13
test   %rbx,%rbx
je     11b3 <genrsa+0x53>
movq   $0x0,(%rbx)
mov    %r13,%rdi
mov    %rbx,%rsi
call   1080 <i2d_RSAPrivateKey@plt>
test   %r12,%r12
je     11af <genrsa+0x4f>
mov    %eax,(%r12)
test   %eax,%eax
jle    11d6 <genrsa+0x76>
test   %r15,%r15
je     1215 <genrsa+0xb5>
movq   $0x0,(%r15)
mov    %r13,%rdi
mov    %r15,%rsi
call   1030 <i2d_RSAPublicKey@plt>
test   %r14,%r14
je     11d2 <genrsa+0x72>
mov    %eax,(%r14)
test   %eax,%eax
jg     1215 <genrsa+0xb5>
xor    %eax,%eax
call   1060 <ssl_strerr@plt>
lea    0xe36(%rip),%rsi
mov    $0x2,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <debug@plt>
mov    %r13,%rdi
call   1050 <RSA_free@plt>
xor    %r13d,%r13d
jmp    1215 <genrsa+0xb5>
lea    0xdfa(%rip),%rsi
xor    %r13d,%r13d
mov    $0x2,%edi
xor    %eax,%eax
call   1040 <debug@plt>
mov    %r13,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@RSA_F4 = global i32 0
@.str = [26 x i8] c"RSA key generation failed\00"
@.str.1 = [31 x i8] c"Cannot dump RSA public key: %s\00"
define i32* @genrsa(i32 %0, i32** %1, i32* %2, i32** %3, i32* %4) {
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32**
%9 = alloca i32*
%10 = alloca i32**
%11 = alloca i32*
%12 = alloca i32*
%13 = alloca i32
store i32 %0, i32* %7
store i32** %1, i32*** %8
store i32* %2, i32** %9
store i32** %3, i32*** %10
store i32* %4, i32** %11
store i32* null, i32** %12
%14 = load i32, i32* %7
%15 = load i32, i32* @RSA_F4
%16 = call i32* @RSA_generate_key(i32 %14, i32 %15, i32* null, i32* null)
store i32* %16, i32** %12
%17 = load i32*, i32** %12
%18 = icmp ne i32* %17, null
br i1 %18, label %21, label %19
19: ; preds = %5
%20 = call i32 (i32, i8*, ...) @debug(i32 2, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %63
21: ; preds = %5
%22 = load i32**, i32*** %10
%23 = icmp ne i32** %22, null
br i1 %23, label %24, label %41
24: ; preds = %21
%25 = load i32**, i32*** %10
store i32* null, i32** %25
%26 = load i32*, i32** %12
%27 = load i32**, i32*** %10
%28 = call i32 @i2d_RSAPrivateKey(i32* %26, i32** %27)
store i32 %28, i32* %13
%29 = load i32*, i32** %11
%30 = icmp ne i32* %29, null
br i1 %30, label %31, label %34
31: ; preds = %24
%32 = load i32, i32* %13
%33 = load i32*, i32** %11
store i32 %32, i32* %33
br label %34
34: ; preds = %31, %24
%35 = load i32, i32* %13
%36 = icmp sle i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = call i32 (...) @ssl_strerr()
%39 = call i32 (i32, i8*, ...) @debug(i32 2, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %38)
br label %63
40: ; preds = %34
br label %41
41: ; preds = %40, %21
%42 = load i32**, i32*** %8
%43 = icmp ne i32** %42, null
br i1 %43, label %44, label %61
44: ; preds = %41
%45 = load i32**, i32*** %8
store i32* null, i32** %45
%46 = load i32*, i32** %12
%47 = load i32**, i32*** %8
%48 = call i32 @i2d_RSAPublicKey(i32* %46, i32** %47)
store i32 %48, i32* %13
%49 = load i32*, i32** %9
%50 = icmp ne i32* %49, null
br i1 %50, label %51, label %54
51: ; preds = %44
%52 = load i32, i32* %13
%53 = load i32*, i32** %9
store i32 %52, i32* %53
br label %54
54: ; preds = %51, %44
%55 = load i32, i32* %13
%56 = icmp sle i32 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %54
%58 = call i32 (...) @ssl_strerr()
%59 = call i32 (i32, i8*, ...) @debug(i32 2, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %58)
br label %63
60: ; preds = %54
br label %61
61: ; preds = %60, %41
%62 = load i32*, i32** %12
store i32* %62, i32** %6
br label %70
63: ; preds = %57, %37, %19
%64 = load i32*, i32** %12
%65 = icmp ne i32* %64, null
br i1 %65, label %66, label %69
66: ; preds = %63
%67 = load i32*, i32** %12
%68 = call i32 @RSA_free(i32* %67)
br label %69
69: ; preds = %66, %63
store i32* null, i32** %6
br label %70
70: ; preds = %69, %61
%71 = load i32*, i32** %6
ret i32* %71
}

LLVM IR (-O2):
@RSA_F4 = global i32 0
@.str = [26 x i8] c"RSA key generation failed\00"
@.str.1 = [31 x i8] c"Cannot dump RSA public key: %s\00"
define i32* @genrsa(i32 %0, i32** %1, i32* writeonly %2, i32** %3, i32* writeonly %4) {
%6 = load i32, i32* @RSA_F4
%7 = tail call i32* @RSA_generate_key(i32 %0, i32 %6, i32* null, i32* null)
%8 = icmp eq i32* %7, null
br i1 %8, label %25, label %9
9: ; preds = %5
%10 = icmp eq i32** %3, null
br i1 %10, label %17, label %11
11: ; preds = %9
store i32* null, i32** %3
%12 = tail call i32 @i2d_RSAPrivateKey(i32* %7, i32** %3)
%13 = icmp eq i32* %4, null
br i1 %13, label %15, label %14
14: ; preds = %11
store i32 %12, i32* %4
br label %15
15: ; preds = %14, %11
%16 = icmp slt i32 %12, 1
br i1 %16, label %27, label %17
17: ; preds = %15, %9
%18 = icmp eq i32** %1, null
br i1 %18, label %31, label %19
19: ; preds = %17
store i32* null, i32** %1
%20 = tail call i32 @i2d_RSAPublicKey(i32* %7, i32** %1)
%21 = icmp eq i32* %2, null
br i1 %21, label %23, label %22
22: ; preds = %19
store i32 %20, i32* %2
br label %23
23: ; preds = %22, %19
%24 = icmp slt i32 %20, 1
br i1 %24, label %27, label %31
25: ; preds = %5
%26 = tail call i32 (i32, i8*, ...) @debug(i32 2, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %31
27: ; preds = %23, %15
%28 = tail call i32 (...) @ssl_strerr()
%29 = tail call i32 (i32, i8*, ...) @debug(i32 2, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %28)
%30 = tail call i32 @RSA_free(i32* %7)
br label %31
31: ; preds = %25, %27, %17, %23
%32 = phi i32* [ %7, %23 ], [ %7, %17 ], [ null, %27 ], [ null, %25 ]
ret i32* %32
}