#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;

/* Variables and functions */
int /*<<< orphan*/ OPENSSL_free(unsigned char *);
int /*<<< orphan*/ SHA1(unsigned char *, int, unsigned char *);
int i2d_RSAPublicKey(int /*<<< orphan*/ *, unsigned char **);
int /*<<< orphan*/ onion_encode(char *, unsigned char *);
bool rsa_to_onion(RSA *r, char *o) {
  if (!r || !o)
    return false;
  unsigned char *bin = NULL;
  int i = i2d_RSAPublicKey(r, &bin);
  if (!bin)
    return false;
  if (i < 0)
    goto fail;
  unsigned char d[20] = {0};
  SHA1(bin, i, d);
  OPENSSL_free(bin);
  onion_encode(&o[0], &d[0]);
  o[16] = 0;
  return true;
fail:
  if (bin) {
    OPENSSL_free(bin);
  }
  return false;
}


x86 assembly (clang, -O0):
<rsa_to_onion>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1176 <rsa_to_onion+0x26>
cmpq   $0x0,-0x18(%rbp)
jne    1182 <rsa_to_onion+0x32>
movl   $0x0,-0x4(%rbp)
jmp    1225 <rsa_to_onion+0xd5>
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1030 <i2d_RSAPublicKey@plt>
mov    %eax,-0x24(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b1 <rsa_to_onion+0x61>
movl   $0x0,-0x4(%rbp)
jmp    1225 <rsa_to_onion+0xd5>
cmpl   $0x0,-0x24(%rbp)
jge    11c0 <rsa_to_onion+0x70>
jmp    120a <rsa_to_onion+0xba>
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1040 <memset@plt>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
lea    -0x40(%rbp),%rdx
call   1060 <SHA1@plt>
mov    -0x20(%rbp),%rdi
call   1070 <OPENSSL_free@plt>
mov    -0x18(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1050 <onion_encode@plt>
mov    -0x18(%rbp),%rax
movb   $0x0,0x10(%rax)
movl   $0x1,-0x4(%rbp)
jmp    1225 <rsa_to_onion+0xd5>
cmpq   $0x0,-0x20(%rbp)
je     121e <rsa_to_onion+0xce>
mov    -0x20(%rbp),%rdi
call   1070 <OPENSSL_free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rsa_to_onion>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
xor    %ebp,%ebp
test   %rdi,%rdi
je     11bc <rsa_to_onion+0x7c>
mov    %rsi,%rbx
test   %rsi,%rsi
je     11bc <rsa_to_onion+0x7c>
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rsi
call   1030 <i2d_RSAPublicKey@plt>
mov    0x8(%rsp),%rdi
xor    %ebp,%ebp
test   %rdi,%rdi
je     11bc <rsa_to_onion+0x7c>
test   %eax,%eax
js     11b7 <rsa_to_onion+0x77>
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movl   $0x0,0x20(%rsp)
lea    0x10(%rsp),%rbp
mov    %eax,%esi
mov    %rbp,%rdx
call   1050 <SHA1@plt>
mov    0x8(%rsp),%rdi
call   1060 <OPENSSL_free@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1040 <onion_encode@plt>
movb   $0x0,0x10(%rbx)
mov    $0x1,%ebp
jmp    11bc <rsa_to_onion+0x7c>
call   1060 <OPENSSL_free@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @rsa_to_onion(i32* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [20 x i8]
store i32* %0, i32** %4
store i8* %1, i8** %5
%9 = load i32*, i32** %4
%10 = icmp ne i32* %9, null
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i8*, i8** %5
%13 = icmp ne i8* %12, null
br i1 %13, label %15, label %14
14: ; preds = %11, %2
store i32 0, i32* %3
br label %46
15: ; preds = %11
store i8* null, i8** %6
%16 = load i32*, i32** %4
%17 = call i32 @i2d_RSAPublicKey(i32* %16, i8** %6)
store i32 %17, i32* %7
%18 = load i8*, i8** %6
%19 = icmp ne i8* %18, null
br i1 %19, label %21, label %20
20: ; preds = %15
store i32 0, i32* %3
br label %46
21: ; preds = %15
%22 = load i32, i32* %7
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %21
br label %39
25: ; preds = %21
%26 = bitcast [20 x i8]* %8 to i8*
%27 = load i8*, i8** %6
%28 = load i32, i32* %7
%29 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%30 = call i32 @SHA1(i8* %27, i32 %28, i8* %29)
%31 = load i8*, i8** %6
%32 = call i32 @OPENSSL_free(i8* %31)
%33 = load i8*, i8** %5
%34 = getelementptr inbounds i8, i8* %33, i64 0
%35 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%36 = call i32 @onion_encode(i8* %34, i8* %35)
%37 = load i8*, i8** %5
%38 = getelementptr inbounds i8, i8* %37, i64 16
store i8 0, i8* %38
store i32 1, i32* %3
br label %46
39: ; preds = %24
%40 = load i8*, i8** %6
%41 = icmp ne i8* %40, null
br i1 %41, label %42, label %45
42: ; preds = %39
%43 = load i8*, i8** %6
%44 = call i32 @OPENSSL_free(i8* %43)
br label %45
45: ; preds = %42, %39
store i32 0, i32* %3
br label %46
46: ; preds = %45, %25, %20, %14
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
define i32 @rsa_to_onion(i32* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca [20 x i8]
%5 = icmp ne i32* %0, null
%6 = icmp ne i8* %1, null
%7 = and i1 %5, %6
br i1 %7, label %8, label %26
8: ; preds = %2
%9 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%10 = call i32 @i2d_RSAPublicKey(i32* %0, i8** %3)
%11 = load i8*, i8** %3
%12 = icmp eq i8* %11, null
br i1 %12, label %24, label %13
13: ; preds = %8
%14 = icmp slt i32 %10, 0
br i1 %14, label %22, label %15
15: ; preds = %13
%16 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%17 = call i32 @SHA1(i8* %11, i32 %10, i8* %16)
%18 = load i8*, i8** %3
%19 = call i32 @OPENSSL_free(i8* %18)
%20 = call i32 @onion_encode(i8* %1, i8* %16)
%21 = getelementptr inbounds i8, i8* %1, i64 16
store i8 0, i8* %21
br label %24
22: ; preds = %13
%23 = call i32 @OPENSSL_free(i8* %11)
br label %24
24: ; preds = %8, %22, %15
%25 = phi i32 [ 0, %22 ], [ 1, %15 ], [ 0, %8 ]
br label %26
26: ; preds = %2, %24
%27 = phi i32 [ %25, %24 ], [ 0, %2 ]
ret i32 %27
}