#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 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);
int /*<<< orphan*/ printf(char *);
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);
  int rst;
  *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));
  rst = BIO_flush(bio);
  if (rst <= 0) {
    printf("Call BIO_flush failed, So bad!\n");
  }
  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  0xdfa(%rip),%xmm1
divsd  %xmm1,%xmm0
call   1050 <ceil@plt>
shl    $0x2,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
add    $0x1,%edi
call   10c0 <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    0xdca(%rip),%rdx
call   1040 <fmemopen@plt>
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1090 <BIO_f_base64@plt>
mov    %eax,%edi
call   10d0 <BIO_new@plt>
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2d84(%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    0x2d4d(%rip),%rax
mov    (%rax),%esi
call   10b0 <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   10f0 <BIO_write@plt>
mov    -0x18(%rbp),%rdi
call   1100 <BIO_flush@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jg     12dd <Base64Encode+0xfd>
lea    0xd32(%rip),%rdi
call   10a0 <printf@plt>
mov    -0x18(%rbp),%rdi
call   10e0 <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   1090 <strlen@plt>
cvtsi2sd %rax,%xmm0
divsd  0xe01(%rip),%xmm0
call   1060 <ceil@plt>
lea    0x1(,%rax,4),%ebp
mov    %ebp,%edi
call   10c0 <malloc@plt>
mov    %rax,(%rbx)
lea    0xdec(%rip),%rdx
mov    %rax,%rdi
mov    %ebp,%esi
call   1040 <fmemopen@plt>
mov    %rax,%r14
xor    %eax,%eax
call   10a0 <BIO_f_base64@plt>
mov    %eax,%edi
call   10d0 <BIO_new@plt>
mov    %rax,%rbx
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1030 <BIO_new_fp@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1070 <BIO_push@plt>
mov    %rax,%rbx
mov    0x2d78(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10b0 <BIO_set_flags@plt>
mov    %r15,%rdi
call   1090 <strlen@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %rax,%rdx
call   10f0 <BIO_write@plt>
mov    %rbx,%rdi
call   1100 <BIO_flush@plt>
test   %eax,%eax
jg     1298 <Base64Encode+0xb8>
lea    0xd77(%rip),%rdi
call   1050 <puts@plt>
mov    %rbx,%rdi
call   10e0 <BIO_free_all@plt>
mov    %r14,%rdi
call   1080 <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
@.str.1 = [32 x i8] c"Call BIO_flush failed, So bad!\0A\00"
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
%9 = alloca i32
store i8* %0, i8** %3
store i8** %1, i8*** %4
%10 = load i8*, i8** %3
%11 = call i64 @strlen(i8* %10)
%12 = sitofp i64 %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* %8
%16 = load i32, i32* %8
%17 = add nsw i32 %16, 1
%18 = call i64 @malloc(i32 %17)
%19 = inttoptr i64 %18 to i8*
%20 = load i8**, i8*** %4
store i8* %19, i8** %20
%21 = load i8**, i8*** %4
%22 = load i8*, i8** %21
%23 = load i32, i32* %8
%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** %7
%26 = call i32 (...) @BIO_f_base64()
%27 = call i32* @BIO_new(i32 %26)
store i32* %27, i32** %6
%28 = load i32*, i32** %7
%29 = load i32, i32* @BIO_NOCLOSE
%30 = call i32* @BIO_new_fp(i32* %28, i32 %29)
store i32* %30, i32** %5
%31 = load i32*, i32** %6
%32 = load i32*, i32** %5
%33 = call i32* @BIO_push(i32* %31, i32* %32)
store i32* %33, i32** %5
%34 = load i32*, i32** %5
%35 = load i32, i32* @BIO_FLAGS_BASE64_NO_NL
%36 = call i32 @BIO_set_flags(i32* %34, i32 %35)
%37 = load i32*, i32** %5
%38 = load i8*, i8** %3
%39 = load i8*, i8** %3
%40 = call i64 @strlen(i8* %39)
%41 = call i32 @BIO_write(i32* %37, i8* %38, i64 %40)
%42 = load i32*, i32** %5
%43 = call i32 @BIO_flush(i32* %42)
store i32 %43, i32* %9
%44 = load i32, i32* %9
%45 = icmp sle i32 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %2
%47 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
br label %48
48: ; preds = %46, %2
%49 = load i32*, i32** %5
%50 = call i32 @BIO_free_all(i32* %49)
%51 = load i32*, i32** %7
%52 = call i32 @fclose(i32* %51)
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
@str = [31 x i8] c"Call BIO_flush failed, So bad!\00"
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 = icmp slt i32 %21, 1
br i1 %22, label %23, label %25
23: ; preds = %2
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
br label %25
25: ; preds = %23, %2
%26 = tail call i32 @BIO_free_all(i32* %16)
%27 = tail call i32 @fclose(i32* %11)
ret i32 0
}