#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_f_base64();
int /*<<< orphan*/ 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_write(int /*<<< orphan*/ *, unsigned char const *, int);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/, int);
char *util_base64_encode(const unsigned char *input, int length) {
  BIO *bmem, *b64;
  BUF_MEM *bptr;
  b64 = BIO_new(BIO_f_base64());
  bmem = BIO_new(BIO_s_mem());
  b64 = BIO_push(b64, bmem);
  BIO_write(b64, input, length);
  BIO_flush(b64);
  BIO_get_mem_ptr(b64, &bptr);
  char *buff = (char *)malloc(bptr->length);
  memcpy(buff, bptr->data, bptr->length - 1);
  buff[bptr->length - 1] = 0;
  BIO_free_all(b64);
  return buff;
}


x86 assembly (clang, -O0):
<util_base64_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    $0x0,%al
call   1040 <BIO_f_base64@plt>
mov    %eax,%edi
call   1070 <BIO_new@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   10b0 <BIO_s_mem@plt>
mov    %eax,%edi
call   1070 <BIO_new@plt>
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <BIO_push@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   10a0 <BIO_write@plt>
mov    -0x20(%rbp),%rdi
call   10c0 <BIO_flush@plt>
mov    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1090 <BIO_get_mem_ptr@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
sub    $0x1,%edx
call   1050 <memcpy@plt>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
call   1080 <BIO_free_all@plt>
mov    -0x30(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_base64_encode>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%rbp
xor    %eax,%eax
call   1040 <BIO_f_base64@plt>
mov    %eax,%edi
call   1070 <BIO_new@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   10b0 <BIO_s_mem@plt>
mov    %eax,%edi
call   1070 <BIO_new@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1030 <BIO_push@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %rbp,%rsi
mov    %r14d,%edx
call   10a0 <BIO_write@plt>
mov    %rbx,%rdi
call   10c0 <BIO_flush@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1090 <BIO_get_mem_ptr@plt>
mov    0x8(%rsp),%rax
mov    (%rax),%edi
call   1060 <malloc@plt>
mov    %rax,%rbp
mov    0x8(%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
add    $0xffffffff,%edx
mov    %rbp,%rdi
call   1050 <memcpy@plt>
mov    0x8(%rsp),%rax
movslq (%rax),%rax
movb   $0x0,-0x1(%rax,%rbp,1)
mov    %rbx,%rdi
call   1080 <BIO_free_all@plt>
mov    %rbp,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32, i32 }
define i8* @util_base64_encode(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca %struct.TYPE_3__*
%8 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
%9 = call i32 (...) @BIO_f_base64()
%10 = call i32* @BIO_new(i32 %9)
store i32* %10, i32** %6
%11 = call i32 (...) @BIO_s_mem()
%12 = call i32* @BIO_new(i32 %11)
store i32* %12, i32** %5
%13 = load i32*, i32** %6
%14 = load i32*, i32** %5
%15 = call i32* @BIO_push(i32* %13, i32* %14)
store i32* %15, i32** %6
%16 = load i32*, i32** %6
%17 = load i8*, i8** %3
%18 = load i32, i32* %4
%19 = call i32 @BIO_write(i32* %16, i8* %17, i32 %18)
%20 = load i32*, i32** %6
%21 = call i32 @BIO_flush(i32* %20)
%22 = load i32*, i32** %6
%23 = call i32 @BIO_get_mem_ptr(i32* %22, %struct.TYPE_3__** %7)
%24 = load %struct.TYPE_3__*, %struct.TYPE_3__** %7
%25 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %24, i32 0, i32 0
%26 = load i32, i32* %25
%27 = call i64 @malloc(i32 %26)
%28 = inttoptr i64 %27 to i8*
store i8* %28, i8** %8
%29 = load i8*, i8** %8
%30 = load %struct.TYPE_3__*, %struct.TYPE_3__** %7
%31 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %30, i32 0, i32 1
%32 = load i32, i32* %31
%33 = load %struct.TYPE_3__*, %struct.TYPE_3__** %7
%34 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = sub nsw i32 %35, 1
%37 = call i32 @memcpy(i8* %29, i32 %32, i32 %36)
%38 = load i8*, i8** %8
%39 = load %struct.TYPE_3__*, %struct.TYPE_3__** %7
%40 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %39, i32 0, i32 0
%41 = load i32, i32* %40
%42 = sub nsw i32 %41, 1
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %38, i64 %43
store i8 0, i8* %44
%45 = load i32*, i32** %6
%46 = call i32 @BIO_free_all(i32* %45)
%47 = load i8*, i8** %8
ret i8* %47
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32, i32 }
define i8* @util_base64_encode(i8* %0, i32 %1) {
%3 = alloca %struct.TYPE_3__*
%4 = bitcast %struct.TYPE_3__** %3 to i8*
%5 = tail call i32 (...) @BIO_f_base64()
%6 = tail call i32* @BIO_new(i32 %5)
%7 = tail call i32 (...) @BIO_s_mem()
%8 = tail call i32* @BIO_new(i32 %7)
%9 = tail call i32* @BIO_push(i32* %6, i32* %8)
%10 = tail call i32 @BIO_write(i32* %9, i8* %0, i32 %1)
%11 = tail call i32 @BIO_flush(i32* %9)
%12 = call i32 @BIO_get_mem_ptr(i32* %9, %struct.TYPE_3__** %3)
%13 = load %struct.TYPE_3__*, %struct.TYPE_3__** %3
%14 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %13, i64 0, i32 0
%15 = load i32, i32* %14
%16 = call i64 @malloc(i32 %15)
%17 = inttoptr i64 %16 to i8*
%18 = load %struct.TYPE_3__*, %struct.TYPE_3__** %3
%19 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %18, i64 0, i32 1
%20 = load i32, i32* %19
%21 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %18, i64 0, i32 0
%22 = load i32, i32* %21
%23 = add nsw i32 %22, -1
%24 = call i32 @memcpy(i8* %17, i32 %20, i32 %23)
%25 = load %struct.TYPE_3__*, %struct.TYPE_3__** %3
%26 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %25, i64 0, i32 0
%27 = load i32, i32* %26
%28 = add nsw i32 %27, -1
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %17, i64 %29
store i8 0, i8* %30
%31 = call i32 @BIO_free_all(i32* %9)
ret i8* %17
}