#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 base64_length_for_data_length(int);
int /*<<< orphan*/ base64encode_block(unsigned char const *, char *, int, int);
char *malloc(int);
int num_base64_padding_chars_for_data_length(int);
char *base64_encode(const unsigned char *data, int data_length) {
  int base64len = base64_length_for_data_length(data_length);
  char *base64 = malloc(base64len + 1);
  base64[base64len] = 0;
  int numPadingChars = num_base64_padding_chars_for_data_length(data_length);
  int numBlocks = base64len / 4;
  int i;
  for (i = 0; i < numBlocks; i++) {
    base64encode_block(data, base64, i,
                       ((i == (numBlocks - 1)) ? numPadingChars : 0));
  }
  return base64;
}


x86 assembly (clang, -O0):
<base64_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1060 <base64_length_for_data_length@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
add    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0xc(%rbp),%edi
call   1050 <num_base64_padding_chars_for_data_length@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    11fe <base64_encode+0xbe>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x20(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    11d3 <base64_encode+0x93>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
jmp    11dd <base64_encode+0x9d>
xor    %eax,%eax
mov    %eax,-0x3c(%rbp)
jmp    11dd <base64_encode+0x9d>
mov    -0x28(%rbp),%edx
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdi
mov    -0x3c(%rbp),%ecx
call   1030 <base64encode_block@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1195 <base64_encode+0x55>
mov    -0x18(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<base64_encode>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r12d
mov    %rdi,%r14
mov    %esi,%edi
call   1060 <base64_length_for_data_length@plt>
mov    %eax,%ebp
lea    0x1(%rbp),%edi
call   1040 <malloc@plt>
mov    %rax,%r15
movslq %ebp,%rbx
movb   $0x0,(%rax,%rbx,1)
mov    %r12d,%edi
call   1050 <num_base64_padding_chars_for_data_length@plt>
lea    0x3(%rbx),%ebp
test   %ebx,%ebx
cmovns %ebx,%ebp
cmp    $0x4,%ebx
jl     11b5 <base64_encode+0x75>
mov    %eax,%r12d
sar    $0x2,%ebp
neg    %ebp
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
lea    (%rbx,%rbp,1),%eax
cmp    $0xffffffff,%eax
mov    $0x0,%ecx
cmove  %r12d,%ecx
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1030 <base64encode_block@plt>
add    $0x1,%ebx
mov    %ebp,%eax
add    %ebx,%eax
jne    1190 <base64_encode+0x50>
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @base64_encode(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%10 = load i32, i32* %4
%11 = call i32 @base64_length_for_data_length(i32 %10)
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = add nsw i32 %12, 1
%14 = call i8* @malloc(i32 %13)
store i8* %14, i8** %6
%15 = load i8*, i8** %6
%16 = load i32, i32* %5
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %15, i64 %17
store i8 0, i8* %18
%19 = load i32, i32* %4
%20 = call i32 @num_base64_padding_chars_for_data_length(i32 %19)
store i32 %20, i32* %7
%21 = load i32, i32* %5
%22 = sdiv i32 %21, 4
store i32 %22, i32* %8
store i32 0, i32* %9
br label %23
23: ; preds = %41, %2
%24 = load i32, i32* %9
%25 = load i32, i32* %8
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %44
27: ; preds = %23
%28 = load i8*, i8** %3
%29 = load i8*, i8** %6
%30 = load i32, i32* %9
%31 = load i32, i32* %9
%32 = load i32, i32* %8
%33 = sub nsw i32 %32, 1
%34 = icmp eq i32 %31, %33
br i1 %34, label %35, label %37
35: ; preds = %27
%36 = load i32, i32* %7
br label %38
37: ; preds = %27
br label %38
38: ; preds = %37, %35
%39 = phi i32 [ %36, %35 ], [ 0, %37 ]
%40 = call i32 @base64encode_block(i8* %28, i8* %29, i32 %30, i32 %39)
br label %41
41: ; preds = %38
%42 = load i32, i32* %9
%43 = add nsw i32 %42, 1
store i32 %43, i32* %9
br label %23
44: ; preds = %23
%45 = load i8*, i8** %6
ret i8* %45
}

LLVM IR (-O2):
define i8* @base64_encode(i8* %0, i32 %1) {
%3 = tail call i32 @base64_length_for_data_length(i32 %1)
%4 = add nsw i32 %3, 1
%5 = tail call i8* @malloc(i32 %4)
%6 = sext i32 %3 to i64
%7 = getelementptr inbounds i8, i8* %5, i64 %6
store i8 0, i8* %7
%8 = tail call i32 @num_base64_padding_chars_for_data_length(i32 %1)
%9 = sdiv i32 %3, 4
%10 = icmp sgt i32 %3, 3
br i1 %10, label %11, label %20
11: ; preds = %2
%12 = add nsw i32 %9, -1
br label %13
13: ; preds = %11, %13
%14 = phi i32 [ 0, %11 ], [ %18, %13 ]
%15 = icmp eq i32 %14, %12
%16 = select i1 %15, i32 %8, i32 0
%17 = tail call i32 @base64encode_block(i8* %0, i8* %5, i32 %14, i32 %16)
%18 = add nuw nsw i32 %14, 1
%19 = icmp eq i32 %18, %9
br i1 %19, label %20, label %13
20: ; preds = %13, %2
ret i8* %5
}