#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_FLAGS_BASE64_NO_NL;
int /*<<< orphan*/ BIO_f_base64();
scalar_t__ 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*/);
scalar_t__ BIO_write(int /*<<< orphan*/ *, char const *, size_t);
int /*<<< orphan*/ die(char *);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/, int);
char *xmalloc(int);
char *base64(const char *bytes, size_t len) {
  BIO *memory, *b64;
  BUF_MEM *buffer;
  char *output;
  b64 = BIO_new(BIO_f_base64());
  BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
  memory = BIO_new(BIO_s_mem());
  if (!b64 || !memory)
    goto error;
  b64 = BIO_push(b64, memory);
  if (!b64)
    goto error;
  if (BIO_write(b64, bytes, len) < 0 || BIO_flush(b64) < 0)
    goto error;
  BIO_get_mem_ptr(b64, &buffer);
  output = xmalloc(buffer->length + 1);
  memcpy(output, buffer->data, buffer->length);
  output[buffer->length] = '\0';
  BIO_free_all(b64);
  return output;
error:
  die("Could not base64 the given bytes.");
}


x86 assembly (clang, -O0):
<base64>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    $0x0,%al
call   1040 <BIO_f_base64@plt>
mov    %eax,%edi
call   1080 <BIO_new@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
call   1060 <BIO_set_flags@plt>
mov    $0x0,%al
call   10d0 <BIO_s_mem@plt>
mov    %eax,%edi
call   1080 <BIO_new@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     121c <base64+0x5c>
cmpq   $0x0,-0x20(%rbp)
jne    1221 <base64+0x61>
jmp    12cf <base64+0x10f>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <BIO_push@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1242 <base64+0x82>
jmp    12cf <base64+0x10f>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   10c0 <BIO_write@plt>
cmp    $0x0,%rax
jl     1270 <base64+0xb0>
mov    -0x28(%rbp),%rdi
call   10e0 <BIO_flush@plt>
cmp    $0x0,%rax
jge    1275 <base64+0xb5>
jmp    12cf <base64+0x10f>
mov    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   10b0 <BIO_get_mem_ptr@plt>
mov    -0x30(%rbp),%rax
mov    (%rax),%edi
add    $0x1,%edi
call   1050 <xmalloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   1070 <memcpy@plt>
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
movslq (%rcx),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
call   10a0 <BIO_free_all@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12db <base64+0x11b>
lea    0xd2a(%rip),%rdi
call   1090 <die@plt>
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<base64>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
xor    %eax,%eax
call   1040 <BIO_f_base64@plt>
mov    %eax,%edi
call   1080 <BIO_new@plt>
mov    %rax,%rbx
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <BIO_set_flags@plt>
xor    %eax,%eax
call   10d0 <BIO_s_mem@plt>
mov    %eax,%edi
call   1080 <BIO_new@plt>
test   %rbx,%rbx
je     127d <base64+0xbd>
test   %rax,%rax
je     127d <base64+0xbd>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1030 <BIO_push@plt>
test   %rax,%rax
je     127d <base64+0xbd>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   10c0 <BIO_write@plt>
test   %rax,%rax
js     127d <base64+0xbd>
mov    %r12,%rdi
call   10e0 <BIO_flush@plt>
test   %rax,%rax
js     127d <base64+0xbd>
mov    %rsp,%rsi
mov    %r12,%rdi
call   10b0 <BIO_get_mem_ptr@plt>
mov    (%rsp),%rax
mov    (%rax),%edi
add    $0x1,%edi
call   1050 <xmalloc@plt>
mov    %rax,%rbx
mov    (%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %rbx,%rdi
call   1070 <memcpy@plt>
mov    (%rsp),%rax
movslq (%rax),%rax
movb   $0x0,(%rbx,%rax,1)
mov    %r12,%rdi
call   10a0 <BIO_free_all@plt>
jmp    1289 <base64+0xc9>
lea    0xd7c(%rip),%rdi
call   1090 <die@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32, i32 }
@BIO_FLAGS_BASE64_NO_NL = global i32 0
@.str = [34 x i8] c"Could not base64 the given bytes.\00"
define i8* @base64(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca %struct.TYPE_3__*
%9 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
%10 = call i32 (...) @BIO_f_base64()
%11 = call i32* @BIO_new(i32 %10)
store i32* %11, i32** %7
%12 = load i32*, i32** %7
%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** %7
%18 = icmp ne i32* %17, null
br i1 %18, label %19, label %22
19: ; preds = %2
%20 = load i32*, i32** %6
%21 = icmp ne i32* %20, null
br i1 %21, label %23, label %22
22: ; preds = %19, %2
br label %66
23: ; preds = %19
%24 = load i32*, i32** %7
%25 = load i32*, i32** %6
%26 = call i32* @BIO_push(i32* %24, i32* %25)
store i32* %26, i32** %7
%27 = load i32*, i32** %7
%28 = icmp ne i32* %27, null
br i1 %28, label %30, label %29
29: ; preds = %23
br label %66
30: ; preds = %23
%31 = load i32*, i32** %7
%32 = load i8*, i8** %4
%33 = load i64, i64* %5
%34 = call i64 @BIO_write(i32* %31, i8* %32, i64 %33)
%35 = icmp slt i64 %34, 0
br i1 %35, label %40, label %36
36: ; preds = %30
%37 = load i32*, i32** %7
%38 = call i64 @BIO_flush(i32* %37)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %36, %30
br label %66
41: ; preds = %36
%42 = load i32*, i32** %7
%43 = call i32 @BIO_get_mem_ptr(i32* %42, %struct.TYPE_3__** %8)
%44 = load %struct.TYPE_3__*, %struct.TYPE_3__** %8
%45 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %44, i32 0, i32 0
%46 = load i32, i32* %45
%47 = add nsw i32 %46, 1
%48 = call i8* @xmalloc(i32 %47)
store i8* %48, i8** %9
%49 = load i8*, i8** %9
%50 = load %struct.TYPE_3__*, %struct.TYPE_3__** %8
%51 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %50, i32 0, i32 1
%52 = load i32, i32* %51
%53 = load %struct.TYPE_3__*, %struct.TYPE_3__** %8
%54 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %53, i32 0, i32 0
%55 = load i32, i32* %54
%56 = call i32 @memcpy(i8* %49, i32 %52, i32 %55)
%57 = load i8*, i8** %9
%58 = load %struct.TYPE_3__*, %struct.TYPE_3__** %8
%59 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %58, i32 0, i32 0
%60 = load i32, i32* %59
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i8, i8* %57, i64 %61
store i8 0, i8* %62
%63 = load i32*, i32** %7
%64 = call i32 @BIO_free_all(i32* %63)
%65 = load i8*, i8** %9
store i8* %65, i8** %3
br label %68
66: ; preds = %40, %29, %22
%67 = call i32 @die(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
br label %68
68: ; preds = %66, %41
%69 = load i8*, i8** %3
ret i8* %69
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32, i32 }
@BIO_FLAGS_BASE64_NO_NL = global i32 0
@.str = [34 x i8] c"Could not base64 the given bytes.\00"
define i8* @base64(i8* %0, i64 %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 = load i32, i32* @BIO_FLAGS_BASE64_NO_NL
%8 = tail call i32 @BIO_set_flags(i32* %6, i32 %7)
%9 = tail call i32 (...) @BIO_s_mem()
%10 = tail call i32* @BIO_new(i32 %9)
%11 = icmp ne i32* %6, null
%12 = icmp ne i32* %10, null
%13 = select i1 %11, i1 %12, i1 false
br i1 %13, label %14, label %42
14: ; preds = %2
%15 = tail call i32* @BIO_push(i32* %6, i32* %10)
%16 = icmp eq i32* %15, null
br i1 %16, label %42, label %17
17: ; preds = %14
%18 = tail call i64 @BIO_write(i32* %15, i8* %0, i64 %1)
%19 = icmp slt i64 %18, 0
br i1 %19, label %42, label %20
20: ; preds = %17
%21 = tail call i64 @BIO_flush(i32* %15)
%22 = icmp slt i64 %21, 0
br i1 %22, label %42, label %23
23: ; preds = %20
%24 = call i32 @BIO_get_mem_ptr(i32* %15, %struct.TYPE_3__** %3)
%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 = call i8* @xmalloc(i32 %28)
%30 = load %struct.TYPE_3__*, %struct.TYPE_3__** %3
%31 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %30, i64 0, i32 1
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %30, i64 0, i32 0
%34 = load i32, i32* %33
%35 = call i32 @memcpy(i8* %29, i32 %32, i32 %34)
%36 = load %struct.TYPE_3__*, %struct.TYPE_3__** %3
%37 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %36, i64 0, i32 0
%38 = load i32, i32* %37
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %29, i64 %39
store i8 0, i8* %40
%41 = call i32 @BIO_free_all(i32* %15)
br label %44
42: ; preds = %17, %20, %14, %2
%43 = tail call i32 @die(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
br label %44
44: ; preds = %42, %23
%45 = phi i8* [ undef, %42 ], [ %29, %23 ]
ret i8* %45
}