#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*/ uint8_t;
typedef int /*<<< orphan*/ BIO;

/* Variables and functions */
int /*<<< orphan*/ BIO_FLAGS_BASE64_NO_NL;
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_push(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int BIO_read(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ BIO_s_mem();
int /*<<< orphan*/ BIO_set_flags(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ BIO_write(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ *malloc(int);
int strlen(char *);
uint8_t *base64_dec(char *input, int *outlen) {
  BIO *bmem, *b64;
  int inlen = strlen(input);
  b64 = BIO_new(BIO_f_base64());
  BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
  bmem = BIO_new(BIO_s_mem());
  b64 = BIO_push(b64, bmem);
  BIO_write(bmem, input, inlen);
  while (inlen++ & 3)
    BIO_write(bmem, "=", 1);
  BIO_flush(bmem);
  int bufsize = strlen(input) * 3 / 4 + 1;
  uint8_t *buf = malloc(bufsize);
  int nread;
  nread = BIO_read(b64, buf, bufsize);
  BIO_free_all(bmem);
  *outlen = nread;
  return buf;
}


x86 assembly (clang, -O0):
<base64_dec>:
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    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1060 <BIO_f_base64@plt>
mov    %eax,%edi
call   1090 <BIO_new@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
call   1070 <BIO_set_flags@plt>
mov    $0x0,%al
call   10c0 <BIO_s_mem@plt>
mov    %eax,%edi
call   1090 <BIO_new@plt>
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <BIO_push@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   10b0 <BIO_write@plt>
mov    -0x24(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x24(%rbp)
and    $0x3,%eax
cmp    $0x0,%eax
je     1254 <base64_dec+0xa4>
mov    -0x18(%rbp),%rdi
lea    0xdbb(%rip),%rsi
mov    $0x1,%edx
call   10b0 <BIO_write@plt>
jmp    1223 <base64_dec+0x73>
mov    -0x18(%rbp),%rdi
call   10d0 <BIO_flush@plt>
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
imul   $0x3,%eax,%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1040 <BIO_read@plt>
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rdi
call   10a0 <BIO_free_all@plt>
mov    -0x34(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<base64_dec>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,(%rsp)
mov    %rdi,%r15
call   1050 <strlen@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   1060 <BIO_f_base64@plt>
mov    %eax,%edi
call   1090 <BIO_new@plt>
mov    %rax,%r12
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   1070 <BIO_set_flags@plt>
xor    %eax,%eax
call   10c0 <BIO_s_mem@plt>
mov    %eax,%edi
call   1090 <BIO_new@plt>
mov    %rax,%rbx
mov    %r12,%rdi
mov    %rax,%rsi
call   1030 <BIO_push@plt>
mov    %rax,%r12
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   10b0 <BIO_write@plt>
test   $0x3,%r14b
je     124c <base64_dec+0x9c>
add    $0x1,%r14d
lea    0xdd6(%rip),%r13
mov    %r14d,%ebp
nopl   (%rax)
mov    %rbx,%rdi
mov    %r13,%rsi
mov    $0x1,%edx
call   10b0 <BIO_write@plt>
add    $0x1,%ebp
test   $0x3,%r14b
mov    %ebp,%r14d
jne    1230 <base64_dec+0x80>
mov    %rbx,%rdi
call   10d0 <BIO_flush@plt>
mov    %r15,%rdi
call   1050 <strlen@plt>
lea    (%rax,%rax,2),%ecx
lea    (%rax,%rax,2),%ebp
add    $0x3,%ebp
test   %ecx,%ecx
cmovns %ecx,%ebp
sar    $0x2,%ebp
add    $0x1,%ebp
mov    %ebp,%edi
call   1080 <malloc@plt>
mov    %rax,%r15
mov    %r12,%rdi
mov    %rax,%rsi
mov    %ebp,%edx
call   1040 <BIO_read@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   10a0 <BIO_free_all@plt>
mov    (%rsp),%rax
mov    %ebp,(%rax)
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
@.str = [2 x i8] c"=\00"
define i32* @base64_dec(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
store i8* %0, i8** %3
store i32* %1, i32** %4
%11 = load i8*, i8** %3
%12 = call i32 @strlen(i8* %11)
store i32 %12, i32* %7
%13 = call i32 (...) @BIO_f_base64()
%14 = call i32* @BIO_new(i32 %13)
store i32* %14, i32** %6
%15 = load i32*, i32** %6
%16 = load i32, i32* @BIO_FLAGS_BASE64_NO_NL
%17 = call i32 @BIO_set_flags(i32* %15, i32 %16)
%18 = call i32 (...) @BIO_s_mem()
%19 = call i32* @BIO_new(i32 %18)
store i32* %19, i32** %5
%20 = load i32*, i32** %6
%21 = load i32*, i32** %5
%22 = call i32* @BIO_push(i32* %20, i32* %21)
store i32* %22, i32** %6
%23 = load i32*, i32** %5
%24 = load i8*, i8** %3
%25 = load i32, i32* %7
%26 = call i32 @BIO_write(i32* %23, i8* %24, i32 %25)
br label %27
27: ; preds = %32, %2
%28 = load i32, i32* %7
%29 = add nsw i32 %28, 1
store i32 %29, i32* %7
%30 = and i32 %28, 3
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %27
%33 = load i32*, i32** %5
%34 = call i32 @BIO_write(i32* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
br label %27
35: ; preds = %27
%36 = load i32*, i32** %5
%37 = call i32 @BIO_flush(i32* %36)
%38 = load i8*, i8** %3
%39 = call i32 @strlen(i8* %38)
%40 = mul nsw i32 %39, 3
%41 = sdiv i32 %40, 4
%42 = add nsw i32 %41, 1
store i32 %42, i32* %8
%43 = load i32, i32* %8
%44 = call i32* @malloc(i32 %43)
store i32* %44, i32** %9
%45 = load i32*, i32** %6
%46 = load i32*, i32** %9
%47 = load i32, i32* %8
%48 = call i32 @BIO_read(i32* %45, i32* %46, i32 %47)
store i32 %48, i32* %10
%49 = load i32*, i32** %5
%50 = call i32 @BIO_free_all(i32* %49)
%51 = load i32, i32* %10
%52 = load i32*, i32** %4
store i32 %51, i32* %52
%53 = load i32*, i32** %9
ret i32* %53
}

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