#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 */
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
struct TYPE_3__ {
  int length;
  int /*<<< orphan*/ data;
};
typedef TYPE_1__ BUF_MEM;
typedef int /*<<< orphan*/ BIO;

/* Variables and functions */
int /*<<< orphan*/ BIO_f_base64();
int /*<<< orphan*/ BIO_flush(int /*<<< orphan*/ *);
int /*<<< orphan*/ BIO_free_all(int /*<<< orphan*/ *);
int /*<<< orphan*/ BIO_get_mem_ptr(int /*<<< orphan*/ *, TYPE_1__ **);
int /*<<< orphan*/ *BIO_new(int /*<<< orphan*/);
int /*<<< orphan*/ *BIO_push(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ BIO_s_mem();
int /*<<< orphan*/ BIO_write(int /*<<< orphan*/ *, unsigned char *, int);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/, int);
int Base64Encode(unsigned char *input, int inLen, char *output, int outLen) {
  if (!input || !output) {
    return -1;
  }
  BIO *bmem, *b64;
  BUF_MEM *bptr;
  b64 = BIO_new(BIO_f_base64());
  bmem = BIO_new(BIO_s_mem());
  b64 = BIO_push(b64, bmem);
  BIO_write(b64, input, inLen);
  BIO_flush(b64);
  BIO_get_mem_ptr(b64, &bptr);
  if (bptr->length > outLen) {
    outLen = bptr->length;
    return -2;
  }
  memcpy(output, bptr->data, bptr->length - 1);
  output[bptr->length - 1] = 0;
  outLen = bptr->length - 1;
  BIO_free_all(b64);
  return 0;
}


