#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 */
struct stat {
  scalar_t__ st_size;
};
typedef int /*<<< orphan*/ list;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
char EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
void *fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stat(char *, struct stat *);
int main(void) {
  FILE *fp = fopen("fgetc.temp", "w");
  char list[] = {'x', 255, -1, 'x', '\n', 'E', 'N', 'D'};
  printf("writing %lu bytes to %s; please view it with xxd.\n", sizeof(list),
         "fgetc.temp");
  fwrite(list, sizeof(list), 1, fp);
  fclose(fp);
  struct stat s;
  stat("fgetc.temp", &s);
  printf("file size is: %u\n", (int)s.st_size);
  printf("This loop won't read all bytes in the file:\n");
  fp = fopen("fgetc.temp", "r");
  char c;
  do {
    c = fgetc(fp);
    printf("read byte: %c (decimal value is %d)\n", c, c);
  } while (c != EOF);
  if (feof(fp) == 0)
    printf("we exited the do-while loop because we thought we hit EOF, but "
           "feof() says we didn't.\n");
  fclose(fp);
  printf("This loop will read all bytes in the file:\n");
  fp = fopen("fgetc.temp", "r");
  int i;
  do {
    i = fgetc(fp);
    printf("read byte: %c (decimal value is %d)\n", (char)i, i);
  } while (i != EOF);
  if (feof(fp) == 0)
    printf("we exited the do-while loop because we thought we hit EOF, but "
           "feof() says we didn't.\n");
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe7a(%rip),%rdi
lea    0xe7e(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    0xf8c(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    0xe65(%rip),%rdi
mov    $0x8,%esi
lea    0xe4c(%rip),%rdx
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x8,%esi
mov    $0x1,%edx
call   1090 <fwrite@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xe1e(%rip),%rdi
lea    -0x20(%rbp),%rsi
call   1070 <stat@plt>
mov    -0x20(%rbp),%rax
mov    %eax,%esi
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xdec(%rip),%rdi
lea    0xe64(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %al,-0x21(%rbp)
movsbl -0x21(%rbp),%esi
movsbl -0x21(%rbp),%edx
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movsbl -0x21(%rbp),%eax
mov    0x2d97(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    1224 <main+0xb4>
mov    -0x10(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%rax
jne    127d <main+0x10d>
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xe70(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xd65(%rip),%rdi
lea    0xddd(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
movsbl %al,%esi
mov    -0x28(%rbp),%edx
lea    0xdba(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%eax
mov    0x2d10(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    12ab <main+0x13b>
mov    -0x10(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%rax
jne    1304 <main+0x194>
lea    0xda9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0xe6f(%rip),%r14
lea    0xe73(%rip),%rsi
mov    %r14,%rdi
call   1090 <fopen@plt>
mov    %rax,%rbx
movabs $0x444e450a78ffff78,%rax
45 4e 44
mov    %rax,0x10(%rsp)
lea    0xe54(%rip),%rdi
mov    $0x8,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x10(%rsp),%rdi
mov    $0x8,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   10a0 <fwrite@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
call   1080 <stat@plt>
mov    0x8(%rsp),%esi
lea    0xe41(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe6c(%rip),%rdi
call   1030 <puts@plt>
lea    0xe39(%rip),%rsi
mov    %r14,%rdi
call   1090 <fopen@plt>
mov    %rax,%rbx
lea    0xe29(%rip),%r14
mov    0x2db6(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1060 <fgetc@plt>
movsbl %al,%ebp
mov    %r14,%rdi
mov    %ebp,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <printf@plt>
movsbl (%r15),%eax
cmp    %eax,%ebp
jne    1240 <main+0xc0>
mov    %rbx,%rdi
call   1070 <feof@plt>
test   %rax,%rax
jne    127a <main+0xfa>
lea    0xe5b(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xe1c(%rip),%rdi
call   1030 <puts@plt>
lea    0xd6b(%rip),%rdi
lea    0xdb6(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%rbx
lea    0xda9(%rip),%r14
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1060 <fgetc@plt>
mov    %rax,%rbp
movsbl %bpl,%esi
mov    %r14,%rdi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <printf@plt>
movsbl (%r15),%eax
cmp    %eax,%ebp
jne    12b0 <main+0x130>
mov    %rbx,%rdi
call   1070 <feof@plt>
test   %rax,%rax
jne    12ec <main+0x16c>
lea    0xde9(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [11 x i8] c"fgetc.temp\00"
@.str.1 = [2 x i8] c"w\00"
@__const.main.list = [8 x i8] c"x\FF\FFx\0AEND"
@.str.2 = [51 x i8] c"writing %lu bytes to %s; please view it with xxd.\0A\00"
@.str.3 = [18 x i8] c"file size is: %u\0A\00"
@.str.4 = [45 x i8] c"This loop won't read all bytes in the file:\0A\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [37 x i8] c"read byte: %c (decimal value is %d)\0A\00"
@EOF = global i8 0
@.str.7 = [87 x i8] c"we exited the do-while loop because we thought we hit EOF, but feof() says we didn't.\0A\00"
@.str.8 = [44 x i8] c"This loop will read all bytes in the file:\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [8 x i8]
%4 = alloca %struct.stat
%5 = alloca i8
%6 = alloca i32
store i32 0, i32* %1
%7 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 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** %2
%8 = bitcast [8 x i8]* %3 to i8*
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i64 8, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%10 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
%11 = load i32*, i32** %2
%12 = call i32 @fwrite(i8* %10, i32 8, i32 1, i32* %11)
%13 = load i32*, i32** %2
%14 = call i32 @fclose(i32* %13)
%15 = call i32 @stat(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), %struct.stat* %4)
%16 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%17 = load i64, i64* %16
%18 = trunc i64 %17 to i32
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %18)
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0))
%21 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %21, i32** %2
br label %22
22: ; preds = %31, %0
%23 = load i32*, i32** %2
%24 = call i8* @fgetc(i32* %23)
%25 = ptrtoint i8* %24 to i8
store i8 %25, i8* %5
%26 = load i8, i8* %5
%27 = sext i8 %26 to i32
%28 = load i8, i8* %5
%29 = sext i8 %28 to i32
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0), i32 %27, i32 %29)
br label %31
31: ; preds = %22
%32 = load i8, i8* %5
%33 = sext i8 %32 to i32
%34 = load i8, i8* @EOF
%35 = sext i8 %34 to i32
%36 = icmp ne i32 %33, %35
br i1 %36, label %22, label %37
37: ; preds = %31
%38 = load i32*, i32** %2
%39 = call i64 @feof(i32* %38)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %37
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([87 x i8], [87 x i8]* @.str.7, i64 0, i64 0))
br label %43
43: ; preds = %41, %37
%44 = load i32*, i32** %2
%45 = call i32 @fclose(i32* %44)
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.8, i64 0, i64 0))
%47 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %47, i32** %2
br label %48
48: ; preds = %57, %43
%49 = load i32*, i32** %2
%50 = call i8* @fgetc(i32* %49)
%51 = ptrtoint i8* %50 to i32
store i32 %51, i32* %6
%52 = load i32, i32* %6
%53 = trunc i32 %52 to i8
%54 = sext i8 %53 to i32
%55 = load i32, i32* %6
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0), i32 %54, i32 %55)
br label %57
57: ; preds = %48
%58 = load i32, i32* %6
%59 = load i8, i8* @EOF
%60 = sext i8 %59 to i32
%61 = icmp ne i32 %58, %60
br i1 %61, label %48, label %62
62: ; preds = %57
%63 = load i32*, i32** %2
%64 = call i64 @feof(i32* %63)
%65 = icmp eq i64 %64, 0
br i1 %65, label %66, label %68
66: ; preds = %62
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([87 x i8], [87 x i8]* @.str.7, i64 0, i64 0))
br label %68
68: ; preds = %66, %62
%69 = load i32*, i32** %2
%70 = call i32 @fclose(i32* %69)
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [11 x i8] c"fgetc.temp\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [51 x i8] c"writing %lu bytes to %s; please view it with xxd.\0A\00"
@.str.3 = [18 x i8] c"file size is: %u\0A\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [37 x i8] c"read byte: %c (decimal value is %d)\0A\00"
@EOF = global i8 0
@str = [44 x i8] c"This loop won't read all bytes in the file:\00"
@str.9 = [43 x i8] c"This loop will read all bytes in the file:\00"
@str.11 = [86 x i8] c"we exited the do-while loop because we thought we hit EOF, but feof() says we didn't.\00"
define i32 @main() {
%1 = alloca i64
%2 = alloca %struct.stat
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = bitcast i64* %1 to i8*
store i64 4921947354044563320, i64* %1
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i64 8, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%6 = call i32 @fwrite(i8* %4, i32 8, i32 1, i32* %3)
%7 = tail call i32 @fclose(i32* %3)
%8 = bitcast %struct.stat* %2 to i8*
%9 = call i32 @stat(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), %struct.stat* %2)
%10 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%11 = load i64, i64* %10
%12 = trunc i64 %11 to i32
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %12)
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str, i64 0, i64 0))
%15 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
br label %16
16: ; preds = %16, %0
%17 = tail call i8* @fgetc(i32* %15)
%18 = ptrtoint i8* %17 to i64
%19 = trunc i64 %18 to i32
%20 = shl i32 %19, 24
%21 = ashr exact i32 %20, 24
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0), i32 %21, i32 %21)
%23 = load i8, i8* @EOF
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %21, %24
br i1 %25, label %26, label %16
26: ; preds = %16
%27 = tail call i64 @feof(i32* %15)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([86 x i8], [86 x i8]* @str.11, i64 0, i64 0))
br label %31
31: ; preds = %29, %26
%32 = tail call i32 @fclose(i32* %15)
%33 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str.9, i64 0, i64 0))
%34 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
br label %35
35: ; preds = %35, %31
%36 = tail call i8* @fgetc(i32* %34)
%37 = ptrtoint i8* %36 to i64
%38 = trunc i64 %37 to i32
%39 = shl i32 %38, 24
%40 = ashr exact i32 %39, 24
%41 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0), i32 %40, i32 %38)
%42 = load i8, i8* @EOF
%43 = sext i8 %42 to i32
%44 = icmp eq i32 %38, %43
br i1 %44, label %45, label %35
45: ; preds = %35
%46 = tail call i64 @feof(i32* %34)
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %45
%49 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([86 x i8], [86 x i8]* @str.11, i64 0, i64 0))
br label %50
50: ; preds = %48, %45
%51 = tail call i32 @fclose(i32* %34)
ret i32 0
}