#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*/ *, char *, int);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
int main(void) {
  BIO *base64 = NULL;
  BIO *mem = NULL;
  BUF_MEM *bptr = NULL;
  char *buf = NULL;
  base64 = BIO_new(BIO_f_base64());
  mem = BIO_new(BIO_s_mem());
  base64 = BIO_push(base64, mem);
  BIO_write(base64, "ABCDE", 5);
  BIO_flush(base64);
  BIO_get_mem_ptr(base64, &bptr);
  buf = (char *)malloc(bptr->length);
  memcpy(buf, bptr->data, bptr->length - 1);
  buf[bptr->length - 1] = '\0';
  printf("buf = %s\n", buf);
  free(buf);
  BIO_free_all(base64);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    $0x0,%al
call   1050 <BIO_f_base64@plt>
mov    %eax,%edi
call   1090 <BIO_new@plt>
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   10d0 <BIO_s_mem@plt>
mov    %eax,%edi
call   1090 <BIO_new@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <BIO_push@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xdd1(%rip),%rsi
mov    $0x5,%edx
call   10c0 <BIO_write@plt>
mov    -0x10(%rbp),%rdi
call   10e0 <BIO_flush@plt>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   10b0 <BIO_get_mem_ptr@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
sub    $0x1,%edx
call   1070 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rsi
lea    0xd70(%rip),%rdi
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
call   10a0 <BIO_free_all@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
movq   $0x0,(%rsp)
xor    %eax,%eax
call   1050 <BIO_f_base64@plt>
mov    %eax,%edi
call   1090 <BIO_new@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   10d0 <BIO_s_mem@plt>
mov    %eax,%edi
call   1090 <BIO_new@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1040 <BIO_push@plt>
mov    %rax,%r14
lea    0xe00(%rip),%rsi
mov    %rax,%rdi
mov    $0x5,%edx
call   10c0 <BIO_write@plt>
mov    %r14,%rdi
call   10e0 <BIO_flush@plt>
mov    %rsp,%rsi
mov    %r14,%rdi
call   10b0 <BIO_get_mem_ptr@plt>
mov    (%rsp),%rax
mov    (%rax),%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    (%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
add    $0xffffffff,%edx
mov    %rbx,%rdi
call   1070 <memcpy@plt>
mov    (%rsp),%rax
movslq (%rax),%rax
movb   $0x0,-0x1(%rax,%rbx,1)
lea    0xdb1(%rip),%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   10a0 <BIO_free_all@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32, i32 }
@.str = [6 x i8] c"ABCDE\00"
@.str.1 = [10 x i8] c"buf = %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca %struct.TYPE_3__*
%5 = alloca i8*
store i32 0, i32* %1
store i32* null, i32** %2
store i32* null, i32** %3
store %struct.TYPE_3__* null, %struct.TYPE_3__** %4
store i8* null, i8** %5
%6 = call i32 (...) @BIO_f_base64()
%7 = call i32* @BIO_new(i32 %6)
store i32* %7, i32** %2
%8 = call i32 (...) @BIO_s_mem()
%9 = call i32* @BIO_new(i32 %8)
store i32* %9, i32** %3
%10 = load i32*, i32** %2
%11 = load i32*, i32** %3
%12 = call i32* @BIO_push(i32* %10, i32* %11)
store i32* %12, i32** %2
%13 = load i32*, i32** %2
%14 = call i32 @BIO_write(i32* %13, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
%15 = load i32*, i32** %2
%16 = call i32 @BIO_flush(i32* %15)
%17 = load i32*, i32** %2
%18 = call i32 @BIO_get_mem_ptr(i32* %17, %struct.TYPE_3__** %4)
%19 = load %struct.TYPE_3__*, %struct.TYPE_3__** %4
%20 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %19, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i64 @malloc(i32 %21)
%23 = inttoptr i64 %22 to i8*
store i8* %23, i8** %5
%24 = load i8*, i8** %5
%25 = load %struct.TYPE_3__*, %struct.TYPE_3__** %4
%26 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %25, i32 0, i32 1
%27 = load i32, i32* %26
%28 = load %struct.TYPE_3__*, %struct.TYPE_3__** %4
%29 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %28, i32 0, i32 0
%30 = load i32, i32* %29
%31 = sub nsw i32 %30, 1
%32 = call i32 @memcpy(i8* %24, i32 %27, i32 %31)
%33 = load i8*, i8** %5
%34 = load %struct.TYPE_3__*, %struct.TYPE_3__** %4
%35 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %34, i32 0, i32 0
%36 = load i32, i32* %35
%37 = sub nsw i32 %36, 1
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %33, i64 %38
store i8 0, i8* %39
%40 = load i8*, i8** %5
%41 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %40)
%42 = load i8*, i8** %5
%43 = call i32 @free(i8* %42)
%44 = load i32*, i32** %2
%45 = call i32 @BIO_free_all(i32* %44)
ret i32 0
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32, i32 }
@.str = [6 x i8] c"ABCDE\00"
@.str.1 = [10 x i8] c"buf = %s\0A\00"
define i32 @main() {
%1 = alloca %struct.TYPE_3__*
%2 = bitcast %struct.TYPE_3__** %1 to i8*
store %struct.TYPE_3__* null, %struct.TYPE_3__** %1
%3 = tail call i32 (...) @BIO_f_base64()
%4 = tail call i32* @BIO_new(i32 %3)
%5 = tail call i32 (...) @BIO_s_mem()
%6 = tail call i32* @BIO_new(i32 %5)
%7 = tail call i32* @BIO_push(i32* %4, i32* %6)
%8 = tail call i32 @BIO_write(i32* %7, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
%9 = tail call i32 @BIO_flush(i32* %7)
%10 = call i32 @BIO_get_mem_ptr(i32* %7, %struct.TYPE_3__** %1)
%11 = load %struct.TYPE_3__*, %struct.TYPE_3__** %1
%12 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %11, i64 0, i32 0
%13 = load i32, i32* %12
%14 = call i64 @malloc(i32 %13)
%15 = inttoptr i64 %14 to i8*
%16 = load %struct.TYPE_3__*, %struct.TYPE_3__** %1
%17 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %16, i64 0, i32 1
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %16, i64 0, i32 0
%20 = load i32, i32* %19
%21 = add nsw i32 %20, -1
%22 = call i32 @memcpy(i8* %15, i32 %18, i32 %21)
%23 = load %struct.TYPE_3__*, %struct.TYPE_3__** %1
%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 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %15, i64 %27
store i8 0, i8* %28
%29 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %15)
%30 = call i32 @free(i8* %15)
%31 = call i32 @BIO_free_all(i32* %7)
ret i32 0
}