x86 assembly (clang, -O0):
<Base64Encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11bc <Base64Encode+0x2c>
cmpq   $0x0,-0x20(%rbp)
jne    11c8 <Base64Encode+0x38>
movl   $0xffffffff,-0x4(%rbp)
jmp    1290 <Base64Encode+0x100>
mov    $0x0,%al
call   1040 <BIO_f_base64@plt>
mov    %eax,%edi
call   1060 <BIO_new@plt>
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   10a0 <BIO_s_mem@plt>
mov    %eax,%edi
call   1060 <BIO_new@plt>
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1030 <BIO_push@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1090 <BIO_write@plt>
mov    -0x38(%rbp),%rdi
call   10b0 <BIO_flush@plt>
mov    -0x38(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1080 <BIO_get_mem_ptr@plt>
mov    -0x40(%rbp),%rax
mov    (%rax),%eax
cmp    -0x24(%rbp),%eax
jle    1247 <Base64Encode+0xb7>
mov    -0x40(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
movl   $0xfffffffe,-0x4(%rbp)
jmp    1290 <Base64Encode+0x100>
mov    -0x20(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x40(%rbp),%rax
mov    (%rax),%edx
sub    $0x1,%edx
call   1050 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x40(%rbp),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x40(%rbp),%rax
mov    (%rax),%eax
sub    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x38(%rbp),%rdi
call   1070 <BIO_free_all@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Base64Encode>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    $0xffffffff,%eax
test   %rdi,%rdi
je     1246 <Base64Encode+0xb6>
mov    %rdx,%r15
test   %rdx,%rdx
je     1246 <Base64Encode+0xb6>
mov    %ecx,%r14d
mov    %esi,%r12d
mov    %rdi,%rbx
xor    %eax,%eax
call   1040 <BIO_f_base64@plt>
mov    %eax,%edi
call   1060 <BIO_new@plt>
mov    %rax,%rbp
xor    %eax,%eax
call   10a0 <BIO_s_mem@plt>
mov    %eax,%edi
call   1060 <BIO_new@plt>
mov    %rbp,%rdi
mov    %rax,%rsi
call   1030 <BIO_push@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1090 <BIO_write@plt>
mov    %rbp,%rdi
call   10b0 <BIO_flush@plt>
lea    0x8(%rsp),%rsi
mov    %rbp,%rdi
call   1080 <BIO_get_mem_ptr@plt>
mov    0x8(%rsp),%rcx
mov    (%rcx),%edx
mov    $0xfffffffe,%eax
cmp    %r14d,%edx
jg     1246 <Base64Encode+0xb6>
mov    0x4(%rcx),%esi
add    $0xffffffff,%edx
mov    %r15,%rdi
call   1050 <memcpy@plt>
mov    0x8(%rsp),%rax
movslq (%rax),%rax
movb   $0x0,-0x1(%rax,%r15,1)
mov    %rbp,%rdi
call   1070 <BIO_free_all@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32, i32 }
define i32 @Base64Encode(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca %struct.TYPE_3__*
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%13 = load i8*, i8** %6
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %4
%16 = load i8*, i8** %8
%17 = icmp ne i8* %16, null
br i1 %17, label %19, label %18
18: ; preds = %15, %4
store i32 -1, i32* %5
br label %67
19: ; preds = %15
%20 = call i32 (...) @BIO_f_base64()
%21 = call i32* @BIO_new(i32 %20)
store i32* %21, i32** %11
%22 = call i32 (...) @BIO_s_mem()
%23 = call i32* @BIO_new(i32 %22)
store i32* %23, i32** %10
%24 = load i32*, i32** %11
%25 = load i32*, i32** %10
%26 = call i32* @BIO_push(i32* %24, i32* %25)
store i32* %26, i32** %11
%27 = load i32*, i32** %11
%28 = load i8*, i8** %6
%29 = load i32, i32* %7
%30 = call i32 @BIO_write(i32* %27, i8* %28, i32 %29)
%31 = load i32*, i32** %11
%32 = call i32 @BIO_flush(i32* %31)
%33 = load i32*, i32** %11
%34 = call i32 @BIO_get_mem_ptr(i32* %33, %struct.TYPE_3__** %12)
%35 = load %struct.TYPE_3__*, %struct.TYPE_3__** %12
%36 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %35, i32 0, i32 0
%37 = load i32, i32* %36
%38 = load i32, i32* %9
%39 = icmp sgt i32 %37, %38
br i1 %39, label %40, label %44
40: ; preds = %19
%41 = load %struct.TYPE_3__*, %struct.TYPE_3__** %12
%42 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %41, i32 0, i32 0
%43 = load i32, i32* %42
store i32 %43, i32* %9
store i32 -2, i32* %5
br label %67
44: ; preds = %19
%45 = load i8*, i8** %8
%46 = load %struct.TYPE_3__*, %struct.TYPE_3__** %12
%47 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %46, i32 0, i32 1
%48 = load i32, i32* %47
%49 = load %struct.TYPE_3__*, %struct.TYPE_3__** %12
%50 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %49, i32 0, i32 0
%51 = load i32, i32* %50
%52 = sub nsw i32 %51, 1
%53 = call i32 @memcpy(i8* %45, i32 %48, i32 %52)
%54 = load i8*, i8** %8
%55 = load %struct.TYPE_3__*, %struct.TYPE_3__** %12
%56 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %55, i32 0, i32 0
%57 = load i32, i32* %56
%58 = sub nsw i32 %57, 1
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %54, i64 %59
store i8 0, i8* %60
%61 = load %struct.TYPE_3__*, %struct.TYPE_3__** %12
%62 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %61, i32 0, i32 0
%63 = load i32, i32* %62
%64 = sub nsw i32 %63, 1
store i32 %64, i32* %9
%65 = load i32*, i32** %11
%66 = call i32 @BIO_free_all(i32* %65)
store i32 0, i32* %5
br label %67
67: ; preds = %44, %40, %18
%68 = load i32, i32* %5
ret i32 %68
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32, i32 }
define i32 @Base64Encode(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca %struct.TYPE_3__*
%6 = icmp ne i8* %0, null
%7 = icmp ne i8* %2, null
%8 = and i1 %6, %7
br i1 %8, label %9, label %37
9: ; preds = %4
%10 = bitcast %struct.TYPE_3__** %5 to i8*
%11 = tail call i32 (...) @BIO_f_base64()
%12 = tail call i32* @BIO_new(i32 %11)
%13 = tail call i32 (...) @BIO_s_mem()
%14 = tail call i32* @BIO_new(i32 %13)
%15 = tail call i32* @BIO_push(i32* %12, i32* %14)
%16 = tail call i32 @BIO_write(i32* %15, i8* %0, i32 %1)
%17 = tail call i32 @BIO_flush(i32* %15)
%18 = call i32 @BIO_get_mem_ptr(i32* %15, %struct.TYPE_3__** %5)
%19 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%20 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %19, i64 0, i32 0
%21 = load i32, i32* %20
%22 = icmp sgt i32 %21, %3
br i1 %22, label %35, label %23
23: ; preds = %9
%24 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %19, i64 0, i32 1
%25 = load i32, i32* %24
%26 = add nsw i32 %21, -1
%27 = call i32 @memcpy(i8* %2, i32 %25, i32 %26)
%28 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%29 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %28, i64 0, i32 0
%30 = load i32, i32* %29
%31 = add nsw i32 %30, -1
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %2, i64 %32
store i8 0, i8* %33
%34 = call i32 @BIO_free_all(i32* %15)
br label %35
35: ; preds = %9, %23
%36 = phi i32 [ 0, %23 ], [ -2, %9 ]
br label %37
37: ; preds = %4, %35
%38 = phi i32 [ %36, %35 ], [ -1, %4 ]
ret i32 %38
}