#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*/ abort();
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ remove(char *);
int main() {
  remove("sulong_test_file");
  FILE *writeableFile = fopen("sulong_test_file", "a");
  if (writeableFile == NULL) {
    printf("Failed to open file\n");
    abort();
  }
  if (writeableFile == NULL) {
    printf("error opening file!\n");
    exit(2);
  }
  fprintf(writeableFile, "asdfasdfasdf");
  fclose(writeableFile);
  FILE *readableFile = fopen("sulong_test_file", "r");
  if (readableFile == NULL) {
    printf("Failed to open file\n");
    abort();
  }
  if (readableFile == NULL) {
    printf("error opening file!\n");
    exit(3);
  }
  char buff[1000];
  while (fgets(buff, 1000, readableFile) != NULL) {
    printf("%s\n", &buff[0]);
  }
  if (remove("sulong_test_file")) {
    printf("error removing file!\n");
    exit(4);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x400,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe67(%rip),%rdi
call   1040 <remove@plt>
lea    0xe5b(%rip),%rdi
lea    0xe65(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11d5 <main+0x55>
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
cmpq   $0x0,-0x10(%rbp)
jne    11f8 <main+0x78>
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rdi
lea    0xe3a(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
lea    0xde8(%rip),%rdi
lea    0xe2b(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1248 <main+0xc8>
lea    0xdd9(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
cmpq   $0x0,-0x18(%rbp)
jne    126b <main+0xeb>
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x3,%edi
call   10a0 <exit@plt>
jmp    1270 <main+0xf0>
lea    -0x400(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x3e8,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     12a9 <main+0x129>
lea    -0x400(%rbp),%rsi
lea    0xdaf(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1270 <main+0xf0>
lea    0xd50(%rip),%rdi
call   1040 <remove@plt>
cmp    $0x0,%rax
je     12d7 <main+0x157>
lea    0xd8a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x4,%edi
call   10a0 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x400,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x3e8,%rsp
lea    0xe6f(%rip),%rbx
mov    %rbx,%rdi
call   1040 <remove@plt>
lea    0xe71(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     124b <main+0xcb>
mov    %rax,%rbx
lea    0xe58(%rip),%rdi
mov    $0xc,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10a0 <fwrite@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
lea    0xe24(%rip),%rdi
lea    0xe3d(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     124b <main+0xcb>
mov    %rax,%r14
mov    %rsp,%rdi
mov    $0x3e8,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     122d <main+0xad>
mov    %rsp,%rbx
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1050 <puts@plt>
mov    %rbx,%rdi
mov    $0x3e8,%esi
mov    %r14,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
jne    1210 <main+0x90>
lea    0xdcc(%rip),%rdi
call   1040 <remove@plt>
test   %rax,%rax
jne    125e <main+0xde>
xor    %eax,%eax
add    $0x3e8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xde5(%rip),%rdi
call   1050 <puts@plt>
xor    %eax,%eax
call   1030 <abort@plt>
lea    0xdbd(%rip),%rdi
call   1050 <puts@plt>
mov    $0x4,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"sulong_test_file\00"
@.str.1 = [2 x i8] c"a\00"
@.str.2 = [21 x i8] c"Failed to open file\0A\00"
@.str.3 = [21 x i8] c"error opening file!\0A\00"
@.str.4 = [13 x i8] c"asdfasdfasdf\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [4 x i8] c"%s\0A\00"
@.str.7 = [22 x i8] c"error removing file!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca [1000 x i8]
store i32 0, i32* %1
%5 = call i64 @remove(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%6 = call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %2
%7 = load i32*, i32** %2
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%11 = call i32 (...) @abort()
unreachable
12: ; preds = %0
%13 = load i32*, i32** %2
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%17 = call i32 @exit(i32 2)
unreachable
18: ; preds = %12
%19 = load i32*, i32** %2
%20 = call i32 @fprintf(i32* %19, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%21 = load i32*, i32** %2
%22 = call i32 @fclose(i32* %21)
%23 = call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %23, i32** %3
%24 = load i32*, i32** %3
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %29
26: ; preds = %18
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%28 = call i32 (...) @abort()
unreachable
29: ; preds = %18
%30 = load i32*, i32** %3
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%34 = call i32 @exit(i32 3)
unreachable
35: ; preds = %29
br label %36
36: ; preds = %41, %35
%37 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 0
%38 = load i32*, i32** %3
%39 = call i32* @fgets(i8* %37, i32 1000, i32* %38)
%40 = icmp ne i32* %39, null
br i1 %40, label %41, label %44
41: ; preds = %36
%42 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 0
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %42)
br label %36
44: ; preds = %36
%45 = call i64 @remove(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %44
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0))
%49 = call i32 @exit(i32 4)
unreachable
50: ; preds = %44
%51 = load i32, i32* %1
ret i32 %51
}

LLVM IR (-O2):
@.str = [17 x i8] c"sulong_test_file\00"
@.str.1 = [2 x i8] c"a\00"
@.str.4 = [13 x i8] c"asdfasdfasdf\00"
@.str.5 = [2 x i8] c"r\00"
@str = [21 x i8] c"error removing file!\00"
@str.9 = [20 x i8] c"Failed to open file\00"
define i32 @main() {
%1 = alloca [1000 x i8]
%2 = tail call i64 @remove(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.9, i64 0, i64 0))
%7 = tail call i32 (...) @abort()
unreachable
8: ; preds = %0
%9 = tail call i64 @fwrite(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i64 12, i64 1, i32* %3)
%10 = tail call i32 @fclose(i32* %3)
%11 = tail call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %8
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.9, i64 0, i64 0))
%15 = tail call i32 (...) @abort()
unreachable
16: ; preds = %8
%17 = getelementptr inbounds [1000 x i8], [1000 x i8]* %1, i64 0, i64 0
%18 = call i32* @fgets(i8* %17, i32 1000, i32* %11)
%19 = icmp eq i32* %18, null
br i1 %19, label %24, label %20
20: ; preds = %16, %20
%21 = call i32 @puts(i8* %17)
%22 = call i32* @fgets(i8* %17, i32 1000, i32* %11)
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %20
24: ; preds = %20, %16
%25 = call i64 @remove(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%26 = icmp eq i64 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %24
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%29 = call i32 @exit(i32 4)
unreachable
30: ; preds = %24
ret i32 0
}