#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*/ BIO;

/* Variables and functions */
int /*<<< orphan*/ BIO_FLAGS_BASE64_NO_NL;
int /*<<< orphan*/ BIO_f_base64();
int /*<<< orphan*/ BIO_free_all(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BIO_new(int /*<<< orphan*/);
int /*<<< orphan*/ BIO_push(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ BIO_read(int /*<<< orphan*/ *, unsigned char *, int);
int /*<<< orphan*/ BIO_s_mem();
int /*<<< orphan*/ BIO_set_flags(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ BIO_write(int /*<<< orphan*/ *, char *, size_t);
unsigned char *calloc(size_t, int);
size_t strlen(char *);
unsigned char *b64decode(char *b64message, size_t *out_length) {
  BIO *b64_bio, *mem_bio;
  size_t b64_len = strlen(b64message);
  unsigned char *base64_decoded = calloc((b64_len * 3) / 4 + 1, sizeof(char));
  b64_bio = BIO_new(BIO_f_base64());
  mem_bio = BIO_new(BIO_s_mem());
  BIO_write(mem_bio, b64message, b64_len);
  BIO_push(b64_bio, mem_bio);
  if (b64message[b64_len - 1] != '\n') {
    BIO_set_flags(b64_bio, BIO_FLAGS_BASE64_NO_NL);
  }
  int decoded_byte_index = 0;
  while (0 < BIO_read(b64_bio, base64_decoded + decoded_byte_index, 1)) {
    decoded_byte_index++;
  }
  *out_length = decoded_byte_index;
  BIO_free_all(b64_bio);
  return base64_decoded;
}


x86 assembly (clang, -O0):
<b64decode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x28(%rbp)
imul   $0x3,-0x28(%rbp),%rdi
shr    $0x2,%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1060 <BIO_f_base64@plt>
mov    %eax,%edi
call   1090 <BIO_new@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   10c0 <BIO_s_mem@plt>
mov    %eax,%edi
call   1090 <BIO_new@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   10b0 <BIO_write@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <BIO_push@plt>
mov    -0x8(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
je     1245 <b64decode+0xa5>
mov    -0x18(%rbp),%rdi
mov    0x2da2(%rip),%rax
mov    (%rax),%esi
call   1080 <BIO_set_flags@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
movslq -0x34(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1040 <BIO_read@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jge    1281 <b64decode+0xe1>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    124c <b64decode+0xac>
movslq -0x34(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rdi
call   10a0 <BIO_free_all@plt>
mov    -0x30(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<b64decode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
call   1050 <strlen@plt>
mov    %rax,%r13
lea    (%rax,%rax,2),%rdi
shr    $0x2,%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,%r15
xor    %eax,%eax
call   1060 <BIO_f_base64@plt>
mov    %eax,%edi
call   1090 <BIO_new@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   10c0 <BIO_s_mem@plt>
mov    %eax,%edi
call   1090 <BIO_new@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   10b0 <BIO_write@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1030 <BIO_push@plt>
cmpb   $0xa,-0x1(%r13,%r12,1)
je     1226 <b64decode+0x86>
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <BIO_set_flags@plt>
mov    $0xffffffffffffffff,%rbp
nopl   (%rax)
lea    (%r15,%rbp,1),%rsi
add    $0x1,%rsi
mov    %rbx,%rdi
mov    $0x1,%edx
call   1040 <BIO_read@plt>
add    $0x1,%rbp
test   %rax,%rax
jg     1230 <b64decode+0x90>
mov    %ebp,%eax
mov    %rax,(%r14)
mov    %rbx,%rdi
call   10a0 <BIO_free_all@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@BIO_FLAGS_BASE64_NO_NL = global i32 0
define i8* @b64decode(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i64*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %3
store i64* %1, i64** %4
%10 = load i8*, i8** %3
%11 = call i64 @strlen(i8* %10)
store i64 %11, i64* %7
%12 = load i64, i64* %7
%13 = mul i64 %12, 3
%14 = udiv i64 %13, 4
%15 = add i64 %14, 1
%16 = call i8* @calloc(i64 %15, i32 1)
store i8* %16, i8** %8
%17 = call i32 (...) @BIO_f_base64()
%18 = call i32* @BIO_new(i32 %17)
store i32* %18, i32** %5
%19 = call i32 (...) @BIO_s_mem()
%20 = call i32* @BIO_new(i32 %19)
store i32* %20, i32** %6
%21 = load i32*, i32** %6
%22 = load i8*, i8** %3
%23 = load i64, i64* %7
%24 = call i32 @BIO_write(i32* %21, i8* %22, i64 %23)
%25 = load i32*, i32** %5
%26 = load i32*, i32** %6
%27 = call i32 @BIO_push(i32* %25, i32* %26)
%28 = load i8*, i8** %3
%29 = load i64, i64* %7
%30 = sub i64 %29, 1
%31 = getelementptr inbounds i8, i8* %28, i64 %30
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp ne i32 %33, 10
br i1 %34, label %35, label %39
35: ; preds = %2
%36 = load i32*, i32** %5
%37 = load i32, i32* @BIO_FLAGS_BASE64_NO_NL
%38 = call i32 @BIO_set_flags(i32* %36, i32 %37)
br label %39
39: ; preds = %35, %2
store i32 0, i32* %9
br label %40
40: ; preds = %48, %39
%41 = load i32*, i32** %5
%42 = load i8*, i8** %8
%43 = load i32, i32* %9
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
%46 = call i64 @BIO_read(i32* %41, i8* %45, i32 1)
%47 = icmp slt i64 0, %46
br i1 %47, label %48, label %51
48: ; preds = %40
%49 = load i32, i32* %9
%50 = add nsw i32 %49, 1
store i32 %50, i32* %9
br label %40
51: ; preds = %40
%52 = load i32, i32* %9
%53 = sext i32 %52 to i64
%54 = load i64*, i64** %4
store i64 %53, i64* %54
%55 = load i32*, i32** %5
%56 = call i32 @BIO_free_all(i32* %55)
%57 = load i8*, i8** %8
ret i8* %57
}

LLVM IR (-O2):
@BIO_FLAGS_BASE64_NO_NL = global i32 0
define i8* @b64decode(i8* %0, i64* nocapture writeonly %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%4 = mul i64 %3, 3
%5 = lshr i64 %4, 2
%6 = add nuw nsw i64 %5, 1
%7 = tail call i8* @calloc(i64 %6, i32 1)
%8 = tail call i32 (...) @BIO_f_base64()
%9 = tail call i32* @BIO_new(i32 %8)
%10 = tail call i32 (...) @BIO_s_mem()
%11 = tail call i32* @BIO_new(i32 %10)
%12 = tail call i32 @BIO_write(i32* %11, i8* %0, i64 %3)
%13 = tail call i32 @BIO_push(i32* %9, i32* %11)
%14 = add i64 %3, -1
%15 = getelementptr inbounds i8, i8* %0, i64 %14
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 10
br i1 %17, label %21, label %18
18: ; preds = %2
%19 = load i32, i32* @BIO_FLAGS_BASE64_NO_NL
%20 = tail call i32 @BIO_set_flags(i32* %9, i32 %19)
br label %21
21: ; preds = %18, %2
br label %22
22: ; preds = %21, %22
%23 = phi i64 [ %27, %22 ], [ 0, %21 ]
%24 = getelementptr inbounds i8, i8* %7, i64 %23
%25 = tail call i64 @BIO_read(i32* %9, i8* %24, i32 1)
%26 = icmp sgt i64 %25, 0
%27 = add nuw i64 %23, 1
br i1 %26, label %22, label %28
28: ; preds = %22
%29 = and i64 %23, 4294967295
store i64 %29, i64* %1
%30 = tail call i32 @BIO_free_all(i32* %9)
ret i8* %7
}