#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int main(int argc, char *argv[]) {
  printf("starting");
  printf("\n");
  int numItems;
  FILE *infile = fopen("test.dat", "r");
  if (infile != NULL) {
    printf("Reading File");
    printf("\n");
    fscanf(infile, "%d", &numItems);
    printf("data is read");
    char line[255];
    sprintf(line, "%d", numItems);
    puts(line);
  }
  fclose(infile);
  numItems = 42;
  FILE *outfile = fopen("test.dat", "w");
  printf("Writing file");
  fprintf(outfile, "%d", numItems);
  fclose(outfile);
  printf("Ending");
  printf("\n");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe70(%rip),%rdi
call   1050 <printf@plt>
lea    0xe6d(%rip),%rdi
call   1050 <printf@plt>
lea    0xe63(%rip),%rdi
lea    0xe65(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     121d <main+0xad>
lea    0xe4c(%rip),%rdi
call   1050 <printf@plt>
lea    0xe33(%rip),%rdi
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
lea    0xe3d(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1060 <fscanf@plt>
lea    0xe30(%rip),%rdi
call   1050 <printf@plt>
lea    -0x120(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xe17(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x120(%rbp),%rdi
call   1030 <puts@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x2a,-0x14(%rbp)
lea    0xdd7(%rip),%rdi
lea    0xdf8(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x128(%rbp)
lea    0xde7(%rip),%rdi
call   1050 <printf@plt>
mov    -0x128(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xdbf(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x128(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xdc6(%rip),%rdi
call   1050 <printf@plt>
lea    0xd81(%rip),%rdi
call   1050 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x110,%rsp
lea    0xe6d(%rip),%rdi
call   1060 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0xe60(%rip),%rdi
lea    0xe62(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1212 <main+0x92>
lea    0xe50(%rip),%rdi
call   1060 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0xe47(%rip),%r14
lea    0xc(%rsp),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <fscanf@plt>
lea    0xe33(%rip),%rdi
call   1060 <printf@plt>
mov    0xc(%rsp),%edx
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    %r14,%rsi
call   10a0 <sprintf@plt>
mov    %r15,%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
movl   $0x2a,0xc(%rsp)
lea    0xde0(%rip),%rdi
lea    0xe01(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%rbx
lea    0xdf4(%rip),%rdi
call   1060 <printf@plt>
mov    0xc(%rsp),%edx
lea    0xdd2(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fprintf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xdda(%rip),%rdi
call   1060 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
add    $0x110,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [9 x i8] c"starting\00"
@.str.1 = [2 x i8] c"\0A\00"
@.str.2 = [9 x i8] c"test.dat\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [13 x i8] c"Reading File\00"
@.str.5 = [3 x i8] c"%d\00"
@.str.6 = [13 x i8] c"data is read\00"
@.str.7 = [2 x i8] c"w\00"
@.str.8 = [13 x i8] c"Writing file\00"
@.str.9 = [7 x i8] c"Ending\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32*
%8 = alloca [255 x i8]
%9 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %12, i32** %7
%13 = load i32*, i32** %7
%14 = icmp ne i32* %13, null
br i1 %14, label %15, label %26
15: ; preds = %2
%16 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%17 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32*, i32** %7
%19 = call i32 @fscanf(i32* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %6)
%20 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
%21 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%22 = load i32, i32* %6
%23 = call i32 @sprintf(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 %22)
%24 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%25 = call i32 @puts(i8* %24)
br label %26
26: ; preds = %15, %2
%27 = load i32*, i32** %7
%28 = call i32 @fclose(i32* %27)
store i32 42, i32* %6
%29 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
store i32* %29, i32** %9
%30 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0))
%31 = load i32*, i32** %9
%32 = load i32, i32* %6
%33 = call i32 @fprintf(i32* %31, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 %32)
%34 = load i32*, i32** %9
%35 = call i32 @fclose(i32* %34)
%36 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.9, i64 0, i64 0))
%37 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
@.str = [9 x i8] c"starting\00"
@.str.2 = [9 x i8] c"test.dat\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [13 x i8] c"Reading File\00"
@.str.5 = [3 x i8] c"%d\00"
@.str.6 = [13 x i8] c"data is read\00"
@.str.7 = [2 x i8] c"w\00"
@.str.8 = [13 x i8] c"Writing file\00"
@.str.9 = [7 x i8] c"Ending\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca [255 x i8]
%5 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 @putchar(i32 10)
%7 = bitcast i32* %3 to i8*
%8 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %19, label %10
10: ; preds = %2
%11 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%12 = tail call i32 @putchar(i32 10)
%13 = call i32 @fscanf(i32* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %3)
%14 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
%15 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%16 = load i32, i32* %3
%17 = call i32 @sprintf(i8* %15, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 %16)
%18 = call i32 @puts(i8* %15)
br label %19
19: ; preds = %10, %2
%20 = call i32 @fclose(i32* %8)
store i32 42, i32* %3
%21 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
%22 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0))
%23 = load i32, i32* %3
%24 = call i32 @fprintf(i32* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 %23)
%25 = call i32 @fclose(i32* %21)
%26 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.9, i64 0, i64 0))
%27 = call i32 @putchar(i32 10)
ret i32 0
}