#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 */
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
struct TYPE_3__ {
  int length;
  int /*<<< orphan*/ data;
};
typedef TYPE_1__ BUF_MEM;
typedef int /*<<< orphan*/ BIO;

/* Variables and functions */
int /*<<< orphan*/ BIO_FLAGS_BASE64_NO_NL;
int /*<<< orphan*/ BIO_f_base64();
scalar_t__ BIO_flush(int /*<<< orphan*/ *);
int /*<<< orphan*/ BIO_free_all(int /*<<< orphan*/ *);
int /*<<< orphan*/ BIO_get_mem_ptr(int /*<<< orphan*/ *, TYPE_1__ **);
int /*<<< orphan*/ *BIO_new(int /*<<< orphan*/);
int /*<<< orphan*/ *BIO_push(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ BIO_s_mem();
int /*<<< orphan*/ BIO_set_flags(int /*<<< orphan*/ *, int /*<<< orphan*/);
scalar_t__ BIO_write(int /*<<< orphan*/ *, char const *, size_t const);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/, int);
char *aBase64Encode(const char *input, const size_t length) {
  char *buff = NULL;
  BIO *b64 = BIO_new(BIO_f_base64());
  if (b64 == NULL) {
    return NULL;
  }
  BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
  BIO *bmem = BIO_new(BIO_s_mem());
  if (bmem == NULL) {
    BIO_free_all(b64);
    return NULL;
  }
  b64 = BIO_push(b64, bmem);
  if (b64 == NULL) {
    BIO_free_all(b64);
    return NULL;
  }
  if (BIO_write(b64, input, length) <= 0) {
    BIO_free_all(b64);
    return NULL;
  }
  if (BIO_flush(b64) <= 0) {
    BIO_free_all(b64);
    return NULL;
  }
  BUF_MEM *bptr = NULL;
  BIO_get_mem_ptr(b64, &bptr);
  if (bptr == NULL) {
    BIO_free_all(b64);
    return NULL;
  }
  buff = (char *)malloc(bptr->length + 1);
  if (buff == NULL) {
    BIO_free_all(b64);
    return NULL;
  }
  memcpy(buff, bptr->data, bptr->length);
  buff[bptr->length] = 0;
  BIO_free_all(b64);
  return buff;
}


