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

/* Variables and functions */
int RSA_bits(int /*<<< orphan*/ *);
int /*<<< orphan*/ RSA_get0_crt_params(int /*<<< orphan*/ *,
                                       int /*<<< orphan*/ const **,
                                       int /*<<< orphan*/ const **,
                                       int /*<<< orphan*/ const **);
int /*<<< orphan*/ RSA_get0_factors(int /*<<< orphan*/ *,
                                    int /*<<< orphan*/ const **,
                                    int /*<<< orphan*/ const **);
int /*<<< orphan*/ RSA_get0_key(int /*<<< orphan*/ *,
                                int /*<<< orphan*/ const **,
                                int /*<<< orphan*/ const **,
                                int /*<<< orphan*/ const **);
int RSA_size(int /*<<< orphan*/ *);
int /*<<< orphan*/ write_lebn(unsigned char **, int /*<<< orphan*/ const *,
                              int);
void write_rsa(unsigned char **out, RSA *rsa, int ispub) {
  int nbyte, hnbyte;
  const BIGNUM *n, *d, *e, *p, *q, *iqmp, *dmp1, *dmq1;
  nbyte = RSA_size(rsa);
  hnbyte = (RSA_bits(rsa) + 15) >> 4;
  RSA_get0_key(rsa, &n, &e, &d);
  write_lebn(out, e, 4);
  write_lebn(out, n, -1);
  if (ispub)
    return;
  RSA_get0_factors(rsa, &p, &q);
  RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
  write_lebn(out, p, hnbyte);
  write_lebn(out, q, hnbyte);
  write_lebn(out, dmp1, hnbyte);
  write_lebn(out, dmq1, hnbyte);
  write_lebn(out, iqmp, hnbyte);
  write_lebn(out, d, nbyte);
}


