#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 BIO_write(int /*<<< orphan*/ *, char *, int);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/, int);
int strlen(char *);
char *encode_base64(char *s) {
  BIO *bmem, *b64;
  BUF_MEM *bptr;
  char *res = NULL;
  int len = strlen(s);
  if (!(b64 = BIO_new(BIO_f_base64())))
    goto done;
  BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
  if (!(bmem = BIO_new(BIO_s_mem())))
    goto done;
  b64 = BIO_push(b64, bmem);
  if (BIO_write(b64, s, len) != len)
    goto done;
  if (BIO_flush(b64) != 1)
    goto done;
  BIO_get_mem_ptr(b64, &bptr);
  if (!(res = malloc(bptr->length + 1)))
    goto done;
  memcpy(res, bptr->data, bptr->length);
  res[bptr->length] = '\0';
done:
  if (b64)
    BIO_free_all(b64);
  return res;
}


x86 assembly (clang, -O0):
<encode_base64>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1050 <BIO_f_base64@plt>
mov    %eax,%edi
call   1090 <BIO_new@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1201 <encode_base64+0x41>
jmp    12cd <encode_base64+0x10d>
mov    -0x18(%rbp),%rdi
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
call   1060 <BIO_set_flags@plt>
mov    $0x0,%al
call   10d0 <BIO_s_mem@plt>
mov    %eax,%edi
call   1090 <BIO_new@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1234 <encode_base64+0x74>
jmp    12cd <encode_base64+0x10d>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <BIO_push@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   10c0 <BIO_write@plt>
cmp    -0x2c(%rbp),%eax
je     1263 <encode_base64+0xa3>
jmp    12cd <encode_base64+0x10d>
mov    -0x18(%rbp),%rdi
call   10e0 <BIO_flush@plt>
cmp    $0x1,%eax
je     127a <encode_base64+0xba>
jmp    12cd <encode_base64+0x10d>
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   10b0 <BIO_get_mem_ptr@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    12a8 <encode_base64+0xe8>
jmp    12cd <encode_base64+0x10d>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   1070 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
movslq (%rcx),%rcx
movb   $0x0,(%rax,%rcx,1)
cmpq   $0x0,-0x18(%rbp)
je     12e1 <encode_base64+0x121>
mov    -0x18(%rbp),%rdi
call   10a0 <BIO_free_all@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<encode_base64>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r15
call   1040 <strlen@plt>
mov    %eax,%ebp
xor    %r14d,%r14d
xor    %eax,%eax
call   1050 <BIO_f_base64@plt>
mov    %eax,%edi
call   1090 <BIO_new@plt>
test   %rax,%rax
je     1296 <encode_base64+0xd6>
mov    %rax,%rbx
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <BIO_set_flags@plt>
xor    %r14d,%r14d
xor    %eax,%eax
call   10d0 <BIO_s_mem@plt>
mov    %eax,%edi
call   1090 <BIO_new@plt>
test   %rax,%rax
je     128e <encode_base64+0xce>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1030 <BIO_push@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   10c0 <BIO_write@plt>
cmp    %ebp,%eax
jne    1286 <encode_base64+0xc6>
mov    %rbx,%rdi
call   10e0 <BIO_flush@plt>
cmp    $0x1,%eax
jne    1286 <encode_base64+0xc6>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   10b0 <BIO_get_mem_ptr@plt>
mov    (%rsp),%rbp
mov    0x0(%rbp),%r15d
lea    0x1(%r15),%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1286 <encode_base64+0xc6>
mov    %rax,%r14
mov    0x4(%rbp),%esi
mov    %rax,%rdi
mov    %r15d,%edx
call   1070 <memcpy@plt>
mov    (%rsp),%rax
movslq (%rax),%rax
movb   $0x0,(%r14,%rax,1)
test   %rbx,%rbx
jne    128e <encode_base64+0xce>
jmp    1296 <encode_base64+0xd6>
xor    %r14d,%r14d
test   %rbx,%rbx
je     1296 <encode_base64+0xd6>
mov    %rbx,%rdi
call   10a0 <BIO_free_all@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
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 i8* @encode_base64(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca %struct.TYPE_3__*
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %2
store i8* null, i8** %6
%8 = load i8*, i8** %2
%9 = call i32 @strlen(i8* %8)
store i32 %9, i32* %7
%10 = call i32 (...) @BIO_f_base64()
%11 = call i32* @BIO_new(i32 %10)
store i32* %11, i32** %4
%12 = icmp ne i32* %11, null
br i1 %12, label %14, label %13
13: ; preds = %1
br label %63
14: ; preds = %1
%15 = load i32*, i32** %4
%16 = load i32, i32* @BIO_FLAGS_BASE64_NO_NL
%17 = call i32 @BIO_set_flags(i32* %15, i32 %16)
%18 = call i32 (...) @BIO_s_mem()
%19 = call i32* @BIO_new(i32 %18)
store i32* %19, i32** %3
%20 = icmp ne i32* %19, null
br i1 %20, label %22, label %21
21: ; preds = %14
br label %63
22: ; preds = %14
%23 = load i32*, i32** %4
%24 = load i32*, i32** %3
%25 = call i32* @BIO_push(i32* %23, i32* %24)
store i32* %25, i32** %4
%26 = load i32*, i32** %4
%27 = load i8*, i8** %2
%28 = load i32, i32* %7
%29 = call i32 @BIO_write(i32* %26, i8* %27, i32 %28)
%30 = load i32, i32* %7
%31 = icmp ne i32 %29, %30
br i1 %31, label %32, label %33
32: ; preds = %22
br label %63
33: ; preds = %22
%34 = load i32*, i32** %4
%35 = call i32 @BIO_flush(i32* %34)
%36 = icmp ne i32 %35, 1
br i1 %36, label %37, label %38
37: ; preds = %33
br label %63
38: ; preds = %33
%39 = load i32*, i32** %4
%40 = call i32 @BIO_get_mem_ptr(i32* %39, %struct.TYPE_3__** %5)
%41 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%42 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %41, i32 0, i32 0
%43 = load i32, i32* %42
%44 = add nsw i32 %43, 1
%45 = call i8* @malloc(i32 %44)
store i8* %45, i8** %6
%46 = icmp ne i8* %45, null
br i1 %46, label %48, label %47
47: ; preds = %38
br label %63
48: ; preds = %38
%49 = load i8*, i8** %6
%50 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%51 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %50, i32 0, i32 1
%52 = load i32, i32* %51
%53 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%54 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %53, i32 0, i32 0
%55 = load i32, i32* %54
%56 = call i32 @memcpy(i8* %49, i32 %52, i32 %55)
%57 = load i8*, i8** %6
%58 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%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
br label %63
63: ; preds = %48, %47, %37, %32, %21, %13
%64 = load i32*, i32** %4
%65 = icmp ne i32* %64, null
br i1 %65, label %66, label %69
66: ; preds = %63
%67 = load i32*, i32** %4
%68 = call i32 @BIO_free_all(i32* %67)
br label %69
69: ; preds = %66, %63
%70 = load i8*, i8** %6
ret i8* %70
}

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