#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 *,
                             unsigned int);
char *calloc(int, int);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/, int);
int base64_encode(unsigned char *in, unsigned int in_len, char **encoded) {
  BIO *bmem, *b64;
  BUF_MEM *bptr;
  b64 = BIO_new(BIO_f_base64());
  BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
  bmem = BIO_new(BIO_s_mem());
  b64 = BIO_push(b64, bmem);
  BIO_write(b64, in, in_len);
  if (BIO_flush(b64) != 1) {
    BIO_free_all(b64);
    return 1;
  }
  BIO_get_mem_ptr(b64, &bptr);
  *encoded = calloc(bptr->length + 1, 1);
  if (!(*encoded)) {
    BIO_free_all(b64);
    return 1;
  }
  memcpy(*encoded, bptr->data, bptr->length);
  (*encoded)[bptr->length] = '\0';
  BIO_free_all(b64);
  return 0;
}


x86 assembly (clang, -O0):
<base64_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    $0x0,%al
call   1040 <BIO_f_base64@plt>
mov    %eax,%edi
call   1080 <BIO_new@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
call   1060 <BIO_set_flags@plt>
mov    $0x0,%al
call   10c0 <BIO_s_mem@plt>
mov    %eax,%edi
call   1080 <BIO_new@plt>
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1030 <BIO_push@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   10b0 <BIO_write@plt>
mov    -0x30(%rbp),%rdi
call   10d0 <BIO_flush@plt>
cmp    $0x1,%eax
je     1241 <base64_encode+0x91>
mov    -0x30(%rbp),%rdi
call   1090 <BIO_free_all@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c9 <base64_encode+0x119>
mov    -0x30(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   10a0 <BIO_get_mem_ptr@plt>
mov    -0x38(%rbp),%rax
mov    (%rax),%edi
add    $0x1,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
jne    128e <base64_encode+0xde>
mov    -0x30(%rbp),%rdi
call   1090 <BIO_free_all@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c9 <base64_encode+0x119>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    -0x38(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x38(%rbp),%rax
mov    (%rax),%edx
call   1070 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    -0x38(%rbp),%rcx
movslq (%rcx),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rdi
call   1090 <BIO_free_all@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<base64_encode>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %esi,%r15d
mov    %rdi,%rbp
xor    %eax,%eax
call   1040 <BIO_f_base64@plt>
mov    %eax,%edi
call   1080 <BIO_new@plt>
mov    %rax,%rbx
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <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    %rbp,%rsi
mov    %r15d,%edx
call   10b0 <BIO_write@plt>
mov    %rbx,%rdi
call   10d0 <BIO_flush@plt>
cmp    $0x1,%eax
jne    127a <base64_encode+0xca>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   10a0 <BIO_get_mem_ptr@plt>
mov    0x8(%rsp),%r12
mov    (%r12),%r15d
lea    0x1(%r15),%edi
mov    $0x1,%ebp
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     1289 <base64_encode+0xd9>
mov    0x4(%r12),%esi
mov    %rax,%rdi
mov    %r15d,%edx
call   1070 <memcpy@plt>
mov    (%r14),%rax
mov    0x8(%rsp),%rcx
movslq (%rcx),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    %rbx,%rdi
call   1090 <BIO_free_all@plt>
xor    %ebp,%ebp
jmp    1291 <base64_encode+0xe1>
mov    %rbx,%rdi
call   1090 <BIO_free_all@plt>
mov    $0x1,%ebp
jmp    1291 <base64_encode+0xe1>
mov    %rbx,%rdi
call   1090 <BIO_free_all@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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