#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*/ freddy;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  FILE *f = fopen("fred.txt", "w");
  fwrite("hello\nhello\n", 1, 12, f);
  fclose(f);
  char freddy[7];
  f = fopen("fred.txt", "r");
  if (fread(freddy, 1, 6, f) != 6)
    printf("couldn't read fred.txt\n");
  freddy[6] = '\0';
  fclose(f);
  printf("%s", freddy);
  int InChar;
  char ShowChar;
  f = fopen("fred.txt", "r");
  while ((InChar = fgetc(f)) != EOF) {
    ShowChar = InChar;
    if (ShowChar < ' ')
      ShowChar = '.';
    printf("ch: %d '%c'\n", InChar, ShowChar);
  }
  fclose(f);
  f = fopen("fred.txt", "r");
  while ((InChar = getc(f)) != EOF) {
    ShowChar = InChar;
    if (ShowChar < ' ')
      ShowChar = '.';
    printf("ch: %d '%c'\n", InChar, ShowChar);
  }
  fclose(f);
  f = fopen("fred.txt", "r");
  while (fgets(freddy, sizeof(freddy), f) != NULL)
    printf("x: %s", freddy);
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe6a(%rip),%rdi
lea    0xe6c(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rcx
lea    0xe5a(%rip),%rdi
mov    $0x1,%esi
mov    $0xc,%edx
call   1090 <fwrite@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xe30(%rip),%rdi
lea    0xe41(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
lea    -0x17(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x6,%edx
call   1030 <fread@plt>
cmp    $0x6,%eax
je     120e <main+0x8e>
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movb   $0x0,-0x11(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
lea    -0x17(%rbp),%rsi
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xdcc(%rip),%rdi
lea    0xddd(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d91(%rip),%rcx
cmp    (%rcx),%eax
je     1290 <main+0x110>
mov    -0x1c(%rbp),%eax
mov    %al,-0x1d(%rbp)
movsbl -0x1d(%rbp),%eax
cmp    $0x20,%eax
jge    1276 <main+0xf6>
movb   $0x2e,-0x1d(%rbp)
mov    -0x1c(%rbp),%esi
movsbl -0x1d(%rbp),%edx
lea    0xdae(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1244 <main+0xc4>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xd60(%rip),%rdi
lea    0xd71(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   10a0 <getc@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d25(%rip),%rcx
cmp    (%rcx),%eax
je     12fc <main+0x17c>
mov    -0x1c(%rbp),%eax
mov    %al,-0x1d(%rbp)
movsbl -0x1d(%rbp),%eax
cmp    $0x20,%eax
jge    12e2 <main+0x162>
movb   $0x2e,-0x1d(%rbp)
mov    -0x1c(%rbp),%esi
movsbl -0x1d(%rbp),%edx
lea    0xd42(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12b0 <main+0x130>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xcf4(%rip),%rdi
lea    0xd05(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
lea    -0x17(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x7,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     134f <main+0x1cf>
lea    -0x17(%rbp),%rsi
lea    0xcfc(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    131c <main+0x19c>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
lea    0xe94(%rip),%r14
lea    0xe5b(%rip),%rsi
mov    %r14,%rdi
call   1090 <fopen@plt>
mov    %rax,%rbx
lea    0xe4b(%rip),%rdi
mov    $0x1,%esi
mov    $0xc,%edx
mov    %rax,%rcx
call   10a0 <fwrite@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xe37(%rip),%rsi
mov    %r14,%rdi
call   1090 <fopen@plt>
mov    %rax,%rbx
lea    0x1(%rsp),%rdi
mov    $0x1,%esi
mov    $0x6,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
cmp    $0x6,%eax
je     120b <main+0x7b>
lea    0xe1e(%rip),%rdi
call   1030 <puts@plt>
movb   $0x0,0x7(%rsp)
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xe02(%rip),%rdi
lea    0x1(%rsp),%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe00(%rip),%rdi
lea    0xdd6(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <fgetc@plt>
mov    0x2d98(%rip),%r15
cmp    (%r15),%eax
je     1291 <main+0x101>
mov    $0x2e,%ebp
lea    0xdb0(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmp    $0x20,%al
movsbl %al,%edx
cmovl  %ebp,%edx
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1070 <fgetc@plt>
cmp    (%r15),%eax
jne    1270 <main+0xe0>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xd92(%rip),%rdi
lea    0xd68(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   10b0 <getc@plt>
cmp    (%r15),%eax
je     12f1 <main+0x161>
mov    $0x2e,%ebp
lea    0xd49(%rip),%r14
nopl   0x0(%rax,%rax,1)
cmp    $0x20,%al
movsbl %al,%edx
cmovl  %ebp,%edx
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   10b0 <getc@plt>
cmp    (%r15),%eax
jne    12d0 <main+0x140>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xd32(%rip),%rdi
lea    0xd08(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%r15
lea    0x1(%rsp),%rdi
mov    $0x7,%esi
mov    %rax,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     1362 <main+0x1d2>
lea    0xcf1(%rip),%r14
lea    0x1(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
mov    $0x7,%esi
mov    %r15,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
jne    1340 <main+0x1b0>
mov    %r15,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"fred.txt\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [13 x i8] c"hello\0Ahello\0A\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [24 x i8] c"couldn't read fred.txt\0A\00"
@.str.5 = [3 x i8] c"%s\00"
@EOF = global i32 0
@.str.6 = [13 x i8] c"ch: %d '%c'\0A\00"
@.str.7 = [6 x i8] c"x: %s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [7 x i8]
%4 = alloca i32
%5 = alloca i8
store i32 0, i32* %1
%6 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %2
%7 = load i32*, i32** %2
%8 = call i32 @fwrite(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 1, i32 12, i32* %7)
%9 = load i32*, i32** %2
%10 = call i32 @fclose(i32* %9)
%11 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %11, i32** %2
%12 = getelementptr inbounds [7 x i8], [7 x i8]* %3, i64 0, i64 0
%13 = load i32*, i32** %2
%14 = call i32 @fread(i8* %12, i32 1, i32 6, i32* %13)
%15 = icmp ne i32 %14, 6
br i1 %15, label %16, label %18
16: ; preds = %0
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
br label %18
18: ; preds = %16, %0
%19 = getelementptr inbounds [7 x i8], [7 x i8]* %3, i64 0, i64 6
store i8 0, i8* %19
%20 = load i32*, i32** %2
%21 = call i32 @fclose(i32* %20)
%22 = getelementptr inbounds [7 x i8], [7 x i8]* %3, i64 0, i64 0
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %22)
%24 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %24, i32** %2
br label %25
25: ; preds = %37, %18
%26 = load i32*, i32** %2
%27 = call i32 @fgetc(i32* %26)
store i32 %27, i32* %4
%28 = load i32, i32* @EOF
%29 = icmp ne i32 %27, %28
br i1 %29, label %30, label %42
30: ; preds = %25
%31 = load i32, i32* %4
%32 = trunc i32 %31 to i8
store i8 %32, i8* %5
%33 = load i8, i8* %5
%34 = sext i8 %33 to i32
%35 = icmp slt i32 %34, 32
br i1 %35, label %36, label %37
36: ; preds = %30
store i8 46, i8* %5
br label %37
37: ; preds = %36, %30
%38 = load i32, i32* %4
%39 = load i8, i8* %5
%40 = sext i8 %39 to i32
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i32 %38, i32 %40)
br label %25
42: ; preds = %25
%43 = load i32*, i32** %2
%44 = call i32 @fclose(i32* %43)
%45 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %45, i32** %2
br label %46
46: ; preds = %58, %42
%47 = load i32*, i32** %2
%48 = call i32 @getc(i32* %47)
store i32 %48, i32* %4
%49 = load i32, i32* @EOF
%50 = icmp ne i32 %48, %49
br i1 %50, label %51, label %63
51: ; preds = %46
%52 = load i32, i32* %4
%53 = trunc i32 %52 to i8
store i8 %53, i8* %5
%54 = load i8, i8* %5
%55 = sext i8 %54 to i32
%56 = icmp slt i32 %55, 32
br i1 %56, label %57, label %58
57: ; preds = %51
store i8 46, i8* %5
br label %58
58: ; preds = %57, %51
%59 = load i32, i32* %4
%60 = load i8, i8* %5
%61 = sext i8 %60 to i32
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i32 %59, i32 %61)
br label %46
63: ; preds = %46
%64 = load i32*, i32** %2
%65 = call i32 @fclose(i32* %64)
%66 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %66, i32** %2
br label %67
67: ; preds = %72, %63
%68 = getelementptr inbounds [7 x i8], [7 x i8]* %3, i64 0, i64 0
%69 = load i32*, i32** %2
%70 = call i32* @fgets(i8* %68, i32 7, i32* %69)
%71 = icmp ne i32* %70, null
br i1 %71, label %72, label %75
72: ; preds = %67
%73 = getelementptr inbounds [7 x i8], [7 x i8]* %3, i64 0, i64 0
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i8* %73)
br label %67
75: ; preds = %67
%76 = load i32*, i32** %2
%77 = call i32 @fclose(i32* %76)
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"fred.txt\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [13 x i8] c"hello\0Ahello\0A\00"
@.str.3 = [2 x i8] c"r\00"
@.str.5 = [3 x i8] c"%s\00"
@EOF = global i32 0
@.str.6 = [13 x i8] c"ch: %d '%c'\0A\00"
@.str.7 = [6 x i8] c"x: %s\00"
@str = [23 x i8] c"couldn't read fred.txt\00"
define i32 @main() {
%1 = alloca [7 x i8]
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%3 = tail call i32 @fwrite(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 1, i32 12, i32* %2)
%4 = tail call i32 @fclose(i32* %2)
%5 = getelementptr inbounds [7 x i8], [7 x i8]* %1, i64 0, i64 0
%6 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%7 = call i32 @fread(i8* %5, i32 1, i32 6, i32* %6)
%8 = icmp eq i32 %7, 6
br i1 %8, label %11, label %9
9: ; preds = %0
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
br label %11
11: ; preds = %9, %0
%12 = getelementptr inbounds [7 x i8], [7 x i8]* %1, i64 0, i64 6
store i8 0, i8* %12
%13 = tail call i32 @fclose(i32* %6)
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %5)
%15 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%16 = call i32 @fgetc(i32* %15)
%17 = load i32, i32* @EOF
%18 = icmp eq i32 %16, %17
br i1 %18, label %30, label %19
19: ; preds = %11, %19
%20 = phi i32 [ %27, %19 ], [ %16, %11 ]
%21 = trunc i32 %20 to i8
%22 = icmp slt i8 %21, 32
%23 = shl i32 %20, 24
%24 = ashr exact i32 %23, 24
%25 = select i1 %22, i32 46, i32 %24
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i32 %20, i32 %25)
%27 = call i32 @fgetc(i32* %15)
%28 = load i32, i32* @EOF
%29 = icmp eq i32 %27, %28
br i1 %29, label %30, label %19
30: ; preds = %19, %11
%31 = call i32 @fclose(i32* %15)
%32 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%33 = call i32 @getc(i32* %32)
%34 = load i32, i32* @EOF
%35 = icmp eq i32 %33, %34
br i1 %35, label %47, label %36
36: ; preds = %30, %36
%37 = phi i32 [ %44, %36 ], [ %33, %30 ]
%38 = trunc i32 %37 to i8
%39 = icmp slt i8 %38, 32
%40 = shl i32 %37, 24
%41 = ashr exact i32 %40, 24
%42 = select i1 %39, i32 46, i32 %41
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i32 %37, i32 %42)
%44 = call i32 @getc(i32* %32)
%45 = load i32, i32* @EOF
%46 = icmp eq i32 %44, %45
br i1 %46, label %47, label %36
47: ; preds = %36, %30
%48 = call i32 @fclose(i32* %32)
%49 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%50 = call i32* @fgets(i8* %5, i32 7, i32* %49)
%51 = icmp eq i32* %50, null
br i1 %51, label %56, label %52
52: ; preds = %47, %52
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i8* %5)
%54 = call i32* @fgets(i8* %5, i32 7, i32* %49)
%55 = icmp eq i32* %54, null
br i1 %55, label %56, label %52
56: ; preds = %52, %47
%57 = call i32 @fclose(i32* %49)
ret i32 0
}