#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_FLAGS_BASE64_NO_NL;
int /*<<< orphan*/ BIO_f_base64();
int 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_set_flags(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ BIO_write(int /*<<< orphan*/ *, unsigned char *, size_t);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/, int);
char *openssl_base64_encode(unsigned char *decoded_bytes,
                            size_t decoded_length) {
  int x;
  BIO *bioMem, *b64;
  BUF_MEM *bufPtr;
  char *buff = NULL;
  b64 = BIO_new(BIO_f_base64());
  BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
  bioMem = BIO_new(BIO_s_mem());
  b64 = BIO_push(b64, bioMem);
  BIO_write(b64, decoded_bytes, decoded_length);
  x = BIO_flush(b64);
  if (x < 1)
    goto END;
  BIO_get_mem_ptr(b64, &bufPtr);
  buff = (char *)malloc(bufPtr->length + 1);
  if (buff == NULL)
    goto END;
  memcpy(buff, bufPtr->data, bufPtr->length);
  buff[bufPtr->length] = 0;
END:
  BIO_free_all(b64);
  return buff;
}


x86 assembly (clang, -O0):
<openssl_base64_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x38(%rbp)
mov    $0x0,%al
call   1040 <BIO_f_base64@plt>
mov    %eax,%edi
call   1080 <BIO_new@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
call   1050 <BIO_set_flags@plt>
mov    $0x0,%al
call   10c0 <BIO_s_mem@plt>
mov    %eax,%edi
call   1080 <BIO_new@plt>
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <BIO_push@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   10b0 <BIO_write@plt>
mov    -0x28(%rbp),%rdi
call   10d0 <BIO_flush@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jge    123b <openssl_base64_encode+0x8b>
jmp    128f <openssl_base64_encode+0xdf>
mov    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   10a0 <BIO_get_mem_ptr@plt>
mov    -0x30(%rbp),%rax
mov    (%rax),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    126a <openssl_base64_encode+0xba>
jmp    128f <openssl_base64_encode+0xdf>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   1060 <memcpy@plt>
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
movslq (%rcx),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
call   1090 <BIO_free_all@plt>
mov    -0x38(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openssl_base64_encode>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r12
xor    %r14d,%r14d
xor    %eax,%eax
call   1040 <BIO_f_base64@plt>
mov    %eax,%edi
call   1080 <BIO_new@plt>
mov    %rax,%rbx
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1050 <BIO_set_flags@plt>
xor    %eax,%eax
call   10c0 <BIO_s_mem@plt>
mov    %eax,%edi
call   1080 <BIO_new@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1030 <BIO_push@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
call   10b0 <BIO_write@plt>
mov    %rbx,%rdi
call   10d0 <BIO_flush@plt>
test   %eax,%eax
jle    125c <openssl_base64_encode+0xac>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   10a0 <BIO_get_mem_ptr@plt>
mov    (%rsp),%rax
mov    (%rax),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     1259 <openssl_base64_encode+0xa9>
mov    %rax,%r14
mov    (%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %r14,%rdi
call   1060 <memcpy@plt>
mov    (%rsp),%rax
movslq (%rax),%rax
movb   $0x0,(%r14,%rax,1)
jmp    125c <openssl_base64_encode+0xac>
xor    %r14d,%r14d
mov    %rbx,%rdi
call   1090 <BIO_free_all@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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