#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*/ error_msg_and_die(char *);
char *base64enc(char *p, char *buf, int len) {
  char al[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
              "0123456789+/";
  char *s = buf;
  while (*p) {
    if (s >= buf + len - 4)
      error_msg_and_die("buffer overflow");
    *(s++) = al[(*p >> 2) & 0x3F];
    *(s++) = al[((*p << 4) & 0x30) | ((*(p + 1) >> 4) & 0x0F)];
    *s = *(s + 1) = '=';
    *(s + 2) = 0;
    if (!*(++p))
      break;
    *(s++) = al[((*p << 2) & 0x3C) | ((*(p + 1) >> 6) & 0x03)];
    if (!*(++p))
      break;
    *(s++) = al[*(p++) & 0x3F];
  }
  return buf;
}


x86 assembly (clang, -O0):
<base64enc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x60(%rbp),%rdi
lea    0xec2(%rip),%rsi
mov    $0x41,%edx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x8(%rbp),%rax
cmpb   $0x0,(%rax)
je     1295 <base64enc+0x175>
mov    -0x68(%rbp),%rax
mov    -0x10(%rbp),%rcx
movslq -0x14(%rbp),%rdx
add    %rdx,%rcx
add    $0xfffffffffffffffc,%rcx
cmp    %rcx,%rax
jb     1185 <base64enc+0x65>
lea    0xec1(%rip),%rdi
call   1030 <error_msg_and_die@plt>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
sar    $0x2,%eax
and    $0x3f,%eax
cltq
mov    -0x60(%rbp,%rax,1),%cl
mov    -0x68(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x68(%rbp)
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
shl    $0x4,%eax
and    $0x30,%eax
mov    -0x8(%rbp),%rcx
movsbl 0x1(%rcx),%ecx
sar    $0x4,%ecx
and    $0xf,%ecx
or     %ecx,%eax
cltq
mov    -0x60(%rbp,%rax,1),%cl
mov    -0x68(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x68(%rbp)
mov    %cl,(%rax)
mov    -0x68(%rbp),%rax
movb   $0x3d,0x1(%rax)
mov    -0x68(%rbp),%rax
movb   $0x3d,(%rax)
mov    -0x68(%rbp),%rax
movb   $0x0,0x2(%rax)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
cmpb   $0x0,0x1(%rax)
jne    1212 <base64enc+0xf2>
jmp    1295 <base64enc+0x175>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
shl    $0x2,%eax
and    $0x3c,%eax
mov    -0x8(%rbp),%rcx
movsbl 0x1(%rcx),%ecx
sar    $0x6,%ecx
and    $0x3,%ecx
or     %ecx,%eax
cltq
mov    -0x60(%rbp,%rax,1),%cl
mov    -0x68(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x68(%rbp)
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
cmpb   $0x0,0x1(%rax)
jne    1264 <base64enc+0x144>
jmp    1295 <base64enc+0x175>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movsbl (%rax),%eax
and    $0x3f,%eax
cltq
mov    -0x60(%rbp,%rax,1),%cl
mov    -0x68(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x68(%rbp)
mov    %cl,(%rax)
jmp    1150 <base64enc+0x30>
mov    -0x10(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<base64enc>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    (%rdi),%al
test   %al,%al
je     11e1 <base64enc+0xd1>
mov    %rdi,%rbx
movslq %edx,%rcx
lea    (%r14,%rcx,1),%r12
add    $0xfffffffffffffffc,%r12
add    $0x3,%rbx
lea    0xebf(%rip),%r13
lea    0xef9(%rip),%r15
mov    %r14,%rbp
nopl   0x0(%rax,%rax,1)
cmp    %r12,%rbp
jb     1161 <base64enc+0x51>
mov    %r15,%rdi
call   1030 <error_msg_and_die@plt>
movzbl -0x3(%rbx),%eax
movzbl %al,%eax
shr    $0x2,%rax
movzbl (%rax,%r13,1),%eax
mov    %al,0x0(%rbp)
movzbl -0x3(%rbx),%eax
shl    $0x4,%eax
and    $0x30,%eax
movzbl -0x2(%rbx),%ecx
shr    $0x4,%rcx
or     %rax,%rcx
movzbl 0x0(%r13,%rcx,1),%eax
mov    %al,0x1(%rbp)
movw   $0x3d3d,0x2(%rbp)
movb   $0x0,0x4(%rbp)
movsbq -0x2(%rbx),%rax
test   %rax,%rax
je     11e1 <base64enc+0xd1>
and    $0xf,%eax
movzbl -0x1(%rbx),%ecx
shr    $0x6,%rcx
lea    (%rcx,%rax,4),%rax
movzbl 0x0(%r13,%rax,1),%eax
mov    %al,0x2(%rbp)
movzbl -0x1(%rbx),%eax
test   %rax,%rax
je     11e1 <base64enc+0xd1>
and    $0x3f,%eax
movzbl (%rax,%r13,1),%eax
mov    %al,0x3(%rbp)
movzbl (%rbx),%eax
add    $0x3,%rbx
add    $0x4,%rbp
test   %al,%al
jne    1150 <base64enc+0x40>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.base64enc.al = [65 x i8] c"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\00"
@.str = [16 x i8] c"buffer overflow\00"
define i8* @base64enc(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [65 x i8]
%8 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = bitcast [65 x i8]* %7 to i8*
%10 = load i8*, i8** %5
store i8* %10, i8** %8
br label %11
11: ; preds = %86, %3
%12 = load i8*, i8** %4
%13 = load i8, i8* %12
%14 = icmp ne i8 %13, 0
br i1 %14, label %15, label %97
15: ; preds = %11
%16 = load i8*, i8** %8
%17 = load i8*, i8** %5
%18 = load i32, i32* %6
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = getelementptr inbounds i8, i8* %20, i64 -4
%22 = icmp uge i8* %16, %21
br i1 %22, label %23, label %25
23: ; preds = %15
%24 = call i32 @error_msg_and_die(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %25
25: ; preds = %23, %15
%26 = load i8*, i8** %4
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = ashr i32 %28, 2
%30 = and i32 %29, 63
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds [65 x i8], [65 x i8]* %7, i64 0, i64 %31
%33 = load i8, i8* %32
%34 = load i8*, i8** %8
%35 = getelementptr inbounds i8, i8* %34, i32 1
store i8* %35, i8** %8
store i8 %33, i8* %34
%36 = load i8*, i8** %4
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = shl i32 %38, 4
%40 = and i32 %39, 48
%41 = load i8*, i8** %4
%42 = getelementptr inbounds i8, i8* %41, i64 1
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = ashr i32 %44, 4
%46 = and i32 %45, 15
%47 = or i32 %40, %46
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds [65 x i8], [65 x i8]* %7, i64 0, i64 %48
%50 = load i8, i8* %49
%51 = load i8*, i8** %8
%52 = getelementptr inbounds i8, i8* %51, i32 1
store i8* %52, i8** %8
store i8 %50, i8* %51
%53 = load i8*, i8** %8
%54 = getelementptr inbounds i8, i8* %53, i64 1
store i8 61, i8* %54
%55 = load i8*, i8** %8
store i8 61, i8* %55
%56 = load i8*, i8** %8
%57 = getelementptr inbounds i8, i8* %56, i64 2
store i8 0, i8* %57
%58 = load i8*, i8** %4
%59 = getelementptr inbounds i8, i8* %58, i32 1
store i8* %59, i8** %4
%60 = load i8, i8* %59
%61 = icmp ne i8 %60, 0
br i1 %61, label %63, label %62
62: ; preds = %25
br label %97
63: ; preds = %25
%64 = load i8*, i8** %4
%65 = load i8, i8* %64
%66 = sext i8 %65 to i32
%67 = shl i32 %66, 2
%68 = and i32 %67, 60
%69 = load i8*, i8** %4
%70 = getelementptr inbounds i8, i8* %69, i64 1
%71 = load i8, i8* %70
%72 = sext i8 %71 to i32
%73 = ashr i32 %72, 6
%74 = and i32 %73, 3
%75 = or i32 %68, %74
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds [65 x i8], [65 x i8]* %7, i64 0, i64 %76
%78 = load i8, i8* %77
%79 = load i8*, i8** %8
%80 = getelementptr inbounds i8, i8* %79, i32 1
store i8* %80, i8** %8
store i8 %78, i8* %79
%81 = load i8*, i8** %4
%82 = getelementptr inbounds i8, i8* %81, i32 1
store i8* %82, i8** %4
%83 = load i8, i8* %82
%84 = icmp ne i8 %83, 0
br i1 %84, label %86, label %85
85: ; preds = %63
br label %97
86: ; preds = %63
%87 = load i8*, i8** %4
%88 = getelementptr inbounds i8, i8* %87, i32 1
store i8* %88, i8** %4
%89 = load i8, i8* %87
%90 = sext i8 %89 to i32
%91 = and i32 %90, 63
%92 = sext i32 %91 to i64
%93 = getelementptr inbounds [65 x i8], [65 x i8]* %7, i64 0, i64 %92
%94 = load i8, i8* %93
%95 = load i8*, i8** %8
%96 = getelementptr inbounds i8, i8* %95, i32 1
store i8* %96, i8** %8
store i8 %94, i8* %95
br label %11
97: ; preds = %85, %62, %11
%98 = load i8*, i8** %5
ret i8* %98
}

LLVM IR (-O2):
@__const.base64enc.al = [65 x i8] c"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\00"
@.str = [16 x i8] c"buffer overflow\00"
define i8* @base64enc(i8* nocapture %0, i8* returned writeonly %1, i32 %2) {
%4 = load i8, i8* %0
%5 = icmp eq i8 %4, 0
br i1 %5, label %62, label %6
6: ; preds = %3
%7 = sext i32 %2 to i64
%8 = getelementptr inbounds i8, i8* %1, i64 %7
%9 = getelementptr inbounds i8, i8* %8, i64 -4
br label %10
10: ; preds = %6, %54
%11 = phi i8 [ %4, %6 ], [ %60, %54 ]
%12 = phi i8* [ %1, %6 ], [ %38, %54 ]
%13 = phi i8* [ %0, %6 ], [ %55, %54 ]
%14 = icmp ult i8* %12, %9
br i1 %14, label %18, label %15
15: ; preds = %10
%16 = tail call i32 @error_msg_and_die(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%17 = load i8, i8* %13
br label %18
18: ; preds = %15, %10
%19 = phi i8 [ %17, %15 ], [ %11, %10 ]
%20 = lshr i8 %19, 2
%21 = zext i8 %20 to i64
%22 = getelementptr inbounds [65 x i8], [65 x i8]* @__const.base64enc.al, i64 0, i64 %21
%23 = load i8, i8* %22
%24 = getelementptr inbounds i8, i8* %12, i64 1
store i8 %23, i8* %12
%25 = load i8, i8* %13
%26 = sext i8 %25 to i64
%27 = shl nsw i64 %26, 4
%28 = and i64 %27, 48
%29 = getelementptr inbounds i8, i8* %13, i64 1
%30 = load i8, i8* %29
%31 = lshr i8 %30, 4
%32 = zext i8 %31 to i64
%33 = or i64 %28, %32
%34 = getelementptr inbounds [65 x i8], [65 x i8]* @__const.base64enc.al, i64 0, i64 %33
%35 = load i8, i8* %34
%36 = getelementptr inbounds i8, i8* %12, i64 2
store i8 %35, i8* %24
%37 = getelementptr inbounds i8, i8* %12, i64 3
store i8 61, i8* %37
store i8 61, i8* %36
%38 = getelementptr inbounds i8, i8* %12, i64 4
store i8 0, i8* %38
%39 = load i8, i8* %29
%40 = icmp eq i8 %39, 0
br i1 %40, label %62, label %41
41: ; preds = %18
%42 = sext i8 %39 to i64
%43 = shl nsw i64 %42, 2
%44 = and i64 %43, 60
%45 = getelementptr inbounds i8, i8* %13, i64 2
%46 = load i8, i8* %45
%47 = lshr i8 %46, 6
%48 = zext i8 %47 to i64
%49 = or i64 %44, %48
%50 = getelementptr inbounds [65 x i8], [65 x i8]* @__const.base64enc.al, i64 0, i64 %49
%51 = load i8, i8* %50
store i8 %51, i8* %36
%52 = load i8, i8* %45
%53 = icmp eq i8 %52, 0
br i1 %53, label %62, label %54
54: ; preds = %41
%55 = getelementptr inbounds i8, i8* %13, i64 3
%56 = and i8 %52, 63
%57 = zext i8 %56 to i64
%58 = getelementptr inbounds [65 x i8], [65 x i8]* @__const.base64enc.al, i64 0, i64 %57
%59 = load i8, i8* %58
store i8 %59, i8* %37
%60 = load i8, i8* %55
%61 = icmp eq i8 %60, 0
br i1 %61, label %62, label %10
62: ; preds = %54, %18, %41, %3
ret i8* %1
}