#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 /*<<< orphan*/ temp;

/* Variables and functions */
int /*<<< orphan*/ _base64_encode_triple(unsigned char *, char *);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char *, unsigned int);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
int base64_encode(unsigned char *source, unsigned int sourcelen, char *target,
                  unsigned int targetlen) {
  if ((sourcelen + 2) / 3 * 4 > targetlen - 1)
    return 0;
  while (sourcelen >= 3) {
    _base64_encode_triple(source, target);
    sourcelen -= 3;
    source += 3;
    target += 4;
  }
  if (sourcelen > 0) {
    unsigned char temp[3];
    memset(temp, 0, sizeof(temp));
    memcpy(temp, source, sourcelen);
    _base64_encode_triple(temp, target);
    target[3] = '=';
    if (sourcelen == 1)
      target[2] = '=';
    target += 4;
  }
  target[0] = 0;
  return 1;
}


x86 assembly (clang, -O0):
<base64_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x14(%rbp),%eax
add    $0x2,%eax
mov    $0x3,%ecx
xor    %edx,%edx
div    %ecx
shl    $0x2,%eax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jbe    1172 <base64_encode+0x42>
movl   $0x0,-0x4(%rbp)
jmp    121f <base64_encode+0xef>
jmp    1177 <base64_encode+0x47>
cmpl   $0x3,-0x14(%rbp)
jb     11b4 <base64_encode+0x84>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <_base64_encode_triple@plt>
mov    -0x14(%rbp),%eax
sub    $0x3,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x20(%rbp)
jmp    1177 <base64_encode+0x47>
cmpl   $0x0,-0x14(%rbp)
jbe    1211 <base64_encode+0xe1>
lea    -0x27(%rbp),%rdi
xor    %esi,%esi
mov    $0x3,%edx
call   1040 <memset@plt>
lea    -0x27(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <memcpy@plt>
lea    -0x27(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <_base64_encode_triple@plt>
mov    -0x20(%rbp),%rax
movb   $0x3d,0x3(%rax)
cmpl   $0x1,-0x14(%rbp)
jne    1205 <base64_encode+0xd5>
mov    -0x20(%rbp),%rax
movb   $0x3d,0x2(%rax)
mov    -0x20(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<base64_encode>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%r15d
lea    0x2(%r15),%eax
mov    $0xaaaaaaab,%edx
imul   %rax,%rdx
shr    $0x20,%rdx
add    %edx,%edx
and    $0xfffffffc,%edx
add    $0xffffffff,%ecx
xor    %eax,%eax
cmp    %ecx,%edx
ja     11dc <base64_encode+0xac>
mov    %rdi,%rbx
cmp    $0x3,%r15d
jb     118d <base64_encode+0x5d>
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <_base64_encode_triple@plt>
add    $0xfffffffd,%r15d
add    $0x3,%rbx
add    $0x4,%r14
cmp    $0x2,%r15d
ja     1170 <base64_encode+0x40>
test   %r15d,%r15d
je     11d3 <base64_encode+0xa3>
lea    0x5(%rsp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x3,%edx
call   1040 <memset@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1050 <memcpy@plt>
mov    %r12,%rdi
mov    %r14,%rsi
call   1030 <_base64_encode_triple@plt>
movb   $0x3d,0x3(%r14)
cmp    $0x1,%r15d
jne    11cf <base64_encode+0x9f>
movb   $0x3d,0x2(%r14)
add    $0x4,%r14
movb   $0x0,(%r14)
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @base64_encode(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca [3 x i8]
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%11 = load i32, i32* %7
%12 = add i32 %11, 2
%13 = udiv i32 %12, 3
%14 = mul i32 %13, 4
%15 = load i32, i32* %9
%16 = sub i32 %15, 1
%17 = icmp ugt i32 %14, %16
br i1 %17, label %18, label %19
18: ; preds = %4
store i32 0, i32* %5
br label %59
19: ; preds = %4
br label %20
20: ; preds = %23, %19
%21 = load i32, i32* %7
%22 = icmp uge i32 %21, 3
br i1 %22, label %23, label %33
23: ; preds = %20
%24 = load i8*, i8** %6
%25 = load i8*, i8** %8
%26 = call i32 @_base64_encode_triple(i8* %24, i8* %25)
%27 = load i32, i32* %7
%28 = sub i32 %27, 3
store i32 %28, i32* %7
%29 = load i8*, i8** %6
%30 = getelementptr inbounds i8, i8* %29, i64 3
store i8* %30, i8** %6
%31 = load i8*, i8** %8
%32 = getelementptr inbounds i8, i8* %31, i64 4
store i8* %32, i8** %8
br label %20
33: ; preds = %20
%34 = load i32, i32* %7
%35 = icmp ugt i32 %34, 0
br i1 %35, label %36, label %56
36: ; preds = %33
%37 = getelementptr inbounds [3 x i8], [3 x i8]* %10, i64 0, i64 0
%38 = call i32 @memset(i8* %37, i32 0, i32 3)
%39 = getelementptr inbounds [3 x i8], [3 x i8]* %10, i64 0, i64 0
%40 = load i8*, i8** %6
%41 = load i32, i32* %7
%42 = call i32 @memcpy(i8* %39, i8* %40, i32 %41)
%43 = getelementptr inbounds [3 x i8], [3 x i8]* %10, i64 0, i64 0
%44 = load i8*, i8** %8
%45 = call i32 @_base64_encode_triple(i8* %43, i8* %44)
%46 = load i8*, i8** %8
%47 = getelementptr inbounds i8, i8* %46, i64 3
store i8 61, i8* %47
%48 = load i32, i32* %7
%49 = icmp eq i32 %48, 1
br i1 %49, label %50, label %53
50: ; preds = %36
%51 = load i8*, i8** %8
%52 = getelementptr inbounds i8, i8* %51, i64 2
store i8 61, i8* %52
br label %53
53: ; preds = %50, %36
%54 = load i8*, i8** %8
%55 = getelementptr inbounds i8, i8* %54, i64 4
store i8* %55, i8** %8
br label %56
56: ; preds = %53, %33
%57 = load i8*, i8** %8
%58 = getelementptr inbounds i8, i8* %57, i64 0
store i8 0, i8* %58
store i32 1, i32* %5
br label %59
59: ; preds = %56, %18
%60 = load i32, i32* %5
ret i32 %60
}

LLVM IR (-O2):
define i32 @base64_encode(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca [3 x i8]
%6 = add i32 %1, 2
%7 = udiv i32 %6, 3
%8 = shl i32 %7, 2
%9 = add i32 %3, -1
%10 = icmp ugt i32 %8, %9
br i1 %10, label %40, label %11
11: ; preds = %4
%12 = icmp ugt i32 %1, 2
br i1 %12, label %13, label %22
13: ; preds = %11, %13
%14 = phi i8* [ %20, %13 ], [ %2, %11 ]
%15 = phi i32 [ %18, %13 ], [ %1, %11 ]
%16 = phi i8* [ %19, %13 ], [ %0, %11 ]
%17 = tail call i32 @_base64_encode_triple(i8* %16, i8* %14)
%18 = add i32 %15, -3
%19 = getelementptr inbounds i8, i8* %16, i64 3
%20 = getelementptr inbounds i8, i8* %14, i64 4
%21 = icmp ugt i32 %18, 2
br i1 %21, label %13, label %22
22: ; preds = %13, %11
%23 = phi i8* [ %0, %11 ], [ %19, %13 ]
%24 = phi i32 [ %1, %11 ], [ %18, %13 ]
%25 = phi i8* [ %2, %11 ], [ %20, %13 ]
%26 = icmp eq i32 %24, 0
br i1 %26, label %38, label %27
27: ; preds = %22
%28 = getelementptr inbounds [3 x i8], [3 x i8]* %5, i64 0, i64 0
%29 = call i32 @memset(i8* %28, i32 0, i32 3)
%30 = call i32 @memcpy(i8* %28, i8* %23, i32 %24)
%31 = call i32 @_base64_encode_triple(i8* %28, i8* %25)
%32 = getelementptr inbounds i8, i8* %25, i64 3
store i8 61, i8* %32
%33 = icmp eq i32 %24, 1
br i1 %33, label %34, label %36
34: ; preds = %27
%35 = getelementptr inbounds i8, i8* %25, i64 2
store i8 61, i8* %35
br label %36
36: ; preds = %34, %27
%37 = getelementptr inbounds i8, i8* %25, i64 4
br label %38
38: ; preds = %36, %22
%39 = phi i8* [ %37, %36 ], [ %25, %22 ]
store i8 0, i8* %39
br label %40
40: ; preds = %4, %38
%41 = phi i32 [ 1, %38 ], [ 0, %4 ]
ret i32 %41
}