#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_free_all(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*/ *, void const *, int);
char *calloc(size_t, int);
int /*<<< orphan*/ free(char *);
int saslc__crypto_encode_base64(const void *in, size_t inlen, char **out,
                                size_t *outlen) {
  BIO *bio;
  BIO *b64;
  size_t enclen;
  char *r;
  int n;
  enclen = (((inlen + 2) / 3)) * 4;
  r = calloc(enclen + 1, sizeof(*r));
  if (r == NULL)
    return -1;
  if ((bio = BIO_new(BIO_s_mem())) == NULL)
    goto err;
  if ((b64 = BIO_new(BIO_f_base64())) == NULL) {
    BIO_free(bio);
    goto err;
  }
  BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
  b64 = BIO_push(b64, bio);
  if (BIO_write(b64, in, (int)inlen) != (int)inlen) {
    BIO_free_all(b64);
    goto err;
  }
  (void)BIO_flush(b64);
  n = BIO_read(bio, r, (int)enclen);
  BIO_free_all(b64);
  if (n < 0)
    goto err;
  if (out)
    *out = r;
  if (outlen)
    *outlen = n;
  return 0;
err:
  free(r);
  return -1;
}


x86 assembly (clang, -O0):
<saslc__crypto_encode_base64>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x18(%rbp),%rax
add    $0x2,%rax
mov    $0x3,%ecx
xor    %edx,%edx
div    %rcx
shl    $0x2,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    121f <saslc__crypto_encode_base64+0x5f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1339 <saslc__crypto_encode_base64+0x179>
mov    $0x0,%al
call   10d0 <BIO_s_mem@plt>
mov    %eax,%edi
call   1090 <BIO_new@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    1240 <saslc__crypto_encode_base64+0x80>
jmp    1329 <saslc__crypto_encode_base64+0x169>
mov    $0x0,%al
call   1060 <BIO_f_base64@plt>
mov    %eax,%edi
call   1090 <BIO_new@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    126a <saslc__crypto_encode_base64+0xaa>
mov    -0x30(%rbp),%rdi
call   10a0 <BIO_free@plt>
jmp    1329 <saslc__crypto_encode_base64+0x169>
mov    -0x38(%rbp),%rdi
mov    0x2d6b(%rip),%rax
mov    (%rax),%esi
call   1080 <BIO_set_flags@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1040 <BIO_push@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    %eax,%edx
call   10c0 <BIO_write@plt>
mov    -0x18(%rbp),%rcx
cmp    %ecx,%eax
je     12ba <saslc__crypto_encode_base64+0xfa>
mov    -0x38(%rbp),%rdi
call   10b0 <BIO_free_all@plt>
jmp    1329 <saslc__crypto_encode_base64+0x169>
mov    -0x38(%rbp),%rdi
call   10e0 <BIO_flush@plt>
mov    -0x30(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    -0x40(%rbp),%rax
mov    %eax,%edx
call   1050 <BIO_read@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x38(%rbp),%rdi
call   10b0 <BIO_free_all@plt>
cmpl   $0x0,-0x4c(%rbp)
jge    12f1 <saslc__crypto_encode_base64+0x131>
jmp    1329 <saslc__crypto_encode_base64+0x169>
cmpq   $0x0,-0x20(%rbp)
je     1307 <saslc__crypto_encode_base64+0x147>
mov    -0x48(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x28(%rbp)
je     131d <saslc__crypto_encode_base64+0x15d>
movslq -0x4c(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1339 <saslc__crypto_encode_base64+0x179>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<saslc__crypto_encode_base64>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r15
mov    %rsi,%r12
lea    0x2(%rsi),%rax
movabs $0xaaaaaaaaaaaaaaab,%rdx
aa aa aa
mul    %rdx
mov    %rdx,%rbp
mov    %rcx,%rbx
mov    %rdi,0x10(%rsp)
add    %rdx,%rbp
and    $0xfffffffffffffffc,%rbp
lea    0x1(%rbp),%rdi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
je     12f1 <saslc__crypto_encode_base64+0x131>
mov    %rax,%r13
mov    %r15,(%rsp)
mov    %rbx,0x8(%rsp)
xor    %eax,%eax
call   10d0 <BIO_s_mem@plt>
mov    %eax,%edi
call   1090 <BIO_new@plt>
test   %rax,%rax
je     12e9 <saslc__crypto_encode_base64+0x129>
mov    %rax,%r15
xor    %eax,%eax
call   1060 <BIO_f_base64@plt>
mov    %eax,%edi
call   1090 <BIO_new@plt>
test   %rax,%rax
je     12d1 <saslc__crypto_encode_base64+0x111>
mov    %rax,%rbx
mov    0x2d85(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <BIO_set_flags@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1040 <BIO_push@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    0x10(%rsp),%rsi
mov    %r12d,%edx
call   10c0 <BIO_write@plt>
cmp    %r12d,%eax
jne    12db <saslc__crypto_encode_base64+0x11b>
mov    %r14,%rdi
call   10e0 <BIO_flush@plt>
mov    %r15,%rdi
mov    %r13,%rsi
mov    %ebp,%edx
call   1050 <BIO_read@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   10b0 <BIO_free_all@plt>
test   %ebx,%ebx
mov    $0xffffffff,%r14d
js     12e9 <saslc__crypto_encode_base64+0x129>
mov    (%rsp),%rax
test   %rax,%rax
je     12bd <saslc__crypto_encode_base64+0xfd>
mov    %r13,(%rax)
xor    %r14d,%r14d
mov    0x8(%rsp),%rcx
test   %rcx,%rcx
je     12f1 <saslc__crypto_encode_base64+0x131>
mov    %ebx,%eax
mov    %rax,(%rcx)
jmp    12f1 <saslc__crypto_encode_base64+0x131>
mov    %r15,%rdi
call   10a0 <BIO_free@plt>
jmp    12e9 <saslc__crypto_encode_base64+0x129>
mov    %r14,%rdi
call   10b0 <BIO_free_all@plt>
mov    $0xffffffff,%r14d
mov    %r13,%rdi
call   1030 <free@plt>
mov    %r14d,%eax
add    $0x18,%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 @saslc__crypto_encode_base64(i8* %0, i64 %1, i8** %2, i64* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8**
%9 = alloca i64*
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i64
%13 = alloca i8*
%14 = alloca i32
store i8* %0, i8** %6
store i64 %1, i64* %7
store i8** %2, i8*** %8
store i64* %3, i64** %9
%15 = load i64, i64* %7
%16 = add i64 %15, 2
%17 = udiv i64 %16, 3
%18 = mul i64 %17, 4
store i64 %18, i64* %12
%19 = load i64, i64* %12
%20 = add i64 %19, 1
%21 = call i8* @calloc(i64 %20, i32 1)
store i8* %21, i8** %13
%22 = load i8*, i8** %13
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %25
24: ; preds = %4
store i32 -1, i32* %5
br label %85
25: ; preds = %4
%26 = call i32 (...) @BIO_s_mem()
%27 = call i32* @BIO_new(i32 %26)
store i32* %27, i32** %10
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %30
29: ; preds = %25
br label %82
30: ; preds = %25
%31 = call i32 (...) @BIO_f_base64()
%32 = call i32* @BIO_new(i32 %31)
store i32* %32, i32** %11
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = load i32*, i32** %10
%36 = call i32 @BIO_free(i32* %35)
br label %82
37: ; preds = %30
%38 = load i32*, i32** %11
%39 = load i32, i32* @BIO_FLAGS_BASE64_NO_NL
%40 = call i32 @BIO_set_flags(i32* %38, i32 %39)
%41 = load i32*, i32** %11
%42 = load i32*, i32** %10
%43 = call i32* @BIO_push(i32* %41, i32* %42)
store i32* %43, i32** %11
%44 = load i32*, i32** %11
%45 = load i8*, i8** %6
%46 = load i64, i64* %7
%47 = trunc i64 %46 to i32
%48 = call i32 @BIO_write(i32* %44, i8* %45, i32 %47)
%49 = load i64, i64* %7
%50 = trunc i64 %49 to i32
%51 = icmp ne i32 %48, %50
br i1 %51, label %52, label %55
52: ; preds = %37
%53 = load i32*, i32** %11
%54 = call i32 @BIO_free_all(i32* %53)
br label %82
55: ; preds = %37
%56 = load i32*, i32** %11
%57 = call i32 @BIO_flush(i32* %56)
%58 = load i32*, i32** %10
%59 = load i8*, i8** %13
%60 = load i64, i64* %12
%61 = trunc i64 %60 to i32
%62 = call i32 @BIO_read(i32* %58, i8* %59, i32 %61)
store i32 %62, i32* %14
%63 = load i32*, i32** %11
%64 = call i32 @BIO_free_all(i32* %63)
%65 = load i32, i32* %14
%66 = icmp slt i32 %65, 0
br i1 %66, label %67, label %68
67: ; preds = %55
br label %82
68: ; preds = %55
%69 = load i8**, i8*** %8
%70 = icmp ne i8** %69, null
br i1 %70, label %71, label %74
71: ; preds = %68
%72 = load i8*, i8** %13
%73 = load i8**, i8*** %8
store i8* %72, i8** %73
br label %74
74: ; preds = %71, %68
%75 = load i64*, i64** %9
%76 = icmp ne i64* %75, null
br i1 %76, label %77, label %81
77: ; preds = %74
%78 = load i32, i32* %14
%79 = sext i32 %78 to i64
%80 = load i64*, i64** %9
store i64 %79, i64* %80
br label %81
81: ; preds = %77, %74
store i32 0, i32* %5
br label %85
82: ; preds = %67, %52, %34, %29
%83 = load i8*, i8** %13
%84 = call i32 @free(i8* %83)
store i32 -1, i32* %5
br label %85
85: ; preds = %82, %81, %24
%86 = load i32, i32* %5
ret i32 %86
}

LLVM IR (-O2):
@BIO_FLAGS_BASE64_NO_NL = global i32 0
define i32 @saslc__crypto_encode_base64(i8* %0, i64 %1, i8** writeonly %2, i64* writeonly %3) {
%5 = add i64 %1, 2
%6 = udiv i64 %5, 3
%7 = shl i64 %6, 2
%8 = or i64 %7, 1
%9 = tail call i8* @calloc(i64 %8, i32 1)
%10 = icmp eq i8* %9, null
br i1 %10, label %45, label %11
11: ; preds = %4
%12 = tail call i32 (...) @BIO_s_mem()
%13 = tail call i32* @BIO_new(i32 %12)
%14 = icmp eq i32* %13, null
br i1 %14, label %43, label %15
15: ; preds = %11
%16 = tail call i32 (...) @BIO_f_base64()
%17 = tail call i32* @BIO_new(i32 %16)
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = tail call i32 @BIO_free(i32* %13)
br label %43
21: ; preds = %15
%22 = load i32, i32* @BIO_FLAGS_BASE64_NO_NL
%23 = tail call i32 @BIO_set_flags(i32* %17, i32 %22)
%24 = tail call i32* @BIO_push(i32* %17, i32* %13)
%25 = trunc i64 %1 to i32
%26 = tail call i32 @BIO_write(i32* %24, i8* %0, i32 %25)
%27 = icmp eq i32 %26, %25
br i1 %27, label %30, label %28
28: ; preds = %21
%29 = tail call i32 @BIO_free_all(i32* %24)
br label %43
30: ; preds = %21
%31 = tail call i32 @BIO_flush(i32* %24)
%32 = trunc i64 %7 to i32
%33 = tail call i32 @BIO_read(i32* %13, i8* %9, i32 %32)
%34 = tail call i32 @BIO_free_all(i32* %24)
%35 = icmp slt i32 %33, 0
br i1 %35, label %43, label %36
36: ; preds = %30
%37 = icmp eq i8** %2, null
br i1 %37, label %39, label %38
38: ; preds = %36
store i8* %9, i8** %2
br label %39
39: ; preds = %38, %36
%40 = icmp eq i64* %3, null
br i1 %40, label %45, label %41
41: ; preds = %39
%42 = zext i32 %33 to i64
store i64 %42, i64* %3
br label %45
43: ; preds = %30, %11, %28, %19
%44 = tail call i32 @free(i8* %9)
br label %45
45: ; preds = %39, %41, %4, %43
%46 = phi i32 [ -1, %43 ], [ -1, %4 ], [ 0, %41 ], [ 0, %39 ]
ret i32 %46
}