#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*/ *, char const *, unsigned int);
int ceil(double);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fmemopen(char *, int, char *);
scalar_t__ malloc(int);
int base64_encode(const char *message, unsigned int numBytes, char **buffer) {
  BIO *bio, *b64;
  FILE *stream;
  int encodedSize = 4 * ceil((double)numBytes / 3);
  *buffer = (char *)malloc(encodedSize + 1);
  stream = fmemopen(*buffer, encodedSize + 1, "w");
  b64 = BIO_new(BIO_f_base64());
  bio = BIO_new_fp(stream, BIO_NOCLOSE);
  bio = BIO_push(b64, bio);
  BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
  BIO_write(bio, message, numBytes);
  BIO_flush(bio);
  BIO_free_all(bio);
  fclose(stream);
  return (0);
}


x86 assembly (clang, -O0):
<base64_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0xc(%rbp),%eax
cvtsi2sd %rax,%xmm0
movsd  0xe1d(%rip),%xmm1
divsd  %xmm1,%xmm0
call   1050 <ceil@plt>
shl    $0x2,%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edi
add    $0x1,%edi
call   10a0 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x34(%rbp),%esi
add    $0x1,%esi
lea    0xded(%rip),%rdx
call   1040 <fmemopen@plt>
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1080 <BIO_f_base64@plt>
mov    %eax,%edi
call   10b0 <BIO_new@plt>
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
call   1030 <BIO_new_fp@plt>
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <BIO_push@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2d70(%rip),%rax
mov    (%rax),%esi
call   1090 <BIO_set_flags@plt>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   10d0 <BIO_write@plt>
mov    -0x20(%rbp),%rdi
call   10e0 <BIO_flush@plt>
mov    -0x20(%rbp),%rdi
call   10c0 <BIO_free_all@plt>
mov    -0x30(%rbp),%rdi
call   1070 <fclose@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<base64_encode>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %esi,%r15d
mov    %rdi,%r14
mov    %esi,%eax
cvtsi2sd %rax,%xmm0
divsd  0xe21(%rip),%xmm0
call   1050 <ceil@plt>
lea    0x1(,%rax,4),%ebp
mov    %ebp,%edi
call   10a0 <malloc@plt>
mov    %rax,(%rbx)
lea    0xe0c(%rip),%rdx
mov    %rax,%rdi
mov    %ebp,%esi
call   1040 <fmemopen@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1080 <BIO_f_base64@plt>
mov    %eax,%edi
call   10b0 <BIO_new@plt>
mov    %rax,%rbp
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1030 <BIO_new_fp@plt>
mov    %rbp,%rdi
mov    %rax,%rsi
call   1060 <BIO_push@plt>
mov    %rax,%rbp
mov    0x2d98(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1090 <BIO_set_flags@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   10d0 <BIO_write@plt>
mov    %rbp,%rdi
call   10e0 <BIO_flush@plt>
mov    %rbp,%rdi
call   10c0 <BIO_free_all@plt>
mov    %rbx,%rdi
call   1070 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
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 i32 @base64_encode(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
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8** %2, i8*** %6
%11 = load i32, i32* %5
%12 = uitofp i32 %11 to double
%13 = fdiv double %12, 3.000000e+00
%14 = call i32 @ceil(double %13)
%15 = mul nsw i32 4, %14
store i32 %15, i32* %10
%16 = load i32, i32* %10
%17 = add nsw i32 %16, 1
%18 = call i64 @malloc(i32 %17)
%19 = inttoptr i64 %18 to i8*
%20 = load i8**, i8*** %6
store i8* %19, i8** %20
%21 = load i8**, i8*** %6
%22 = load i8*, i8** %21
%23 = load i32, i32* %10
%24 = add nsw i32 %23, 1
%25 = call i32* @fmemopen(i8* %22, i32 %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %25, i32** %9
%26 = call i32 (...) @BIO_f_base64()
%27 = call i32* @BIO_new(i32 %26)
store i32* %27, i32** %8
%28 = load i32*, i32** %9
%29 = load i32, i32* @BIO_NOCLOSE
%30 = call i32* @BIO_new_fp(i32* %28, i32 %29)
store i32* %30, i32** %7
%31 = load i32*, i32** %8
%32 = load i32*, i32** %7
%33 = call i32* @BIO_push(i32* %31, i32* %32)
store i32* %33, i32** %7
%34 = load i32*, i32** %7
%35 = load i32, i32* @BIO_FLAGS_BASE64_NO_NL
%36 = call i32 @BIO_set_flags(i32* %34, i32 %35)
%37 = load i32*, i32** %7
%38 = load i8*, i8** %4
%39 = load i32, i32* %5
%40 = call i32 @BIO_write(i32* %37, i8* %38, i32 %39)
%41 = load i32*, i32** %7
%42 = call i32 @BIO_flush(i32* %41)
%43 = load i32*, i32** %7
%44 = call i32 @BIO_free_all(i32* %43)
%45 = load i32*, i32** %9
%46 = call i32 @fclose(i32* %45)
ret i32 0
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@BIO_NOCLOSE = global i32 0
@BIO_FLAGS_BASE64_NO_NL = global i32 0
define i32 @base64_encode(i8* %0, i32 %1, i8** nocapture writeonly %2) {
%4 = uitofp i32 %1 to double
%5 = fdiv double %4, 3.000000e+00
%6 = tail call i32 @ceil(double %5)
%7 = shl nsw i32 %6, 2
%8 = or i32 %7, 1
%9 = tail call i64 @malloc(i32 %8)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %2
%11 = tail call i32* @fmemopen(i8* %10, i32 %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 (...) @BIO_f_base64()
%13 = tail call i32* @BIO_new(i32 %12)
%14 = load i32, i32* @BIO_NOCLOSE
%15 = tail call i32* @BIO_new_fp(i32* %11, i32 %14)
%16 = tail call i32* @BIO_push(i32* %13, i32* %15)
%17 = load i32, i32* @BIO_FLAGS_BASE64_NO_NL
%18 = tail call i32 @BIO_set_flags(i32* %16, i32 %17)
%19 = tail call i32 @BIO_write(i32* %16, i8* %0, i32 %1)
%20 = tail call i32 @BIO_flush(i32* %16)
%21 = tail call i32 @BIO_free_all(i32* %16)
%22 = tail call i32 @fclose(i32* %11)
ret i32 0
}