#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 /*<<< orphan*/ BN_add(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_lshift(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ *BN_new();
int /*<<< orphan*/ BN_set_word(int /*<<< orphan*/ *, char const);
int /*<<< orphan*/ BN_zero(int /*<<< orphan*/ *);
BIGNUM *binEncode(const char *input, unsigned long len) {
  BIGNUM *num = BN_new();
  BN_zero(num);
  BIGNUM *var = BN_new();
  for (int t = 0; t < len; t++) {
    BN_set_word(var, *(input + t));
    BN_lshift(num, num, 8);
    BN_add(num, num, var);
  }
  BN_free(var);
  return num;
}


x86 assembly (clang, -O0):
<binEncode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1050 <BN_new@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <BN_zero@plt>
mov    $0x0,%al
call   1050 <BN_new@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movslq -0x24(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11ea <binEncode+0x8a>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsbl (%rax,%rcx,1),%esi
call   1040 <BN_set_word@plt>
mov    -0x18(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1070 <BN_lshift@plt>
mov    -0x18(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <BN_add@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1196 <binEncode+0x36>
mov    -0x20(%rbp),%rdi
call   1060 <BN_free@plt>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<binEncode>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
xor    %eax,%eax
call   1050 <BN_new@plt>
mov    %rax,%r13
mov    %rax,%rdi
call   1080 <BN_zero@plt>
xor    %eax,%eax
call   1050 <BN_new@plt>
mov    %rax,%r12
test   %r15,%r15
je     11d4 <binEncode+0x74>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movsbl (%r14,%rbx,1),%esi
mov    %r12,%rdi
call   1040 <BN_set_word@plt>
mov    %r13,%rdi
mov    %r13,%rsi
mov    $0x8,%edx
call   1070 <BN_lshift@plt>
mov    %r13,%rdi
mov    %r13,%rsi
mov    %r12,%rdx
call   1030 <BN_add@plt>
add    $0x1,%rbx
cmp    %rbx,%r15
jne    11a0 <binEncode+0x40>
mov    %r12,%rdi
call   1060 <BN_free@plt>
mov    %r13,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32* @binEncode(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
store i8* %0, i8** %3
store i64 %1, i64* %4
%8 = call i32* (...) @BN_new()
store i32* %8, i32** %5
%9 = load i32*, i32** %5
%10 = call i32 @BN_zero(i32* %9)
%11 = call i32* (...) @BN_new()
store i32* %11, i32** %6
store i32 0, i32* %7
br label %12
12: ; preds = %32, %2
%13 = load i32, i32* %7
%14 = sext i32 %13 to i64
%15 = load i64, i64* %4
%16 = icmp ult i64 %14, %15
br i1 %16, label %17, label %35
17: ; preds = %12
%18 = load i32*, i32** %6
%19 = load i8*, i8** %3
%20 = load i32, i32* %7
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %19, i64 %21
%23 = load i8, i8* %22
%24 = call i32 @BN_set_word(i32* %18, i8 signext %23)
%25 = load i32*, i32** %5
%26 = load i32*, i32** %5
%27 = call i32 @BN_lshift(i32* %25, i32* %26, i32 8)
%28 = load i32*, i32** %5
%29 = load i32*, i32** %5
%30 = load i32*, i32** %6
%31 = call i32 @BN_add(i32* %28, i32* %29, i32* %30)
br label %32
32: ; preds = %17
%33 = load i32, i32* %7
%34 = add nsw i32 %33, 1
store i32 %34, i32* %7
br label %12
35: ; preds = %12
%36 = load i32*, i32** %6
%37 = call i32 @BN_free(i32* %36)
%38 = load i32*, i32** %5
ret i32* %38
}

LLVM IR (-O2):
define i32* @binEncode(i8* nocapture %0, i64 %1) {
%3 = tail call i32* (...) @BN_new()
%4 = tail call i32 @BN_zero(i32* %3)
%5 = tail call i32* (...) @BN_new()
%6 = icmp eq i64 %1, 0
br i1 %6, label %7, label %9
7: ; preds = %9, %2
%8 = tail call i32 @BN_free(i32* %5)
ret i32* %3
9: ; preds = %2, %9
%10 = phi i64 [ %16, %9 ], [ 0, %2 ]
%11 = getelementptr inbounds i8, i8* %0, i64 %10
%12 = load i8, i8* %11
%13 = tail call i32 @BN_set_word(i32* %5, i8 signext %12)
%14 = tail call i32 @BN_lshift(i32* %3, i32* %3, i32 8)
%15 = tail call i32 @BN_add(i32* %3, i32* %3, i32* %5)
%16 = add nuw i64 %10, 1
%17 = icmp eq i64 %16, %1
br i1 %17, label %7, label %9
}