x86 assembly (clang, -O0):
<write_rsa>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <RSA_size@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <RSA_bits@plt>
add    $0xf,%eax
sar    $0x4,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x30(%rbp),%rcx
call   1060 <RSA_get0_key@plt>
mov    -0x8(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    $0x4,%edx
call   1080 <write_lebn@plt>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    $0xffffffff,%edx
call   1080 <write_lebn@plt>
cmpl   $0x0,-0x14(%rbp)
je     11d9 <write_rsa+0x79>
jmp    125f <write_rsa+0xff>
mov    -0x10(%rbp),%rdi
lea    -0x40(%rbp),%rsi
lea    -0x48(%rbp),%rdx
call   1070 <RSA_get0_factors@plt>
mov    -0x10(%rbp),%rdi
lea    -0x58(%rbp),%rsi
lea    -0x60(%rbp),%rdx
lea    -0x50(%rbp),%rcx
call   1030 <RSA_get0_crt_params@plt>
mov    -0x8(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1080 <write_lebn@plt>
mov    -0x8(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1080 <write_lebn@plt>
mov    -0x8(%rbp),%rdi
mov    -0x58(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1080 <write_lebn@plt>
mov    -0x8(%rbp),%rdi
mov    -0x60(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1080 <write_lebn@plt>
mov    -0x8(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1080 <write_lebn@plt>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1080 <write_lebn@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_rsa>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
mov    %edx,%r12d
mov    %rsi,%rbp
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1040 <RSA_size@plt>
mov    %eax,%r14d
mov    %rbp,%rdi
call   1050 <RSA_bits@plt>
mov    %eax,%r15d
lea    0x38(%rsp),%rsi
lea    0x28(%rsp),%rdx
lea    0x30(%rsp),%rcx
mov    %rbp,%rdi
call   1060 <RSA_get0_key@plt>
mov    0x28(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x4,%edx
call   1080 <write_lebn@plt>
mov    0x38(%rsp),%rsi
mov    %rbx,%rdi
mov    $0xffffffff,%edx
call   1080 <write_lebn@plt>
test   %r12d,%r12d
jne    125d <write_rsa+0xfd>
add    $0xf,%r15d
sar    $0x4,%r15d
lea    0x20(%rsp),%rsi
lea    0x18(%rsp),%rdx
mov    %rbp,%rdi
call   1070 <RSA_get0_factors@plt>
lea    0x8(%rsp),%rsi
mov    %rsp,%rdx
lea    0x10(%rsp),%rcx
mov    %rbp,%rdi
call   1030 <RSA_get0_crt_params@plt>
mov    0x20(%rsp),%rsi
mov    %rbx,%rdi
mov    %r15d,%edx
call   1080 <write_lebn@plt>
mov    0x18(%rsp),%rsi
mov    %rbx,%rdi
mov    %r15d,%edx
call   1080 <write_lebn@plt>
mov    0x8(%rsp),%rsi
mov    %rbx,%rdi
mov    %r15d,%edx
call   1080 <write_lebn@plt>
mov    (%rsp),%rsi
mov    %rbx,%rdi
mov    %r15d,%edx
call   1080 <write_lebn@plt>
mov    0x10(%rsp),%rsi
mov    %rbx,%rdi
mov    %r15d,%edx
call   1080 <write_lebn@plt>
mov    0x30(%rsp),%rsi
mov    %rbx,%rdi
mov    %r14d,%edx
call   1080 <write_lebn@plt>
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @write_rsa(i8** %0, i32* %1, i32 %2) {
%4 = alloca i8**
%5 = alloca i32*
%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 i32*
%16 = alloca i32*
store i8** %0, i8*** %4
store i32* %1, i32** %5
store i32 %2, i32* %6
%17 = load i32*, i32** %5
%18 = call i32 @RSA_size(i32* %17)
store i32 %18, i32* %7
%19 = load i32*, i32** %5
%20 = call i32 @RSA_bits(i32* %19)
%21 = add nsw i32 %20, 15
%22 = ashr i32 %21, 4
store i32 %22, i32* %8
%23 = load i32*, i32** %5
%24 = call i32 @RSA_get0_key(i32* %23, i32** %9, i32** %11, i32** %10)
%25 = load i8**, i8*** %4
%26 = load i32*, i32** %11
%27 = call i32 @write_lebn(i8** %25, i32* %26, i32 4)
%28 = load i8**, i8*** %4
%29 = load i32*, i32** %9
%30 = call i32 @write_lebn(i8** %28, i32* %29, i32 -1)
%31 = load i32, i32* %6
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %3
br label %63
34: ; preds = %3
%35 = load i32*, i32** %5
%36 = call i32 @RSA_get0_factors(i32* %35, i32** %12, i32** %13)
%37 = load i32*, i32** %5
%38 = call i32 @RSA_get0_crt_params(i32* %37, i32** %15, i32** %16, i32** %14)
%39 = load i8**, i8*** %4
%40 = load i32*, i32** %12
%41 = load i32, i32* %8
%42 = call i32 @write_lebn(i8** %39, i32* %40, i32 %41)
%43 = load i8**, i8*** %4
%44 = load i32*, i32** %13
%45 = load i32, i32* %8
%46 = call i32 @write_lebn(i8** %43, i32* %44, i32 %45)
%47 = load i8**, i8*** %4
%48 = load i32*, i32** %15
%49 = load i32, i32* %8
%50 = call i32 @write_lebn(i8** %47, i32* %48, i32 %49)
%51 = load i8**, i8*** %4
%52 = load i32*, i32** %16
%53 = load i32, i32* %8
%54 = call i32 @write_lebn(i8** %51, i32* %52, i32 %53)
%55 = load i8**, i8*** %4
%56 = load i32*, i32** %14
%57 = load i32, i32* %8
%58 = call i32 @write_lebn(i8** %55, i32* %56, i32 %57)
%59 = load i8**, i8*** %4
%60 = load i32*, i32** %10
%61 = load i32, i32* %7
%62 = call i32 @write_lebn(i8** %59, i32* %60, i32 %61)
br label %63
63: ; preds = %34, %33
ret void
}

LLVM IR (-O2):
define void @write_rsa(i8** %0, i32* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32*
%12 = bitcast i32** %4 to i8*
%13 = bitcast i32** %5 to i8*
%14 = bitcast i32** %6 to i8*
%15 = bitcast i32** %7 to i8*
%16 = bitcast i32** %8 to i8*
%17 = bitcast i32** %9 to i8*
%18 = bitcast i32** %10 to i8*
%19 = bitcast i32** %11 to i8*
%20 = tail call i32 @RSA_size(i32* %1)
%21 = tail call i32 @RSA_bits(i32* %1)
%22 = add nsw i32 %21, 15
%23 = ashr i32 %22, 4
%24 = call i32 @RSA_get0_key(i32* %1, i32** %4, i32** %6, i32** %5)
%25 = load i32*, i32** %6
%26 = call i32 @write_lebn(i8** %0, i32* %25, i32 4)
%27 = load i32*, i32** %4
%28 = call i32 @write_lebn(i8** %0, i32* %27, i32 -1)
%29 = icmp eq i32 %2, 0
br i1 %29, label %30, label %45
30: ; preds = %3
%31 = call i32 @RSA_get0_factors(i32* %1, i32** %7, i32** %8)
%32 = call i32 @RSA_get0_crt_params(i32* %1, i32** %10, i32** %11, i32** %9)
%33 = load i32*, i32** %7
%34 = call i32 @write_lebn(i8** %0, i32* %33, i32 %23)
%35 = load i32*, i32** %8
%36 = call i32 @write_lebn(i8** %0, i32* %35, i32 %23)
%37 = load i32*, i32** %10
%38 = call i32 @write_lebn(i8** %0, i32* %37, i32 %23)
%39 = load i32*, i32** %11
%40 = call i32 @write_lebn(i8** %0, i32* %39, i32 %23)
%41 = load i32*, i32** %9
%42 = call i32 @write_lebn(i8** %0, i32* %41, i32 %23)
%43 = load i32*, i32** %5
%44 = call i32 @write_lebn(i8** %0, i32* %43, i32 %20)
br label %45
45: ; preds = %3, %30
ret void
}