#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*/ X509_REQ;
typedef int /*<<< orphan*/ RSA;
typedef int /*<<< orphan*/ FILE;
typedef int /*<<< orphan*/ EVP_PKEY;

/* Variables and functions */
int /*<<< orphan*/ *EVP_PKEY_new();
int /*<<< orphan*/ EVP_PKEY_set1_RSA(int /*<<< orphan*/ *,
                                     int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_sha256();
int /*<<< orphan*/
PEM_write_RSAPrivateKey(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                        int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                        int /*<<< orphan*/, int /*<<< orphan*/ *,
                        int /*<<< orphan*/ *);
int /*<<< orphan*/ PEM_write_X509_REQ(int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ *);
int /*<<< orphan*/ RSA_F4;
int /*<<< orphan*/ *RSA_generate_key(int, int /*<<< orphan*/,
                                     int /*<<< orphan*/ *,
                                     int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ *makereq(int /*<<< orphan*/ *, char *, int /*<<< orphan*/);
void makersareq(char *commonname, int bits) {
  RSA *key;
  EVP_PKEY *pkey;
  X509_REQ *req;
  FILE *out;
  key = RSA_generate_key(bits, RSA_F4, NULL, NULL);
  pkey = EVP_PKEY_new();
  EVP_PKEY_set1_RSA(pkey, key);
  req = makereq(pkey, commonname, EVP_sha256());
  out = fopen("rsa_req.pem", "w");
  if (out == NULL)
    exit(-1);
  PEM_write_X509_REQ(out, req);
  fclose(out);
  out = fopen("rsa_req_key.pem", "w");
  if (out == NULL)
    exit(-1);
  PEM_write_RSAPrivateKey(out, key, NULL, NULL, 0, NULL, NULL);
  fclose(out);
}


x86 assembly (clang, -O0):
<makersareq>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   10b0 <RSA_generate_key@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1060 <EVP_PKEY_new@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10c0 <EVP_PKEY_set1_RSA@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   10a0 <EVP_sha256@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
call   1080 <makereq@plt>
mov    %rax,-0x28(%rbp)
lea    0xdec(%rip),%rdi
lea    0xdf1(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1239 <makersareq+0x99>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1050 <PEM_write_X509_REQ@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xdb8(%rip),%rdi
lea    0xdaf(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    127b <makersareq+0xdb>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x30(%rbp),%rdi
mov    -0x18(%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    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<makersareq>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%eax
mov    %rdi,%r15
mov    0x2e2b(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
xor    %edx,%edx
xor    %ecx,%ecx
call   10b0 <RSA_generate_key@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1060 <EVP_PKEY_new@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   10c0 <EVP_PKEY_set1_RSA@plt>
xor    %eax,%eax
call   10a0 <EVP_sha256@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1080 <makereq@plt>
mov    %rax,%r15
lea    0xe08(%rip),%rdi
lea    0xe0d(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1268 <makersareq+0xc8>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
call   1050 <PEM_write_X509_REQ@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xde8(%rip),%rdi
lea    0xddf(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1268 <makersareq+0xc8>
mov    %rax,%rbx
movq   $0x0,(%rsp)
mov    %rax,%rdi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1030 <PEM_write_RSAPrivateKey@plt>
mov    %rbx,%rdi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <fclose@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@RSA_F4 = global i32 0
@.str = [12 x i8] c"rsa_req.pem\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [16 x i8] c"rsa_req_key.pem\00"
define void @makersareq(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
store i8* %0, i8** %3
store i32 %1, i32* %4
%9 = load i32, i32* %4
%10 = load i32, i32* @RSA_F4
%11 = call i32* @RSA_generate_key(i32 %9, i32 %10, i32* null, i32* null)
store i32* %11, i32** %5
%12 = call i32* (...) @EVP_PKEY_new()
store i32* %12, i32** %6
%13 = load i32*, i32** %6
%14 = load i32*, i32** %5
%15 = call i32 @EVP_PKEY_set1_RSA(i32* %13, i32* %14)
%16 = load i32*, i32** %6
%17 = load i8*, i8** %3
%18 = call i32 (...) @EVP_sha256()
%19 = call i32* @makereq(i32* %16, i8* %17, i32 %18)
store i32* %19, i32** %7
%20 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %20, i32** %8
%21 = load i32*, i32** %8
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %25
23: ; preds = %2
%24 = call i32 @exit(i32 -1)
unreachable
25: ; preds = %2
%26 = load i32*, i32** %8
%27 = load i32*, i32** %7
%28 = call i32 @PEM_write_X509_REQ(i32* %26, i32* %27)
%29 = load i32*, i32** %8
%30 = call i32 @fclose(i32* %29)
%31 = call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %31, i32** %8
%32 = load i32*, i32** %8
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %36
34: ; preds = %25
%35 = call i32 @exit(i32 -1)
unreachable
36: ; preds = %25
%37 = load i32*, i32** %8
%38 = load i32*, i32** %5
%39 = call i32 @PEM_write_RSAPrivateKey(i32* %37, i32* %38, i32* null, i32* null, i32 0, i32* null, i32* null)
%40 = load i32*, i32** %8
%41 = call i32 @fclose(i32* %40)
ret void
}

LLVM IR (-O2):
@RSA_F4 = global i32 0
@.str = [12 x i8] c"rsa_req.pem\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [16 x i8] c"rsa_req_key.pem\00"
define void @makersareq(i8* %0, i32 %1) {
%3 = load i32, i32* @RSA_F4
%4 = tail call i32* @RSA_generate_key(i32 %1, i32 %3, i32* null, i32* null)
%5 = tail call i32* (...) @EVP_PKEY_new()
%6 = tail call i32 @EVP_PKEY_set1_RSA(i32* %5, i32* %4)
%7 = tail call i32 (...) @EVP_sha256()
%8 = tail call i32* @makereq(i32* %5, i8* %0, i32 %7)
%9 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = tail call i32 @exit(i32 -1)
unreachable
13: ; preds = %2
%14 = tail call i32 @PEM_write_X509_REQ(i32* %9, i32* %8)
%15 = tail call i32 @fclose(i32* %9)
%16 = tail call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = tail call i32 @exit(i32 -1)
unreachable
20: ; preds = %13
%21 = tail call i32 @PEM_write_RSAPrivateKey(i32* %16, i32* %4, i32* null, i32* null, i32 0, i32* null, i32* null)
%22 = tail call i32 @fclose(i32* %16)
ret void
}