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

/* Variables and functions */
int /*<<< orphan*/ CT_F_CT_BASE64_DECODE;
int /*<<< orphan*/ CT_R_BASE64_DECODE_ERROR;
int /*<<< orphan*/ CTerr(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ ERR_R_MALLOC_FAILURE;
int EVP_DecodeBlock(unsigned char *, unsigned char *, size_t);
int /*<<< orphan*/ OPENSSL_free(unsigned char *);
unsigned char *OPENSSL_malloc(int);
size_t strlen(char const *);
int ct_base64_decode(const char *in, unsigned char **out) {
  size_t inlen = strlen(in);
  int outlen, i;
  unsigned char *outbuf = NULL;
  if (inlen == 0) {
    *out = NULL;
    return 0;
  }
  outlen = (inlen / 4) * 3;
  outbuf = OPENSSL_malloc(outlen);
  if (outbuf == NULL) {
    CTerr(CT_F_CT_BASE64_DECODE, ERR_R_MALLOC_FAILURE);
    goto err;
  }
  outlen = EVP_DecodeBlock(outbuf, (unsigned char *)in, inlen);
  if (outlen < 0) {
    CTerr(CT_F_CT_BASE64_DECODE, CT_R_BASE64_DECODE_ERROR);
    goto err;
  }
  i = 0;
  while (in[--inlen] == '=') {
    --outlen;
    if (++i > 2)
      goto err;
  }
  *out = outbuf;
  return outlen;
err:
  OPENSSL_free(outbuf);
  return -1;
}


x86 assembly (clang, -O0):
<ct_base64_decode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1197 <ct_base64_decode+0x47>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1286 <ct_base64_decode+0x136>
mov    -0x20(%rbp),%rax
shr    $0x2,%rax
imul   $0x3,%rax,%rax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   1060 <OPENSSL_malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11d9 <ct_base64_decode+0x89>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
call   1050 <CTerr@plt>
jmp    1276 <ct_base64_decode+0x126>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <EVP_DecodeBlock@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1213 <ct_base64_decode+0xc3>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
call   1050 <CTerr@plt>
jmp    1276 <ct_base64_decode+0x126>
movl   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rdx
add    $0xffffffffffffffff,%rdx
mov    %rdx,-0x20(%rbp)
movsbl -0x1(%rax,%rcx,1),%eax
cmp    $0x3d,%eax
jne    1260 <ct_base64_decode+0x110>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
cmp    $0x2,%eax
jle    125b <ct_base64_decode+0x10b>
jmp    1276 <ct_base64_decode+0x126>
jmp    121a <ct_base64_decode+0xca>
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1286 <ct_base64_decode+0x136>
mov    -0x30(%rbp),%rdi
call   1070 <OPENSSL_free@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ct_base64_decode>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
call   1040 <strlen@plt>
test   %rax,%rax
je     11ae <ct_base64_decode+0x5e>
mov    %rax,%rbx
shr    $0x2,%rax
lea    (%rax,%rax,2),%edi
call   1060 <OPENSSL_malloc@plt>
mov    %rax,%r15
test   %rax,%rax
je     11b9 <ct_base64_decode+0x69>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1030 <EVP_DecodeBlock@plt>
test   %eax,%eax
js     11c2 <ct_base64_decode+0x72>
cmpb   $0x3d,-0x1(%rbx,%r12,1)
jne    11eb <ct_base64_decode+0x9b>
cmpb   $0x3d,-0x2(%rbx,%r12,1)
jne    11e8 <ct_base64_decode+0x98>
cmpb   $0x3d,-0x3(%rbx,%r12,1)
je     11d9 <ct_base64_decode+0x89>
add    $0xfffffffe,%eax
jmp    11eb <ct_base64_decode+0x9b>
movq   $0x0,(%r14)
xor    %eax,%eax
jmp    11ee <ct_base64_decode+0x9e>
mov    0x2e28(%rip),%rax
jmp    11c9 <ct_base64_decode+0x79>
mov    0x2e17(%rip),%rax
mov    0x2e00(%rip),%rcx
mov    (%rcx),%edi
mov    (%rax),%esi
call   1050 <CTerr@plt>
mov    %r15,%rdi
call   1070 <OPENSSL_free@plt>
mov    $0xffffffff,%eax
jmp    11ee <ct_base64_decode+0x9e>
add    $0xffffffff,%eax
mov    %r15,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@CT_F_CT_BASE64_DECODE = global i32 0
@ERR_R_MALLOC_FAILURE = global i32 0
@CT_R_BASE64_DECODE_ERROR = global i32 0
define i32 @ct_base64_decode(i8* %0, i8** %1) {
%3 = alloca i32
%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
store i8* null, i8** %9
%12 = load i64, i64* %6
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = load i8**, i8*** %5
store i8* null, i8** %15
store i32 0, i32* %3
br label %64
16: ; preds = %2
%17 = load i64, i64* %6
%18 = udiv i64 %17, 4
%19 = mul i64 %18, 3
%20 = trunc i64 %19 to i32
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = call i8* @OPENSSL_malloc(i32 %21)
store i8* %22, i8** %9
%23 = load i8*, i8** %9
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %29
25: ; preds = %16
%26 = load i32, i32* @CT_F_CT_BASE64_DECODE
%27 = load i32, i32* @ERR_R_MALLOC_FAILURE
%28 = call i32 @CTerr(i32 %26, i32 %27)
br label %61
29: ; preds = %16
%30 = load i8*, i8** %9
%31 = load i8*, i8** %4
%32 = load i64, i64* %6
%33 = call i32 @EVP_DecodeBlock(i8* %30, i8* %31, i64 %32)
store i32 %33, i32* %7
%34 = load i32, i32* %7
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %29
%37 = load i32, i32* @CT_F_CT_BASE64_DECODE
%38 = load i32, i32* @CT_R_BASE64_DECODE_ERROR
%39 = call i32 @CTerr(i32 %37, i32 %38)
br label %61
40: ; preds = %29
store i32 0, i32* %8
br label %41
41: ; preds = %56, %40
%42 = load i8*, i8** %4
%43 = load i64, i64* %6
%44 = add i64 %43, -1
store i64 %44, i64* %6
%45 = getelementptr inbounds i8, i8* %42, i64 %44
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = icmp eq i32 %47, 61
br i1 %48, label %49, label %57
49: ; preds = %41
%50 = load i32, i32* %7
%51 = add nsw i32 %50, -1
store i32 %51, i32* %7
%52 = load i32, i32* %8
%53 = add nsw i32 %52, 1
store i32 %53, i32* %8
%54 = icmp sgt i32 %53, 2
br i1 %54, label %55, label %56
55: ; preds = %49
br label %61
56: ; preds = %49
br label %41
57: ; preds = %41
%58 = load i8*, i8** %9
%59 = load i8**, i8*** %5
store i8* %58, i8** %59
%60 = load i32, i32* %7
store i32 %60, i32* %3
br label %64
61: ; preds = %55, %36, %25
%62 = load i8*, i8** %9
%63 = call i32 @OPENSSL_free(i8* %62)
store i32 -1, i32* %3
br label %64
64: ; preds = %61, %57, %14
%65 = load i32, i32* %3
ret i32 %65
}

LLVM IR (-O2):
@CT_F_CT_BASE64_DECODE = global i32 0
@ERR_R_MALLOC_FAILURE = global i32 0
@CT_R_BASE64_DECODE_ERROR = global i32 0
define i32 @ct_base64_decode(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 %5, label %6
5: ; preds = %2
store i8* null, i8** %1
br label %41
6: ; preds = %2
%7 = lshr i64 %3, 2
%8 = trunc i64 %7 to i32
%9 = mul i32 %8, 3
%10 = tail call i8* @OPENSSL_malloc(i32 %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %34, label %12
12: ; preds = %6
%13 = tail call i32 @EVP_DecodeBlock(i8* %10, i8* %0, i64 %3)
%14 = icmp slt i32 %13, 0
br i1 %14, label %34, label %15
15: ; preds = %12
%16 = add i64 %3, -1
%17 = getelementptr inbounds i8, i8* %0, i64 %16
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 61
br i1 %19, label %20, label %32
20: ; preds = %15
%21 = add nsw i32 %13, -1
%22 = add i64 %3, -2
%23 = getelementptr inbounds i8, i8* %0, i64 %22
%24 = load i8, i8* %23
%25 = icmp eq i8 %24, 61
br i1 %25, label %26, label %32
26: ; preds = %20
%27 = add nsw i32 %13, -2
%28 = add i64 %3, -3
%29 = getelementptr inbounds i8, i8* %0, i64 %28
%30 = load i8, i8* %29
%31 = icmp eq i8 %30, 61
br i1 %31, label %39, label %32
32: ; preds = %26, %20, %15
%33 = phi i32 [ %13, %15 ], [ %21, %20 ], [ %27, %26 ]
store i8* %10, i8** %1
br label %41
34: ; preds = %12, %6
%35 = phi i32* [ @ERR_R_MALLOC_FAILURE, %6 ], [ @CT_R_BASE64_DECODE_ERROR, %12 ]
%36 = load i32, i32* @CT_F_CT_BASE64_DECODE
%37 = load i32, i32* %35
%38 = tail call i32 @CTerr(i32 %36, i32 %37)
br label %39
39: ; preds = %34, %26
%40 = tail call i32 @OPENSSL_free(i8* %10)
br label %41
41: ; preds = %39, %32, %5
%42 = phi i32 [ 0, %5 ], [ -1, %39 ], [ %33, %32 ]
ret i32 %42
}