#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*/ printf(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", "w");
  if (writeableFile == NULL) {
    printf("error opening file!\n");
    exit(1);
  }
  char buff[1000];
  if (fgets(buff, 1000, writeableFile) != NULL) {
    printf("error!");
  }
  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
movl   $0x0,-0x4(%rbp)
lea    0xe77(%rip),%rdi
lea    0xe81(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ba <main+0x4a>
lea    0xe68(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xe67(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe61(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xe17(%rip),%rdi
lea    0xe21(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    121a <main+0xaa>
lea    0xe08(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    -0x400(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x3e8,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     1245 <main+0xd5>
lea    0xe1a(%rip),%rdi
call   1050 <printf@plt>
lea    0xdb4(%rip),%rdi
call   1030 <remove@plt>
cmp    $0x0,%rax
je     1271 <main+0x101>
lea    0xdff(%rip),%rdi
call   1050 <printf@plt>
mov    $0x3,%edi
call   1090 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x400,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x3f0,%rsp
lea    0xe71(%rip),%rdi
lea    0xe7b(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1216 <main+0x96>
mov    %rax,%rbx
lea    0xe76(%rip),%rsi
lea    0xe62(%rip),%rdx
mov    %rax,%rdi
call   1080 <fprintf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xe38(%rip),%rdi
lea    0xe42(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1216 <main+0x96>
mov    %rsp,%rdi
mov    $0x3e8,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     11fa <main+0x7a>
lea    0xe50(%rip),%rdi
call   1060 <printf@plt>
lea    0xdff(%rip),%rdi
call   1030 <remove@plt>
test   %rax,%rax
jne    122c <main+0xac>
xor    %eax,%eax
add    $0x3f0,%rsp
pop    %rbx
ret
lea    0xe44(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xe19(%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 = [7 x i8] c"error!\00"
@.str.6 = [22 x i8] c"error removing file!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca [1000 x i8]
store i32 0, i32* %1
%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** %2
%6 = load i32*, i32** %2
%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** %3
%12 = load i32*, i32** %2
%13 = load i8*, i8** %3
%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** %2
%16 = call i32 @fclose(i32* %15)
%17 = 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* %17, i32** %2
%18 = load i32*, i32** %2
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %23
20: ; preds = %11
%21 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %11
%24 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 0
%25 = load i32*, i32** %2
%26 = call i32* @fgets(i8* %24, i32 1000, i32* %25)
%27 = icmp ne i32* %26, null
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %30
30: ; preds = %28, %23
%31 = call i64 @remove(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0))
%35 = call i32 @exit(i32 3)
unreachable
36: ; preds = %30
%37 = load i32, i32* %1
ret i32 %37
}

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 = [7 x i8] c"error!\00"
@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 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.1, 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.8, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %7
%16 = getelementptr inbounds [1000 x i8], [1000 x i8]* %1, i64 0, i64 0
%17 = call i32* @fgets(i8* %16, i32 1000, i32* %10)
%18 = icmp eq i32* %17, null
br i1 %18, label %21, label %19
19: ; preds = %15
%20 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %21
21: ; preds = %19, %15
%22 = call i64 @remove(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%23 = icmp eq i64 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %21
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%26 = call i32 @exit(i32 3)
unreachable
27: ; preds = %21
ret i32 0
}