#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 *, int, char *, int);
char *malloc(int);
int base64_encode_alloc(const char *in, int inlen, char **out) {
  int 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    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x14(%rbp),%eax
add    $0x2,%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
shl    $0x2,%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x24(%rbp),%eax
jle    116d <base64_encode_alloc+0x4d>
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11b7 <base64_encode_alloc+0x97>
mov    -0x24(%rbp),%edi
call   1030 <malloc@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1198 <base64_encode_alloc+0x78>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b7 <base64_encode_alloc+0x97>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
mov    -0x24(%rbp),%ecx
call   1040 <base64_encode@plt>
mov    -0x24(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<base64_encode_alloc>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r15
mov    %esi,%r12d
lea    0x2(%r12),%eax
cltq
imul   $0x55555556,%rax,%rbx
mov    %rbx,%rax
shr    $0x3f,%rax
shr    $0x20,%rbx
add    %eax,%ebx
lea    0x1(,%rbx,4),%ebp
cmp    %esi,%ebp
jge    115f <base64_encode_alloc+0x3f>
movq   $0x0,(%r15)
xor    %ebp,%ebp
jmp    1186 <base64_encode_alloc+0x66>
mov    %rdi,%r14
mov    %ebp,%edi
call   1030 <malloc@plt>
mov    %rax,(%r15)
test   %rax,%rax
je     1186 <base64_encode_alloc+0x66>
shl    $0x2,%ebx
mov    %r14,%rdi
mov    %r12d,%esi
mov    %rax,%rdx
mov    %ebp,%ecx
call   1040 <base64_encode@plt>
mov    %ebx,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @base64_encode_alloc(i8* %0, i32 %1, i8** nocapture writeonly %2) {
%4 = add nsw i32 %1, 2
%5 = sdiv i32 %4, 3
%6 = shl nsw i32 %5, 2
%7 = or i32 %6, 1
%8 = icmp slt i32 %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(i32 %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, i32 %1, i8* %11, i32 %7)
br label %15
15: ; preds = %10, %13, %9
%16 = phi i32 [ 0, %9 ], [ %6, %13 ], [ %7, %10 ]
ret i32 %16
}