#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 *, scalar_t__);
int ceil(double);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fmemopen(char *, int, char *);
scalar_t__ malloc(int);
scalar_t__ strlen(char const *);
int base64encode(const char *message, char **buffer) {
  BIO *bio, *b64;
  FILE *stream;
  int encodedSize = 4 * ceil((double)strlen(message) / 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, strlen(message));
  BIO_flush(bio);
  BIO_free_all(bio);
  fclose(stream);
  return (0);
}


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

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