#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*/ buffer;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
char EOF;
int char_location(char *, char const);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
char getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ terminate_at_location(char *, int);
void appendwriter(char *filename, const char terminate) {
  printf("appendwriter app\n");
  FILE *fr = fopen(filename, "r");
  char inp;
  if (fr != NULL) {
    do {
      inp = getc(fr);
      if (inp != EOF) {
        printf("%c", inp);
      }
    } while (inp != EOF);
  } else {
    printf("FILE READ ERROR\n");
  }
  fclose(fr);
  FILE *fo = fopen(filename, "a");
  if (fo != NULL) {
    int loc;
    char buffer[4096];
    do {
      fgets(buffer, sizeof(buffer), stdin);
      loc = char_location(buffer, terminate);
      if (loc != -1) {
        terminate_at_location(buffer, loc);
      }
      fputs(buffer, fo);
    } while (loc == -1);
  } else {
    printf("FILE OUTPUT ERROR\n");
  }
  fclose(fo);
}


x86 assembly (clang, -O0):
<appendwriter>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%rdi
lea    0xe64(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     121b <appendwriter+0x9b>
jmp    11c7 <appendwriter+0x47>
mov    -0x18(%rbp),%rdi
call   10a0 <getc@plt>
mov    %al,-0x19(%rbp)
movsbl -0x19(%rbp),%eax
mov    0x2e02(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     11fb <appendwriter+0x7b>
movsbl -0x19(%rbp),%esi
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1200 <appendwriter+0x80>
movsbl -0x19(%rbp),%eax
mov    0x2dd5(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    11c7 <appendwriter+0x47>
jmp    1229 <appendwriter+0xa9>
lea    0xdf5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x8(%rbp),%rdi
lea    0xdeb(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     12bb <appendwriter+0x13b>
jmp    1256 <appendwriter+0xd6>
lea    -0x1030(%rbp),%rdi
mov    0x2d84(%rip),%rax
mov    (%rax),%edx
mov    $0x1000,%esi
call   1070 <fgets@plt>
lea    -0x1030(%rbp),%rdi
movsbl -0x9(%rbp),%esi
call   1030 <char_location@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
je     129c <appendwriter+0x11c>
lea    -0x1030(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1090 <terminate_at_location@plt>
lea    -0x1030(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1060 <fputs@plt>
cmpl   $0xffffffff,-0x2c(%rbp)
je     1256 <appendwriter+0xd6>
jmp    12c9 <appendwriter+0x149>
lea    0xd68(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<appendwriter>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1000,%rsp
mov    %esi,%r14d
mov    %rdi,%r15
lea    0xe58(%rip),%rdi
call   1040 <puts@plt>
lea    0xe48(%rip),%rsi
mov    %r15,%rdi
call   1090 <fopen@plt>
mov    %rax,%r12
test   %rax,%rax
je     11ee <appendwriter+0x5e>
mov    0x2e11(%rip),%rbp
nop
mov    %r12,%rdi
call   10b0 <getc@plt>
cmp    0x0(%rbp),%al
je     11fa <appendwriter+0x6a>
mov    %eax,%ebx
movsbl %al,%edi
call   1030 <putchar@plt>
cmp    0x0(%rbp),%bl
jne    11d0 <appendwriter+0x40>
jmp    11fa <appendwriter+0x6a>
lea    0xe20(%rip),%rdi
call   1040 <puts@plt>
mov    %r12,%rdi
call   1060 <fclose@plt>
lea    0xdf9(%rip),%rsi
mov    %r15,%rdi
call   1090 <fopen@plt>
mov    %rax,%r15
test   %rax,%rax
je     1295 <appendwriter+0x105>
mov    0x2dc8(%rip),%r12
mov    (%r12),%edx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1080 <fgets@plt>
movsbl %r14b,%ebp
mov    %rbx,%rdi
mov    %ebp,%esi
call   1050 <char_location@plt>
cmp    $0xffffffff,%eax
jne    127b <appendwriter+0xeb>
mov    %rsp,%rbx
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r15,%rsi
call   1070 <fputs@plt>
mov    (%r12),%edx
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1080 <fgets@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1050 <char_location@plt>
cmp    $0xffffffff,%eax
je     1250 <appendwriter+0xc0>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %eax,%esi
call   10a0 <terminate_at_location@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1070 <fputs@plt>
jmp    12a1 <appendwriter+0x111>
lea    0xd89(%rip),%rdi
call   1040 <puts@plt>
mov    %r15,%rdi
call   1060 <fclose@plt>
add    $0x1000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"appendwriter app\0A\00"
@.str.1 = [2 x i8] c"r\00"
@EOF = global i8 0
@.str.2 = [3 x i8] c"%c\00"
@.str.3 = [17 x i8] c"FILE READ ERROR\0A\00"
@.str.4 = [2 x i8] c"a\00"
@stdin = global i32 0
@.str.5 = [19 x i8] c"FILE OUTPUT ERROR\0A\00"
define void @appendwriter(i8* %0, i8 signext %1) {
%3 = alloca i8*
%4 = alloca i8
%5 = alloca i32*
%6 = alloca i8
%7 = alloca i32*
%8 = alloca i32
%9 = alloca [4096 x i8]
store i8* %0, i8** %3
store i8 %1, i8* %4
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%11 = load i8*, i8** %3
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %12, i32** %5
%13 = load i32*, i32** %5
%14 = icmp ne i32* %13, null
br i1 %14, label %15, label %36
15: ; preds = %2
br label %16
16: ; preds = %29, %15
%17 = load i32*, i32** %5
%18 = call signext i8 @getc(i32* %17)
store i8 %18, i8* %6
%19 = load i8, i8* %6
%20 = sext i8 %19 to i32
%21 = load i8, i8* @EOF
%22 = sext i8 %21 to i32
%23 = icmp ne i32 %20, %22
br i1 %23, label %24, label %28
24: ; preds = %16
%25 = load i8, i8* %6
%26 = sext i8 %25 to i32
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %26)
br label %28
28: ; preds = %24, %16
br label %29
29: ; preds = %28
%30 = load i8, i8* %6
%31 = sext i8 %30 to i32
%32 = load i8, i8* @EOF
%33 = sext i8 %32 to i32
%34 = icmp ne i32 %31, %33
br i1 %34, label %16, label %35
35: ; preds = %29
br label %38
36: ; preds = %2
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %38
38: ; preds = %36, %35
%39 = load i32*, i32** %5
%40 = call i32 @fclose(i32* %39)
%41 = load i8*, i8** %3
%42 = call i32* @fopen(i8* %41, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %42, i32** %7
%43 = load i32*, i32** %7
%44 = icmp ne i32* %43, null
br i1 %44, label %45, label %67
45: ; preds = %38
br label %46
46: ; preds = %63, %45
%47 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%48 = load i32, i32* @stdin
%49 = call i32 @fgets(i8* %47, i32 4096, i32 %48)
%50 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%51 = load i8, i8* %4
%52 = call i32 @char_location(i8* %50, i8 signext %51)
store i32 %52, i32* %8
%53 = load i32, i32* %8
%54 = icmp ne i32 %53, -1
br i1 %54, label %55, label %59
55: ; preds = %46
%56 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%57 = load i32, i32* %8
%58 = call i32 @terminate_at_location(i8* %56, i32 %57)
br label %59
59: ; preds = %55, %46
%60 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%61 = load i32*, i32** %7
%62 = call i32 @fputs(i8* %60, i32* %61)
br label %63
63: ; preds = %59
%64 = load i32, i32* %8
%65 = icmp eq i32 %64, -1
br i1 %65, label %46, label %66
66: ; preds = %63
br label %69
67: ; preds = %38
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
br label %69
69: ; preds = %67, %66
%70 = load i32*, i32** %7
%71 = call i32 @fclose(i32* %70)
ret void
}

LLVM IR (-O2):
@.str.1 = [2 x i8] c"r\00"
@EOF = global i8 0
@.str.4 = [2 x i8] c"a\00"
@stdin = global i32 0
@str = [17 x i8] c"appendwriter app\00"
@str.6 = [16 x i8] c"FILE READ ERROR\00"
@str.7 = [18 x i8] c"FILE OUTPUT ERROR\00"
define void @appendwriter(i8* nocapture %0, i8 signext %1) {
%3 = alloca [4096 x i8]
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %16, label %7
7: ; preds = %2, %11
%8 = tail call signext i8 @getc(i32* %5)
%9 = load i8, i8* @EOF
%10 = icmp eq i8 %8, %9
br i1 %10, label %18, label %11
11: ; preds = %7
%12 = sext i8 %8 to i32
%13 = tail call i32 @putchar(i32 %12)
%14 = load i8, i8* @EOF
%15 = icmp eq i8 %8, %14
br i1 %15, label %18, label %7
16: ; preds = %2
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.6, i64 0, i64 0))
br label %18
18: ; preds = %7, %11, %16
%19 = tail call i32 @fclose(i32* %5)
%20 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%21 = icmp eq i32* %20, null
br i1 %21, label %38, label %22
22: ; preds = %18
%23 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%24 = load i32, i32* @stdin
%25 = call i32 @fgets(i8* %23, i32 4096, i32 %24)
%26 = call i32 @char_location(i8* %23, i8 signext %1)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %34
28: ; preds = %22, %28
%29 = call i32 @fputs(i8* %23, i32* %20)
%30 = load i32, i32* @stdin
%31 = call i32 @fgets(i8* %23, i32 4096, i32 %30)
%32 = call i32 @char_location(i8* %23, i8 signext %1)
%33 = icmp eq i32 %32, -1
br i1 %33, label %28, label %34
34: ; preds = %28, %22
%35 = phi i32 [ %26, %22 ], [ %32, %28 ]
%36 = call i32 @terminate_at_location(i8* %23, i32 %35)
%37 = call i32 @fputs(i8* %23, i32* %20)
br label %40
38: ; preds = %18
%39 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.7, i64 0, i64 0))
br label %40
40: ; preds = %38, %34
%41 = call i32 @fclose(i32* %20)
ret void
}