#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 *, int);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
int Base64_encode(unsigned char *source, int sourcelen, char *target,
                  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
cltd
idiv   %ecx
shl    $0x2,%eax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jle    1171 <Base64_encode+0x41>
movl   $0x0,-0x4(%rbp)
jmp    121e <Base64_encode+0xee>
jmp    1176 <Base64_encode+0x46>
cmpl   $0x3,-0x14(%rbp)
jl     11b3 <Base64_encode+0x83>
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    1176 <Base64_encode+0x46>
cmpl   $0x0,-0x14(%rbp)
jle    1210 <Base64_encode+0xe0>
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    1204 <Base64_encode+0xd4>
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   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%r15d
lea    0x2(%r15),%eax
cltq
imul   $0x55555556,%rax,%rdx
mov    %rdx,%rax
shr    $0x3f,%rax
shr    $0x20,%rdx
add    %eax,%edx
shl    $0x2,%edx
xor    %eax,%eax
cmp    %ecx,%edx
jge    11e5 <Base64_encode+0xb5>
mov    %rdi,%rbx
cmp    $0x3,%r15d
jl     1196 <Base64_encode+0x66>
nopl   (%rax)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <base64_encode_triple@plt>
lea    -0x3(%r15),%ebp
add    $0x3,%rbx
add    $0x4,%r14
cmp    $0x5,%r15d
mov    %ebp,%r15d
ja     1170 <Base64_encode+0x40>
test   %ebp,%ebp
jg     119d <Base64_encode+0x6d>
jmp    11dc <Base64_encode+0xac>
mov    %r15d,%ebp
test   %ebp,%ebp
jle    11dc <Base64_encode+0xac>
lea    0x5(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x3,%edx
call   1040 <memset@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1050 <memcpy@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1030 <base64_encode_triple@plt>
movb   $0x3d,0x3(%r14)
cmp    $0x1,%ebp
jne    11d8 <Base64_encode+0xa8>
movb   $0x3d,0x2(%r14)
add    $0x4,%r14
movb   $0x0,(%r14)
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
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 nsw i32 %11, 2
%13 = sdiv i32 %12, 3
%14 = mul nsw i32 %13, 4
%15 = load i32, i32* %9
%16 = sub nsw i32 %15, 1
%17 = icmp sgt 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 sge 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 nsw 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 sgt 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 nsw i32 %1, 2
%7 = sdiv i32 %6, 3
%8 = shl nsw i32 %7, 2
%9 = icmp slt i32 %8, %3
br i1 %9, label %10, label %39
10: ; preds = %4
%11 = icmp sgt i32 %1, 2
br i1 %11, label %12, label %21
12: ; preds = %10, %12
%13 = phi i8* [ %19, %12 ], [ %2, %10 ]
%14 = phi i32 [ %17, %12 ], [ %1, %10 ]
%15 = phi i8* [ %18, %12 ], [ %0, %10 ]
%16 = tail call i32 @base64_encode_triple(i8* %15, i8* %13)
%17 = add nsw i32 %14, -3
%18 = getelementptr inbounds i8, i8* %15, i64 3
%19 = getelementptr inbounds i8, i8* %13, i64 4
%20 = icmp ugt i32 %14, 5
br i1 %20, label %12, label %21
21: ; preds = %12, %10
%22 = phi i8* [ %0, %10 ], [ %18, %12 ]
%23 = phi i32 [ %1, %10 ], [ %17, %12 ]
%24 = phi i8* [ %2, %10 ], [ %19, %12 ]
%25 = icmp sgt i32 %23, 0
br i1 %25, label %26, label %37
26: ; preds = %21
%27 = getelementptr inbounds [3 x i8], [3 x i8]* %5, i64 0, i64 0
%28 = call i32 @memset(i8* %27, i32 0, i32 3)
%29 = call i32 @memcpy(i8* %27, i8* %22, i32 %23)
%30 = call i32 @base64_encode_triple(i8* %27, i8* %24)
%31 = getelementptr inbounds i8, i8* %24, i64 3
store i8 61, i8* %31
%32 = icmp eq i32 %23, 1
br i1 %32, label %33, label %35
33: ; preds = %26
%34 = getelementptr inbounds i8, i8* %24, i64 2
store i8 61, i8* %34
br label %35
35: ; preds = %33, %26
%36 = getelementptr inbounds i8, i8* %24, i64 4
br label %37
37: ; preds = %35, %21
%38 = phi i8* [ %36, %35 ], [ %24, %21 ]
store i8 0, i8* %38
br label %39
39: ; preds = %4, %37
%40 = phi i32 [ 1, %37 ], [ 0, %4 ]
ret i32 %40
}