#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 *,
                             size_t);
int base64Encode(const unsigned char *buffer, size_t length, char **b64text) {
  BIO *bio, *b64;
  BUF_MEM *bufferPtr;
  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, buffer, length);
  BIO_flush(bio);
  BIO_get_mem_ptr(bio, &bufferPtr);
  BIO_set_close(bio, BIO_NOCLOSE);
  BIO_free_all(bio);
  *b64text = (*bufferPtr).data;
  return (0);
}


x86 assembly (clang, -O0):
<base64Encode>:
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),%rdx
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    0x2dbb(%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)
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<base64Encode>:
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    %r15,%rdx
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)
xor    %eax,%eax
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 i32 @base64Encode(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 = call i32 @BIO_write(i32* %20, i8* %21, i64 %22)
%24 = load i32*, i32** %7
%25 = call i32 @BIO_flush(i32* %24)
%26 = load i32*, i32** %7
%27 = call i32 @BIO_get_mem_ptr(i32* %26, %struct.TYPE_3__** %9)
%28 = load i32*, i32** %7
%29 = load i32, i32* @BIO_NOCLOSE
%30 = call i32 @BIO_set_close(i32* %28, i32 %29)
%31 = load i32*, i32** %7
%32 = call i32 @BIO_free_all(i32* %31)
%33 = load %struct.TYPE_3__*, %struct.TYPE_3__** %9
%34 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %33, i32 0, i32 0
%35 = load i8*, i8** %34
%36 = load i8**, i8*** %6
store i8* %35, i8** %36
ret i32 0
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i8* }
@BIO_FLAGS_BASE64_NO_NL = global i32 0
@BIO_NOCLOSE = global i32 0
define i32 @base64Encode(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 = tail call i32 @BIO_write(i32* %10, i8* %0, i64 %1)
%14 = tail call i32 @BIO_flush(i32* %10)
%15 = call i32 @BIO_get_mem_ptr(i32* %10, %struct.TYPE_3__** %4)
%16 = load i32, i32* @BIO_NOCLOSE
%17 = call i32 @BIO_set_close(i32* %10, i32 %16)
%18 = call i32 @BIO_free_all(i32* %10)
%19 = load %struct.TYPE_3__*, %struct.TYPE_3__** %4
%20 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %19, i64 0, i32 0
%21 = load i8*, i8** %20
store i8* %21, i8** %2
ret i32 0
}