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

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
unsigned char *base64_decode(unsigned char *, size_t, size_t *);
unsigned char *base64_encode(unsigned char *, size_t, size_t *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(unsigned char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(unsigned char *, int, size_t, int /*<<< orphan*/ *);
unsigned char *malloc(size_t);
int /*<<< orphan*/ printf(char *);
scalar_t__ strcmp(char *, char *);
int main(int argc, char *argv[]) {
  FILE *f;
  size_t len, elen;
  unsigned char *buf, *e;
  if (argc != 4) {
    printf("Usage: base64 <encode|decode> <in file> <out file>\n");
    return -1;
  }
  f = fopen(argv[2], "r");
  if (f == NULL)
    return -1;
  fseek(f, 0, SEEK_END);
  len = ftell(f);
  fseek(f, 0, SEEK_SET);
  buf = malloc(len);
  if (buf == NULL) {
    fclose(f);
    return -1;
  }
  fread(buf, 1, len, f);
  fclose(f);
  if (strcmp(argv[1], "encode") == 0)
    e = base64_encode(buf, len, &elen);
  else
    e = base64_decode(buf, len, &elen);
  if (e == NULL)
    return -2;
  f = fopen(argv[3], "w");
  if (f == NULL)
    return -3;
  fwrite(e, 1, elen, f);
  fclose(f);
  free(e);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
je     11f8 <main+0x38>
lea    0xe19(%rip),%rdi
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    136a <main+0x1aa>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe2d(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1227 <main+0x67>
movl   $0xffffffff,-0x4(%rbp)
jmp    136a <main+0x1aa>
mov    -0x18(%rbp),%rdi
mov    0x2db6(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10b0 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1080 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2d85(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10b0 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1090 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1289 <main+0xc9>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    136a <main+0x1aa>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xd7f(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    12e0 <main+0x120>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   10a0 <base64_encode@plt>
mov    %rax,-0x38(%rbp)
jmp    12f5 <main+0x135>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   10e0 <base64_decode@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    130c <main+0x14c>
movl   $0xfffffffe,-0x4(%rbp)
jmp    136a <main+0x1aa>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
lea    0xd22(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    133b <main+0x17b>
movl   $0xfffffffd,-0x4(%rbp)
jmp    136a <main+0x1aa>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   10d0 <fwrite@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
cmp    $0x4,%edi
jne    127c <main+0xbc>
mov    %rsi,%r15
mov    0x10(%rsi),%rdi
lea    0xe1d(%rip),%rsi
call   10c0 <fopen@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
je     128d <main+0xcd>
mov    %rax,%rbx
mov    0x2de8(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10b0 <fseek@plt>
mov    %rbx,%rdi
call   1080 <ftell@plt>
mov    %rax,%r14
mov    0x2dba(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10b0 <fseek@plt>
mov    %r14,%rdi
call   1090 <malloc@plt>
test   %rax,%rax
je     129c <main+0xdc>
mov    %rax,%r12
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1050 <fread@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    0x8(%r15),%rdi
lea    0xda2(%rip),%rsi
call   1070 <strcmp@plt>
lea    0x8(%rsp),%rdx
mov    %r12,%rdi
mov    %r14,%rsi
test   %rax,%rax
je     12a6 <main+0xe6>
call   10e0 <base64_decode@plt>
jmp    12ab <main+0xeb>
lea    0xd88(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
call   1060 <fclose@plt>
jmp    128d <main+0xcd>
call   10a0 <base64_encode@plt>
mov    %rax,%rbx
test   %rax,%rax
je     12f4 <main+0x134>
mov    0x18(%r15),%rdi
lea    0xd4b(%rip),%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
je     12fb <main+0x13b>
mov    %rax,%rbp
mov    0x8(%rsp),%rdx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %rax,%rcx
call   10d0 <fwrite@plt>
mov    %rbp,%rdi
call   1060 <fclose@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebp,%ebp
jmp    128d <main+0xcd>
mov    $0xfffffffe,%ebp
jmp    128d <main+0xcd>
mov    $0xfffffffd,%ebp
jmp    128d <main+0xcd>

LLVM IR (-O0):
@.str = [52 x i8] c"Usage: base64 <encode|decode> <in file> <out file>\0A\00"
@.str.1 = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [7 x i8] c"encode\00"
@.str.3 = [2 x i8] c"w\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 4
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %80
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 2
%18 = load i8*, i8** %17
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %19, i32** %6
%20 = load i32*, i32** %6
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %23
22: ; preds = %15
store i32 -1, i32* %3
br label %80
23: ; preds = %15
%24 = load i32*, i32** %6
%25 = load i32, i32* @SEEK_END
%26 = call i32 @fseek(i32* %24, i32 0, i32 %25)
%27 = load i32*, i32** %6
%28 = call i64 @ftell(i32* %27)
store i64 %28, i64* %7
%29 = load i32*, i32** %6
%30 = load i32, i32* @SEEK_SET
%31 = call i32 @fseek(i32* %29, i32 0, i32 %30)
%32 = load i64, i64* %7
%33 = call i8* @malloc(i64 %32)
store i8* %33, i8** %9
%34 = load i8*, i8** %9
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %39
36: ; preds = %23
%37 = load i32*, i32** %6
%38 = call i32 @fclose(i32* %37)
store i32 -1, i32* %3
br label %80
39: ; preds = %23
%40 = load i8*, i8** %9
%41 = load i64, i64* %7
%42 = load i32*, i32** %6
%43 = call i32 @fread(i8* %40, i32 1, i64 %41, i32* %42)
%44 = load i32*, i32** %6
%45 = call i32 @fclose(i32* %44)
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 1
%48 = load i8*, i8** %47
%49 = call i64 @strcmp(i8* %48, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%50 = icmp eq i64 %49, 0
br i1 %50, label %51, label %55
51: ; preds = %39
%52 = load i8*, i8** %9
%53 = load i64, i64* %7
%54 = call i8* @base64_encode(i8* %52, i64 %53, i64* %8)
store i8* %54, i8** %10
br label %59
55: ; preds = %39
%56 = load i8*, i8** %9
%57 = load i64, i64* %7
%58 = call i8* @base64_decode(i8* %56, i64 %57, i64* %8)
store i8* %58, i8** %10
br label %59
59: ; preds = %55, %51
%60 = load i8*, i8** %10
%61 = icmp eq i8* %60, null
br i1 %61, label %62, label %63
62: ; preds = %59
store i32 -2, i32* %3
br label %80
63: ; preds = %59
%64 = load i8**, i8*** %5
%65 = getelementptr inbounds i8*, i8** %64, i64 3
%66 = load i8*, i8** %65
%67 = call i32* @fopen(i8* %66, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %67, i32** %6
%68 = load i32*, i32** %6
%69 = icmp eq i32* %68, null
br i1 %69, label %70, label %71
70: ; preds = %63
store i32 -3, i32* %3
br label %80
71: ; preds = %63
%72 = load i8*, i8** %10
%73 = load i64, i64* %8
%74 = load i32*, i32** %6
%75 = call i32 @fwrite(i8* %72, i32 1, i64 %73, i32* %74)
%76 = load i32*, i32** %6
%77 = call i32 @fclose(i32* %76)
%78 = load i8*, i8** %10
%79 = call i32 @free(i8* %78)
store i32 0, i32* %3
br label %80
80: ; preds = %71, %70, %62, %36, %22, %13
%81 = load i32, i32* %3
ret i32 %81
}

LLVM IR (-O2):
@.str.1 = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [7 x i8] c"encode\00"
@.str.3 = [2 x i8] c"w\00"
@str = [51 x i8] c"Usage: base64 <encode|decode> <in file> <out file>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to i8*
%5 = icmp eq i32 %0, 4
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @str, i64 0, i64 0))
br label %47
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 2
%10 = load i8*, i8** %9
%11 = tail call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %47, label %13
13: ; preds = %8
%14 = load i32, i32* @SEEK_END
%15 = tail call i32 @fseek(i32* %11, i32 0, i32 %14)
%16 = tail call i64 @ftell(i32* %11)
%17 = load i32, i32* @SEEK_SET
%18 = tail call i32 @fseek(i32* %11, i32 0, i32 %17)
%19 = tail call i8* @malloc(i64 %16)
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %23
21: ; preds = %13
%22 = tail call i32 @fclose(i32* %11)
br label %47
23: ; preds = %13
%24 = tail call i32 @fread(i8* %19, i32 1, i64 %16, i32* %11)
%25 = tail call i32 @fclose(i32* %11)
%26 = getelementptr inbounds i8*, i8** %1, i64 1
%27 = load i8*, i8** %26
%28 = tail call i64 @strcmp(i8* %27, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %23
%31 = call i8* @base64_encode(i8* %19, i64 %16, i64* %3)
br label %34
32: ; preds = %23
%33 = call i8* @base64_decode(i8* %19, i64 %16, i64* %3)
br label %34
34: ; preds = %32, %30
%35 = phi i8* [ %31, %30 ], [ %33, %32 ]
%36 = icmp eq i8* %35, null
br i1 %36, label %47, label %37
37: ; preds = %34
%38 = getelementptr inbounds i8*, i8** %1, i64 3
%39 = load i8*, i8** %38
%40 = call i32* @fopen(i8* %39, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%41 = icmp eq i32* %40, null
br i1 %41, label %47, label %42
42: ; preds = %37
%43 = load i64, i64* %3
%44 = call i32 @fwrite(i8* %35, i32 1, i64 %43, i32* %40)
%45 = call i32 @fclose(i32* %40)
%46 = call i32 @free(i8* %35)
br label %47
47: ; preds = %37, %34, %8, %42, %21, %6
%48 = phi i32 [ -1, %6 ], [ -1, %21 ], [ 0, %42 ], [ -1, %8 ], [ -2, %34 ], [ -3, %37 ]
ret i32 %48
}