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

/* Variables and functions */
int BN_bn2bin(int /*<<< orphan*/ const *, unsigned char *);
int BN_num_bytes(int /*<<< orphan*/ const *);
char *base64buf_url(char *, int);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ warn(char *);
int /*<<< orphan*/ warnx(char *);
char *bn2string(const BIGNUM *bn) {
  int len;
  char *buf, *bbuf;
  len = BN_num_bytes(bn);
  if (NULL == (buf = malloc(len))) {
    warn("malloc");
    return (NULL);
  } else if (len != BN_bn2bin(bn, (unsigned char *)buf)) {
    warnx("BN_bn2bin");
    free(buf);
    return (NULL);
  }
  if (NULL == (bbuf = base64buf_url(buf, len))) {
    warnx("base64buf_url");
    free(buf);
    return (NULL);
  }
  free(buf);
  return (bbuf);
}


x86 assembly (clang, -O0):
<bn2string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <BN_num_bytes@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    %rcx,-0x20(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    11bb <bn2string+0x4b>
lea    0xe57(%rip),%rdi
call   1040 <warn@plt>
movq   $0x0,-0x8(%rbp)
jmp    1253 <bn2string+0xe3>
mov    -0x14(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1090 <BN_bn2bin@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
cmp    %ecx,%eax
je     11fd <bn2string+0x8d>
lea    0xe25(%rip),%rdi
call   1060 <warnx@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1253 <bn2string+0xe3>
jmp    1202 <bn2string+0x92>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <base64buf_url@plt>
mov    %rax,%rcx
mov    %rcx,-0x28(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    1242 <bn2string+0xd2>
lea    0xdea(%rip),%rdi
call   1060 <warnx@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1253 <bn2string+0xe3>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bn2string>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1080 <BN_num_bytes@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     11bb <bn2string+0x4b>
mov    %rax,%r15
mov    %rbx,%rdi
mov    %rax,%rsi
call   1090 <BN_bn2bin@plt>
cmp    %eax,%ebp
jne    11c9 <bn2string+0x59>
mov    %r15,%rdi
mov    %ebp,%esi
call   1050 <base64buf_url@plt>
test   %rax,%rax
je     11d2 <bn2string+0x62>
mov    %rax,%r14
mov    %r15,%rdi
call   1030 <free@plt>
jmp    11e9 <bn2string+0x79>
lea    0xe3e(%rip),%rdi
call   1040 <warn@plt>
jmp    11e6 <bn2string+0x76>
lea    0xe37(%rip),%rdi
jmp    11d9 <bn2string+0x69>
lea    0xe38(%rip),%rdi
call   1060 <warnx@plt>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"malloc\00"
@.str.1 = [10 x i8] c"BN_bn2bin\00"
@.str.2 = [14 x i8] c"base64buf_url\00"
define i8* @bn2string(i32* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
store i32* %0, i32** %3
%7 = load i32*, i32** %3
%8 = call i32 @BN_num_bytes(i32* %7)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = call i8* @malloc(i32 %9)
store i8* %10, i8** %5
%11 = icmp eq i8* null, %10
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = call i32 @warn(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %2
br label %38
14: ; preds = %1
%15 = load i32, i32* %4
%16 = load i32*, i32** %3
%17 = load i8*, i8** %5
%18 = call i32 @BN_bn2bin(i32* %16, i8* %17)
%19 = icmp ne i32 %15, %18
br i1 %19, label %20, label %24
20: ; preds = %14
%21 = call i32 @warnx(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%22 = load i8*, i8** %5
%23 = call i32 @free(i8* %22)
store i8* null, i8** %2
br label %38
24: ; preds = %14
br label %25
25: ; preds = %24
%26 = load i8*, i8** %5
%27 = load i32, i32* %4
%28 = call i8* @base64buf_url(i8* %26, i32 %27)
store i8* %28, i8** %6
%29 = icmp eq i8* null, %28
br i1 %29, label %30, label %34
30: ; preds = %25
%31 = call i32 @warnx(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%32 = load i8*, i8** %5
%33 = call i32 @free(i8* %32)
store i8* null, i8** %2
br label %38
34: ; preds = %25
%35 = load i8*, i8** %5
%36 = call i32 @free(i8* %35)
%37 = load i8*, i8** %6
store i8* %37, i8** %2
br label %38
38: ; preds = %34, %30, %20, %12
%39 = load i8*, i8** %2
ret i8* %39
}

LLVM IR (-O2):
@.str = [7 x i8] c"malloc\00"
@.str.1 = [10 x i8] c"BN_bn2bin\00"
@.str.2 = [14 x i8] c"base64buf_url\00"
define i8* @bn2string(i32* %0) {
%2 = tail call i32 @BN_num_bytes(i32* %0)
%3 = tail call i8* @malloc(i32 %2)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32 @warn(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %21
7: ; preds = %1
%8 = tail call i32 @BN_bn2bin(i32* %0, i8* %3)
%9 = icmp eq i32 %2, %8
br i1 %9, label %13, label %10
10: ; preds = %7
%11 = tail call i32 @warnx(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%12 = tail call i32 @free(i8* %3)
br label %21
13: ; preds = %7
%14 = tail call i8* @base64buf_url(i8* %3, i32 %2)
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = tail call i32 @warnx(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%18 = tail call i32 @free(i8* %3)
br label %21
19: ; preds = %13
%20 = tail call i32 @free(i8* %3)
br label %21
21: ; preds = %19, %16, %10, %5
%22 = phi i8* [ null, %5 ], [ null, %10 ], [ null, %16 ], [ %14, %19 ]
ret i8* %22
}