#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*/ FILE;
typedef int /*<<< orphan*/ BIO;

/* Variables and functions */
int /*<<< orphan*/ BIO_FLAGS_BASE64_NO_NL;
int /*<<< orphan*/ BIO_NOCLOSE;
int /*<<< orphan*/ BIO_f_base64();
int /*<<< orphan*/ BIO_flush(int /*<<< orphan*/ *);
int /*<<< orphan*/ BIO_free_all(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BIO_new(int /*<<< orphan*/);
int /*<<< orphan*/ *BIO_new_fp(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ *BIO_push(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ BIO_set_flags(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ BIO_write(int /*<<< orphan*/ *, unsigned char *, int);
unsigned char *calloc(int, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fmemopen(unsigned char *, int, char *);
unsigned char *b64_enc(int length, unsigned char *data) {
  int output_length = ((length - 1) / 3) * 4 + 4;
  unsigned char *buffer = calloc(1, output_length + 1);
  FILE *stream = fmemopen(buffer, output_length + 1, "w");
  BIO *b64 = BIO_new(BIO_f_base64());
  BIO *bio = BIO_new_fp(stream, BIO_NOCLOSE);
  bio = BIO_push(b64, bio);
  BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
  BIO_write(bio, data, length);
  BIO_flush(bio);
  BIO_free_all(bio);
  fclose(stream);
  return buffer;
}


x86 assembly (clang, -O0):
<b64_enc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%eax
sub    $0x1,%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
shl    $0x2,%eax
add    $0x4,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
add    $0x1,%esi
mov    $0x1,%edi
call   1080 <calloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
add    $0x1,%esi
lea    0xe05(%rip),%rdx
call   1040 <fmemopen@plt>
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1070 <BIO_f_base64@plt>
mov    %eax,%edi
call   10a0 <BIO_new@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
call   1030 <BIO_new_fp@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1050 <BIO_push@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    0x2d90(%rip),%rax
mov    (%rax),%esi
call   1090 <BIO_set_flags@plt>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x4(%rbp),%edx
call   10c0 <BIO_write@plt>
mov    -0x38(%rbp),%rdi
call   10d0 <BIO_flush@plt>
mov    -0x38(%rbp),%rdi
call   10b0 <BIO_free_all@plt>
mov    -0x28(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<b64_enc>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %edi,%r15d
lea    -0x1(%r15),%eax
cltq
imul   $0x55555556,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
shr    $0x20,%rax
add    %ecx,%eax
lea    0x5(,%rax,4),%ebx
mov    $0x1,%edi
mov    %ebx,%esi
call   1080 <calloc@plt>
mov    %rax,%r12
lea    0xe0a(%rip),%rdx
mov    %rax,%rdi
mov    %ebx,%esi
call   1040 <fmemopen@plt>
mov    %rax,%r13
xor    %eax,%eax
call   1070 <BIO_f_base64@plt>
mov    %eax,%edi
call   10a0 <BIO_new@plt>
mov    %rax,%rbx
mov    0x2dcd(%rip),%rax
mov    (%rax),%esi
mov    %r13,%rdi
call   1030 <BIO_new_fp@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1050 <BIO_push@plt>
mov    %rax,%rbx
mov    0x2d9e(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <BIO_set_flags@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   10c0 <BIO_write@plt>
mov    %rbx,%rdi
call   10d0 <BIO_flush@plt>
mov    %rbx,%rdi
call   10b0 <BIO_free_all@plt>
mov    %r13,%rdi
call   1060 <fclose@plt>
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@BIO_NOCLOSE = global i32 0
@BIO_FLAGS_BASE64_NO_NL = global i32 0
define i8* @b64_enc(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32*
store i32 %0, i32* %3
store i8* %1, i8** %4
%10 = load i32, i32* %3
%11 = sub nsw i32 %10, 1
%12 = sdiv i32 %11, 3
%13 = mul nsw i32 %12, 4
%14 = add nsw i32 %13, 4
store i32 %14, i32* %5
%15 = load i32, i32* %5
%16 = add nsw i32 %15, 1
%17 = call i8* @calloc(i32 1, i32 %16)
store i8* %17, i8** %6
%18 = load i8*, i8** %6
%19 = load i32, i32* %5
%20 = add nsw i32 %19, 1
%21 = call i32* @fmemopen(i8* %18, i32 %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %21, i32** %7
%22 = call i32 (...) @BIO_f_base64()
%23 = call i32* @BIO_new(i32 %22)
store i32* %23, i32** %8
%24 = load i32*, i32** %7
%25 = load i32, i32* @BIO_NOCLOSE
%26 = call i32* @BIO_new_fp(i32* %24, i32 %25)
store i32* %26, i32** %9
%27 = load i32*, i32** %8
%28 = load i32*, i32** %9
%29 = call i32* @BIO_push(i32* %27, i32* %28)
store i32* %29, i32** %9
%30 = load i32*, i32** %9
%31 = load i32, i32* @BIO_FLAGS_BASE64_NO_NL
%32 = call i32 @BIO_set_flags(i32* %30, i32 %31)
%33 = load i32*, i32** %9
%34 = load i8*, i8** %4
%35 = load i32, i32* %3
%36 = call i32 @BIO_write(i32* %33, i8* %34, i32 %35)
%37 = load i32*, i32** %9
%38 = call i32 @BIO_flush(i32* %37)
%39 = load i32*, i32** %9
%40 = call i32 @BIO_free_all(i32* %39)
%41 = load i32*, i32** %7
%42 = call i32 @fclose(i32* %41)
%43 = load i8*, i8** %6
ret i8* %43
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@BIO_NOCLOSE = global i32 0
@BIO_FLAGS_BASE64_NO_NL = global i32 0
define i8* @b64_enc(i32 %0, i8* %1) {
%3 = add nsw i32 %0, -1
%4 = sdiv i32 %3, 3
%5 = shl nsw i32 %4, 2
%6 = add nsw i32 %5, 5
%7 = tail call i8* @calloc(i32 1, i32 %6)
%8 = tail call i32* @fmemopen(i8* %7, i32 %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 (...) @BIO_f_base64()
%10 = tail call i32* @BIO_new(i32 %9)
%11 = load i32, i32* @BIO_NOCLOSE
%12 = tail call i32* @BIO_new_fp(i32* %8, i32 %11)
%13 = tail call i32* @BIO_push(i32* %10, i32* %12)
%14 = load i32, i32* @BIO_FLAGS_BASE64_NO_NL
%15 = tail call i32 @BIO_set_flags(i32* %13, i32 %14)
%16 = tail call i32 @BIO_write(i32* %13, i8* %1, i32 %0)
%17 = tail call i32 @BIO_flush(i32* %13)
%18 = tail call i32 @BIO_free_all(i32* %13)
%19 = tail call i32 @fclose(i32* %8)
ret i8* %7
}