#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 */
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*/ *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ terminate_at_location(char *, int);
void filewriter(char *filename, const char terminate) {
  printf("filewriter app\n");
  FILE *fo = fopen(filename, "w");
  if (fo != 0) {
    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):
<filewriter>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
lea    0xe74(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%rdi
lea    0xe74(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     121a <filewriter+0xaa>
jmp    11b5 <filewriter+0x45>
lea    -0x1020(%rbp),%rdi
mov    0x2e25(%rip),%rax
mov    (%rax),%edx
mov    $0x1000,%esi
call   1070 <fgets@plt>
lea    -0x1020(%rbp),%rdi
movsbl -0x9(%rbp),%esi
call   1030 <char_location@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
je     11fb <filewriter+0x8b>
lea    -0x1020(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1090 <terminate_at_location@plt>
lea    -0x1020(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <fputs@plt>
cmpl   $0xffffffff,-0x1c(%rbp)
je     11b5 <filewriter+0x45>
jmp    1226 <filewriter+0xb6>
lea    0xdf1(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<filewriter>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0xe79(%rip),%rdi
call   1030 <puts@plt>
lea    0xe6b(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
mov    %rax,%r14
test   %rax,%rax
je     1224 <filewriter+0xb4>
mov    0x2e3c(%rip),%r15
mov    (%r15),%edx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1070 <fgets@plt>
movsbl %bpl,%ebp
mov    %rbx,%rdi
mov    %ebp,%esi
call   1040 <char_location@plt>
cmp    $0xffffffff,%eax
jne    120a <filewriter+0x9a>
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <fputs@plt>
mov    (%r15),%edx
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1070 <fgets@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1040 <char_location@plt>
cmp    $0xffffffff,%eax
je     11e0 <filewriter+0x70>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %eax,%esi
call   1090 <terminate_at_location@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <fputs@plt>
jmp    1230 <filewriter+0xc0>
lea    0xde6(%rip),%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
add    $0x1008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"filewriter app\0A\00"
@.str.1 = [2 x i8] c"w\00"
@stdin = global i32 0
@.str.2 = [19 x i8] c"FILE OUTPUT ERROR\0A\00"
define void @filewriter(i8* %0, i8 signext %1) {
%3 = alloca i8*
%4 = alloca i8
%5 = alloca i32*
%6 = alloca i32
%7 = alloca [4096 x i8]
store i8* %0, i8** %3
store i8 %1, i8* %4
%8 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%9 = load i8*, i8** %3
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %10, i32** %5
%11 = load i32*, i32** %5
%12 = icmp ne i32* %11, null
br i1 %12, label %13, label %35
13: ; preds = %2
br label %14
14: ; preds = %31, %13
%15 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%16 = load i32, i32* @stdin
%17 = call i32 @fgets(i8* %15, i32 4096, i32 %16)
%18 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%19 = load i8, i8* %4
%20 = call i32 @char_location(i8* %18, i8 signext %19)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = icmp ne i32 %21, -1
br i1 %22, label %23, label %27
23: ; preds = %14
%24 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%25 = load i32, i32* %6
%26 = call i32 @terminate_at_location(i8* %24, i32 %25)
br label %27
27: ; preds = %23, %14
%28 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%29 = load i32*, i32** %5
%30 = call i32 @fputs(i8* %28, i32* %29)
br label %31
31: ; preds = %27
%32 = load i32, i32* %6
%33 = icmp eq i32 %32, -1
br i1 %33, label %14, label %34
34: ; preds = %31
br label %37
35: ; preds = %2
%36 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %37
37: ; preds = %35, %34
%38 = load i32*, i32** %5
%39 = call i32 @fclose(i32* %38)
ret void
}

LLVM IR (-O2):
@.str.1 = [2 x i8] c"w\00"
@stdin = global i32 0
@str = [15 x i8] c"filewriter app\00"
@str.3 = [18 x i8] c"FILE OUTPUT ERROR\00"
define void @filewriter(i8* nocapture %0, i8 signext %1) {
%3 = alloca [4096 x i8]
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 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 %23, label %7
7: ; preds = %2
%8 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%9 = load i32, i32* @stdin
%10 = call i32 @fgets(i8* %8, i32 4096, i32 %9)
%11 = call i32 @char_location(i8* %8, i8 signext %1)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %19
13: ; preds = %7, %13
%14 = call i32 @fputs(i8* %8, i32* %5)
%15 = load i32, i32* @stdin
%16 = call i32 @fgets(i8* %8, i32 4096, i32 %15)
%17 = call i32 @char_location(i8* %8, i8 signext %1)
%18 = icmp eq i32 %17, -1
br i1 %18, label %13, label %19
19: ; preds = %13, %7
%20 = phi i32 [ %11, %7 ], [ %17, %13 ]
%21 = call i32 @terminate_at_location(i8* %8, i32 %20)
%22 = call i32 @fputs(i8* %8, i32* %5)
br label %25
23: ; preds = %2
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.3, i64 0, i64 0))
br label %25
25: ; preds = %23, %19
%26 = call i32 @fclose(i32* %5)
ret void
}