#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);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/, int);
scalar_t__ strlen(char *);
int base64encode(const unsigned char *source, int length, char **target) {
  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, source, length);
  BIO_flush(b64);
  BIO_get_mem_ptr(b64, &bptr);
  if (*target) {
    free(*target);
  }
  *target = (char *)malloc(bptr->length);
  memcpy(*target, bptr->data, bptr->length - 1);
  (*target)[bptr->length - 1] = 0;
  BIO_free_all(b64);
  return (int)strlen(*target);
}


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

x86 assembly (clang, -O2):
<base64encode>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%r15d
mov    %rdi,%rbp
xor    %eax,%eax
call   1060 <BIO_f_base64@plt>
mov    %eax,%edi
call   1090 <BIO_new@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   10d0 <BIO_s_mem@plt>
mov    %eax,%edi
call   1090 <BIO_new@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1040 <BIO_push@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %rbp,%rsi
mov    %r15d,%edx
call   10c0 <BIO_write@plt>
mov    %rbx,%rdi
call   10e0 <BIO_flush@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   10b0 <BIO_get_mem_ptr@plt>
mov    (%r14),%rdi
test   %rdi,%rdi
je     122b <base64encode+0x6b>
call   1030 <free@plt>
mov    (%rsp),%rax
mov    (%rax),%edi
call   1080 <malloc@plt>
mov    %rax,(%r14)
mov    (%rsp),%rcx
mov    (%rcx),%edx
mov    0x4(%rcx),%esi
add    $0xffffffff,%edx
mov    %rax,%rdi
call   1070 <memcpy@plt>
mov    (%r14),%rax
mov    (%rsp),%rcx
movslq (%rcx),%rcx
movb   $0x0,-0x1(%rcx,%rax,1)
mov    %rbx,%rdi
call   10a0 <BIO_free_all@plt>
mov    (%r14),%rdi
call   1050 <strlen@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32, i32 }
define i32 @base64encode(i8* %0, i32 %1, i8** %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca %struct.TYPE_3__*
store i8* %0, i8** %4
store i32 %1, i32* %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** %8
%17 = load i32*, i32** %8
%18 = load i8*, i8** %4
%19 = load i32, i32* %5
%20 = call i32 @BIO_write(i32* %17, i8* %18, i32 %19)
%21 = load i32*, i32** %8
%22 = call i32 @BIO_flush(i32* %21)
%23 = load i32*, i32** %8
%24 = call i32 @BIO_get_mem_ptr(i32* %23, %struct.TYPE_3__** %9)
%25 = load i8**, i8*** %6
%26 = load i8*, i8** %25
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %32
28: ; preds = %3
%29 = load i8**, i8*** %6
%30 = load i8*, i8** %29
%31 = call i32 @free(i8* %30)
br label %32
32: ; preds = %28, %3
%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 i32, i32* %34
%36 = call i64 @malloc(i32 %35)
%37 = inttoptr i64 %36 to i8*
%38 = load i8**, i8*** %6
store i8* %37, i8** %38
%39 = load i8**, i8*** %6
%40 = load i8*, i8** %39
%41 = load %struct.TYPE_3__*, %struct.TYPE_3__** %9
%42 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %41, i32 0, i32 1
%43 = load i32, i32* %42
%44 = load %struct.TYPE_3__*, %struct.TYPE_3__** %9
%45 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %44, i32 0, i32 0
%46 = load i32, i32* %45
%47 = sub nsw i32 %46, 1
%48 = call i32 @memcpy(i8* %40, i32 %43, i32 %47)
%49 = load i8**, i8*** %6
%50 = load i8*, i8** %49
%51 = load %struct.TYPE_3__*, %struct.TYPE_3__** %9
%52 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %51, i32 0, i32 0
%53 = load i32, i32* %52
%54 = sub nsw i32 %53, 1
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %50, i64 %55
store i8 0, i8* %56
%57 = load i32*, i32** %8
%58 = call i32 @BIO_free_all(i32* %57)
%59 = load i8**, i8*** %6
%60 = load i8*, i8** %59
%61 = call i64 @strlen(i8* %60)
%62 = trunc i64 %61 to i32
ret i32 %62
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32, i32 }
define i32 @base64encode(i8* %0, i32 %1, i8** nocapture %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 = tail call i32 @BIO_write(i32* %10, i8* %0, i32 %1)
%12 = tail call i32 @BIO_flush(i32* %10)
%13 = call i32 @BIO_get_mem_ptr(i32* %10, %struct.TYPE_3__** %4)
%14 = load i8*, i8** %2
%15 = icmp eq i8* %14, null
br i1 %15, label %18, label %16
16: ; preds = %3
%17 = call i32 @free(i8* %14)
br label %18
18: ; preds = %16, %3
%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 i32, i32* %20
%22 = call i64 @malloc(i32 %21)
%23 = inttoptr i64 %22 to i8*
store i8* %23, i8** %2
%24 = load %struct.TYPE_3__*, %struct.TYPE_3__** %4
%25 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %24, i64 0, i32 1
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %24, i64 0, i32 0
%28 = load i32, i32* %27
%29 = add nsw i32 %28, -1
%30 = call i32 @memcpy(i8* %23, i32 %26, i32 %29)
%31 = load i8*, i8** %2
%32 = load %struct.TYPE_3__*, %struct.TYPE_3__** %4
%33 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %32, i64 0, i32 0
%34 = load i32, i32* %33
%35 = add nsw i32 %34, -1
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %31, i64 %36
store i8 0, i8* %37
%38 = call i32 @BIO_free_all(i32* %10)
%39 = load i8*, i8** %2
%40 = call i64 @strlen(i8* dereferenceable(1) %39)
%41 = trunc i64 %40 to i32
ret i32 %41
}