x86 assembly (clang, -O0):
<aBase64Encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    $0x0,%al
call   1040 <BIO_f_base64@plt>
mov    %eax,%edi
call   1080 <BIO_new@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11f2 <aBase64Encode+0x42>
movq   $0x0,-0x8(%rbp)
jmp    1362 <aBase64Encode+0x1b2>
mov    -0x28(%rbp),%rdi
mov    0x2de3(%rip),%rax
mov    (%rax),%esi
call   1050 <BIO_set_flags@plt>
mov    $0x0,%al
call   10c0 <BIO_s_mem@plt>
mov    %eax,%edi
call   1080 <BIO_new@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1237 <aBase64Encode+0x87>
mov    -0x28(%rbp),%rdi
call   1090 <BIO_free_all@plt>
movq   $0x0,-0x8(%rbp)
jmp    1362 <aBase64Encode+0x1b2>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1030 <BIO_push@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1269 <aBase64Encode+0xb9>
mov    -0x28(%rbp),%rdi
call   1090 <BIO_free_all@plt>
movq   $0x0,-0x8(%rbp)
jmp    1362 <aBase64Encode+0x1b2>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   10b0 <BIO_write@plt>
cmp    $0x0,%rax
jg     129a <aBase64Encode+0xea>
mov    -0x28(%rbp),%rdi
call   1090 <BIO_free_all@plt>
movq   $0x0,-0x8(%rbp)
jmp    1362 <aBase64Encode+0x1b2>
mov    -0x28(%rbp),%rdi
call   10d0 <BIO_flush@plt>
cmp    $0x0,%rax
jg     12c3 <aBase64Encode+0x113>
mov    -0x28(%rbp),%rdi
call   1090 <BIO_free_all@plt>
movq   $0x0,-0x8(%rbp)
jmp    1362 <aBase64Encode+0x1b2>
movq   $0x0,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   10a0 <BIO_get_mem_ptr@plt>
cmpq   $0x0,-0x38(%rbp)
jne    12f9 <aBase64Encode+0x149>
mov    -0x28(%rbp),%rdi
call   1090 <BIO_free_all@plt>
movq   $0x0,-0x8(%rbp)
jmp    1362 <aBase64Encode+0x1b2>
mov    -0x38(%rbp),%rax
mov    (%rax),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    132c <aBase64Encode+0x17c>
mov    -0x28(%rbp),%rdi
call   1090 <BIO_free_all@plt>
movq   $0x0,-0x8(%rbp)
jmp    1362 <aBase64Encode+0x1b2>
mov    -0x20(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x38(%rbp),%rax
mov    (%rax),%edx
call   1060 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x38(%rbp),%rcx
movslq (%rcx),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
call   1090 <BIO_free_all@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<aBase64Encode>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r15
mov    %rdi,%r12
xor    %r13d,%r13d
xor    %eax,%eax
call   1040 <BIO_f_base64@plt>
mov    %eax,%edi
call   1080 <BIO_new@plt>
test   %rax,%rax
je     12b2 <aBase64Encode+0x102>
mov    %rax,%rbx
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1050 <BIO_set_flags@plt>
xor    %eax,%eax
call   10c0 <BIO_s_mem@plt>
mov    %eax,%edi
call   1080 <BIO_new@plt>
mov    %rbx,%rdi
test   %rax,%rax
je     1296 <aBase64Encode+0xe6>
mov    %rax,%rsi
call   1030 <BIO_push@plt>
test   %rax,%rax
je     12a0 <aBase64Encode+0xf0>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
call   10b0 <BIO_write@plt>
test   %rax,%rax
jle    1293 <aBase64Encode+0xe3>
mov    %r14,%rdi
call   10d0 <BIO_flush@plt>
test   %rax,%rax
jle    1293 <aBase64Encode+0xe3>
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
call   10a0 <BIO_get_mem_ptr@plt>
mov    0x8(%rsp),%rax
test   %rax,%rax
je     12a7 <aBase64Encode+0xf7>
mov    (%rax),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     12a7 <aBase64Encode+0xf7>
mov    %rax,%r13
mov    0x8(%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %r13,%rdi
call   1060 <memcpy@plt>
mov    0x8(%rsp),%rax
movslq (%rax),%rax
movb   $0x0,0x0(%r13,%rax,1)
jmp    12aa <aBase64Encode+0xfa>
mov    %r14,%rdi
call   1090 <BIO_free_all@plt>
xor    %r13d,%r13d
jmp    12b2 <aBase64Encode+0x102>
xor    %r13d,%r13d
xor    %edi,%edi
jmp    12ad <aBase64Encode+0xfd>
xor    %r13d,%r13d
mov    %r14,%rdi
call   1090 <BIO_free_all@plt>
mov    %r13,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32, i32 }
@BIO_FLAGS_BASE64_NO_NL = global i32 0
define i8* @aBase64Encode(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca %struct.TYPE_3__*
store i8* %0, i8** %4
store i64 %1, i64* %5
store i8* null, i8** %6
%10 = call i32 (...) @BIO_f_base64()
%11 = call i32* @BIO_new(i32 %10)
store i32* %11, i32** %7
%12 = load i32*, i32** %7
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %15
14: ; preds = %2
store i8* null, i8** %3
br label %89
15: ; preds = %2
%16 = load i32*, i32** %7
%17 = load i32, i32* @BIO_FLAGS_BASE64_NO_NL
%18 = call i32 @BIO_set_flags(i32* %16, i32 %17)
%19 = call i32 (...) @BIO_s_mem()
%20 = call i32* @BIO_new(i32 %19)
store i32* %20, i32** %8
%21 = load i32*, i32** %8
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %26
23: ; preds = %15
%24 = load i32*, i32** %7
%25 = call i32 @BIO_free_all(i32* %24)
store i8* null, i8** %3
br label %89
26: ; preds = %15
%27 = load i32*, i32** %7
%28 = load i32*, i32** %8
%29 = call i32* @BIO_push(i32* %27, i32* %28)
store i32* %29, i32** %7
%30 = load i32*, i32** %7
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %35
32: ; preds = %26
%33 = load i32*, i32** %7
%34 = call i32 @BIO_free_all(i32* %33)
store i8* null, i8** %3
br label %89
35: ; preds = %26
%36 = load i32*, i32** %7
%37 = load i8*, i8** %4
%38 = load i64, i64* %5
%39 = call i64 @BIO_write(i32* %36, i8* %37, i64 %38)
%40 = icmp sle i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %35
%42 = load i32*, i32** %7
%43 = call i32 @BIO_free_all(i32* %42)
store i8* null, i8** %3
br label %89
44: ; preds = %35
%45 = load i32*, i32** %7
%46 = call i64 @BIO_flush(i32* %45)
%47 = icmp sle i64 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %44
%49 = load i32*, i32** %7
%50 = call i32 @BIO_free_all(i32* %49)
store i8* null, i8** %3
br label %89
51: ; preds = %44
store %struct.TYPE_3__* null, %struct.TYPE_3__** %9
%52 = load i32*, i32** %7
%53 = call i32 @BIO_get_mem_ptr(i32* %52, %struct.TYPE_3__** %9)
%54 = load %struct.TYPE_3__*, %struct.TYPE_3__** %9
%55 = icmp eq %struct.TYPE_3__* %54, null
br i1 %55, label %56, label %59
56: ; preds = %51
%57 = load i32*, i32** %7
%58 = call i32 @BIO_free_all(i32* %57)
store i8* null, i8** %3
br label %89
59: ; preds = %51
%60 = load %struct.TYPE_3__*, %struct.TYPE_3__** %9
%61 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %60, i32 0, i32 0
%62 = load i32, i32* %61
%63 = add nsw i32 %62, 1
%64 = call i64 @malloc(i32 %63)
%65 = inttoptr i64 %64 to i8*
store i8* %65, i8** %6
%66 = load i8*, i8** %6
%67 = icmp eq i8* %66, null
br i1 %67, label %68, label %71
68: ; preds = %59
%69 = load i32*, i32** %7
%70 = call i32 @BIO_free_all(i32* %69)
store i8* null, i8** %3
br label %89
71: ; preds = %59
%72 = load i8*, i8** %6
%73 = load %struct.TYPE_3__*, %struct.TYPE_3__** %9
%74 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %73, i32 0, i32 1
%75 = load i32, i32* %74
%76 = load %struct.TYPE_3__*, %struct.TYPE_3__** %9
%77 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %76, i32 0, i32 0
%78 = load i32, i32* %77
%79 = call i32 @memcpy(i8* %72, i32 %75, i32 %78)
%80 = load i8*, i8** %6
%81 = load %struct.TYPE_3__*, %struct.TYPE_3__** %9
%82 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %81, i32 0, i32 0
%83 = load i32, i32* %82
%84 = sext i32 %83 to i64
%85 = getelementptr inbounds i8, i8* %80, i64 %84
store i8 0, i8* %85
%86 = load i32*, i32** %7
%87 = call i32 @BIO_free_all(i32* %86)
%88 = load i8*, i8** %6
store i8* %88, i8** %3
br label %89
89: ; preds = %71, %68, %56, %48, %41, %32, %23, %14
%90 = load i8*, i8** %3
ret i8* %90
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32, i32 }
@BIO_FLAGS_BASE64_NO_NL = global i32 0
define i8* @aBase64Encode(i8* %0, i64 %1) {
%3 = alloca %struct.TYPE_3__*
%4 = tail call i32 (...) @BIO_f_base64()
%5 = tail call i32* @BIO_new(i32 %4)
%6 = icmp eq i32* %5, null
br i1 %6, label %57, label %7
7: ; preds = %2
%8 = load i32, i32* @BIO_FLAGS_BASE64_NO_NL
%9 = tail call i32 @BIO_set_flags(i32* %5, i32 %8)
%10 = tail call i32 (...) @BIO_s_mem()
%11 = tail call i32* @BIO_new(i32 %10)
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %7
%14 = tail call i32 @BIO_free_all(i32* %5)
br label %57
15: ; preds = %7
%16 = tail call i32* @BIO_push(i32* %5, i32* %11)
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = tail call i32 @BIO_free_all(i32* null)
br label %57
20: ; preds = %15
%21 = tail call i64 @BIO_write(i32* %16, i8* %0, i64 %1)
%22 = icmp slt i64 %21, 1
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = tail call i32 @BIO_free_all(i32* %16)
br label %57
25: ; preds = %20
%26 = tail call i64 @BIO_flush(i32* %16)
%27 = icmp slt i64 %26, 1
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = tail call i32 @BIO_free_all(i32* %16)
br label %57
30: ; preds = %25
%31 = bitcast %struct.TYPE_3__** %3 to i8*
store %struct.TYPE_3__* null, %struct.TYPE_3__** %3
%32 = call i32 @BIO_get_mem_ptr(i32* %16, %struct.TYPE_3__** %3)
%33 = load %struct.TYPE_3__*, %struct.TYPE_3__** %3
%34 = icmp eq %struct.TYPE_3__* %33, null
br i1 %34, label %54, label %35
35: ; preds = %30
%36 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %33, i64 0, i32 0
%37 = load i32, i32* %36
%38 = add nsw i32 %37, 1
%39 = call i64 @malloc(i32 %38)
%40 = inttoptr i64 %39 to i8*
%41 = icmp eq i64 %39, 0
br i1 %41, label %54, label %42
42: ; preds = %35
%43 = load %struct.TYPE_3__*, %struct.TYPE_3__** %3
%44 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %43, i64 0, i32 1
%45 = load i32, i32* %44
%46 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %43, i64 0, i32 0
%47 = load i32, i32* %46
%48 = call i32 @memcpy(i8* %40, i32 %45, i32 %47)
%49 = load %struct.TYPE_3__*, %struct.TYPE_3__** %3
%50 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %49, i64 0, i32 0
%51 = load i32, i32* %50
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %40, i64 %52
store i8 0, i8* %53
br label %54
54: ; preds = %35, %30, %42
%55 = phi i8* [ %40, %42 ], [ null, %30 ], [ null, %35 ]
%56 = call i32 @BIO_free_all(i32* %16)
br label %57
57: ; preds = %13, %18, %23, %28, %54, %2
%58 = phi i8* [ null, %2 ], [ null, %13 ], [ null, %18 ], [ null, %23 ], [ null, %28 ], [ %55, %54 ]
ret i8* %58
}