#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__ {
  scalar_t__ length;
  int /*<<< orphan*/ data;
};
typedef TYPE_1__ BUF_MEM;
typedef int /*<<< orphan*/ BIO_METHOD;
typedef int /*<<< orphan*/ BIO;

/* Variables and functions */
int /*<<< orphan*/ BIO_FLAGS_BASE64_NO_NL;
scalar_t__ 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_set_flags(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ BIO_write(int /*<<< orphan*/ *, unsigned char *, int);
scalar_t__ calloc(scalar_t__, int);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/, scalar_t__);
char *base64(unsigned char *input, int length) {
  BIO *b64 = NULL;
  BIO *bmem = NULL;
  BUF_MEM *bptr = NULL;
  char *output = NULL;
  b64 = BIO_new((BIO_METHOD *)BIO_f_base64());
  BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
  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);
  output = (char *)calloc(bptr->length + 1, sizeof(char));
  memcpy(output, bptr->data, bptr->length);
  BIO_free_all(b64);
  return output;
}


x86 assembly (clang, -O0):
<base64>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    $0x0,%al
call   1040 <BIO_f_base64@plt>
mov    %rax,%rdi
call   1080 <BIO_new@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2de3(%rip),%rax
mov    (%rax),%esi
call   1060 <BIO_set_flags@plt>
mov    $0x0,%al
call   10c0 <BIO_s_mem@plt>
mov    %rax,%rdi
call   1080 <BIO_new@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <BIO_push@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   10b0 <BIO_write@plt>
mov    -0x18(%rbp),%rdi
call   10d0 <BIO_flush@plt>
mov    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   10a0 <BIO_get_mem_ptr@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%esi
mov    -0x28(%rbp),%rax
mov    (%rax),%rdx
call   1070 <memcpy@plt>
mov    -0x18(%rbp),%rdi
call   1090 <BIO_free_all@plt>
mov    -0x30(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<base64>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
movq   $0x0,0x8(%rsp)
xor    %eax,%eax
call   1040 <BIO_f_base64@plt>
mov    %rax,%rdi
call   1080 <BIO_new@plt>
mov    %rax,%rbp
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1060 <BIO_set_flags@plt>
xor    %eax,%eax
call   10c0 <BIO_s_mem@plt>
mov    %rax,%rdi
call   1080 <BIO_new@plt>
mov    %rbp,%rdi
mov    %rax,%rsi
call   1030 <BIO_push@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   10b0 <BIO_write@plt>
mov    %rbp,%rdi
call   10d0 <BIO_flush@plt>
lea    0x8(%rsp),%rsi
mov    %rbp,%rdi
call   10a0 <BIO_get_mem_ptr@plt>
mov    0x8(%rsp),%rax
mov    (%rax),%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,%rbx
mov    0x8(%rsp),%rax
mov    0x8(%rax),%esi
mov    (%rax),%rdx
mov    %rbx,%rdi
call   1070 <memcpy@plt>
mov    %rbp,%rdi
call   1090 <BIO_free_all@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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