#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__ {
  unsigned long 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*/ *, void *, int);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/, unsigned long);
unsigned long base64encode(void *data, int len_data, char *output,
                           unsigned long size) {
  BIO *b64_bio, *mem_bio;
  BUF_MEM *mem_bio_mem_ptr;
  unsigned long min_size = 0;
  b64_bio = BIO_new(BIO_f_base64());
  mem_bio = BIO_new(BIO_s_mem());
  BIO_push(b64_bio, mem_bio);
  BIO_set_flags(b64_bio, BIO_FLAGS_BASE64_NO_NL);
  BIO_write(b64_bio, data, len_data);
  BIO_flush(b64_bio);
  BIO_get_mem_ptr(mem_bio, &mem_bio_mem_ptr);
  min_size =
      (size - 1 < mem_bio_mem_ptr->length ? size - 1 : mem_bio_mem_ptr->length);
  memcpy(output, mem_bio_mem_ptr->data, min_size);
  output[min_size] = 0;
  BIO_free_all(b64_bio);
  return min_size;
}


x86 assembly (clang, -O0):
<base64encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x0,-0x40(%rbp)
mov    $0x0,%al
call   1040 <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,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1030 <BIO_push@plt>
mov    -0x28(%rbp),%rdi
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
call   1050 <BIO_set_flags@plt>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   10a0 <BIO_write@plt>
mov    -0x28(%rbp),%rdi
call   10c0 <BIO_flush@plt>
mov    -0x30(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1090 <BIO_get_mem_ptr@plt>
mov    -0x20(%rbp),%rax
sub    $0x1,%rax
mov    -0x38(%rbp),%rcx
cmp    (%rcx),%rax
jae    124e <base64encode+0xae>
mov    -0x20(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x48(%rbp)
jmp    1259 <base64encode+0xb9>
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%esi
mov    -0x40(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x40(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
call   1080 <BIO_free_all@plt>
mov    -0x40(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<base64encode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%rbx
mov    %rdx,%r14
mov    %esi,%r12d
mov    %rdi,%r13
xor    %eax,%eax
call   1040 <BIO_f_base64@plt>
mov    %eax,%edi
call   1070 <BIO_new@plt>
mov    %rax,%r15
xor    %eax,%eax
call   10b0 <BIO_s_mem@plt>
mov    %eax,%edi
call   1070 <BIO_new@plt>
mov    %rax,%rbp
mov    %r15,%rdi
mov    %rax,%rsi
call   1030 <BIO_push@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   1050 <BIO_set_flags@plt>
mov    %r15,%rdi
mov    %r13,%rsi
mov    %r12d,%edx
call   10a0 <BIO_write@plt>
mov    %r15,%rdi
call   10c0 <BIO_flush@plt>
mov    %rsp,%rsi
mov    %rbp,%rdi
call   1090 <BIO_get_mem_ptr@plt>
add    $0xffffffffffffffff,%rbx
mov    (%rsp),%rax
mov    (%rax),%rcx
cmp    %rcx,%rbx
cmovae %rcx,%rbx
mov    0x8(%rax),%esi
mov    %r14,%rdi
mov    %rbx,%rdx
call   1060 <memcpy@plt>
movb   $0x0,(%r14,%rbx,1)
mov    %r15,%rdi
call   1080 <BIO_free_all@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.TYPE_3__ = type { i64, i32 }
@BIO_FLAGS_BASE64_NO_NL = global i32 0
define i64 @base64encode(i8* %0, i32 %1, i8* %2, i64 %3) {
%5 = alloca %struct.TYPE_3__*
%6 = bitcast %struct.TYPE_3__** %5 to i8*
%7 = tail call i32 (...) @BIO_f_base64()
%8 = tail call i32* @BIO_new(i32 %7)
%9 = tail call i32 (...) @BIO_s_mem()
%10 = tail call i32* @BIO_new(i32 %9)
%11 = tail call i32 @BIO_push(i32* %8, i32* %10)
%12 = load i32, i32* @BIO_FLAGS_BASE64_NO_NL
%13 = tail call i32 @BIO_set_flags(i32* %8, i32 %12)
%14 = tail call i32 @BIO_write(i32* %8, i8* %0, i32 %1)
%15 = tail call i32 @BIO_flush(i32* %8)
%16 = call i32 @BIO_get_mem_ptr(i32* %10, %struct.TYPE_3__** %5)
%17 = add i64 %3, -1
%18 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%19 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %18, i64 0, i32 0
%20 = load i64, i64* %19
%21 = icmp ult i64 %17, %20
%22 = select i1 %21, i64 %17, i64 %20
%23 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %18, i64 0, i32 1
%24 = load i32, i32* %23
%25 = call i32 @memcpy(i8* %2, i32 %24, i64 %22)
%26 = getelementptr inbounds i8, i8* %2, i64 %22
store i8 0, i8* %26
%27 = call i32 @BIO_free_all(i32* %8)
ret i64 %22
}