#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 /*<<< orphan*/ base64_encode_binary(char *, unsigned char *, size_t);
size_t base64_encoded_size(size_t);
scalar_t__ malloc(int);
size_t strlen(char const *);
char *base64_encode(const char *in, size_t size) {
  char *out;
  size_t outlen;
  if (in == NULL)
    return (NULL);
  if (size == 0)
    size = strlen(in);
  outlen = base64_encoded_size(size);
  if ((out = (char *)malloc(sizeof(char) * (outlen + 1))) == NULL)
    return (NULL);
  base64_encode_binary(out, (unsigned char *)in, size);
  return (out);
}


x86 assembly (clang, -O0):
<base64_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1168 <base64_encode+0x28>
movq   $0x0,-0x8(%rbp)
jmp    11d4 <base64_encode+0x94>
cmpq   $0x0,-0x18(%rbp)
jne    1180 <base64_encode+0x40>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <base64_encoded_size@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11bb <base64_encode+0x7b>
movq   $0x0,-0x8(%rbp)
jmp    11d4 <base64_encode+0x94>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <base64_encode_binary@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<base64_encode>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     1188 <base64_encode+0x48>
mov    %rsi,%r15
mov    %rdi,%r14
test   %rsi,%rsi
jne    1160 <base64_encode+0x20>
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %rax,%r15
mov    %r15,%rdi
call   1050 <base64_encoded_size@plt>
lea    0x1(%rax),%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1188 <base64_encode+0x48>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <base64_encode_binary@plt>
jmp    118a <base64_encode+0x4a>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @base64_encode(i8* %0, i64 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %18, label %4
4: ; preds = %2
%5 = icmp eq i64 %1, 0
br i1 %5, label %6, label %8
6: ; preds = %4
%7 = tail call i64 @strlen(i8* %0)
br label %8
8: ; preds = %6, %4
%9 = phi i64 [ %7, %6 ], [ %1, %4 ]
%10 = tail call i64 @base64_encoded_size(i64 %9)
%11 = trunc i64 %10 to i32
%12 = add i32 %11, 1
%13 = tail call i64 @malloc(i32 %12)
%14 = inttoptr i64 %13 to i8*
%15 = icmp eq i64 %13, 0
br i1 %15, label %18, label %16
16: ; preds = %8
%17 = tail call i32 @base64_encode_binary(i8* %14, i8* %0, i64 %9)
br label %18
18: ; preds = %8, %2, %16
%19 = phi i8* [ %14, %16 ], [ null, %2 ], [ null, %8 ]
ret i8* %19
}