#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_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
void test() {
  char buff[128];
  const char *filename = "test.txt";
  FILE *fp = fopen(filename, "r");
  if (fp == NULL) {
    fprintf(stderr, "open file %s error\n", filename);
    exit(EXIT_FAILURE);
  }
  while (NULL != fgets(buff, 128, fp)) {
    printf(buff);
  }
}


x86 assembly (clang, -O0):
<test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
lea    0xe9e(%rip),%rax
mov    %rax,-0x88(%rbp)
mov    -0x88(%rbp),%rdi
lea    0xe92(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x90(%rbp)
cmpq   $0x0,-0x90(%rbp)
jne    11bb <test+0x6b>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
mov    -0x88(%rbp),%rdx
lea    0xe63(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
jmp    11c0 <test+0x70>
lea    -0x80(%rbp),%rdi
mov    -0x90(%rbp),%rdx
mov    $0x80,%esi
call   1040 <fgets@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     11f1 <test+0xa1>
lea    -0x80(%rbp),%rdi
call   1030 <printf@plt>
jmp    11c0 <test+0x70>
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test>:
push   %r14
push   %rbx
sub    $0x88,%rsp
lea    0xe9f(%rip),%rdi
lea    0xea1(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11b8 <test+0x68>
mov    %rax,%r14
mov    %rsp,%rdi
mov    $0x80,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     11ad <test+0x5d>
mov    %rsp,%rbx
nopl   (%rax)
mov    %rbx,%rdi
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    $0x80,%esi
mov    %r14,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
jne    1190 <test+0x40>
add    $0x88,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
lea    0xe43(%rip),%rsi
lea    0xe31(%rip),%rdx
call   1050 <fprintf@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [20 x i8] c"open file %s error\0A\00"
@EXIT_FAILURE = global i32 0
define void @test() {
%1 = alloca [128 x i8]
%2 = alloca i8*
%3 = alloca i32*
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** %2
%4 = load i8*, i8** %2
%5 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %3
%6 = load i32*, i32** %3
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %14
8: ; preds = %0
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %2
%11 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %10)
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %0
br label %15
15: ; preds = %20, %14
%16 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%17 = load i32*, i32** %3
%18 = call i32* @fgets(i8* %16, i32 128, i32* %17)
%19 = icmp ne i32* null, %18
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%22 = call i32 @printf(i8* %21)
br label %15
23: ; preds = %15
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [20 x i8] c"open file %s error\0A\00"
@EXIT_FAILURE = global i32 0
define void @test() {
%1 = alloca [128 x i8]
%2 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%3 = 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))
%4 = icmp eq i32* %3, null
br i1 %4, label %8, label %5
5: ; preds = %0
%6 = call i32* @fgets(i8* %2, i32 128, i32* %3)
%7 = icmp eq i32* %6, null
br i1 %7, label %17, label %13
8: ; preds = %0
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %5, %13
%14 = call i32 @printf(i8* %2)
%15 = call i32* @fgets(i8* %2, i32 128, i32* %3)
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %13
17: ; preds = %13, %5
ret void
}