#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_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 BIO_read(int /*<<< orphan*/ *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ BIO_set_flags(int /*<<< orphan*/ *, int /*<<< orphan*/);
int _calcDecodeLength(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fmemopen(char *, int /*<<< orphan*/, char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ strlen(char *);
int base64_decode(char *b64message, char **buffer) {
  BIO *bio, *b64;
  int decodeLen = _calcDecodeLength(b64message), len = 0;
  *buffer = (char *)malloc(decodeLen + 1);
  FILE *stream = fmemopen(b64message, strlen(b64message), "r");
  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);
  len = BIO_read(bio, *buffer, strlen(b64message));
  (*buffer)[len] = '\0';
  BIO_free_all(bio);
  fclose(stream);
  return (0);
}


x86 assembly (clang, -O0):
<base64_decode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   10a0 <_calcDecodeLength@plt>
mov    %eax,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x24(%rbp),%edi
add    $0x1,%edi
call   10c0 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <strlen@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,%esi
lea    0xdea(%rip),%rdx
call   1040 <fmemopen@plt>
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1090 <BIO_f_base64@plt>
mov    %eax,%edi
call   10d0 <BIO_new@plt>
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2dac(%rip),%rax
mov    (%rax),%esi
call   1030 <BIO_new_fp@plt>
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <BIO_push@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2d75(%rip),%rax
mov    (%rax),%esi
call   10b0 <BIO_set_flags@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <strlen@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
call   1070 <BIO_read@plt>
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
call   10e0 <BIO_free_all@plt>
mov    -0x30(%rbp),%rdi
call   1060 <fclose@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<base64_decode>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r13
call   10a0 <_calcDecodeLength@plt>
lea    0x1(%rax),%edi
call   10c0 <malloc@plt>
mov    %rax,(%r14)
mov    %r13,%rdi
call   1080 <strlen@plt>
lea    0xe12(%rip),%rdx
mov    %r13,%rdi
mov    %eax,%esi
call   1040 <fmemopen@plt>
mov    %rax,%r15
xor    %eax,%eax
call   1090 <BIO_f_base64@plt>
mov    %eax,%edi
call   10d0 <BIO_new@plt>
mov    %rax,%r12
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   1030 <BIO_new_fp@plt>
mov    %r12,%rdi
mov    %rax,%rsi
call   1050 <BIO_push@plt>
mov    %rax,%rbx
mov    0x2da6(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10b0 <BIO_set_flags@plt>
mov    (%r14),%r12
mov    %r13,%rdi
call   1080 <strlen@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %eax,%edx
call   1070 <BIO_read@plt>
mov    (%r14),%rcx
cltq
movb   $0x0,(%rcx,%rax,1)
mov    %rbx,%rdi
call   10e0 <BIO_free_all@plt>
mov    %r15,%rdi
call   1060 <fclose@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@BIO_NOCLOSE = global i32 0
@BIO_FLAGS_BASE64_NO_NL = global i32 0
define i32 @base64_decode(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 i32 @_calcDecodeLength(i8* %10)
store i32 %11, i32* %7
store i32 0, i32* %8
%12 = load i32, i32* %7
%13 = add nsw i32 %12, 1
%14 = call i64 @malloc(i32 %13)
%15 = inttoptr i64 %14 to i8*
%16 = load i8**, i8*** %4
store i8* %15, i8** %16
%17 = load i8*, i8** %3
%18 = load i8*, i8** %3
%19 = call i32 @strlen(i8* %18)
%20 = call i32* @fmemopen(i8* %17, i32 %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %20, i32** %9
%21 = call i32 (...) @BIO_f_base64()
%22 = call i32* @BIO_new(i32 %21)
store i32* %22, i32** %6
%23 = load i32*, i32** %9
%24 = load i32, i32* @BIO_NOCLOSE
%25 = call i32* @BIO_new_fp(i32* %23, i32 %24)
store i32* %25, i32** %5
%26 = load i32*, i32** %6
%27 = load i32*, i32** %5
%28 = call i32* @BIO_push(i32* %26, i32* %27)
store i32* %28, i32** %5
%29 = load i32*, i32** %5
%30 = load i32, i32* @BIO_FLAGS_BASE64_NO_NL
%31 = call i32 @BIO_set_flags(i32* %29, i32 %30)
%32 = load i32*, i32** %5
%33 = load i8**, i8*** %4
%34 = load i8*, i8** %33
%35 = load i8*, i8** %3
%36 = call i32 @strlen(i8* %35)
%37 = call i32 @BIO_read(i32* %32, i8* %34, i32 %36)
store i32 %37, i32* %8
%38 = load i8**, i8*** %4
%39 = load i8*, i8** %38
%40 = load i32, i32* %8
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
store i8 0, i8* %42
%43 = load i32*, i32** %5
%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"r\00"
@BIO_NOCLOSE = global i32 0
@BIO_FLAGS_BASE64_NO_NL = global i32 0
define i32 @base64_decode(i8* %0, i8** nocapture %1) {
%3 = tail call i32 @_calcDecodeLength(i8* %0)
%4 = add nsw i32 %3, 1
%5 = tail call i64 @malloc(i32 %4)
%6 = inttoptr i64 %5 to i8*
store i8* %6, i8** %1
%7 = tail call i32 @strlen(i8* %0)
%8 = tail call i32* @fmemopen(i8* %0, i32 %7, 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 = load i8*, i8** %1
%17 = tail call i32 @strlen(i8* %0)
%18 = tail call i32 @BIO_read(i32* %13, i8* %16, i32 %17)
%19 = load i8*, i8** %1
%20 = sext i32 %18 to i64
%21 = getelementptr inbounds i8, i8* %19, i64 %20
store i8 0, i8* %21
%22 = tail call i32 @BIO_free_all(i32* %13)
%23 = tail call i32 @fclose(i32* %8)
ret i32 0
}