#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(unsigned char *, int);
int /*<<< orphan*/ *BIO_push(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int BIO_read(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ BIO_set_flags(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int base64decode(unsigned char *source, int length, char **target) {
  BIO *b64, *bmem;
  int len, bufmaxlen;
  bufmaxlen = ((length * 6 + 7) / 8) * sizeof(char);
  if (*target) {
    free(*target);
  }
  *target = (char *)malloc(bufmaxlen);
  memset(*target, 0, bufmaxlen);
  b64 = BIO_new(BIO_f_base64());
  bmem = BIO_new_mem_buf(source, length);
  b64 = BIO_push(b64, bmem);
  len = BIO_read(b64, *target, bufmaxlen);
  if (len == 0) {
    BIO_free_all(b64);
    b64 = BIO_new(BIO_f_base64());
    BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
    bmem = BIO_new_mem_buf(source, length);
    b64 = BIO_push(b64, bmem);
    len = BIO_read(b64, *target, bufmaxlen);
  }
  BIO_free_all(b64);
  return len;
}


x86 assembly (clang, -O0):
<base64decode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
imul   $0x6,-0xc(%rbp),%eax
add    $0x7,%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
cltq
shl    $0x0,%rax
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
je     11e5 <base64decode+0x45>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%edi
call   10a0 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x30(%rbp),%edx
xor    %esi,%esi
call   1070 <memset@plt>
mov    $0x0,%al
call   1060 <BIO_f_base64@plt>
mov    %eax,%edi
call   10b0 <BIO_new@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1090 <BIO_new_mem_buf@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <BIO_push@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
mov    -0x30(%rbp),%edx
call   1050 <BIO_read@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    12bf <base64decode+0x11f>
mov    -0x20(%rbp),%rdi
call   10c0 <BIO_free_all@plt>
mov    $0x0,%al
call   1060 <BIO_f_base64@plt>
mov    %eax,%edi
call   10b0 <BIO_new@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2d5f(%rip),%rax
mov    (%rax),%esi
call   1080 <BIO_set_flags@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1090 <BIO_new_mem_buf@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <BIO_push@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
mov    -0x30(%rbp),%edx
call   1050 <BIO_read@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
call   10c0 <BIO_free_all@plt>
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<base64decode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r15d
lea    (%r15,%r15,1),%eax
lea    (%rax,%rax,2),%ecx
add    $0x7,%ecx
lea    (%rax,%rax,2),%r13d
add    $0xe,%r13d
test   %ecx,%ecx
cmovns %ecx,%r13d
mov    %rdx,%r14
mov    %rdi,%r12
sar    $0x3,%r13d
mov    (%rdx),%rdi
test   %rdi,%rdi
je     11dd <base64decode+0x3d>
call   1030 <free@plt>
mov    %r13d,%edi
call   10a0 <malloc@plt>
mov    %rax,(%r14)
mov    %rax,%rdi
xor    %esi,%esi
mov    %r13d,%edx
call   1070 <memset@plt>
xor    %eax,%eax
call   1060 <BIO_f_base64@plt>
mov    %eax,%edi
call   10b0 <BIO_new@plt>
mov    %rax,%rbx
mov    %r12,%rdi
mov    %r15d,%esi
call   1090 <BIO_new_mem_buf@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1040 <BIO_push@plt>
mov    %rax,%rbx
mov    (%r14),%rsi
mov    %rax,%rdi
mov    %r13d,%edx
call   1050 <BIO_read@plt>
test   %eax,%eax
jne    1282 <base64decode+0xe2>
mov    %rbx,%rdi
call   10c0 <BIO_free_all@plt>
xor    %eax,%eax
call   1060 <BIO_f_base64@plt>
mov    %eax,%edi
call   10b0 <BIO_new@plt>
mov    %rax,%rbx
mov    0x2d8f(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <BIO_set_flags@plt>
mov    %r12,%rdi
mov    %r15d,%esi
call   1090 <BIO_new_mem_buf@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1040 <BIO_push@plt>
mov    %rax,%rbx
mov    (%r14),%rsi
mov    %rax,%rdi
mov    %r13d,%edx
call   1050 <BIO_read@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   10c0 <BIO_free_all@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@BIO_FLAGS_BASE64_NO_NL = global i32 0
define i32 @base64decode(i8* %0, i32 %1, i8** %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8** %2, i8*** %6
%11 = load i32, i32* %5
%12 = mul nsw i32 %11, 6
%13 = add nsw i32 %12, 7
%14 = sdiv i32 %13, 8
%15 = sext i32 %14 to i64
%16 = mul i64 %15, 1
%17 = trunc i64 %16 to i32
store i32 %17, i32* %10
%18 = load i8**, i8*** %6
%19 = load i8*, i8** %18
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %25
21: ; preds = %3
%22 = load i8**, i8*** %6
%23 = load i8*, i8** %22
%24 = call i32 @free(i8* %23)
br label %25
25: ; preds = %21, %3
%26 = load i32, i32* %10
%27 = call i64 @malloc(i32 %26)
%28 = inttoptr i64 %27 to i8*
%29 = load i8**, i8*** %6
store i8* %28, i8** %29
%30 = load i8**, i8*** %6
%31 = load i8*, i8** %30
%32 = load i32, i32* %10
%33 = call i32 @memset(i8* %31, i32 0, i32 %32)
%34 = call i32 (...) @BIO_f_base64()
%35 = call i32* @BIO_new(i32 %34)
store i32* %35, i32** %7
%36 = load i8*, i8** %4
%37 = load i32, i32* %5
%38 = call i32* @BIO_new_mem_buf(i8* %36, i32 %37)
store i32* %38, i32** %8
%39 = load i32*, i32** %7
%40 = load i32*, i32** %8
%41 = call i32* @BIO_push(i32* %39, i32* %40)
store i32* %41, i32** %7
%42 = load i32*, i32** %7
%43 = load i8**, i8*** %6
%44 = load i8*, i8** %43
%45 = load i32, i32* %10
%46 = call i32 @BIO_read(i32* %42, i8* %44, i32 %45)
store i32 %46, i32* %9
%47 = load i32, i32* %9
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %68
49: ; preds = %25
%50 = load i32*, i32** %7
%51 = call i32 @BIO_free_all(i32* %50)
%52 = call i32 (...) @BIO_f_base64()
%53 = call i32* @BIO_new(i32 %52)
store i32* %53, i32** %7
%54 = load i32*, i32** %7
%55 = load i32, i32* @BIO_FLAGS_BASE64_NO_NL
%56 = call i32 @BIO_set_flags(i32* %54, i32 %55)
%57 = load i8*, i8** %4
%58 = load i32, i32* %5
%59 = call i32* @BIO_new_mem_buf(i8* %57, i32 %58)
store i32* %59, i32** %8
%60 = load i32*, i32** %7
%61 = load i32*, i32** %8
%62 = call i32* @BIO_push(i32* %60, i32* %61)
store i32* %62, i32** %7
%63 = load i32*, i32** %7
%64 = load i8**, i8*** %6
%65 = load i8*, i8** %64
%66 = load i32, i32* %10
%67 = call i32 @BIO_read(i32* %63, i8* %65, i32 %66)
store i32 %67, i32* %9
br label %68
68: ; preds = %49, %25
%69 = load i32*, i32** %7
%70 = call i32 @BIO_free_all(i32* %69)
%71 = load i32, i32* %9
ret i32 %71
}

LLVM IR (-O2):
@BIO_FLAGS_BASE64_NO_NL = global i32 0
define i32 @base64decode(i8* %0, i32 %1, i8** nocapture %2) {
%4 = mul nsw i32 %1, 6
%5 = add nsw i32 %4, 7
%6 = sdiv i32 %5, 8
%7 = load i8*, i8** %2
%8 = icmp eq i8* %7, null
br i1 %8, label %11, label %9
9: ; preds = %3
%10 = tail call i32 @free(i8* %7)
br label %11
11: ; preds = %9, %3
%12 = tail call i64 @malloc(i32 %6)
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** %2
%14 = tail call i32 @memset(i8* %13, i32 0, i32 %6)
%15 = tail call i32 (...) @BIO_f_base64()
%16 = tail call i32* @BIO_new(i32 %15)
%17 = tail call i32* @BIO_new_mem_buf(i8* %0, i32 %1)
%18 = tail call i32* @BIO_push(i32* %16, i32* %17)
%19 = load i8*, i8** %2
%20 = tail call i32 @BIO_read(i32* %18, i8* %19, i32 %6)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %32
22: ; preds = %11
%23 = tail call i32 @BIO_free_all(i32* %18)
%24 = tail call i32 (...) @BIO_f_base64()
%25 = tail call i32* @BIO_new(i32 %24)
%26 = load i32, i32* @BIO_FLAGS_BASE64_NO_NL
%27 = tail call i32 @BIO_set_flags(i32* %25, i32 %26)
%28 = tail call i32* @BIO_new_mem_buf(i8* %0, i32 %1)
%29 = tail call i32* @BIO_push(i32* %25, i32* %28)
%30 = load i8*, i8** %2
%31 = tail call i32 @BIO_read(i32* %29, i8* %30, i32 %6)
br label %32
32: ; preds = %22, %11
%33 = phi i32 [ %31, %22 ], [ %20, %11 ]
%34 = phi i32* [ %29, %22 ], [ %18, %11 ]
%35 = tail call i32 @BIO_free_all(i32* %34)
ret i32 %33
}