#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 BIO_write(int /*<<< orphan*/ *, char const *, int);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/, int);
char *base64_enc(const char *str, int len) {
  BIO *bio, *b64;
  BUF_MEM *bptr;
  char *buf;
  int ret;
  b64 = BIO_new(BIO_f_base64());
  bio = BIO_new(BIO_s_mem());
  b64 = BIO_push(b64, bio);
  ret = BIO_write(b64, str, len);
  if (ret <= 0) {
    buf = NULL;
    goto err;
  }
  BIO_flush(b64);
  BIO_get_mem_ptr(b64, &bptr);
  buf = malloc(bptr->length);
  if (buf) {
    memcpy(buf, bptr->data, bptr->length - 1);
    buf[bptr->length - 1] = 0;
  }
err:
  BIO_free_all(b64);
  return buf;
}


x86 assembly (clang, -O0):
<base64_enc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%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    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jg     120e <base64_enc+0x6e>
movq   $0x0,-0x30(%rbp)
jmp    1270 <base64_enc+0xd0>
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)
cmpq   $0x0,-0x30(%rbp)
je     126b <base64_enc+0xcb>
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)
jmp    1270 <base64_enc+0xd0>
mov    -0x20(%rbp),%rdi
call   1080 <BIO_free_all@plt>
mov    -0x30(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<base64_enc>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%rbp
xor    %r14d,%r14d
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    %r15d,%edx
call   10a0 <BIO_write@plt>
test   %eax,%eax
jle    123b <base64_enc+0x9b>
mov    %rbx,%rdi
call   10c0 <BIO_flush@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1090 <BIO_get_mem_ptr@plt>
mov    (%rsp),%r15
mov    (%r15),%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1238 <base64_enc+0x98>
mov    %rax,%r14
mov    0x4(%r15),%esi
add    $0xffffffff,%ebp
mov    %rax,%rdi
mov    %ebp,%edx
call   1050 <memcpy@plt>
mov    (%rsp),%rax
movslq (%rax),%rax
movb   $0x0,-0x1(%rax,%r14,1)
jmp    123b <base64_enc+0x9b>
xor    %r14d,%r14d
mov    %rbx,%rdi
call   1080 <BIO_free_all@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32, i32 }
define i8* @base64_enc(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 = icmp slt i32 %10, 1
br i1 %11, label %31, label %12
12: ; preds = %2
%13 = tail call i32 @BIO_flush(i32* %9)
%14 = call i32 @BIO_get_mem_ptr(i32* %9, %struct.TYPE_3__** %3)
%15 = load %struct.TYPE_3__*, %struct.TYPE_3__** %3
%16 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %15, i64 0, i32 0
%17 = load i32, i32* %16
%18 = call i8* @malloc(i32 %17)
%19 = icmp eq i8* %18, null
br i1 %19, label %31, label %20
20: ; preds = %12
%21 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %15, i64 0, i32 1
%22 = load i32, i32* %21
%23 = add nsw i32 %17, -1
%24 = call i32 @memcpy(i8* %18, i32 %22, 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* %18, i64 %29
store i8 0, i8* %30
br label %31
31: ; preds = %2, %12, %20
%32 = phi i8* [ %18, %20 ], [ null, %12 ], [ null, %2 ]
%33 = call i32 @BIO_free_all(i32* %9)
ret i8* %32
}