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


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x400,%rsp
lea    0xe6e(%rip),%rdi
lea    0xe78(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11c3 <main+0x43>
lea    0xe5f(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xe5e(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe58(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x8(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2df6(%rip),%rcx
cmp    (%rcx),%rax
jne    1205 <main+0x85>
mov    $0x4,%edi
call   10a0 <exit@plt>
lea    0xdf4(%rip),%rdi
lea    0xe47(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    123d <main+0xbd>
lea    0xde5(%rip),%rdi
call   1050 <printf@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
lea    -0x400(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x3e8,%esi
call   1070 <fgets@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2d86(%rip),%rcx
cmp    (%rcx),%rax
jne    1275 <main+0xf5>
mov    $0x4,%edi
call   10a0 <exit@plt>
lea    -0x400(%rbp),%rdi
mov    0x2d55(%rip),%rax
mov    (%rax),%esi
call   1060 <fputs@plt>
lea    0xd6f(%rip),%rdi
call   1030 <remove@plt>
cmp    $0x0,%rax
je     12b6 <main+0x136>
lea    0xdb5(%rip),%rdi
call   1050 <printf@plt>
mov    $0x3,%edi
call   10a0 <exit@plt>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x3e8,%rsp
lea    0xe6f(%rip),%rdi
lea    0xe79(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1244 <main+0xc4>
mov    %rax,%rbx
lea    0xe70(%rip),%rsi
lea    0xe5c(%rip),%rdx
mov    %rax,%rdi
call   1080 <fprintf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    0x2e1a(%rip),%r14
cmp    (%r14),%rax
je     123a <main+0xba>
lea    0xe26(%rip),%rdi
lea    0xe64(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     125a <main+0xda>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x3e8,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
cmp    (%r14),%rax
je     123a <main+0xba>
mov    0x2dc6(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   1060 <fputs@plt>
lea    0xddd(%rip),%rdi
call   1030 <remove@plt>
test   %rax,%rax
jne    1270 <main+0xf0>
xor    %eax,%eax
add    $0x3e8,%rsp
pop    %rbx
pop    %r14
ret
mov    $0x4,%edi
call   10a0 <exit@plt>
lea    0xe11(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xdfb(%rip),%rdi
call   1040 <puts@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
lea    0xdd0(%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"
@EOF = global i64 0
@.str.5 = [2 x i8] c"r\00"
@stdout = global i32 0
@.str.6 = [22 x i8] c"error removing file!\0A\00"
define i32 @main() {
%1 = alloca i32*
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca [1000 x i8]
%5 = 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* %5, i32** %1
%6 = load i32*, i32** %1
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %0
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8** %2
%12 = load i32*, i32** %1
%13 = load i8*, i8** %2
%14 = call i32 @fprintf(i32* %12, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i8* %13)
%15 = load i32*, i32** %1
%16 = call i64 @fclose(i32* %15)
%17 = load i64, i64* @EOF
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %21
19: ; preds = %11
%20 = call i32 @exit(i32 4)
unreachable
21: ; preds = %11
%22 = 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* %22, i32** %3
%23 = load i32*, i32** %3
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%27 = call i32 @exit(i32 2)
unreachable
28: ; preds = %21
%29 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 0
%30 = load i32*, i32** %3
%31 = call i32 @fgets(i8* %29, i32 1000, i32* %30)
%32 = load i32*, i32** %3
%33 = call i64 @fclose(i32* %32)
%34 = load i64, i64* @EOF
%35 = icmp eq i64 %33, %34
br i1 %35, label %36, label %38
36: ; preds = %28
%37 = call i32 @exit(i32 4)
unreachable
38: ; preds = %28
%39 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 0
%40 = load i32, i32* @stdout
%41 = call i32 @fputs(i8* %39, i32 %40)
%42 = call i64 @remove(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %38
%45 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0))
%46 = call i32 @exit(i32 3)
unreachable
47: ; preds = %38
ret i32 0
}

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"
@EOF = global i64 0
@.str.5 = [2 x i8] c"r\00"
@stdout = global i32 0
@str = [21 x i8] c"error removing file!\00"
@str.8 = [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.8, 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 i64 @fclose(i32* %2)
%10 = load i64, i64* @EOF
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = tail call i32 @exit(i32 4)
unreachable
14: ; preds = %7
%15 = 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))
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.8, i64 0, i64 0))
%19 = tail call i32 @exit(i32 2)
unreachable
20: ; preds = %14
%21 = getelementptr inbounds [1000 x i8], [1000 x i8]* %1, i64 0, i64 0
%22 = call i32 @fgets(i8* %21, i32 1000, i32* %15)
%23 = call i64 @fclose(i32* %15)
%24 = load i64, i64* @EOF
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = call i32 @exit(i32 4)
unreachable
28: ; preds = %20
%29 = load i32, i32* @stdout
%30 = call i32 @fputs(i8* %21, i32 %29)
%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 = %28
%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 = %28
ret i32 0
}