#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*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int fscanf(int /*<<< orphan*/ *, char *, char *, char *, char *, int *);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  FILE *writeableFile = fopen("sulong_test_file", "w");
  if (writeableFile == NULL) {
    printf("error opening file!\n");
    exit(1);
  }
  fputs("asdfasdf aa 543 -12312 xcvb", writeableFile);
  fclose(writeableFile);
  FILE *readableFile = fopen("sulong_test_file", "r");
  if (readableFile == NULL) {
    printf("error opening file!\n");
    exit(2);
  }
  char buf[100];
  char c1, c2;
  int i1;
  if (fscanf(readableFile, "%s %c%c %d xcvb", buf, &c1, &c2, &i1) != 4) {
    abort();
  }
  fclose(readableFile);
  printf("%s %c %c %d\n", buf, c1, c2, i1);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
lea    0xe7e(%rip),%rdi
lea    0xe88(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11b5 <main+0x45>
lea    0xe6f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%rsi
lea    0xe68(%rip),%rdi
call   1060 <fputs@plt>
mov    -0x8(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xe2b(%rip),%rdi
lea    0xe68(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1208 <main+0x98>
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rdi
lea    -0x80(%rbp),%rdx
lea    0xe2f(%rip),%rsi
lea    -0x81(%rbp),%rcx
lea    -0x82(%rbp),%r8
lea    -0x88(%rbp),%r9
call   1070 <fscanf@plt>
cmp    $0x4,%eax
je     1241 <main+0xd1>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
lea    -0x80(%rbp),%rsi
movsbl -0x81(%rbp),%edx
movsbl -0x82(%rbp),%ecx
mov    -0x88(%rbp),%r8d
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x80,%rsp
lea    0xe71(%rip),%rdi
lea    0xe7b(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1240 <main+0xc0>
mov    %rax,%rbx
lea    0xe65(%rip),%rdi
mov    $0x1b,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10a0 <fwrite@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xe31(%rip),%rdi
lea    0xe59(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1256 <main+0xd6>
mov    %rax,%rbx
lea    0xe47(%rip),%rsi
lea    0x10(%rsp),%rdx
lea    0xb(%rsp),%rcx
lea    0xa(%rsp),%r8
lea    0xc(%rsp),%r9
mov    %rax,%rdi
call   1070 <fscanf@plt>
cmp    $0x4,%eax
jne    126c <main+0xec>
mov    %rbx,%rdi
call   1050 <fclose@plt>
movsbl 0xb(%rsp),%edx
movsbl 0xa(%rsp),%ecx
mov    0xc(%rsp),%r8d
lea    0xe18(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbx
ret
lea    0xe07(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xdf1(%rip),%rdi
call   1040 <puts@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
xor    %eax,%eax
call   1030 <abort@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 = [28 x i8] c"asdfasdf aa 543 -12312 xcvb\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [16 x i8] c"%s %c%c %d xcvb\00"
@.str.6 = [13 x i8] c"%s %c %c %d\0A\00"
define i32 @main() {
%1 = alloca i32*
%2 = alloca i32*
%3 = alloca [100 x i8]
%4 = alloca i8
%5 = alloca i8
%6 = alloca i32
%7 = 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* %7, i32** %1
%8 = load i32*, i32** %1
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = load i32*, i32** %1
%15 = call i32 @fputs(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32* %14)
%16 = load i32*, i32** %1
%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 ([2 x i8], [2 x i8]* @.str.4, 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 = %13
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @exit(i32 2)
unreachable
24: ; preds = %13
%25 = load i32*, i32** %2
%26 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%27 = call i32 @fscanf(i32* %25, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i8* %26, i8* %4, i8* %5, i32* %6)
%28 = icmp ne i32 %27, 4
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = call i32 (...) @abort()
unreachable
31: ; preds = %24
%32 = load i32*, i32** %2
%33 = call i32 @fclose(i32* %32)
%34 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%35 = load i8, i8* %4
%36 = sext i8 %35 to i32
%37 = load i8, i8* %5
%38 = sext i8 %37 to i32
%39 = load i32, i32* %6
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i8* %34, i32 %36, i32 %38, i32 %39)
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 = [28 x i8] c"asdfasdf aa 543 -12312 xcvb\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [16 x i8] c"%s %c%c %d xcvb\00"
@.str.6 = [13 x i8] c"%s %c %c %d\0A\00"
@str.7 = [20 x i8] c"error opening file!\00"
define i32 @main() {
%1 = alloca [100 x i8]
%2 = alloca i8
%3 = alloca i8
%4 = alloca i32
%5 = 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))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.7, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %0
%11 = tail call i64 @fwrite(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i64 27, i64 1, i32* %5)
%12 = tail call i32 @fclose(i32* %5)
%13 = 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.4, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.7, i64 0, i64 0))
%17 = tail call i32 @exit(i32 2)
unreachable
18: ; preds = %10
%19 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%20 = bitcast i32* %4 to i8*
%21 = call i32 @fscanf(i32* %13, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i8* %19, i8* %2, i8* %3, i32* %4)
%22 = icmp eq i32 %21, 4
br i1 %22, label %25, label %23
23: ; preds = %18
%24 = call i32 (...) @abort()
unreachable
25: ; preds = %18
%26 = call i32 @fclose(i32* %13)
%27 = load i8, i8* %2
%28 = sext i8 %27 to i32
%29 = load i8, i8* %3
%30 = sext i8 %29 to i32
%31 = load i32, i32* %4
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i8* %19, i32 %28, i32 %30, i32 %31)
ret i32 0
}