#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() {
  FILE *writeableFile = fopen("sulong_test_file", "w");
  if (writeableFile == NULL) {
    printf("Failed to open file\n");
    abort();
  }
  if (writeableFile == NULL) {
    printf("error opening file!\n");
    exit(1);
  }
  const char *text = "hello world!";
  fprintf(writeableFile, "write this to the writeableFile: %s\n", text);
  fclose(writeableFile);
  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    $0x410,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe67(%rip),%rdi
lea    0xe71(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c9 <main+0x49>
lea    0xe58(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
cmpq   $0x0,-0x10(%rbp)
jne    11ec <main+0x6c>
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xe4a(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe44(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
lea    0xde3(%rip),%rdi
lea    0xe4b(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    124d <main+0xcd>
lea    0xdd4(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
cmpq   $0x0,-0x10(%rbp)
jne    1270 <main+0xf0>
lea    0xdc9(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rdi
lea    0xdf6(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
lea    0xd6e(%rip),%rdi
lea    0xde5(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12c2 <main+0x142>
lea    0xd5f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
cmpq   $0x0,-0x20(%rbp)
jne    12e5 <main+0x165>
lea    0xd54(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x3,%edi
call   10a0 <exit@plt>
jmp    12ea <main+0x16a>
lea    -0x410(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x3e8,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     1323 <main+0x1a3>
lea    -0x410(%rbp),%rsi
lea    0xd54(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    12ea <main+0x16a>
lea    0xcd6(%rip),%rdi
call   1040 <remove@plt>
cmp    $0x0,%rax
je     1351 <main+0x1d1>
lea    0xd40(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x4,%edi
call   10a0 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x3e8,%rsp
lea    0xe5f(%rip),%rdi
lea    0xe69(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     128b <main+0xfb>
mov    %rax,%rbx
lea    0xe60(%rip),%rsi
lea    0xe4c(%rip),%rdx
mov    %rax,%rdi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
lea    0xe20(%rip),%rdi
lea    0xe5e(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     128b <main+0xfb>
mov    %rax,%rbx
lea    0xe48(%rip),%rdi
mov    $0xc,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10b0 <fwrite@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
lea    0xde0(%rip),%rdi
lea    0xe2d(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     128b <main+0xfb>
mov    %rax,%r14
mov    %rsp,%rdi
mov    $0x3e8,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     126d <main+0xdd>
mov    %rsp,%rbx
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1050 <puts@plt>
mov    %rbx,%rdi
mov    $0x3e8,%esi
mov    %r14,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
jne    1250 <main+0xc0>
lea    0xd8c(%rip),%rdi
call   1040 <remove@plt>
test   %rax,%rax
jne    129e <main+0x10e>
xor    %eax,%eax
add    $0x3e8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdd9(%rip),%rdi
call   1050 <puts@plt>
xor    %eax,%eax
call   1030 <abort@plt>
lea    0xdb1(%rip),%rdi
call   1050 <puts@plt>
mov    $0x4,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"sulong_test_file\00"
@.str.1 = [2 x i8] c"w\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"hello world!\00"
@.str.5 = [37 x i8] c"write this to the writeableFile: %s\0A\00"
@.str.6 = [2 x i8] c"a\00"
@.str.7 = [13 x i8] c"asdfasdfasdf\00"
@.str.8 = [2 x i8] c"r\00"
@.str.9 = [4 x i8] c"%s\0A\00"
@.str.10 = [22 x i8] c"error removing file!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca [1000 x i8]
store i32 0, i32* %1
%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 1)
unreachable
18: ; preds = %12
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8** %3
%19 = load i32*, i32** %2
%20 = load i8*, i8** %3
%21 = call i32 (i32*, i8*, ...) @fprintf(i32* %19, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i8* %20)
%22 = load i32*, i32** %2
%23 = call i32 @fclose(i32* %22)
%24 = 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.6, i64 0, i64 0))
store i32* %24, i32** %2
%25 = load i32*, i32** %2
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %30
27: ; preds = %18
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%29 = call i32 (...) @abort()
unreachable
30: ; preds = %18
%31 = load i32*, i32** %2
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%35 = call i32 @exit(i32 2)
unreachable
36: ; preds = %30
%37 = load i32*, i32** %2
%38 = call i32 (i32*, i8*, ...) @fprintf(i32* %37, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
%39 = load i32*, i32** %2
%40 = call i32 @fclose(i32* %39)
%41 = 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.8, i64 0, i64 0))
store i32* %41, i32** %4
%42 = load i32*, i32** %4
%43 = icmp eq i32* %42, null
br i1 %43, label %44, label %47
44: ; preds = %36
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%46 = call i32 (...) @abort()
unreachable
47: ; preds = %36
%48 = load i32*, i32** %4
%49 = icmp eq i32* %48, null
br i1 %49, label %50, label %53
50: ; preds = %47
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%52 = call i32 @exit(i32 3)
unreachable
53: ; preds = %47
br label %54
54: ; preds = %59, %53
%55 = getelementptr inbounds [1000 x i8], [1000 x i8]* %5, i64 0, i64 0
%56 = load i32*, i32** %4
%57 = call i32* @fgets(i8* %55, i32 1000, i32* %56)
%58 = icmp ne i32* %57, null
br i1 %58, label %59, label %62
59: ; preds = %54
%60 = getelementptr inbounds [1000 x i8], [1000 x i8]* %5, i64 0, i64 0
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.9, i64 0, i64 0), i8* %60)
br label %54
62: ; preds = %54
%63 = call i64 @remove(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%64 = icmp ne i64 %63, 0
br i1 %64, label %65, label %68
65: ; preds = %62
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.10, i64 0, i64 0))
%67 = call i32 @exit(i32 4)
unreachable
68: ; preds = %62
%69 = load i32, i32* %1
ret i32 %69
}

LLVM IR (-O2):
@.str = [17 x i8] c"sulong_test_file\00"
@.str.1 = [2 x i8] c"w\00"
@.str.4 = [13 x i8] c"hello world!\00"
@.str.5 = [37 x i8] c"write this to the writeableFile: %s\0A\00"
@.str.6 = [2 x i8] c"a\00"
@.str.7 = [13 x i8] c"asdfasdfasdf\00"
@.str.8 = [2 x i8] c"r\00"
@str = [21 x i8] c"error removing file!\00"
@str.13 = [20 x i8] c"Failed to open file\00"
define i32 @main() {
%1 = alloca [1000 x i8]
%2 = 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))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %7
4: ; preds = %0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.13, i64 0, i64 0))
%6 = tail call i32 (...) @abort()
unreachable
7: ; preds = %0
%8 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %2, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%9 = tail call i32 @fclose(i32* %2)
%10 = 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.6, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %15
12: ; preds = %7
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.13, i64 0, i64 0))
%14 = tail call i32 (...) @abort()
unreachable
15: ; preds = %7
%16 = tail call i64 @fwrite(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0), i64 12, i64 1, i32* %10)
%17 = tail call i32 @fclose(i32* %10)
%18 = 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.8, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.13, i64 0, i64 0))
%22 = tail call i32 (...) @abort()
unreachable
23: ; preds = %15
%24 = getelementptr inbounds [1000 x i8], [1000 x i8]* %1, i64 0, i64 0
%25 = call i32* @fgets(i8* %24, i32 1000, i32* %18)
%26 = icmp eq i32* %25, null
br i1 %26, label %31, label %27
27: ; preds = %23, %27
%28 = call i32 @puts(i8* %24)
%29 = call i32* @fgets(i8* %24, i32 1000, i32* %18)
%30 = icmp eq i32* %29, null
br i1 %30, label %31, label %27
31: ; preds = %27, %23
%32 = call i64 @remove(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%33 = icmp eq i64 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %31
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%36 = call i32 @exit(i32 4)
unreachable
37: ; preds = %31
ret i32 0
}