#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 *);
char *BN_bn2hex(int /*<<< orphan*/ const *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ reverse_endianness(unsigned char *, int);
int strlen(char *);
int custom_bn2lebinpad(const BIGNUM *a, unsigned char *to, int tolen) {
  int len;
  int result;
  char *hexstring;
  hexstring = BN_bn2hex(a);
  len = strlen(hexstring) / 2;
  if (len < tolen)
    memset(to, 0, tolen - len);
  result = BN_bn2bin(a, to + (tolen - len));
  reverse_endianness(to, tolen);
  free(hexstring);
  return result;
}


x86 assembly (clang, -O0):
<custom_bn2lebinpad>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <BN_bn2hex@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11b1 <custom_bn2lebinpad+0x51>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
sub    -0x18(%rbp),%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%eax
sub    -0x18(%rbp),%eax
cltq
add    %rax,%rsi
call   1070 <BN_bn2bin@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1080 <reverse_endianness@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<custom_bn2lebinpad>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %rdi,%r12
call   1060 <BN_bn2hex@plt>
mov    %rax,%r15
mov    %rax,%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %eax,%ecx
sar    %ecx
mov    %r14d,%ebp
sub    %ecx,%ebp
jle    119d <custom_bn2lebinpad+0x3d>
mov    %rbx,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1050 <memset@plt>
movslq %ebp,%rsi
add    %rbx,%rsi
mov    %r12,%rdi
call   1070 <BN_bn2bin@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    %r14d,%esi
call   1080 <reverse_endianness@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @custom_bn2lebinpad(i32* %0, i8* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i32* %0, i32** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%10 = load i32*, i32** %4
%11 = call i8* @BN_bn2hex(i32* %10)
store i8* %11, i8** %9
%12 = load i8*, i8** %9
%13 = call i32 @strlen(i8* %12)
%14 = sdiv i32 %13, 2
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = load i32, i32* %6
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %24
18: ; preds = %3
%19 = load i8*, i8** %5
%20 = load i32, i32* %6
%21 = load i32, i32* %7
%22 = sub nsw i32 %20, %21
%23 = call i32 @memset(i8* %19, i32 0, i32 %22)
br label %24
24: ; preds = %18, %3
%25 = load i32*, i32** %4
%26 = load i8*, i8** %5
%27 = load i32, i32* %6
%28 = load i32, i32* %7
%29 = sub nsw i32 %27, %28
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %26, i64 %30
%32 = call i32 @BN_bn2bin(i32* %25, i8* %31)
store i32 %32, i32* %8
%33 = load i8*, i8** %5
%34 = load i32, i32* %6
%35 = call i32 @reverse_endianness(i8* %33, i32 %34)
%36 = load i8*, i8** %9
%37 = call i32 @free(i8* %36)
%38 = load i32, i32* %8
ret i32 %38
}

LLVM IR (-O2):
define i32 @custom_bn2lebinpad(i32* %0, i8* %1, i32 %2) {
%4 = tail call i8* @BN_bn2hex(i32* %0)
%5 = tail call i32 @strlen(i8* %4)
%6 = sdiv i32 %5, 2
%7 = icmp slt i32 %6, %2
%8 = sub nsw i32 %2, %6
br i1 %7, label %9, label %11
9: ; preds = %3
%10 = tail call i32 @memset(i8* %1, i32 0, i32 %8)
br label %11
11: ; preds = %3, %9
%12 = sext i32 %8 to i64
%13 = getelementptr inbounds i8, i8* %1, i64 %12
%14 = tail call i32 @BN_bn2bin(i32* %0, i8* %13)
%15 = tail call i32 @reverse_endianness(i8* %1, i32 %2)
%16 = tail call i32 @free(i8* %4)
ret i32 %14
}