#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*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  FILE *fp = NULL;
  if ((fp = (fopen("new.txt", "r"))) == NULL)
    printf("open failure");
  char num[10];
  while (1) {
    if ((fgets(num, 10, fp)) == NULL) {
      if (ferror(fp)) {
        printf("read error\n");
        fclose(fp);
        return -1;
      } else {
        printf("reach to end\n");
        fclose(fp);
        return 0;
      }
    } else {
      printf("%s\n", num);
    }
  }
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
lea    0xe92(%rip),%rdi
lea    0xe93(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1196 <main+0x46>
lea    0xe7b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    119b <main+0x4b>
lea    -0x1a(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0xa,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
jne    1210 <main+0xc0>
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     11ed <main+0x9d>
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1227 <main+0xd7>
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    1227 <main+0xd7>
lea    -0x1a(%rbp),%rsi
lea    0xe16(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    119b <main+0x4b>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0xe92(%rip),%rdi
lea    0xeb6(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%r14
test   %rax,%rax
jne    1190 <main+0x30>
lea    0xe7f(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe(%rsp),%rdi
mov    $0xa,%esi
mov    %r14,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     11cd <main+0x6d>
lea    0xe(%rsp),%rbx
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
mov    $0xa,%esi
mov    %r14,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
jne    11b0 <main+0x50>
mov    %r14,%rdi
call   1040 <ferror@plt>
xor    %ebx,%ebx
test   %rax,%rax
setne  %bl
lea    0xe31(%rip),%rax
lea    0xe37(%rip),%rdi
cmove  %rax,%rdi
neg    %ebx
call   1030 <puts@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [8 x i8] c"new.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [13 x i8] c"open failure\00"
@.str.3 = [12 x i8] c"read error\0A\00"
@.str.4 = [14 x i8] c"reach to end\0A\00"
@.str.5 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [10 x i8]
store i32 0, i32* %1
store i32* null, i32** %2
%4 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 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** %2
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %8
8: ; preds = %6, %0
br label %9
9: ; preds = %8, %29
%10 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%11 = load i32*, i32** %2
%12 = call i32* @fgets(i8* %10, i32 10, i32* %11)
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %26
14: ; preds = %9
%15 = load i32*, i32** %2
%16 = call i64 @ferror(i32* %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %14
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%20 = load i32*, i32** %2
%21 = call i32 @fclose(i32* %20)
store i32 -1, i32* %1
br label %30
22: ; preds = %14
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%24 = load i32*, i32** %2
%25 = call i32 @fclose(i32* %24)
store i32 0, i32* %1
br label %30
26: ; preds = %9
%27 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %27)
br label %29
29: ; preds = %26
br label %9
30: ; preds = %22, %18
%31 = load i32, i32* %1
ret i32 %31
}

LLVM IR (-O2):
@.str = [8 x i8] c"new.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [13 x i8] c"open failure\00"
@str = [13 x i8] c"reach to end\00"
@str.6 = [11 x i8] c"read error\00"
define i32 @main() {
%1 = alloca [10 x i8]
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %6
6: ; preds = %4, %0
%7 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 0
%8 = call i32* @fgets(i8* %7, i32 10, i32* %2)
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %18
10: ; preds = %18, %6
%11 = call i64 @ferror(i32* %2)
%12 = icmp eq i64 %11, 0
%13 = select i1 %12, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @str.6, i64 0, i64 0)
%14 = xor i1 %12, true
%15 = sext i1 %14 to i32
%16 = call i32 @puts(i8* dereferenceable(1) %13)
%17 = call i32 @fclose(i32* %2)
ret i32 %15
18: ; preds = %6, %18
%19 = call i32 @puts(i8* %7)
%20 = call i32* @fgets(i8* %7, i32 10, i32* %2)
%21 = icmp eq i32* %20, null
br i1 %21, label %10, label %18
}