#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*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
void test_read_char() {
  FILE *fp;
  int ch;
  long pos;
  if ((fp = fopen("demo.txt", "r")) == NULL) {
    printf("\t Failure to open demo.txt!\n");
    exit(0);
  }
  printf("\n");
  pos = ftell(fp);
  ch = fgetc(fp);
  while (!feof(fp)) {
    printf("%c, %ld\n", ch, pos);
    pos = ftell(fp);
    ch = fgetc(fp);
  }
  fclose(fp);
}


x86 assembly (clang, -O0):
<test_read_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe81(%rip),%rdi
lea    0xe83(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
jne    11ae <test_read_char+0x3e>
lea    0xe6b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11f3 <test_read_char+0x83>
jmp    1226 <test_read_char+0xb6>
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0xc(%rbp)
jmp    11d5 <test_read_char+0x65>
mov    -0x8(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_read_char>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
lea    0xe62(%rip),%rdi
lea    0xe64(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1232 <test_read_char+0xa2>
mov    %rax,%r14
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r14,%rdi
call   1080 <ftell@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1070 <fgetc@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1090 <feof@plt>
test   %eax,%eax
jne    1220 <test_read_char+0x90>
lea    0xe23(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
call   1080 <ftell@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1070 <fgetc@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1090 <feof@plt>
test   %eax,%eax
je     11f0 <test_read_char+0x60>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <fclose@plt>
lea    0xddb(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"demo.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [29 x i8] c"\09 Failure to open demo.txt!\0A\00"
@.str.3 = [2 x i8] c"\0A\00"
@.str.4 = [9 x i8] c"%c, %ld\0A\00"
define void @test_read_char() {
%1 = alloca i32*
%2 = alloca i32
%3 = alloca i64
%4 = 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* %4, i32** %1
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%8 = call i32 @exit(i32 0)
unreachable
9: ; preds = %0
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%11 = load i32*, i32** %1
%12 = call i64 @ftell(i32* %11)
store i64 %12, i64* %3
%13 = load i32*, i32** %1
%14 = call i32 @fgetc(i32* %13)
store i32 %14, i32* %2
br label %15
15: ; preds = %20, %9
%16 = load i32*, i32** %1
%17 = call i32 @feof(i32* %16)
%18 = icmp ne i32 %17, 0
%19 = xor i1 %18, true
br i1 %19, label %20, label %28
20: ; preds = %15
%21 = load i32, i32* %2
%22 = load i64, i64* %3
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %21, i64 %22)
%24 = load i32*, i32** %1
%25 = call i64 @ftell(i32* %24)
store i64 %25, i64* %3
%26 = load i32*, i32** %1
%27 = call i32 @fgetc(i32* %26)
store i32 %27, i32* %2
br label %15
28: ; preds = %15
%29 = load i32*, i32** %1
%30 = call i32 @fclose(i32* %29)
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"demo.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.4 = [9 x i8] c"%c, %ld\0A\00"
@str = [28 x i8] c"\09 Failure to open demo.txt!\00"
define void @test_read_char() {
%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 = icmp eq i32* %1, null
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
%5 = tail call i32 @exit(i32 0)
unreachable
6: ; preds = %0
%7 = tail call i32 @putchar(i32 10)
%8 = tail call i64 @ftell(i32* %1)
%9 = tail call i32 @fgetc(i32* %1)
%10 = tail call i32 @feof(i32* %1)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %20
12: ; preds = %6, %12
%13 = phi i64 [ %16, %12 ], [ %8, %6 ]
%14 = phi i32 [ %17, %12 ], [ %9, %6 ]
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %14, i64 %13)
%16 = tail call i64 @ftell(i32* %1)
%17 = tail call i32 @fgetc(i32* %1)
%18 = tail call i32 @feof(i32* %1)
%19 = icmp eq i32 %18, 0
br i1 %19, label %12, label %20
20: ; preds = %12, %6
%21 = tail call i32 @fclose(i32* %1)
ret void
}