#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_FLAGS_BASE64_NO_NL;
int /*<<< orphan*/ BIO_f_base64();
int /*<<< orphan*/ BIO_flush(int /*<<< orphan*/ *);
int /*<<< orphan*/ BIO_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BIO_new(int /*<<< orphan*/);
int /*<<< orphan*/ BIO_push(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int BIO_read(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ BIO_s_mem();
int /*<<< orphan*/ BIO_set_flags(int /*<<< orphan*/ *, int /*<<< orphan*/);
int BIO_write(int /*<<< orphan*/ *, unsigned char const *, int);
int b64_encode(const unsigned char *in, int in_len, char *out, int out_len) {
  int ret = 0;
  BIO *b64 = BIO_new(BIO_f_base64());
  BIO *bio = BIO_new(BIO_s_mem());
  BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
  BIO_push(b64, bio);
  ret = BIO_write(b64, in, in_len);
  BIO_flush(b64);
  if (ret > 0)
    ret = BIO_read(bio, out, out_len);
  BIO_free(b64);
  return ret;
}


x86 assembly (clang, -O0):
<b64_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    $0x0,%al
call   1050 <BIO_f_base64@plt>
mov    %eax,%edi
call   1070 <BIO_new@plt>
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   10a0 <BIO_s_mem@plt>
mov    %eax,%edi
call   1070 <BIO_new@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
call   1060 <BIO_set_flags@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1030 <BIO_push@plt>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1090 <BIO_write@plt>
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
call   10b0 <BIO_flush@plt>
cmpl   $0x0,-0x20(%rbp)
jle    1229 <b64_encode+0x99>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <BIO_read@plt>
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
call   1080 <BIO_free@plt>
mov    -0x20(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<b64_encode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,0x4(%rsp)
mov    %rdx,%r15
mov    %esi,%r12d
mov    %rdi,%r13
xor    %eax,%eax
call   1050 <BIO_f_base64@plt>
mov    %eax,%edi
call   1070 <BIO_new@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   10a0 <BIO_s_mem@plt>
mov    %eax,%edi
call   1070 <BIO_new@plt>
mov    %rax,%r14
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <BIO_set_flags@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <BIO_push@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %r12d,%edx
call   1090 <BIO_write@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   10b0 <BIO_flush@plt>
test   %ebp,%ebp
jle    1213 <b64_encode+0x83>
mov    %r14,%rdi
mov    %r15,%rsi
mov    0x4(%rsp),%edx
call   1040 <BIO_read@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1080 <BIO_free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@BIO_FLAGS_BASE64_NO_NL = global i32 0
define i32 @b64_encode(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
store i32 0, i32* %9
%12 = call i32 (...) @BIO_f_base64()
%13 = call i32* @BIO_new(i32 %12)
store i32* %13, i32** %10
%14 = call i32 (...) @BIO_s_mem()
%15 = call i32* @BIO_new(i32 %14)
store i32* %15, i32** %11
%16 = load i32*, i32** %10
%17 = load i32, i32* @BIO_FLAGS_BASE64_NO_NL
%18 = call i32 @BIO_set_flags(i32* %16, i32 %17)
%19 = load i32*, i32** %10
%20 = load i32*, i32** %11
%21 = call i32 @BIO_push(i32* %19, i32* %20)
%22 = load i32*, i32** %10
%23 = load i8*, i8** %5
%24 = load i32, i32* %6
%25 = call i32 @BIO_write(i32* %22, i8* %23, i32 %24)
store i32 %25, i32* %9
%26 = load i32*, i32** %10
%27 = call i32 @BIO_flush(i32* %26)
%28 = load i32, i32* %9
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %4
%31 = load i32*, i32** %11
%32 = load i8*, i8** %7
%33 = load i32, i32* %8
%34 = call i32 @BIO_read(i32* %31, i8* %32, i32 %33)
store i32 %34, i32* %9
br label %35
35: ; preds = %30, %4
%36 = load i32*, i32** %10
%37 = call i32 @BIO_free(i32* %36)
%38 = load i32, i32* %9
ret i32 %38
}

LLVM IR (-O2):
@BIO_FLAGS_BASE64_NO_NL = global i32 0
define i32 @b64_encode(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = tail call i32 (...) @BIO_f_base64()
%6 = tail call i32* @BIO_new(i32 %5)
%7 = tail call i32 (...) @BIO_s_mem()
%8 = tail call i32* @BIO_new(i32 %7)
%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_push(i32* %6, i32* %8)
%12 = tail call i32 @BIO_write(i32* %6, i8* %0, i32 %1)
%13 = tail call i32 @BIO_flush(i32* %6)
%14 = icmp sgt i32 %12, 0
br i1 %14, label %15, label %17
15: ; preds = %4
%16 = tail call i32 @BIO_read(i32* %8, i8* %2, i32 %3)
br label %17
17: ; preds = %15, %4
%18 = phi i32 [ %16, %15 ], [ %12, %4 ]
%19 = tail call i32 @BIO_free(i32* %6)
ret i32 %18
}