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

/* Variables and functions */
int /*<<< orphan*/ BIO_FLAGS_BASE64_NO_NL;
int /*<<< orphan*/ BIO_NOCLOSE;
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_set_close(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ BIO_set_flags(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ BIO_write(int /*<<< orphan*/ *, unsigned char const *, int);
void base64_encode(const unsigned char *s, size_t length, char **out) {
  BIO *bio, *b64;
  BUF_MEM *buffer;
  b64 = BIO_new(BIO_f_base64());
  bio = BIO_new(BIO_s_mem());
  bio = BIO_push(b64, bio);
  BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
  BIO_write(bio, s, (int)length);
  BIO_flush(bio);
  BIO_get_mem_ptr(bio, &buffer);
  BIO_set_close(bio, BIO_NOCLOSE);
  BIO_free_all(bio);
  *out = buffer->data;
}


x86 assembly (clang, -O0):
<base64_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0x0,%al
call   1050 <BIO_f_base64@plt>
mov    %eax,%edi
call   1070 <BIO_new@plt>
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   10b0 <BIO_s_mem@plt>
mov    %eax,%edi
call   1070 <BIO_new@plt>
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <BIO_push@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
call   1060 <BIO_set_flags@plt>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rax
mov    %eax,%edx
call   10a0 <BIO_write@plt>
mov    -0x20(%rbp),%rdi
call   10c0 <BIO_flush@plt>
mov    -0x20(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1090 <BIO_get_mem_ptr@plt>
mov    -0x20(%rbp),%rdi
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
call   1030 <BIO_set_close@plt>
mov    -0x20(%rbp),%rdi
call   1080 <BIO_free_all@plt>
mov    -0x30(%rbp),%rax
mov    (%rax),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<base64_encode>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
xor    %eax,%eax
call   1050 <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   1040 <BIO_push@plt>
mov    %rax,%rbx
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <BIO_set_flags@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r15d,%edx
call   10a0 <BIO_write@plt>
mov    %rbx,%rdi
call   10c0 <BIO_flush@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1090 <BIO_get_mem_ptr@plt>
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1030 <BIO_set_close@plt>
mov    %rbx,%rdi
call   1080 <BIO_free_all@plt>
mov    (%rsp),%rax
mov    (%rax),%rax
mov    %rax,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i8* }
@BIO_FLAGS_BASE64_NO_NL = global i32 0
@BIO_NOCLOSE = global i32 0
define void @base64_encode(i8* %0, i64 %1, i8** %2) {
%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** %2, i8*** %6
%10 = call i32 (...) @BIO_f_base64()
%11 = call i32* @BIO_new(i32 %10)
store i32* %11, i32** %8
%12 = call i32 (...) @BIO_s_mem()
%13 = call i32* @BIO_new(i32 %12)
store i32* %13, i32** %7
%14 = load i32*, i32** %8
%15 = load i32*, i32** %7
%16 = call i32* @BIO_push(i32* %14, i32* %15)
store i32* %16, i32** %7
%17 = load i32*, i32** %7
%18 = load i32, i32* @BIO_FLAGS_BASE64_NO_NL
%19 = call i32 @BIO_set_flags(i32* %17, i32 %18)
%20 = load i32*, i32** %7
%21 = load i8*, i8** %4
%22 = load i64, i64* %5
%23 = trunc i64 %22 to i32
%24 = call i32 @BIO_write(i32* %20, i8* %21, i32 %23)
%25 = load i32*, i32** %7
%26 = call i32 @BIO_flush(i32* %25)
%27 = load i32*, i32** %7
%28 = call i32 @BIO_get_mem_ptr(i32* %27, %struct.TYPE_3__** %9)
%29 = load i32*, i32** %7
%30 = load i32, i32* @BIO_NOCLOSE
%31 = call i32 @BIO_set_close(i32* %29, i32 %30)
%32 = load i32*, i32** %7
%33 = call i32 @BIO_free_all(i32* %32)
%34 = load %struct.TYPE_3__*, %struct.TYPE_3__** %9
%35 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %34, i32 0, i32 0
%36 = load i8*, i8** %35
%37 = load i8**, i8*** %6
store i8* %36, i8** %37
ret void
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i8* }
@BIO_FLAGS_BASE64_NO_NL = global i32 0
@BIO_NOCLOSE = global i32 0
define void @base64_encode(i8* %0, i64 %1, i8** nocapture writeonly %2) {
%4 = alloca %struct.TYPE_3__*
%5 = bitcast %struct.TYPE_3__** %4 to i8*
%6 = tail call i32 (...) @BIO_f_base64()
%7 = tail call i32* @BIO_new(i32 %6)
%8 = tail call i32 (...) @BIO_s_mem()
%9 = tail call i32* @BIO_new(i32 %8)
%10 = tail call i32* @BIO_push(i32* %7, i32* %9)
%11 = load i32, i32* @BIO_FLAGS_BASE64_NO_NL
%12 = tail call i32 @BIO_set_flags(i32* %10, i32 %11)
%13 = trunc i64 %1 to i32
%14 = tail call i32 @BIO_write(i32* %10, i8* %0, i32 %13)
%15 = tail call i32 @BIO_flush(i32* %10)
%16 = call i32 @BIO_get_mem_ptr(i32* %10, %struct.TYPE_3__** %4)
%17 = load i32, i32* @BIO_NOCLOSE
%18 = call i32 @BIO_set_close(i32* %10, i32 %17)
%19 = call i32 @BIO_free_all(i32* %10)
%20 = load %struct.TYPE_3__*, %struct.TYPE_3__** %4
%21 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %20, i64 0, i32 0
%22 = load i8*, i8** %21
store i8* %22, i8** %2
ret void
}