#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;
  }
  while (in[--inlen] == '=') {
    --outlen;
  }
  *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    126d <ct_base64_decode+0x11d>
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    125d <ct_base64_decode+0x10d>
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    125d <ct_base64_decode+0x10d>
jmp    1218 <ct_base64_decode+0xc8>
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    1247 <ct_base64_decode+0xf7>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
jmp    1218 <ct_base64_decode+0xc8>
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    126d <ct_base64_decode+0x11d>
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     11b1 <ct_base64_decode+0x61>
mov    %rax,%rbx
shr    $0x2,%rax
lea    (%rax,%rax,2),%edi
call   1060 <OPENSSL_malloc@plt>
mov    %rax,%r15
test   %rax,%rax
je     11bc <ct_base64_decode+0x6c>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1030 <EVP_DecodeBlock@plt>
test   %eax,%eax
js     11c5 <ct_base64_decode+0x75>
lea    (%rbx,%r12,1),%rcx
add    $0xffffffffffffffff,%rcx
add    $0x1,%eax
nopl   0x0(%rax)
add    $0xffffffff,%eax
cmpb   $0x3d,(%rcx)
lea    -0x1(%rcx),%rcx
je     11a0 <ct_base64_decode+0x50>
mov    %r15,(%r14)
jmp    11e9 <ct_base64_decode+0x99>
movq   $0x0,(%r14)
xor    %eax,%eax
jmp    11e9 <ct_base64_decode+0x99>
mov    0x2e25(%rip),%rax
jmp    11cc <ct_base64_decode+0x7c>
mov    0x2e14(%rip),%rax
mov    0x2dfd(%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 %58
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 %55
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 %55
39: ; preds = %28
br label %40
40: ; preds = %48, %39
%41 = load i8*, i8** %4
%42 = load i64, i64* %6
%43 = add i64 %42, -1
store i64 %43, i64* %6
%44 = getelementptr inbounds i8, i8* %41, i64 %43
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = icmp eq i32 %46, 61
br i1 %47, label %48, label %51
48: ; preds = %40
%49 = load i32, i32* %7
%50 = add nsw i32 %49, -1
store i32 %50, i32* %7
br label %40
51: ; preds = %40
%52 = load i8*, i8** %8
%53 = load i8**, i8*** %5
store i8* %52, i8** %53
%54 = load i32, i32* %7
store i32 %54, i32* %3
br label %58
55: ; preds = %35, %24
%56 = load i8*, i8** %8
%57 = call i32 @OPENSSL_free(i8* %56)
store i32 -1, i32* %3
br label %58
58: ; preds = %55, %51, %13
%59 = load i32, i32* %3
ret i32 %59
}

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 %30
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 %24, 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 %24, label %15
15: ; preds = %12, %15
%16 = phi i64 [ %18, %15 ], [ %3, %12 ]
%17 = phi i32 [ %22, %15 ], [ %13, %12 ]
%18 = add i64 %16, -1
%19 = getelementptr inbounds i8, i8* %0, i64 %18
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, 61
%22 = add nsw i32 %17, -1
br i1 %21, label %15, label %23
23: ; preds = %15
store i8* %10, i8** %1
br label %30
24: ; preds = %12, %6
%25 = phi i32* [ @ERR_R_MALLOC_FAILURE, %6 ], [ @CT_R_BASE64_DECODE_ERROR, %12 ]
%26 = load i32, i32* @CT_F_CT_BASE64_DECODE
%27 = load i32, i32* %25
%28 = tail call i32 @CTerr(i32 %26, i32 %27)
%29 = tail call i32 @OPENSSL_free(i8* %10)
br label %30
30: ; preds = %24, %23, %5
%31 = phi i32 [ 0, %5 ], [ -1, %24 ], [ %17, %23 ]
ret i32 %31
}