#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*/ 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 *, char const *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ puts(char *);
scalar_t__ remove(char *);
int main() {
  FILE *writeableFile = fopen("sulong_test_file", "w");
  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", "r+");
  if (writeableFile == NULL) {
    printf("error opening file!\n");
    exit(1);
  }
  fputs("asdfasdfasdf", writeableFile);
  fclose(writeableFile);
  FILE *readableFile = fopen("sulong_test_file", "r+");
  if (readableFile == NULL) {
    printf("error opening file!\n");
    exit(2);
  }
  char buff[1000];
  if (fgets(buff, 1000, readableFile) == NULL) {
    printf("error!");
  }
  puts(buff);
  if (remove("sulong_test_file")) {
    printf("error removing file!\n");
    exit(3);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe57(%rip),%rdi
lea    0xe61(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11da <main+0x4a>
lea    0xe48(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xe47(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe41(%rip),%rsi
call   1090 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
lea    0xdf7(%rip),%rdi
lea    0xe4a(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    123a <main+0xaa>
lea    0xde8(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rsi
lea    0xe18(%rip),%rdi
call   1070 <fputs@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
lea    0xda6(%rip),%rdi
lea    0xdf9(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    128b <main+0xfb>
lea    0xd97(%rip),%rdi
call   1060 <printf@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>
lea    -0x410(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x3e8,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
jne    12b6 <main+0x126>
lea    0xdb9(%rip),%rdi
call   1060 <printf@plt>
lea    -0x410(%rbp),%rdi
call   1040 <puts@plt>
lea    0xd37(%rip),%rdi
call   1030 <remove@plt>
cmp    $0x0,%rax
je     12ee <main+0x15e>
lea    0xd92(%rip),%rdi
call   1060 <printf@plt>
mov    $0x3,%edi
call   10b0 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x3f0,%rsp
lea    0xe61(%rip),%rdi
lea    0xe6b(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1272 <main+0xe2>
mov    %rax,%rbx
lea    0xe62(%rip),%rsi
lea    0xe4e(%rip),%rdx
mov    %rax,%rdi
call   1080 <fprintf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xe24(%rip),%rdi
lea    0xe62(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1272 <main+0xe2>
mov    %rax,%rbx
lea    0xe4d(%rip),%rdi
mov    $0xc,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10b0 <fwrite@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xde4(%rip),%rdi
lea    0xe22(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1288 <main+0xf8>
mov    %rsp,%rdi
mov    $0x3e8,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
jne    124e <main+0xbe>
lea    0xe0c(%rip),%rdi
call   1060 <printf@plt>
mov    %rsp,%rdi
call   1040 <puts@plt>
lea    0xda3(%rip),%rdi
call   1030 <remove@plt>
test   %rax,%rax
jne    129e <main+0x10e>
xor    %eax,%eax
add    $0x3f0,%rsp
pop    %rbx
ret
lea    0xdf8(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xde2(%rip),%rdi
call   1040 <puts@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
lea    0xdb7(%rip),%rdi
call   1040 <puts@plt>
mov    $0x3,%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"error opening file!\0A\00"
@.str.3 = [13 x i8] c"hello world!\00"
@.str.4 = [37 x i8] c"write this to the writeableFile: %s\0A\00"
@.str.5 = [3 x i8] c"r+\00"
@.str.6 = [13 x i8] c"asdfasdfasdf\00"
@.str.7 = [7 x i8] c"error!\00"
@.str.8 = [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 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %0
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8** %3
%13 = load i32*, i32** %2
%14 = load i8*, i8** %3
%15 = call i32 @fprintf(i32* %13, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i8* %14)
%16 = load i32*, i32** %2
%17 = call i32 @fclose(i32* %16)
%18 = call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
store i32* %18, i32** %2
%19 = load i32*, i32** %2
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %24
21: ; preds = %12
%22 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %12
%25 = load i32*, i32** %2
%26 = call i32 @fputs(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i32* %25)
%27 = load i32*, i32** %2
%28 = call i32 @fclose(i32* %27)
%29 = call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
store i32* %29, i32** %4
%30 = load i32*, i32** %4
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %35
32: ; preds = %24
%33 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%34 = call i32 @exit(i32 2)
unreachable
35: ; preds = %24
%36 = getelementptr inbounds [1000 x i8], [1000 x i8]* %5, i64 0, i64 0
%37 = load i32*, i32** %4
%38 = call i32* @fgets(i8* %36, i32 1000, i32* %37)
%39 = icmp eq i32* %38, null
br i1 %39, label %40, label %42
40: ; preds = %35
%41 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
br label %42
42: ; preds = %40, %35
%43 = getelementptr inbounds [1000 x i8], [1000 x i8]* %5, i64 0, i64 0
%44 = call i32 @puts(i8* %43)
%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 = %42
%48 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.8, i64 0, i64 0))
%49 = call i32 @exit(i32 3)
unreachable
50: ; preds = %42
%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"w\00"
@.str.3 = [13 x i8] c"hello world!\00"
@.str.4 = [37 x i8] c"write this to the writeableFile: %s\0A\00"
@.str.5 = [3 x i8] c"r+\00"
@.str.6 = [13 x i8] c"asdfasdfasdf\00"
@.str.7 = [7 x i8] c"error!\00"
@str = [21 x i8] c"error removing file!\00"
@str.11 = [20 x i8] c"error opening 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.11, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %0
%8 = tail call i32 @fprintf(i32* %2, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, 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 ([3 x i8], [3 x i8]* @.str.5, 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.11, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %7
%16 = tail call i64 @fwrite(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, 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 ([3 x i8], [3 x i8]* @.str.5, 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.11, i64 0, i64 0))
%22 = tail call i32 @exit(i32 2)
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 %27, label %29
27: ; preds = %23
%28 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
br label %29
29: ; preds = %27, %23
%30 = call i32 @puts(i8* %24)
%31 = call i64 @remove(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%32 = icmp eq i64 %31, 0
br i1 %32, label %36, label %33
33: ; preds = %29
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%35 = call i32 @exit(i32 3)
unreachable
36: ; preds = %29
ret i32 0
}