#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 */
typedef int /*<<< orphan*/ uint8_t;
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();
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_flags(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ BIO_write(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/, int);
char *base64_enc(uint8_t *input, int length) {
  BIO *bmem, *b64;
  BUF_MEM *bptr;
  b64 = BIO_new(BIO_f_base64());
  bmem = BIO_new(BIO_s_mem());
  b64 = BIO_push(b64, bmem);
  BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
  BIO_write(b64, input, length);
  BIO_flush(b64);
  BIO_get_mem_ptr(b64, &bptr);
  char *buf = (char *)malloc(bptr->length);
  if (bptr->length) {
    memcpy(buf, bptr->data, bptr->length - 1);
    buf[bptr->length - 1] = 0;
  }
  BIO_free_all(bmem);
  return buf;
}


x86 assembly (clang, -O0):
<base64_enc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    $0x0,%al
call   1040 <BIO_f_base64@plt>
mov    %eax,%edi
call   1080 <BIO_new@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   10c0 <BIO_s_mem@plt>
mov    %eax,%edi
call   1080 <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    0x2de1(%rip),%rax
mov    (%rax),%esi
call   1050 <BIO_set_flags@plt>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   10b0 <BIO_write@plt>
mov    -0x20(%rbp),%rdi
call   10d0 <BIO_flush@plt>
mov    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   10a0 <BIO_get_mem_ptr@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
cmpl   $0x0,(%rax)
je     1275 <base64_enc+0xc5>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
sub    $0x1,%edx
call   1060 <memcpy@plt>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
call   1090 <BIO_free_all@plt>
mov    -0x30(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

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

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