#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*/ feof(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, int, int, int);
int main(void) {
  int i = 0;
  int c = 0;
  FILE *p = NULL;
  p = fopen("test.txt", "r");
  c = fgetc(p);
  while (!feof(p)) {
    printf("%d: %c, %d\n", ++i, c, c);
    c = fgetc(p);
  }
  fclose(p);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
lea    0xe84(%rip),%rdi
lea    0xe86(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11b6 <main+0x66>
jmp    11e2 <main+0x92>
mov    -0x8(%rbp),%esi
add    $0x1,%esi
mov    %esi,-0x8(%rbp)
mov    -0xc(%rbp),%edx
mov    -0xc(%rbp),%ecx
lea    0xe3f(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0xc(%rbp)
jmp    1198 <main+0x48>
mov    -0x18(%rbp),%rdi
call   1030 <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    0xea2(%rip),%rdi
lea    0xea4(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1050 <fgetc@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1060 <feof@plt>
test   %eax,%eax
jne    11b7 <main+0x67>
mov    $0x1,%ebx
lea    0xe7c(%rip),%r15
nop
mov    %r15,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %ebp,%ecx
call   1040 <printf@plt>
mov    %r14,%rdi
call   1050 <fgetc@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1060 <feof@plt>
add    $0x1,%ebx
test   %eax,%eax
je     1190 <main+0x40>
mov    %r14,%rdi
call   1030 <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"test.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [12 x i8] c"%d: %c, %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
store i32 0, i32* %1
store i32 0, i32* %2
store i32 0, i32* %3
store i32* null, i32** %4
%5 = 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* %5, i32** %4
%6 = load i32*, i32** %4
%7 = call i32 @fgetc(i32* %6)
store i32 %7, i32* %3
br label %8
8: ; preds = %13, %0
%9 = load i32*, i32** %4
%10 = call i32 @feof(i32* %9)
%11 = icmp ne i32 %10, 0
%12 = xor i1 %11, true
br i1 %12, label %13, label %21
13: ; preds = %8
%14 = load i32, i32* %2
%15 = add nsw i32 %14, 1
store i32 %15, i32* %2
%16 = load i32, i32* %3
%17 = load i32, i32* %3
%18 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %15, i32 %16, i32 %17)
%19 = load i32*, i32** %4
%20 = call i32 @fgetc(i32* %19)
store i32 %20, i32* %3
br label %8
21: ; preds = %8
%22 = load i32*, i32** %4
%23 = call i32 @fclose(i32* %22)
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [12 x i8] c"%d: %c, %d\0A\00"
define i32 @main() {
%1 = 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))
%2 = tail call i32 @fgetc(i32* %1)
%3 = tail call i32 @feof(i32* %1)
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %13
5: ; preds = %0, %5
%6 = phi i32 [ %10, %5 ], [ %2, %0 ]
%7 = phi i32 [ %8, %5 ], [ 0, %0 ]
%8 = add nuw nsw i32 %7, 1
%9 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %8, i32 %6, i32 %6)
%10 = tail call i32 @fgetc(i32* %1)
%11 = tail call i32 @feof(i32* %1)
%12 = icmp eq i32 %11, 0
br i1 %12, label %5, label %13
13: ; preds = %5, %0
%14 = tail call i32 @fclose(i32* %1)
ret i32 0
}