#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*/ *);
scalar_t__ fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
void my_fgetc(char *path) {
  FILE *fp = NULL;
  fp = fopen(path, "r+");
  if (fp == NULL) {
    perror("my_fgetc fopen");
    return;
  }
  char ch;
  ch = (char)fgetc(fp);
  while (!feof(fp)) {
    printf("%c", ch);
    ch = (char)fgetc(fp);
  }
  printf("\n");
  if (fp != NULL) {
    fclose(fp);
    fp = NULL;
  }
}


x86 assembly (clang, -O0):
<my_fgetc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe81(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a4 <my_fgetc+0x44>
lea    0xe69(%rip),%rdi
call   1080 <perror@plt>
jmp    121b <my_fgetc+0xbb>
mov    -0x10(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %al,-0x11(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11ce <my_fgetc+0x6e>
jmp    11f1 <my_fgetc+0x91>
movsbl -0x11(%rbp),%esi
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %al,-0x11(%rbp)
jmp    11b0 <my_fgetc+0x50>
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpq   $0x0,-0x10(%rbp)
je     121b <my_fgetc+0xbb>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
movq   $0x0,-0x10(%rbp)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_fgetc>:
push   %r14
push   %rbx
push   %rax
lea    0xe95(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11ba <my_fgetc+0x5a>
mov    %rax,%r14
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1050 <fgetc@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1060 <feof@plt>
test   %eax,%eax
jne    11a1 <my_fgetc+0x41>
movsbl %bl,%edi
call   1030 <putchar@plt>
jmp    1180 <my_fgetc+0x20>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <fclose@plt>
lea    0xe42(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1080 <perror@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"r+\00"
@.str.1 = [15 x i8] c"my_fgetc fopen\00"
@.str.2 = [3 x i8] c"%c\00"
@.str.3 = [2 x i8] c"\0A\00"
define void @my_fgetc(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8
store i8* %0, i8** %2
store i32* null, i32** %3
%5 = load i8*, i8** %2
%6 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %6, i32** %3
%7 = load i32*, i32** %3
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %34
11: ; preds = %1
%12 = load i32*, i32** %3
%13 = call i64 @fgetc(i32* %12)
%14 = trunc i64 %13 to i8
store i8 %14, i8* %4
br label %15
15: ; preds = %20, %11
%16 = load i32*, i32** %3
%17 = call i32 @feof(i32* %16)
%18 = icmp ne i32 %17, 0
%19 = xor i1 %18, true
br i1 %19, label %20, label %27
20: ; preds = %15
%21 = load i8, i8* %4
%22 = sext i8 %21 to i32
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %22)
%24 = load i32*, i32** %3
%25 = call i64 @fgetc(i32* %24)
%26 = trunc i64 %25 to i8
store i8 %26, i8* %4
br label %15
27: ; preds = %15
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%29 = load i32*, i32** %3
%30 = icmp ne i32* %29, null
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = load i32*, i32** %3
%33 = call i32 @fclose(i32* %32)
store i32* null, i32** %3
br label %34
34: ; preds = %9, %31, %27
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"r+\00"
@.str.1 = [15 x i8] c"my_fgetc fopen\00"
define void @my_fgetc(i8* nocapture %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %22
6: ; preds = %1
%7 = tail call i64 @fgetc(i32* %2)
%8 = tail call i32 @feof(i32* %2)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %19
10: ; preds = %6, %10
%11 = phi i64 [ %16, %10 ], [ %7, %6 ]
%12 = trunc i64 %11 to i32
%13 = shl i32 %12, 24
%14 = ashr exact i32 %13, 24
%15 = tail call i32 @putchar(i32 %14)
%16 = tail call i64 @fgetc(i32* %2)
%17 = tail call i32 @feof(i32* %2)
%18 = icmp eq i32 %17, 0
br i1 %18, label %10, label %19
19: ; preds = %10, %6
%20 = tail call i32 @putchar(i32 10)
%21 = tail call i32 @fclose(i32* %2)
br label %22
22: ; preds = %19, %4
ret void
}