#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;
  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;
  }
  *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    1239 <ct_base64_decode+0xe9>
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    1229 <ct_base64_decode+0xd9>
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    1229 <ct_base64_decode+0xd9>
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1239 <ct_base64_decode+0xe9>
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     1196 <ct_base64_decode+0x46>
mov    %rax,%rbx
shr    $0x2,%rax
lea    (%rax,%rax,2),%edi
call   1060 <OPENSSL_malloc@plt>
mov    %rax,%r15
test   %rax,%rax
je     11a1 <ct_base64_decode+0x51>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1030 <EVP_DecodeBlock@plt>
test   %eax,%eax
js     11aa <ct_base64_decode+0x5a>
mov    %r15,(%r14)
jmp    11ce <ct_base64_decode+0x7e>
movq   $0x0,(%r14)
xor    %eax,%eax
jmp    11ce <ct_base64_decode+0x7e>
mov    0x2e40(%rip),%rax
jmp    11b1 <ct_base64_decode+0x61>
mov    0x2e2f(%rip),%rax
mov    0x2e18(%rip),%rcx
mov    (%rcx),%edi
mov    (%rax),%esi
call   1050 <CTerr@plt>
mov    %r15,%rdi
call   1070 <OPENSSL_free@plt>
mov    $0xffffffff,%eax
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 i8*
store i8* %0, i8** %4
store i8** %1, i8*** %5
%9 = load i8*, i8** %4
%10 = call i64 @strlen(i8* %9)
store i64 %10, i64* %6
store i8* null, i8** %8
%11 = load i64, i64* %6
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = load i8**, i8*** %5
store i8* null, i8** %14
store i32 0, i32* %3
br label %46
15: ; preds = %2
%16 = load i64, i64* %6
%17 = udiv i64 %16, 4
%18 = mul i64 %17, 3
%19 = trunc i64 %18 to i32
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = call i8* @OPENSSL_malloc(i32 %20)
store i8* %21, i8** %8
%22 = load i8*, i8** %8
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %28
24: ; preds = %15
%25 = load i32, i32* @CT_F_CT_BASE64_DECODE
%26 = load i32, i32* @ERR_R_MALLOC_FAILURE
%27 = call i32 @CTerr(i32 %25, i32 %26)
br label %43
28: ; preds = %15
%29 = load i8*, i8** %8
%30 = load i8*, i8** %4
%31 = load i64, i64* %6
%32 = call i32 @EVP_DecodeBlock(i8* %29, i8* %30, i64 %31)
store i32 %32, i32* %7
%33 = load i32, i32* %7
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %28
%36 = load i32, i32* @CT_F_CT_BASE64_DECODE
%37 = load i32, i32* @CT_R_BASE64_DECODE_ERROR
%38 = call i32 @CTerr(i32 %36, i32 %37)
br label %43
39: ; preds = %28
%40 = load i8*, i8** %8
%41 = load i8**, i8*** %5
store i8* %40, i8** %41
%42 = load i32, i32* %7
store i32 %42, i32* %3
br label %46
43: ; preds = %35, %24
%44 = load i8*, i8** %8
%45 = call i32 @OPENSSL_free(i8* %44)
store i32 -1, i32* %3
br label %46
46: ; preds = %43, %39, %13
%47 = load i32, i32* %3
ret i32 %47
}

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 %22
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 %16, 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 %16, label %15
15: ; preds = %12
store i8* %10, i8** %1
br label %22
16: ; preds = %12, %6
%17 = phi i32* [ @ERR_R_MALLOC_FAILURE, %6 ], [ @CT_R_BASE64_DECODE_ERROR, %12 ]
%18 = load i32, i32* @CT_F_CT_BASE64_DECODE
%19 = load i32, i32* %17
%20 = tail call i32 @CTerr(i32 %18, i32 %19)
%21 = tail call i32 @OPENSSL_free(i8* %10)
br label %22
22: ; preds = %16, %15, %5
%23 = phi i32 [ 0, %5 ], [ -1, %16 ], [ %13, %15 ]
ret i32 %23
}