#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 *, size_t);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
int base64_encode(unsigned char *source, size_t sourcelen, char *target,
                  size_t 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    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x18(%rbp),%rax
add    $0x2,%rax
mov    $0x3,%ecx
xor    %edx,%edx
div    %rcx
shl    $0x2,%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jbe    117b <base64_encode+0x4b>
movl   $0x0,-0x4(%rbp)
jmp    122f <base64_encode+0xff>
jmp    1180 <base64_encode+0x50>
cmpq   $0x3,-0x18(%rbp)
jb     11c1 <base64_encode+0x91>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <_base64_encode_triple@plt>
mov    -0x18(%rbp),%rax
sub    $0x3,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x20(%rbp)
jmp    1180 <base64_encode+0x50>
cmpq   $0x0,-0x18(%rbp)
jbe    1221 <base64_encode+0xf1>
lea    -0x2b(%rbp),%rdi
xor    %esi,%esi
mov    $0x3,%edx
call   1040 <memset@plt>
lea    -0x2b(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <memcpy@plt>
lea    -0x2b(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <_base64_encode_triple@plt>
mov    -0x20(%rbp),%rax
movb   $0x3d,0x3(%rax)
cmpq   $0x1,-0x18(%rbp)
jne    1215 <base64_encode+0xe5>
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
lea    0x2(%rsi),%rax
movabs $0xaaaaaaaaaaaaaaab,%rdx
aa aa aa
mul    %rdx
add    %rdx,%rdx
and    $0xfffffffffffffffc,%rdx
add    $0xffffffffffffffff,%rcx
xor    %eax,%eax
cmp    %rcx,%rdx
ja     11dc <base64_encode+0xac>
mov    %rsi,%r15
mov    %rdi,%rbx
cmp    $0x3,%rsi
jb     118d <base64_encode+0x5d>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <_base64_encode_triple@plt>
add    $0xfffffffffffffffd,%r15
add    $0x3,%rbx
add    $0x4,%r14
cmp    $0x2,%r15
ja     1170 <base64_encode+0x40>
test   %r15,%r15
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    %r15,%rdx
call   1050 <memcpy@plt>
mov    %r12,%rdi
mov    %r14,%rsi
call   1030 <_base64_encode_triple@plt>
movb   $0x3d,0x3(%r14)
cmp    $0x1,%r15
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, i64 %1, i8* %2, i64 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca [3 x i8]
store i8* %0, i8** %6
store i64 %1, i64* %7
store i8* %2, i8** %8
store i64 %3, i64* %9
%11 = load i64, i64* %7
%12 = add i64 %11, 2
%13 = udiv i64 %12, 3
%14 = mul i64 %13, 4
%15 = load i64, i64* %9
%16 = sub i64 %15, 1
%17 = icmp ugt i64 %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 i64, i64* %7
%22 = icmp uge i64 %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 i64, i64* %7
%28 = sub i64 %27, 3
store i64 %28, i64* %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 i64, i64* %7
%35 = icmp ugt i64 %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 i64, i64* %7
%42 = call i32 @memcpy(i8* %39, i8* %40, i64 %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 i64, i64* %7
%49 = icmp eq i64 %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, i64 %1, i8* %2, i64 %3) {
%5 = alloca [3 x i8]
%6 = add i64 %1, 2
%7 = udiv i64 %6, 3
%8 = shl i64 %7, 2
%9 = add i64 %3, -1
%10 = icmp ugt i64 %8, %9
br i1 %10, label %40, label %11
11: ; preds = %4
%12 = icmp ugt i64 %1, 2
br i1 %12, label %13, label %22
13: ; preds = %11, %13
%14 = phi i8* [ %20, %13 ], [ %2, %11 ]
%15 = phi i64 [ %18, %13 ], [ %1, %11 ]
%16 = phi i8* [ %19, %13 ], [ %0, %11 ]
%17 = tail call i32 @_base64_encode_triple(i8* %16, i8* %14)
%18 = add i64 %15, -3
%19 = getelementptr inbounds i8, i8* %16, i64 3
%20 = getelementptr inbounds i8, i8* %14, i64 4
%21 = icmp ugt i64 %18, 2
br i1 %21, label %13, label %22
22: ; preds = %13, %11
%23 = phi i8* [ %0, %11 ], [ %19, %13 ]
%24 = phi i64 [ %1, %11 ], [ %18, %13 ]
%25 = phi i8* [ %2, %11 ], [ %20, %13 ]
%26 = icmp eq i64 %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, i64 %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 i64 %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
}