#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 EOF;
int encode_character(int, int);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *);
int main(int argc, char *argv[]) {
  FILE *fp;
  fp = fopen(argv[1], "r");
  FILE *encoded_fp;
  encoded_fp = fopen("encoded.txt", "w");
  if (fp) {
    int c;
    while ((c = getc(fp)) != EOF) {
      fprintf(encoded_fp, "%c", encode_character(c, 3));
    }
  } else {
    printf("There was an error reading the file %s\n", argv[1]);
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe8b(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
lea    0xe7d(%rip),%rdi
lea    0xe82(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11f1 <main+0xa1>
jmp    11a5 <main+0x55>
mov    -0x18(%rbp),%rdi
call   1070 <getc@plt>
mov    %eax,-0x24(%rbp)
mov    0x2e30(%rip),%rcx
cmp    (%rcx),%eax
je     11ec <main+0x9c>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x24(%rbp),%edi
mov    $0x3,%esi
call   1030 <encode_character@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%edx
lea    0xe2e(%rip),%rsi
call   1050 <fprintf@plt>
jmp    11a5 <main+0x55>
jmp    1211 <main+0xc1>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe13(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1218 <main+0xc8>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbp
mov    0x8(%rsi),%rdi
lea    0xe9b(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rbx
lea    0xe8e(%rip),%rdi
lea    0xe93(%rip),%rsi
call   1060 <fopen@plt>
test   %rbx,%rbx
je     11c5 <main+0x75>
mov    %rax,%r14
mov    %rbx,%rdi
call   1070 <getc@plt>
mov    0x2e51(%rip),%rbp
xor    %r15d,%r15d
cmp    0x0(%rbp),%eax
je     11db <main+0x8b>
nop
mov    %eax,%edi
mov    $0x3,%esi
call   1030 <encode_character@plt>
mov    %eax,%edi
mov    %r14,%rsi
call   1050 <fputc@plt>
mov    %rbx,%rdi
call   1070 <getc@plt>
cmp    0x0(%rbp),%eax
jne    11a0 <main+0x50>
jmp    11db <main+0x8b>
mov    0x8(%rbp),%rsi
lea    0xe40(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [12 x i8] c"encoded.txt\00"
@.str.2 = [2 x i8] c"w\00"
@EOF = global i32 0
@.str.3 = [3 x i8] c"%c\00"
@.str.4 = [40 x i8] c"There was an error reading the file %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i8**, i8*** %5
%10 = getelementptr inbounds i8*, i8** %9, i64 1
%11 = load i8*, i8** %10
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %6
%13 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %13, i32** %7
%14 = load i32*, i32** %6
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %28
16: ; preds = %2
br label %17
17: ; preds = %22, %16
%18 = load i32*, i32** %6
%19 = call i32 @getc(i32* %18)
store i32 %19, i32* %8
%20 = load i32, i32* @EOF
%21 = icmp ne i32 %19, %20
br i1 %21, label %22, label %27
22: ; preds = %17
%23 = load i32*, i32** %7
%24 = load i32, i32* %8
%25 = call i32 @encode_character(i32 %24, i32 3)
%26 = call i32 @fprintf(i32* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %25)
br label %17
27: ; preds = %17
br label %33
28: ; preds = %2
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0), i8* %31)
store i32 1, i32* %3
br label %34
33: ; preds = %27
store i32 0, i32* %3
br label %34
34: ; preds = %33, %28
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [12 x i8] c"encoded.txt\00"
@.str.2 = [2 x i8] c"w\00"
@EOF = global i32 0
@.str.4 = [40 x i8] c"There was an error reading the file %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = getelementptr inbounds i8*, i8** %1, i64 1
%4 = load i8*, i8** %3
%5 = tail call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%7 = icmp eq i32* %5, null
br i1 %7, label %19, label %8
8: ; preds = %2
%9 = tail call i32 @getc(i32* %5)
%10 = load i32, i32* @EOF
%11 = icmp eq i32 %9, %10
br i1 %11, label %22, label %12
12: ; preds = %8, %12
%13 = phi i32 [ %16, %12 ], [ %9, %8 ]
%14 = tail call i32 @encode_character(i32 %13, i32 3)
%15 = tail call i32 @fputc(i32 %14, i32* %6)
%16 = tail call i32 @getc(i32* %5)
%17 = load i32, i32* @EOF
%18 = icmp eq i32 %16, %17
br i1 %18, label %22, label %12
19: ; preds = %2
%20 = load i8*, i8** %3
%21 = tail call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0), i8* %20)
br label %22
22: ; preds = %12, %8, %19
%23 = phi i32 [ 1, %19 ], [ 0, %8 ], [ 0, %12 ]
ret i32 %23
}