#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 inlen;

/* Variables and functions */
int /*<<< orphan*/ base64_encode(char const *, size_t, char *, size_t);
char *malloc(size_t);
size_t base64_encode_alloc(const char *in, size_t inlen, char **out) {
  size_t outlen = 1 + ((((inlen) + 2) / 3) * 4);
  if (inlen > outlen) {
    *out = NULL;
    return 0;
  }
  *out = malloc(outlen);
  if (!*out)
    return outlen;
  base64_encode(in, inlen, *out, outlen);
  return outlen - 1;
}


x86 assembly (clang, -O0):
<base64_encode_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
add    $0x2,%rax
mov    $0x3,%ecx
xor    %edx,%edx
div    %rcx
shl    $0x2,%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x28(%rbp),%rax
jbe    1178 <base64_encode_alloc+0x58>
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    11ca <base64_encode_alloc+0xaa>
mov    -0x28(%rbp),%rdi
call   1030 <malloc@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11a6 <base64_encode_alloc+0x86>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ca <base64_encode_alloc+0xaa>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
mov    -0x28(%rbp),%rcx
call   1040 <base64_encode@plt>
mov    -0x28(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<base64_encode_alloc>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r12
lea    0x2(%rsi),%rax
movabs $0xaaaaaaaaaaaaaaab,%rcx
aa aa aa
mul    %rcx
mov    %rdx,%rbx
add    %rdx,%rbx
and    $0xfffffffffffffffc,%rbx
lea    0x1(%rbx),%r13
cmp    %rsi,%r13
jae    115c <base64_encode_alloc+0x3c>
movq   $0x0,(%r12)
xor    %ebx,%ebx
jmp    1189 <base64_encode_alloc+0x69>
mov    %rsi,%r14
mov    %rdi,%r15
mov    %r13,%rdi
call   1030 <malloc@plt>
mov    %rax,(%r12)
test   %rax,%rax
je     1186 <base64_encode_alloc+0x66>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rax,%rdx
mov    %r13,%rcx
call   1040 <base64_encode@plt>
jmp    1189 <base64_encode_alloc+0x69>
mov    %r13,%rbx
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @base64_encode_alloc(i8* %0, i64 %1, i8** %2) {
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8**
%8 = alloca i64
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8** %2, i8*** %7
%9 = load i64, i64* %6
%10 = add i64 %9, 2
%11 = udiv i64 %10, 3
%12 = mul i64 %11, 4
%13 = add i64 1, %12
store i64 %13, i64* %8
%14 = load i64, i64* %6
%15 = load i64, i64* %8
%16 = icmp ugt i64 %14, %15
br i1 %16, label %17, label %19
17: ; preds = %3
%18 = load i8**, i8*** %7
store i8* null, i8** %18
store i64 0, i64* %4
br label %37
19: ; preds = %3
%20 = load i64, i64* %8
%21 = call i8* @malloc(i64 %20)
%22 = load i8**, i8*** %7
store i8* %21, i8** %22
%23 = load i8**, i8*** %7
%24 = load i8*, i8** %23
%25 = icmp ne i8* %24, null
br i1 %25, label %28, label %26
26: ; preds = %19
%27 = load i64, i64* %8
store i64 %27, i64* %4
br label %37
28: ; preds = %19
%29 = load i8*, i8** %5
%30 = load i64, i64* %6
%31 = load i8**, i8*** %7
%32 = load i8*, i8** %31
%33 = load i64, i64* %8
%34 = call i32 @base64_encode(i8* %29, i64 %30, i8* %32, i64 %33)
%35 = load i64, i64* %8
%36 = sub i64 %35, 1
store i64 %36, i64* %4
br label %37
37: ; preds = %28, %26, %17
%38 = load i64, i64* %4
ret i64 %38
}

LLVM IR (-O2):
define i64 @base64_encode_alloc(i8* %0, i64 %1, i8** nocapture writeonly %2) {
%4 = add i64 %1, 2
%5 = udiv i64 %4, 3
%6 = shl i64 %5, 2
%7 = or i64 %6, 1
%8 = icmp ult i64 %7, %1
br i1 %8, label %9, label %10
9: ; preds = %3
store i8* null, i8** %2
br label %15
10: ; preds = %3
%11 = tail call i8* @malloc(i64 %7)
store i8* %11, i8** %2
%12 = icmp eq i8* %11, null
br i1 %12, label %15, label %13
13: ; preds = %10
%14 = tail call i32 @base64_encode(i8* %0, i64 %1, i8* %11, i64 %7)
br label %15
15: ; preds = %10, %13, %9
%16 = phi i64 [ 0, %9 ], [ %6, %13 ], [ %7, %10 ]
ret i64 %16
}