#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*/ *);
char *fgets(char *, int const, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fputs(char const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int main(int argc, char **argv) {
  const char *fn = "test.file";
  const static int maxstring = 1024;
  const char *str = "This is a literal C-string.\n";
  printf("writing file\n");
  FILE *fw = fopen(fn, "w");
  for (int i = 0; i < 5; i++) {
    fputs(str, fw);
  }
  fclose(fw);
  printf("done.\n");
  printf("reading file\n");
  char buf[maxstring];
  FILE *fr = fopen(fn, "r");
  char *rc;
  while ((rc = fgets(buf, maxstring, fr))) {
    printf(buf);
  }
  fclose(fr);
  printf("done\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe93(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    0xe92(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    0xea4(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
lea    0xea2(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpl   $0x5,-0x2c(%rbp)
jge    11c8 <main+0x78>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1050 <fputs@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11a3 <main+0x53>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xe5f(%rip),%rdi
call   1040 <printf@plt>
lea    0xe5a(%rip),%rdi
call   1040 <printf@plt>
mov    %rsp,%rax
mov    %rax,-0x38(%rbp)
mov    %rsp,%rax
add    $0xfffffffffffffc00,%rax
mov    %rax,-0x50(%rbp)
mov    %rax,%rsp
mov    -0x18(%rbp),%rdi
lea    0xe41(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x50(%rbp),%rdi
mov    -0x40(%rbp),%rdx
mov    $0x400,%esi
call   1060 <fgets@plt>
mov    %rax,-0x48(%rbp)
cmp    $0x0,%rax
je     1242 <main+0xf2>
mov    -0x50(%rbp),%rdi
call   1040 <printf@plt>
jmp    1214 <main+0xc4>
mov    -0x40(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xdfc(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x400,%rsp
lea    0xeb8(%rip),%rdi
call   1030 <puts@plt>
lea    0xe81(%rip),%r14
lea    0xea1(%rip),%rsi
mov    %r14,%rdi
call   1070 <fopen@plt>
mov    %rax,%rbx
lea    0xe72(%rip),%r15
mov    $0x1c,%esi
mov    $0x1,%edx
mov    %r15,%rdi
mov    %rax,%rcx
call   1080 <fwrite@plt>
mov    $0x1c,%esi
mov    $0x1,%edx
mov    %r15,%rdi
mov    %rbx,%rcx
call   1080 <fwrite@plt>
mov    $0x1c,%esi
mov    $0x1,%edx
mov    %r15,%rdi
mov    %rbx,%rcx
call   1080 <fwrite@plt>
mov    $0x1c,%esi
mov    $0x1,%edx
mov    %r15,%rdi
mov    %rbx,%rcx
call   1080 <fwrite@plt>
mov    $0x1c,%esi
mov    $0x1,%edx
mov    %r15,%rdi
mov    %rbx,%rcx
call   1080 <fwrite@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xe28(%rip),%rdi
call   1030 <puts@plt>
lea    0xe22(%rip),%rdi
call   1030 <puts@plt>
lea    0xe01(%rip),%rsi
mov    %r14,%rdi
call   1070 <fopen@plt>
mov    %rax,%r14
mov    %rsp,%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     126d <main+0x10d>
mov    %rsp,%rbx
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r14,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
jne    1250 <main+0xf0>
mov    %r14,%rdi
call   1040 <fclose@plt>
lea    0xdcf(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [10 x i8] c"test.file\00"
@main.maxstring = internal constant i32 1024
@.str.1 = [29 x i8] c"This is a literal C-string.\0A\00"
@.str.2 = [14 x i8] c"writing file\0A\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [7 x i8] c"done.\0A\00"
@.str.5 = [14 x i8] c"reading file\0A\00"
@.str.6 = [2 x i8] c"r\00"
@.str.7 = [6 x i8] c"done\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32*
%12 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %6
store i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8** %7
%13 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%14 = load i8*, i8** %6
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %15, i32** %8
store i32 0, i32* %9
br label %16
16: ; preds = %23, %2
%17 = load i32, i32* %9
%18 = icmp slt i32 %17, 5
br i1 %18, label %19, label %26
19: ; preds = %16
%20 = load i8*, i8** %7
%21 = load i32*, i32** %8
%22 = call i32 @fputs(i8* %20, i32* %21)
br label %23
23: ; preds = %19
%24 = load i32, i32* %9
%25 = add nsw i32 %24, 1
store i32 %25, i32* %9
br label %16
26: ; preds = %16
%27 = load i32*, i32** %8
%28 = call i32 @fclose(i32* %27)
%29 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%30 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
%31 = call i8* @llvm.stacksave()
store i8* %31, i8** %10
%32 = alloca i8, i64 1024
%33 = load i8*, i8** %6
%34 = call i32* @fopen(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
store i32* %34, i32** %11
br label %35
35: ; preds = %39, %26
%36 = load i32*, i32** %11
%37 = call i8* @fgets(i8* %32, i32 1024, i32* %36)
store i8* %37, i8** %12
%38 = icmp ne i8* %37, null
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = call i32 @printf(i8* %32)
br label %35
41: ; preds = %35
%42 = load i32*, i32** %11
%43 = call i32 @fclose(i32* %42)
%44 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
store i32 0, i32* %3
%45 = load i8*, i8** %10
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
@.str = [10 x i8] c"test.file\00"
@.str.1 = [29 x i8] c"This is a literal C-string.\0A\00"
@.str.3 = [2 x i8] c"w\00"
@.str.6 = [2 x i8] c"r\00"
@str = [13 x i8] c"writing file\00"
@str.8 = [6 x i8] c"done.\00"
@str.9 = [13 x i8] c"reading file\00"
@str.10 = [5 x i8] c"done\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%5 = tail call i64 @fwrite(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i64 28, i64 1, i32* %4)
%6 = tail call i64 @fwrite(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i64 28, i64 1, i32* %4)
%7 = tail call i64 @fwrite(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i64 28, i64 1, i32* %4)
%8 = tail call i64 @fwrite(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i64 28, i64 1, i32* %4)
%9 = tail call i64 @fwrite(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i64 28, i64 1, i32* %4)
%10 = tail call i32 @fclose(i32* %4)
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str.8, i64 0, i64 0))
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.9, i64 0, i64 0))
%13 = alloca [1024 x i8]
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%15 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%16 = call i8* @fgets(i8* %14, i32 1024, i32* %15)
%17 = icmp eq i8* %16, null
br i1 %17, label %22, label %18
18: ; preds = %2, %18
%19 = call i32 @printf(i8* %14)
%20 = call i8* @fgets(i8* %14, i32 1024, i32* %15)
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %18
22: ; preds = %18, %2
%23 = call i32 @fclose(i32* %15)
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.10, i64 0, i64 0))
ret i32 0
}