#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*/ toencode;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
char *base64_decode(char *, int *);
int /*<<< orphan*/ base64_decode_with_buffer(char *, char *, int *);
char *base64_encode(char *, int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ getchar();
scalar_t__ memcmp(char *, char *, int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ strcmp(char *, char *);
int strlen(char *);
void base64_test(void) {
  char *encoded = base64_encode("hello", strlen("hello"));
  char buf[1024];
  int buflen = sizeof(buf);
  base64_decode_with_buffer(encoded, buf, &buflen);
  buf[buflen] = '\0';
  printf("buf=%s buflen=%d\n", buf, buflen);
  if (strcmp(buf, "hello") == 0) {
    printf("base64 text sucess\n");
  } else {
    printf("base64 text failed\n");
    getchar();
  }
  free(encoded);
  char toencode[] = {'\0', 1, 2, 'a', 'b', 'c'};
  encoded = base64_encode(toencode, sizeof(toencode));
  printf("encoded=%s\n", encoded);
  int decoded_len = 0;
  char *decoded = base64_decode(encoded, &decoded_len);
  printf("decoded_len=%d\n", decoded_len);
  if (memcmp(decoded, toencode, 6) != 0) {
    printf("base64 binary failed\n");
    getchar();
  } else {
    printf("base64 binary sucess\n");
  }
  free(encoded);
  free(decoded);
}


x86 assembly (clang, -O0):
<base64_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
lea    0xe5e(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,%esi
lea    0xe50(%rip),%rdi
call   10a0 <base64_encode@plt>
mov    %rax,-0x8(%rbp)
movl   $0x400,-0x414(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x410(%rbp),%rsi
lea    -0x414(%rbp),%rdx
call   1060 <base64_decode_with_buffer@plt>
movslq -0x414(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
lea    -0x410(%rbp),%rsi
mov    -0x414(%rbp),%edx
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x410(%rbp),%rdi
lea    0xdee(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    1234 <base64_test+0xa4>
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1249 <base64_test+0xb9>
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1090 <getchar@plt>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    0xe30(%rip),%eax
mov    %eax,-0x41a(%rbp)
mov    0xe27(%rip),%ax
mov    %ax,-0x416(%rbp)
lea    -0x41a(%rbp),%rdi
mov    $0x6,%esi
call   10a0 <base64_encode@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xdb4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x420(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x420(%rbp),%rsi
call   10b0 <base64_decode@plt>
mov    %rax,-0x428(%rbp)
mov    -0x420(%rbp),%esi
lea    0xd8b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x428(%rbp),%rdi
lea    -0x41a(%rbp),%rsi
mov    $0x6,%edx
call   1070 <memcmp@plt>
cmp    $0x0,%rax
je     1304 <base64_test+0x174>
lea    0xd6b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1090 <getchar@plt>
jmp    1312 <base64_test+0x182>
lea    0xd67(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    -0x428(%rbp),%rdi
call   1030 <free@plt>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<base64_test>:
push   %r15
push   %r14
push   %rbx
sub    $0x410,%rsp
lea    0xe4d(%rip),%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %r14,%rdi
mov    %eax,%esi
call   10b0 <base64_encode@plt>
mov    %rax,%r15
movl   $0x400,0x4(%rsp)
lea    0x10(%rsp),%rbx
lea    0x4(%rsp),%rdx
mov    %rax,%rdi
mov    %rbx,%rsi
call   1070 <base64_decode_with_buffer@plt>
movslq 0x4(%rsp),%rdx
movb   $0x0,0x10(%rsp,%rdx,1)
lea    0xe10(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1225 <base64_test+0x85>
lea    0xe1d(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   10a0 <getchar@plt>
jmp    1231 <base64_test+0x91>
lea    0xe45(%rip),%rdi
call   1040 <puts@plt>
mov    %r15,%rdi
call   1030 <free@plt>
movw   $0x6362,0xc(%rsp)
movl   $0x61020100,0x8(%rsp)
61
lea    0x8(%rsp),%r15
mov    %r15,%rdi
mov    $0x6,%esi
call   10b0 <base64_encode@plt>
mov    %rax,%rbx
lea    0xdb4(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
movl   $0x0,(%rsp)
mov    %rsp,%rsi
mov    %rbx,%rdi
call   10c0 <base64_decode@plt>
mov    %rax,%r14
mov    (%rsp),%esi
lea    0xd97(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    $0x6,%edx
call   1080 <memcmp@plt>
test   %rax,%rax
je     12be <base64_test+0x11e>
lea    0xdac(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   10a0 <getchar@plt>
jmp    12ca <base64_test+0x12a>
lea    0xd82(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
add    $0x410,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [6 x i8] c"hello\00"
@.str.1 = [18 x i8] c"buf=%s buflen=%d\0A\00"
@.str.2 = [20 x i8] c"base64 text sucess\0A\00"
@.str.3 = [20 x i8] c"base64 text failed\0A\00"
@__const.base64_test.toencode = [6 x i8] c"\00\01\02abc"
@.str.4 = [12 x i8] c"encoded=%s\0A\00"
@.str.5 = [16 x i8] c"decoded_len=%d\0A\00"
@.str.6 = [22 x i8] c"base64 binary failed\0A\00"
@.str.7 = [22 x i8] c"base64 binary sucess\0A\00"
define void @base64_test() {
%1 = alloca i8*
%2 = alloca [1024 x i8]
%3 = alloca i32
%4 = alloca [6 x i8]
%5 = alloca i32
%6 = alloca i8*
%7 = call i32 @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%8 = call i8* @base64_encode(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %7)
store i8* %8, i8** %1
store i32 1024, i32* %3
%9 = load i8*, i8** %1
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%11 = call i32 @base64_decode_with_buffer(i8* %9, i8* %10, i32* %3)
%12 = load i32, i32* %3
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %13
store i8 0, i8* %14
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%16 = load i32, i32* %3
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %15, i32 %16)
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%19 = call i64 @strcmp(i8* %18, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %0
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %26
23: ; preds = %0
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%25 = call i32 (...) @getchar()
br label %26
26: ; preds = %23, %21
%27 = load i8*, i8** %1
%28 = call i32 @free(i8* %27)
%29 = bitcast [6 x i8]* %4 to i8*
%30 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 0
%31 = call i8* @base64_encode(i8* %30, i32 6)
store i8* %31, i8** %1
%32 = load i8*, i8** %1
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %32)
store i32 0, i32* %5
%34 = load i8*, i8** %1
%35 = call i8* @base64_decode(i8* %34, i32* %5)
store i8* %35, i8** %6
%36 = load i32, i32* %5
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 %36)
%38 = load i8*, i8** %6
%39 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 0
%40 = call i64 @memcmp(i8* %38, i8* %39, i32 6)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %26
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0))
%44 = call i32 (...) @getchar()
br label %47
45: ; preds = %26
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0))
br label %47
47: ; preds = %45, %42
%48 = load i8*, i8** %1
%49 = call i32 @free(i8* %48)
%50 = load i8*, i8** %6
%51 = call i32 @free(i8* %50)
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"hello\00"
@.str.1 = [18 x i8] c"buf=%s buflen=%d\0A\00"
@__const.base64_test.toencode = [6 x i8] c"\00\01\02abc"
@.str.4 = [12 x i8] c"encoded=%s\0A\00"
@.str.5 = [16 x i8] c"decoded_len=%d\0A\00"
@str = [19 x i8] c"base64 text failed\00"
@str.8 = [21 x i8] c"base64 binary sucess\00"
@str.9 = [21 x i8] c"base64 binary failed\00"
@str.10 = [19 x i8] c"base64 text sucess\00"
define void @base64_test() {
%1 = alloca [1024 x i8]
%2 = alloca i32
%3 = alloca [6 x i8]
%4 = alloca i32
%5 = tail call i32 @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%6 = tail call i8* @base64_encode(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%8 = bitcast i32* %2 to i8*
store i32 1024, i32* %2
%9 = call i32 @base64_decode_with_buffer(i8* %6, i8* %7, i32* %2)
%10 = load i32, i32* %2
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 %11
store i8 0, i8* %12
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %7, i32 %10)
%14 = call i64 @strcmp(i8* %7, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %0
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.10, i64 0, i64 0))
br label %21
18: ; preds = %0
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%20 = call i32 (...) @getchar()
br label %21
21: ; preds = %18, %16
%22 = call i32 @free(i8* %6)
%23 = getelementptr inbounds [6 x i8], [6 x i8]* %3, i64 0, i64 0
%24 = call i8* @base64_encode(i8* %23, i32 6)
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %24)
%26 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%27 = call i8* @base64_decode(i8* %24, i32* %4)
%28 = load i32, i32* %4
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 %28)
%30 = call i64 @memcmp(i8* %27, i8* %23, i32 6)
%31 = icmp eq i64 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %21
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.9, i64 0, i64 0))
%34 = call i32 (...) @getchar()
br label %37
35: ; preds = %21
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.8, i64 0, i64 0))
br label %37
37: ; preds = %35, %32
%38 = call i32 @free(i8* %24)
%39 = call i32 @free(i8* %27)
ret void
}