#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_new_mem_buf(char *, size_t);
int /*<<< orphan*/ *BIO_push(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
size_t BIO_read(int /*<<< orphan*/ *, unsigned char *, size_t);
int /*<<< orphan*/ BIO_set_flags(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ die(char *);
size_t strlen(char const *);
unsigned char *xcalloc(size_t, int);
size_t unbase64(const char *bytes, unsigned char **unbase64) {
  size_t len;
  BIO *memory, *b64;
  unsigned char *buffer;
  len = strlen(bytes);
  if (!len)
    goto error;
  b64 = BIO_new(BIO_f_base64());
  memory = BIO_new_mem_buf((char *)bytes, len);
  if (!b64 || !memory)
    goto error;
  b64 = BIO_push(b64, memory);
  if (!b64)
    goto error;
  BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
  buffer = xcalloc(len + 1, 1);
  len = BIO_read(b64, buffer, len);
  if ((int)len <= 0)
    goto error;
  buffer[len] = '\0';
  BIO_free_all(b64);
  *unbase64 = buffer;
  return len;
error:
  die("Could not unbase64 the given bytes.");
}


x86 assembly (clang, -O0):
<unbase64>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11cd <unbase64+0x2d>
jmp    12a8 <unbase64+0x108>
mov    $0x0,%al
call   1060 <BIO_f_base64@plt>
mov    %eax,%edi
call   1090 <BIO_new@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1080 <BIO_new_mem_buf@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1206 <unbase64+0x66>
cmpq   $0x0,-0x28(%rbp)
jne    120b <unbase64+0x6b>
jmp    12a8 <unbase64+0x108>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1030 <BIO_push@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    122c <unbase64+0x8c>
jmp    12a8 <unbase64+0x108>
mov    -0x30(%rbp),%rdi
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
call   1070 <BIO_set_flags@plt>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   10c0 <xcalloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <BIO_read@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    $0x0,%eax
jg     127b <unbase64+0xdb>
jmp    12a8 <unbase64+0x108>
mov    -0x38(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rdi
call   10b0 <BIO_free_all@plt>
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12b4 <unbase64+0x114>
lea    0xd51(%rip),%rdi
call   10a0 <die@plt>
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unbase64>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
call   1050 <strlen@plt>
test   %rax,%rax
je     1246 <unbase64+0xa6>
mov    %rax,%r12
xor    %eax,%eax
call   1060 <BIO_f_base64@plt>
mov    %eax,%edi
call   1090 <BIO_new@plt>
mov    %rax,%r15
mov    %rbx,%rdi
mov    %r12,%rsi
call   1080 <BIO_new_mem_buf@plt>
test   %r15,%r15
je     1246 <unbase64+0xa6>
test   %rax,%rax
je     1246 <unbase64+0xa6>
mov    %r15,%rdi
mov    %rax,%rsi
call   1030 <BIO_push@plt>
test   %rax,%rax
je     1246 <unbase64+0xa6>
mov    %rax,%r15
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   1070 <BIO_set_flags@plt>
mov    %r12,%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   10c0 <xcalloc@plt>
mov    %rax,%r13
mov    %r15,%rdi
mov    %rax,%rsi
mov    %r12,%rdx
call   1040 <BIO_read@plt>
mov    %rax,%rbx
test   %ebx,%ebx
jle    1246 <unbase64+0xa6>
movb   $0x0,0x0(%r13,%rbx,1)
mov    %r15,%rdi
call   10b0 <BIO_free_all@plt>
mov    %r13,(%r14)
jmp    1252 <unbase64+0xb2>
lea    0xdb3(%rip),%rdi
call   10a0 <die@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@BIO_FLAGS_BASE64_NO_NL = global i32 0
@.str = [36 x i8] c"Could not unbase64 the given bytes.\00"
define i64 @unbase64(i8* %0, i8** %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i8*
store i8* %0, i8** %4
store i8** %1, i8*** %5
%10 = load i8*, i8** %4
%11 = call i64 @strlen(i8* %10)
store i64 %11, i64* %6
%12 = load i64, i64* %6
%13 = icmp ne i64 %12, 0
br i1 %13, label %15, label %14
14: ; preds = %2
br label %58
15: ; preds = %2
%16 = call i32 (...) @BIO_f_base64()
%17 = call i32* @BIO_new(i32 %16)
store i32* %17, i32** %8
%18 = load i8*, i8** %4
%19 = load i64, i64* %6
%20 = call i32* @BIO_new_mem_buf(i8* %18, i64 %19)
store i32* %20, i32** %7
%21 = load i32*, i32** %8
%22 = icmp ne i32* %21, null
br i1 %22, label %23, label %26
23: ; preds = %15
%24 = load i32*, i32** %7
%25 = icmp ne i32* %24, null
br i1 %25, label %27, label %26
26: ; preds = %23, %15
br label %58
27: ; preds = %23
%28 = load i32*, i32** %8
%29 = load i32*, i32** %7
%30 = call i32* @BIO_push(i32* %28, i32* %29)
store i32* %30, i32** %8
%31 = load i32*, i32** %8
%32 = icmp ne i32* %31, null
br i1 %32, label %34, label %33
33: ; preds = %27
br label %58
34: ; preds = %27
%35 = load i32*, i32** %8
%36 = load i32, i32* @BIO_FLAGS_BASE64_NO_NL
%37 = call i32 @BIO_set_flags(i32* %35, i32 %36)
%38 = load i64, i64* %6
%39 = add i64 %38, 1
%40 = call i8* @xcalloc(i64 %39, i32 1)
store i8* %40, i8** %9
%41 = load i32*, i32** %8
%42 = load i8*, i8** %9
%43 = load i64, i64* %6
%44 = call i64 @BIO_read(i32* %41, i8* %42, i64 %43)
store i64 %44, i64* %6
%45 = load i64, i64* %6
%46 = trunc i64 %45 to i32
%47 = icmp sle i32 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %34
br label %58
49: ; preds = %34
%50 = load i8*, i8** %9
%51 = load i64, i64* %6
%52 = getelementptr inbounds i8, i8* %50, i64 %51
store i8 0, i8* %52
%53 = load i32*, i32** %8
%54 = call i32 @BIO_free_all(i32* %53)
%55 = load i8*, i8** %9
%56 = load i8**, i8*** %5
store i8* %55, i8** %56
%57 = load i64, i64* %6
store i64 %57, i64* %3
br label %60
58: ; preds = %48, %33, %26, %14
%59 = call i32 @die(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %60
60: ; preds = %58, %49
%61 = load i64, i64* %3
ret i64 %61
}

LLVM IR (-O2):
@BIO_FLAGS_BASE64_NO_NL = global i32 0
@.str = [36 x i8] c"Could not unbase64 the given bytes.\00"
define i64 @unbase64(i8* %0, i8** nocapture writeonly %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%4 = icmp eq i64 %3, 0
br i1 %4, label %26, label %5
5: ; preds = %2
%6 = tail call i32 (...) @BIO_f_base64()
%7 = tail call i32* @BIO_new(i32 %6)
%8 = tail call i32* @BIO_new_mem_buf(i8* %0, i64 %3)
%9 = icmp ne i32* %7, null
%10 = icmp ne i32* %8, null
%11 = select i1 %9, i1 %10, i1 false
br i1 %11, label %12, label %26
12: ; preds = %5
%13 = tail call i32* @BIO_push(i32* %7, i32* %8)
%14 = icmp eq i32* %13, null
br i1 %14, label %26, label %15
15: ; preds = %12
%16 = load i32, i32* @BIO_FLAGS_BASE64_NO_NL
%17 = tail call i32 @BIO_set_flags(i32* %13, i32 %16)
%18 = add i64 %3, 1
%19 = tail call i8* @xcalloc(i64 %18, i32 1)
%20 = tail call i64 @BIO_read(i32* %13, i8* %19, i64 %3)
%21 = trunc i64 %20 to i32
%22 = icmp slt i32 %21, 1
br i1 %22, label %26, label %23
23: ; preds = %15
%24 = getelementptr inbounds i8, i8* %19, i64 %20
store i8 0, i8* %24
%25 = tail call i32 @BIO_free_all(i32* %13)
store i8* %19, i8** %1
br label %28
26: ; preds = %15, %12, %5, %2
%27 = tail call i32 @die(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %28
28: ; preds = %26, %23
%29 = phi i64 [ undef, %26 ], [ %20, %23 ]
ret i64 %29
}