#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 */

/* Type definitions */
typedef int /*<<< orphan*/ u_char;
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 BIO_get_mem_data(int /*<<< orphan*/ *, int /*<<< orphan*/ **);
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*/ *, char *, int);
char *strndup(char *, int);
int raw_to_b64(char *r, int l, char **b) {
  BIO *b64bio, *bio, *mbio;
  u_char *p;
  int h;
  mbio = BIO_new(BIO_s_mem());
  b64bio = BIO_new(BIO_f_base64());
  bio = BIO_push(b64bio, mbio);
  BIO_write(bio, r, l);
  BIO_flush(bio);
  h = BIO_get_mem_data(mbio, &p);
  *b = strndup((char *)p, h);
  BIO_free_all(bio);
  return h;
}


x86 assembly (clang, -O0):
<raw_to_b64>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0x0,%al
call   1090 <BIO_s_mem@plt>
mov    %eax,%edi
call   1060 <BIO_new@plt>
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1040 <BIO_f_base64@plt>
mov    %eax,%edi
call   1060 <BIO_new@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1030 <BIO_push@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1080 <BIO_write@plt>
mov    -0x28(%rbp),%rdi
call   10b0 <BIO_flush@plt>
mov    -0x30(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   10a0 <BIO_get_mem_data@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x3c(%rbp),%esi
call   1050 <strndup@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rdi
call   1070 <BIO_free_all@plt>
mov    -0x3c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<raw_to_b64>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %esi,%r15d
mov    %rdi,%r12
xor    %eax,%eax
call   1090 <BIO_s_mem@plt>
mov    %eax,%edi
call   1060 <BIO_new@plt>
mov    %rax,%rbp
xor    %eax,%eax
call   1040 <BIO_f_base64@plt>
mov    %eax,%edi
call   1060 <BIO_new@plt>
mov    %rax,%rdi
mov    %rbp,%rsi
call   1030 <BIO_push@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r12,%rsi
mov    %r15d,%edx
call   1080 <BIO_write@plt>
mov    %rbx,%rdi
call   10b0 <BIO_flush@plt>
lea    0x8(%rsp),%rsi
mov    %rbp,%rdi
call   10a0 <BIO_get_mem_data@plt>
mov    %eax,%ebp
mov    0x8(%rsp),%rdi
mov    %eax,%esi
call   1050 <strndup@plt>
mov    %rax,(%r14)
mov    %rbx,%rdi
call   1070 <BIO_free_all@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @raw_to_b64(i8* %0, i32 %1, i8** %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8** %2, i8*** %6
%12 = call i32 (...) @BIO_s_mem()
%13 = call i32* @BIO_new(i32 %12)
store i32* %13, i32** %9
%14 = call i32 (...) @BIO_f_base64()
%15 = call i32* @BIO_new(i32 %14)
store i32* %15, i32** %7
%16 = load i32*, i32** %7
%17 = load i32*, i32** %9
%18 = call i32* @BIO_push(i32* %16, i32* %17)
store i32* %18, i32** %8
%19 = load i32*, i32** %8
%20 = load i8*, i8** %4
%21 = load i32, i32* %5
%22 = call i32 @BIO_write(i32* %19, i8* %20, i32 %21)
%23 = load i32*, i32** %8
%24 = call i32 @BIO_flush(i32* %23)
%25 = load i32*, i32** %9
%26 = call i32 @BIO_get_mem_data(i32* %25, i32** %10)
store i32 %26, i32* %11
%27 = load i32*, i32** %10
%28 = bitcast i32* %27 to i8*
%29 = load i32, i32* %11
%30 = call i8* @strndup(i8* %28, i32 %29)
%31 = load i8**, i8*** %6
store i8* %30, i8** %31
%32 = load i32*, i32** %8
%33 = call i32 @BIO_free_all(i32* %32)
%34 = load i32, i32* %11
ret i32 %34
}

LLVM IR (-O2):
define i32 @raw_to_b64(i8* %0, i32 %1, i8** nocapture writeonly %2) {
%4 = alloca i32*
%5 = bitcast i32** %4 to i8*
%6 = tail call i32 (...) @BIO_s_mem()
%7 = tail call i32* @BIO_new(i32 %6)
%8 = tail call i32 (...) @BIO_f_base64()
%9 = tail call i32* @BIO_new(i32 %8)
%10 = tail call i32* @BIO_push(i32* %9, i32* %7)
%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_data(i32* %7, i32** %4)
%14 = bitcast i32** %4 to i8**
%15 = load i8*, i8** %14
%16 = call i8* @strndup(i8* %15, i32 %13)
store i8* %16, i8** %2
%17 = call i32 @BIO_free_all(i32* %10)
ret i32 %13
}