#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;
scalar_t__ BIO_eof(int /*<<< orphan*/ *);
int /*<<< orphan*/ BIO_f_base64();
int /*<<< orphan*/ BIO_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BIO_new(int /*<<< orphan*/);
int /*<<< orphan*/ *BIO_new_mem_buf(void const *, int);
int /*<<< orphan*/ BIO_push(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int BIO_read(int /*<<< orphan*/ *, void *, int);
int /*<<< orphan*/ BIO_set_flags(int /*<<< orphan*/ *, int /*<<< orphan*/);
size_t INT_MAX;
void *calloc(int, size_t);
int /*<<< orphan*/ free(void *);
size_t strlen(char const *);
int b64_decode(const char *in, void **ptr, size_t *len) {
  BIO *bio_mem = NULL;
  BIO *bio_b64 = NULL;
  size_t alloc_len;
  int n;
  int ok = 0;
  if (in == NULL || ptr == NULL || len == NULL || strlen(in) > INT_MAX)
    return (0);
  *ptr = NULL;
  *len = 0;
  bio_b64 = BIO_new(BIO_f_base64());
  if (bio_b64 == NULL)
    goto fail;
  bio_mem = BIO_new_mem_buf((const void *)in, -1);
  if (bio_mem == NULL)
    goto fail;
  BIO_set_flags(bio_b64, BIO_FLAGS_BASE64_NO_NL);
  BIO_push(bio_b64, bio_mem);
  alloc_len = strlen(in);
  *ptr = calloc(1, alloc_len);
  if (*ptr == NULL)
    goto fail;
  n = BIO_read(bio_b64, *ptr, (int)alloc_len);
  if (n < 0 || BIO_eof(bio_b64) == 0)
    goto fail;
  *len = (size_t)n;
  ok = 1;
fail:
  BIO_free(bio_b64);
  BIO_free(bio_mem);
  if (!ok) {
    free(*ptr);
    *ptr = NULL;
    *len = 0;
  }
  return (ok);
}


x86 assembly (clang, -O0):
<b64_decode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x40(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1215 <b64_decode+0x65>
cmpq   $0x0,-0x18(%rbp)
je     1215 <b64_decode+0x65>
cmpq   $0x0,-0x20(%rbp)
je     1215 <b64_decode+0x65>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    0x2ddc(%rip),%rcx
cmp    (%rcx),%rax
jbe    1221 <b64_decode+0x71>
movl   $0x0,-0x4(%rbp)
jmp    1363 <b64_decode+0x1b3>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
mov    $0x0,%al
call   1070 <BIO_f_base64@plt>
mov    %eax,%edi
call   10c0 <BIO_new@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1259 <b64_decode+0xa9>
jmp    131f <b64_decode+0x16f>
mov    -0x10(%rbp),%rdi
mov    $0xffffffff,%esi
call   10a0 <BIO_new_mem_buf@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    127b <b64_decode+0xcb>
jmp    131f <b64_decode+0x16f>
mov    -0x30(%rbp),%rdi
mov    0x2d52(%rip),%rax
mov    (%rax),%esi
call   1090 <BIO_set_flags@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <BIO_push@plt>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rsi
mov    $0x1,%edi
call   1080 <calloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    12d2 <b64_decode+0x122>
jmp    131f <b64_decode+0x16f>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
mov    -0x38(%rbp),%rax
mov    %eax,%edx
call   1050 <BIO_read@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jl     1308 <b64_decode+0x158>
mov    -0x30(%rbp),%rdi
call   10b0 <BIO_eof@plt>
cmp    $0x0,%rax
jne    130d <b64_decode+0x15d>
jmp    131f <b64_decode+0x16f>
movslq -0x3c(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x1,-0x40(%rbp)
mov    -0x30(%rbp),%rdi
call   10d0 <BIO_free@plt>
mov    -0x28(%rbp),%rdi
call   10d0 <BIO_free@plt>
cmpl   $0x0,-0x40(%rbp)
jne    135d <b64_decode+0x1ad>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x40(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<b64_decode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %r15d,%r15d
test   %rdi,%rdi
je     12d6 <b64_decode+0x126>
mov    %rsi,%rbx
test   %rsi,%rsi
je     12d6 <b64_decode+0x126>
mov    %rdx,%r14
test   %rdx,%rdx
je     12d6 <b64_decode+0x126>
mov    %rdi,%rbp
call   1060 <strlen@plt>
mov    0x2dfa(%rip),%rcx
cmp    (%rcx),%rax
ja     12d6 <b64_decode+0x126>
movq   $0x0,(%rbx)
movq   $0x0,(%r14)
xor    %r15d,%r15d
xor    %eax,%eax
call   1070 <BIO_f_base64@plt>
mov    %eax,%edi
call   10c0 <BIO_new@plt>
mov    %rax,%r12
test   %rax,%rax
je     12a5 <b64_decode+0xf5>
mov    %rbp,%rdi
mov    $0xffffffff,%esi
call   10a0 <BIO_new_mem_buf@plt>
xor    %r15d,%r15d
test   %rax,%rax
je     12a5 <b64_decode+0xf5>
mov    %rax,%r13
mov    0x2d97(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   1090 <BIO_set_flags@plt>
mov    %r12,%rdi
mov    %r13,%rsi
call   1040 <BIO_push@plt>
mov    %rbp,%rdi
call   1060 <strlen@plt>
mov    %rax,%rbp
mov    $0x1,%edi
mov    %rax,%rsi
call   1080 <calloc@plt>
mov    %rax,(%rbx)
test   %rax,%rax
je     12a8 <b64_decode+0xf8>
mov    %r12,%rdi
mov    %rax,%rsi
mov    %ebp,%edx
call   1050 <BIO_read@plt>
test   %eax,%eax
js     12a8 <b64_decode+0xf8>
mov    %eax,%ebp
mov    %r12,%rdi
call   10b0 <BIO_eof@plt>
test   %rax,%rax
je     12a8 <b64_decode+0xf8>
mov    %ebp,%eax
mov    %rax,(%r14)
mov    $0x1,%r15d
xor    %ebp,%ebp
jmp    12ab <b64_decode+0xfb>
xor    %r13d,%r13d
mov    $0x1,%bpl
mov    %r12,%rdi
call   10d0 <BIO_free@plt>
mov    %r13,%rdi
call   10d0 <BIO_free@plt>
test   %bpl,%bpl
je     12d6 <b64_decode+0x126>
mov    (%rbx),%rdi
call   1030 <free@plt>
movq   $0x0,(%rbx)
movq   $0x0,(%r14)
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@INT_MAX = global i64 0
@BIO_FLAGS_BASE64_NO_NL = global i32 0
define i32 @b64_decode(i8* %0, i8** %1, i64* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i64*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i64* %2, i64** %7
store i32* null, i32** %8
store i32* null, i32** %9
store i32 0, i32* %12
%13 = load i8*, i8** %5
%14 = icmp eq i8* %13, null
br i1 %14, label %26, label %15
15: ; preds = %3
%16 = load i8**, i8*** %6
%17 = icmp eq i8** %16, null
br i1 %17, label %26, label %18
18: ; preds = %15
%19 = load i64*, i64** %7
%20 = icmp eq i64* %19, null
br i1 %20, label %26, label %21
21: ; preds = %18
%22 = load i8*, i8** %5
%23 = call i64 @strlen(i8* %22)
%24 = load i64, i64* @INT_MAX
%25 = icmp ugt i64 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %21, %18, %15, %3
store i32 0, i32* %4
br label %90
27: ; preds = %21
%28 = load i8**, i8*** %6
store i8* null, i8** %28
%29 = load i64*, i64** %7
store i64 0, i64* %29
%30 = call i32 (...) @BIO_f_base64()
%31 = call i32* @BIO_new(i32 %30)
store i32* %31, i32** %9
%32 = load i32*, i32** %9
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %35
34: ; preds = %27
br label %75
35: ; preds = %27
%36 = load i8*, i8** %5
%37 = call i32* @BIO_new_mem_buf(i8* %36, i32 -1)
store i32* %37, i32** %8
%38 = load i32*, i32** %8
%39 = icmp eq i32* %38, null
br i1 %39, label %40, label %41
40: ; preds = %35
br label %75
41: ; preds = %35
%42 = load i32*, i32** %9
%43 = load i32, i32* @BIO_FLAGS_BASE64_NO_NL
%44 = call i32 @BIO_set_flags(i32* %42, i32 %43)
%45 = load i32*, i32** %9
%46 = load i32*, i32** %8
%47 = call i32 @BIO_push(i32* %45, i32* %46)
%48 = load i8*, i8** %5
%49 = call i64 @strlen(i8* %48)
store i64 %49, i64* %10
%50 = load i64, i64* %10
%51 = call i8* @calloc(i32 1, i64 %50)
%52 = load i8**, i8*** %6
store i8* %51, i8** %52
%53 = load i8**, i8*** %6
%54 = load i8*, i8** %53
%55 = icmp eq i8* %54, null
br i1 %55, label %56, label %57
56: ; preds = %41
br label %75
57: ; preds = %41
%58 = load i32*, i32** %9
%59 = load i8**, i8*** %6
%60 = load i8*, i8** %59
%61 = load i64, i64* %10
%62 = trunc i64 %61 to i32
%63 = call i32 @BIO_read(i32* %58, i8* %60, i32 %62)
store i32 %63, i32* %11
%64 = load i32, i32* %11
%65 = icmp slt i32 %64, 0
br i1 %65, label %70, label %66
66: ; preds = %57
%67 = load i32*, i32** %9
%68 = call i64 @BIO_eof(i32* %67)
%69 = icmp eq i64 %68, 0
br i1 %69, label %70, label %71
70: ; preds = %66, %57
br label %75
71: ; preds = %66
%72 = load i32, i32* %11
%73 = sext i32 %72 to i64
%74 = load i64*, i64** %7
store i64 %73, i64* %74
store i32 1, i32* %12
br label %75
75: ; preds = %71, %70, %56, %40, %34
%76 = load i32*, i32** %9
%77 = call i32 @BIO_free(i32* %76)
%78 = load i32*, i32** %8
%79 = call i32 @BIO_free(i32* %78)
%80 = load i32, i32* %12
%81 = icmp ne i32 %80, 0
br i1 %81, label %88, label %82
82: ; preds = %75
%83 = load i8**, i8*** %6
%84 = load i8*, i8** %83
%85 = call i32 @free(i8* %84)
%86 = load i8**, i8*** %6
store i8* null, i8** %86
%87 = load i64*, i64** %7
store i64 0, i64* %87
br label %88
88: ; preds = %82, %75
%89 = load i32, i32* %12
store i32 %89, i32* %4
br label %90
90: ; preds = %88, %26
%91 = load i32, i32* %4
ret i32 %91
}

LLVM IR (-O2):
@INT_MAX = global i64 0
@BIO_FLAGS_BASE64_NO_NL = global i32 0
define i32 @b64_decode(i8* %0, i8** %1, i64* writeonly %2) {
%4 = icmp eq i8* %0, null
%5 = icmp eq i8** %1, null
%6 = or i1 %4, %5
%7 = icmp eq i64* %2, null
%8 = or i1 %6, %7
br i1 %8, label %45, label %9
9: ; preds = %3
%10 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%11 = load i64, i64* @INT_MAX
%12 = icmp ugt i64 %10, %11
br i1 %12, label %45, label %13
13: ; preds = %9
store i8* null, i8** %1
store i64 0, i64* %2
%14 = tail call i32 (...) @BIO_f_base64()
%15 = tail call i32* @BIO_new(i32 %14)
%16 = icmp eq i32* %15, null
br i1 %16, label %36, label %17
17: ; preds = %13
%18 = tail call i32* @BIO_new_mem_buf(i8* %0, i32 -1)
%19 = icmp eq i32* %18, null
br i1 %19, label %36, label %20
20: ; preds = %17
%21 = load i32, i32* @BIO_FLAGS_BASE64_NO_NL
%22 = tail call i32 @BIO_set_flags(i32* %15, i32 %21)
%23 = tail call i32 @BIO_push(i32* %15, i32* %18)
%24 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%25 = tail call i8* @calloc(i32 1, i64 %24)
store i8* %25, i8** %1
%26 = icmp eq i8* %25, null
br i1 %26, label %36, label %27
27: ; preds = %20
%28 = trunc i64 %24 to i32
%29 = tail call i32 @BIO_read(i32* %15, i8* %25, i32 %28)
%30 = icmp slt i32 %29, 0
br i1 %30, label %36, label %31
31: ; preds = %27
%32 = tail call i64 @BIO_eof(i32* %15)
%33 = icmp eq i64 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %31
%35 = zext i32 %29 to i64
store i64 %35, i64* %2
br label %36
36: ; preds = %27, %31, %20, %17, %13, %34
%37 = phi i32* [ null, %13 ], [ null, %17 ], [ %18, %20 ], [ %18, %27 ], [ %18, %31 ], [ %18, %34 ]
%38 = phi i1 [ true, %13 ], [ true, %17 ], [ true, %20 ], [ true, %27 ], [ true, %31 ], [ false, %34 ]
%39 = phi i32 [ 0, %13 ], [ 0, %17 ], [ 0, %20 ], [ 0, %27 ], [ 0, %31 ], [ 1, %34 ]
%40 = tail call i32 @BIO_free(i32* %15)
%41 = tail call i32 @BIO_free(i32* %37)
br i1 %38, label %42, label %45
42: ; preds = %36
%43 = load i8*, i8** %1
%44 = tail call i32 @free(i8* %43)
store i8* null, i8** %1
store i64 0, i64* %2
br label %45
45: ; preds = %36, %42, %3, %9
%46 = phi i32 [ 0, %9 ], [ 0, %3 ], [ %39, %42 ], [ %39, %36 ]
ret i32 %46
}