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

/* Variables and functions */
int UINT_MAX;
char *malloc(unsigned int);
scalar_t__ mz_base64_encode_close(int /*<<< orphan*/, char *, int *, int *);
int mz_base64_encode_step(unsigned char const *, unsigned int,
                          int /*<<< orphan*/, char *, int *, int *);
char *mz_base64_encode(const unsigned char *data, unsigned int len) {
  char *out;
  int state = 0, outlen;
  int save = 0;
  if (!data)
    return NULL;
  if (len <= 0)
    return NULL;
  if (len >= ((UINT_MAX - 1) / 4 - 1) * 3)
    return NULL;
  out = malloc((len / 3 + 1) * 4 + 1);
  outlen = mz_base64_encode_step(data, len, 0, out, &state, &save);
  outlen += mz_base64_encode_close(0, out + outlen, &state, &save);
  out[outlen] = '\0';
  return (char *)out;
}


x86 assembly (clang, -O0):
<mz_base64_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1165 <mz_base64_encode+0x35>
movq   $0x0,-0x8(%rbp)
jmp    122e <mz_base64_encode+0xfe>
cmpl   $0x0,-0x14(%rbp)
ja     117c <mz_base64_encode+0x4c>
movq   $0x0,-0x8(%rbp)
jmp    122e <mz_base64_encode+0xfe>
mov    -0x14(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e5f(%rip),%rax
mov    (%rax),%eax
sub    $0x1,%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x30(%rbp),%eax
sub    $0x1,%ecx
imul   $0x3,%ecx,%ecx
cmp    %ecx,%eax
jb     11b6 <mz_base64_encode+0x86>
movq   $0x0,-0x8(%rbp)
jmp    122e <mz_base64_encode+0xfe>
mov    -0x14(%rbp),%eax
mov    $0x3,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,%edi
add    $0x1,%edi
shl    $0x2,%edi
add    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rcx
xor    %edx,%edx
lea    -0x24(%rbp),%r8
lea    -0x2c(%rbp),%r9
call   1050 <mz_base64_encode_step@plt>
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rsi
movslq -0x28(%rbp),%rax
add    %rax,%rsi
xor    %edi,%edi
lea    -0x24(%rbp),%rdx
lea    -0x2c(%rbp),%rcx
call   1030 <mz_base64_encode_close@plt>
mov    %rax,%rcx
movslq -0x28(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mz_base64_encode>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
movl   $0x0,0xc(%rsp)
movl   $0x0,0x8(%rsp)
xor    %ebx,%ebx
test   %rdi,%rdi
je     11e5 <mz_base64_encode+0xb5>
mov    %esi,%ebp
test   %esi,%esi
je     11e5 <mz_base64_encode+0xb5>
mov    0x2e80(%rip),%rax
mov    (%rax),%eax
lea    -0x1(%rax),%ecx
add    $0x2,%eax
test   %ecx,%ecx
cmovns %ecx,%eax
sar    $0x2,%eax
lea    (%rax,%rax,2),%eax
add    $0xfffffffd,%eax
cmp    %ebp,%eax
jbe    11e3 <mz_base64_encode+0xb3>
mov    %rdi,%r14
mov    %ebp,%eax
mov    $0xaaaaaaab,%ecx
imul   %rax,%rcx
shr    $0x20,%rcx
and    $0xfffffffe,%ecx
lea    (%rcx,%rcx,1),%edi
add    $0x5,%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
lea    0xc(%rsp),%r15
lea    0x8(%rsp),%r12
mov    %r14,%rdi
mov    %ebp,%esi
xor    %edx,%edx
mov    %rax,%rcx
mov    %r15,%r8
mov    %r12,%r9
call   1050 <mz_base64_encode_step@plt>
movslq %eax,%rbp
lea    (%rbx,%rbp,1),%rsi
xor    %edi,%edi
mov    %r15,%rdx
mov    %r12,%rcx
call   1030 <mz_base64_encode_close@plt>
add    %eax,%ebp
movslq %ebp,%rax
movb   $0x0,(%rbx,%rax,1)
jmp    11e5 <mz_base64_encode+0xb5>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@UINT_MAX = global i32 0
define i8* @mz_base64_encode(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %7
store i32 0, i32* %9
%10 = load i8*, i8** %4
%11 = icmp ne i8* %10, null
br i1 %11, label %13, label %12
12: ; preds = %2
store i8* null, i8** %3
br label %51
13: ; preds = %2
%14 = load i32, i32* %5
%15 = icmp ule i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %13
store i8* null, i8** %3
br label %51
17: ; preds = %13
%18 = load i32, i32* %5
%19 = load i32, i32* @UINT_MAX
%20 = sub nsw i32 %19, 1
%21 = sdiv i32 %20, 4
%22 = sub nsw i32 %21, 1
%23 = mul nsw i32 %22, 3
%24 = icmp uge i32 %18, %23
br i1 %24, label %25, label %26
25: ; preds = %17
store i8* null, i8** %3
br label %51
26: ; preds = %17
%27 = load i32, i32* %5
%28 = udiv i32 %27, 3
%29 = add i32 %28, 1
%30 = mul i32 %29, 4
%31 = add i32 %30, 1
%32 = call i8* @malloc(i32 %31)
store i8* %32, i8** %6
%33 = load i8*, i8** %4
%34 = load i32, i32* %5
%35 = load i8*, i8** %6
%36 = call i32 @mz_base64_encode_step(i8* %33, i32 %34, i32 0, i8* %35, i32* %7, i32* %9)
store i32 %36, i32* %8
%37 = load i8*, i8** %6
%38 = load i32, i32* %8
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
%41 = call i64 @mz_base64_encode_close(i32 0, i8* %40, i32* %7, i32* %9)
%42 = load i32, i32* %8
%43 = sext i32 %42 to i64
%44 = add nsw i64 %43, %41
%45 = trunc i64 %44 to i32
store i32 %45, i32* %8
%46 = load i8*, i8** %6
%47 = load i32, i32* %8
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %46, i64 %48
store i8 0, i8* %49
%50 = load i8*, i8** %6
store i8* %50, i8** %3
br label %51
51: ; preds = %26, %25, %16, %12
%52 = load i8*, i8** %3
ret i8* %52
}

LLVM IR (-O2):
@UINT_MAX = global i32 0
define i8* @mz_base64_encode(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%6 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%7 = icmp eq i8* %0, null
%8 = icmp eq i32 %1, 0
%9 = or i1 %7, %8
br i1 %9, label %30, label %10
10: ; preds = %2
%11 = load i32, i32* @UINT_MAX
%12 = add nsw i32 %11, -1
%13 = sdiv i32 %12, 4
%14 = mul nsw i32 %13, 3
%15 = add nsw i32 %14, -3
%16 = icmp ugt i32 %15, %1
br i1 %16, label %17, label %30
17: ; preds = %10
%18 = udiv i32 %1, 3
%19 = shl i32 %18, 2
%20 = add i32 %19, 5
%21 = tail call i8* @malloc(i32 %20)
%22 = call i32 @mz_base64_encode_step(i8* %0, i32 %1, i32 0, i8* %21, i32* %3, i32* %4)
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = call i64 @mz_base64_encode_close(i32 0, i8* %24, i32* %3, i32* %4)
%26 = trunc i64 %25 to i32
%27 = add i32 %22, %26
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %21, i64 %28
store i8 0, i8* %29
br label %30
30: ; preds = %10, %2, %17
%31 = phi i8* [ %21, %17 ], [ null, %2 ], [ null, %10 ]
ret i8* %31
}