#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 */

/* Type definitions */
typedef int /*<<< orphan*/ BIO;

/* Variables and functions */
int /*<<< orphan*/ BIO_CTRL_INFO;
scalar_t__ BIO_ctrl(int /*<<< orphan*/ *, int /*<<< orphan*/,
                    int /*<<< orphan*/, unsigned char *);
int /*<<< orphan*/ BIO_f_base64();
int /*<<< orphan*/ BIO_flush(int /*<<< orphan*/ *);
int /*<<< orphan*/ BIO_free_all(int /*<<< orphan*/ *);
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*/ fprintf(int /*<<< orphan*/, char *);
unsigned char *malloc(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
unsigned char *_encode_m64(unsigned char *orig_ptr, int orig_len) {
  BIO *mbio, *b64bio, *bio;
  unsigned char *m64_ptr = NULL;
  int m64_len = 0;
  unsigned char *ptr = NULL;
  if (orig_len == 0)
    return NULL;
  mbio = BIO_new(BIO_s_mem());
  b64bio = BIO_new(BIO_f_base64());
  bio = BIO_push(b64bio, mbio);
  BIO_write(bio, orig_ptr, orig_len);
  BIO_flush(bio);
  m64_len = (int)BIO_ctrl(mbio, BIO_CTRL_INFO, 0, ptr);
  if (m64_len != 0) {
    m64_ptr = malloc(m64_len + 1);
    if (m64_ptr) {
      strcpy((char *)m64_ptr, (char *)ptr);
    } else {
      fprintf(stderr, "ERROR: malloc failed\n");
    }
  }
  BIO_free_all(bio);
  return m64_ptr;
}


x86 assembly (clang, -O0):
<_encode_m64>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movq   $0x0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
movq   $0x0,-0x48(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    11ed <_encode_m64+0x3d>
movq   $0x0,-0x8(%rbp)
jmp    12b7 <_encode_m64+0x107>
mov    $0x0,%al
call   10c0 <BIO_s_mem@plt>
mov    %eax,%edi
call   1080 <BIO_new@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1050 <BIO_f_base64@plt>
mov    %eax,%edi
call   1080 <BIO_new@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <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>
mov    -0x20(%rbp),%rdi
mov    0x2da2(%rip),%rax
mov    (%rax),%esi
mov    -0x48(%rbp),%rcx
xor    %edx,%edx
call   1090 <BIO_ctrl@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
je     12a6 <_encode_m64+0xf6>
mov    -0x3c(%rbp),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     128c <_encode_m64+0xdc>
mov    -0x38(%rbp),%rdi
mov    -0x48(%rbp),%rsi
call   1030 <strcpy@plt>
jmp    12a1 <_encode_m64+0xf1>
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
lea    0xd64(%rip),%rsi
call   1060 <fprintf@plt>
jmp    12a6 <_encode_m64+0xf6>
mov    -0x30(%rbp),%rdi
call   10a0 <BIO_free_all@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_encode_m64>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %esi,%esi
je     1241 <_encode_m64+0x91>
mov    %esi,%ebp
mov    %rdi,%r15
xor    %r12d,%r12d
xor    %eax,%eax
call   10c0 <BIO_s_mem@plt>
mov    %eax,%edi
call   1080 <BIO_new@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1050 <BIO_f_base64@plt>
mov    %eax,%edi
call   1080 <BIO_new@plt>
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <BIO_push@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   10b0 <BIO_write@plt>
mov    %r14,%rdi
call   10d0 <BIO_flush@plt>
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
xor    %edx,%edx
xor    %ecx,%ecx
call   1090 <BIO_ctrl@plt>
test   %eax,%eax
je     125e <_encode_m64+0xae>
add    $0x1,%eax
mov    %eax,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     1246 <_encode_m64+0x96>
mov    %rax,%r12
mov    %rax,%rdi
xor    %esi,%esi
call   1030 <strcpy@plt>
jmp    125e <_encode_m64+0xae>
xor    %r12d,%r12d
jmp    1266 <_encode_m64+0xb6>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
lea    0xdaa(%rip),%rsi
call   1060 <fprintf@plt>
xor    %r12d,%r12d
mov    %r14,%rdi
call   10a0 <BIO_free_all@plt>
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@BIO_CTRL_INFO = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"ERROR: malloc failed\0A\00"
define i8* @_encode_m64(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* null, i8** %9
store i32 0, i32* %10
store i8* null, i8** %11
%12 = load i32, i32* %5
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %2
store i8* null, i8** %3
br label %54
15: ; preds = %2
%16 = call i32 (...) @BIO_s_mem()
%17 = call i32* @BIO_new(i32 %16)
store i32* %17, i32** %6
%18 = call i32 (...) @BIO_f_base64()
%19 = call i32* @BIO_new(i32 %18)
store i32* %19, i32** %7
%20 = load i32*, i32** %7
%21 = load i32*, i32** %6
%22 = call i32* @BIO_push(i32* %20, i32* %21)
store i32* %22, i32** %8
%23 = load i32*, i32** %8
%24 = load i8*, i8** %4
%25 = load i32, i32* %5
%26 = call i32 @BIO_write(i32* %23, i8* %24, i32 %25)
%27 = load i32*, i32** %8
%28 = call i32 @BIO_flush(i32* %27)
%29 = load i32*, i32** %6
%30 = load i32, i32* @BIO_CTRL_INFO
%31 = load i8*, i8** %11
%32 = call i64 @BIO_ctrl(i32* %29, i32 %30, i32 0, i8* %31)
%33 = trunc i64 %32 to i32
store i32 %33, i32* %10
%34 = load i32, i32* %10
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %50
36: ; preds = %15
%37 = load i32, i32* %10
%38 = add nsw i32 %37, 1
%39 = call i8* @malloc(i32 %38)
store i8* %39, i8** %9
%40 = load i8*, i8** %9
%41 = icmp ne i8* %40, null
br i1 %41, label %42, label %46
42: ; preds = %36
%43 = load i8*, i8** %9
%44 = load i8*, i8** %11
%45 = call i32 @strcpy(i8* %43, i8* %44)
br label %49
46: ; preds = %36
%47 = load i32, i32* @stderr
%48 = call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %49
49: ; preds = %46, %42
br label %50
50: ; preds = %49, %15
%51 = load i32*, i32** %8
%52 = call i32 @BIO_free_all(i32* %51)
%53 = load i8*, i8** %9
store i8* %53, i8** %3
br label %54
54: ; preds = %50, %14
%55 = load i8*, i8** %3
ret i8* %55
}

LLVM IR (-O2):
@BIO_CTRL_INFO = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"ERROR: malloc failed\0A\00"
define i8* @_encode_m64(i8* %0, i32 %1) {
%3 = icmp eq i32 %1, 0
br i1 %3, label %28, label %4
4: ; preds = %2
%5 = tail call i32 (...) @BIO_s_mem()
%6 = tail call i32* @BIO_new(i32 %5)
%7 = tail call i32 (...) @BIO_f_base64()
%8 = tail call i32* @BIO_new(i32 %7)
%9 = tail call i32* @BIO_push(i32* %8, i32* %6)
%10 = tail call i32 @BIO_write(i32* %9, i8* %0, i32 %1)
%11 = tail call i32 @BIO_flush(i32* %9)
%12 = load i32, i32* @BIO_CTRL_INFO
%13 = tail call i64 @BIO_ctrl(i32* %6, i32 %12, i32 0, i8* null)
%14 = trunc i64 %13 to i32
%15 = icmp eq i32 %14, 0
br i1 %15, label %25, label %16
16: ; preds = %4
%17 = add nsw i32 %14, 1
%18 = tail call i8* @malloc(i32 %17)
%19 = icmp eq i8* %18, null
br i1 %19, label %22, label %20
20: ; preds = %16
%21 = tail call i32 @strcpy(i8* %18, i8* null)
br label %25
22: ; preds = %16
%23 = load i32, i32* @stderr
%24 = tail call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %25
25: ; preds = %20, %22, %4
%26 = phi i8* [ %18, %20 ], [ null, %22 ], [ null, %4 ]
%27 = tail call i32 @BIO_free_all(i32* %9)
br label %28
28: ; preds = %2, %25
%29 = phi i8* [ %26, %25 ], [ null, %2 ]
ret i8* %29
}