#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 EOF;
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
void fgetc1() {
  FILE *file = NULL;
  file = fopen("test.txt", "r");
  if (file == NULL) {
    perror("fopen error");
  } else {
    while (!feof(file)) {
      int c = fgetc(file);
      if (c == EOF)
        break;
      printf("%c", c);
    }
    printf("\nread over \n");
  }
  if (fclose(file) == 0)
    printf("fclose file success\n");
}


x86 assembly (clang, -O0):
<fgetc1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movq   $0x0,-0x8(%rbp)
lea    0xe89(%rip),%rdi
lea    0xe95(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11a3 <fgetc1+0x43>
lea    0xe70(%rip),%rdi
call   1080 <perror@plt>
jmp    120d <fgetc1+0xad>
jmp    11a8 <fgetc1+0x48>
mov    -0x8(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11c6 <fgetc1+0x66>
jmp    11ff <fgetc1+0x9f>
mov    -0x8(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    0x2e0c(%rip),%rcx
cmp    (%rcx),%eax
jne    11e9 <fgetc1+0x89>
jmp    11ff <fgetc1+0x9f>
mov    -0xc(%rbp),%esi
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11a8 <fgetc1+0x48>
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1030 <fclose@plt>
cmp    $0x0,%rax
jne    122e <fgetc1+0xce>
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fgetc1>:
push   %r14
push   %rbx
push   %rax
lea    0xe85(%rip),%rdi
lea    0xe91(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1204 <fgetc1+0x94>
mov    %rbx,%rdi
call   1070 <feof@plt>
test   %eax,%eax
jne    11d0 <fgetc1+0x60>
mov    0x2e46(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1060 <fgetc@plt>
cmp    (%r14),%eax
je     11d0 <fgetc1+0x60>
mov    %eax,%edi
call   1030 <putchar@plt>
mov    %rbx,%rdi
call   1070 <feof@plt>
test   %eax,%eax
je     11b0 <fgetc1+0x40>
lea    0xe3e(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
test   %rax,%rax
je     11f1 <fgetc1+0x81>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe29(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <puts@plt>
lea    0xdfe(%rip),%rdi
call   1090 <perror@plt>
jmp    11dc <fgetc1+0x6c>

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"fopen error\00"
@EOF = global i32 0
@.str.3 = [3 x i8] c"%c\00"
@.str.4 = [13 x i8] c"\0Aread over \0A\00"
@.str.5 = [21 x i8] c"fclose file success\0A\00"
define void @fgetc1() {
%1 = alloca i32*
%2 = alloca i32
store i32* null, i32** %1
%3 = 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* %3, i32** %1
%4 = load i32*, i32** %1
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %26
8: ; preds = %0
br label %9
9: ; preds = %21, %8
%10 = load i32*, i32** %1
%11 = call i32 @feof(i32* %10)
%12 = icmp ne i32 %11, 0
%13 = xor i1 %12, true
br i1 %13, label %14, label %24
14: ; preds = %9
%15 = load i32*, i32** %1
%16 = call i32 @fgetc(i32* %15)
store i32 %16, i32* %2
%17 = load i32, i32* %2
%18 = load i32, i32* @EOF
%19 = icmp eq i32 %17, %18
br i1 %19, label %20, label %21
20: ; preds = %14
br label %24
21: ; preds = %14
%22 = load i32, i32* %2
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %22)
br label %9
24: ; preds = %20, %9
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %26
26: ; preds = %24, %6
%27 = load i32*, i32** %1
%28 = call i64 @fclose(i32* %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
br label %32
32: ; preds = %30, %26
ret void
}

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"fopen error\00"
@EOF = global i32 0
@str = [12 x i8] c"\0Aread over \00"
@str.6 = [20 x i8] c"fclose file success\00"
define void @fgetc1() {
%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 %6, label %3
3: ; preds = %0
%4 = tail call i32 @feof(i32* %1)
%5 = icmp eq i32 %4, 0
br i1 %5, label %8, label %16
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %18
8: ; preds = %3, %12
%9 = tail call i32 @fgetc(i32* %1)
%10 = load i32, i32* @EOF
%11 = icmp eq i32 %9, %10
br i1 %11, label %16, label %12
12: ; preds = %8
%13 = tail call i32 @putchar(i32 %9)
%14 = tail call i32 @feof(i32* %1)
%15 = icmp eq i32 %14, 0
br i1 %15, label %8, label %16
16: ; preds = %12, %8, %3
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %18
18: ; preds = %16, %6
%19 = tail call i64 @fclose(i32* %1)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.6, i64 0, i64 0))
br label %23
23: ; preds = %21, %18
ret void
}