#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*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ puts(char *);
void fgets_t() {
  FILE *fp;
  char str[60];
  fp = fopen("file.txt", "r");
  if (fp == NULL) {
    perror("Error opening file");
    return;
  }
  if (fgets(str, 60, fp) != NULL) {
    puts(str);
  }
  fclose(fp);
}


x86 assembly (clang, -O0):
<fgets_t>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
lea    0xea1(%rip),%rdi
lea    0xea3(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    118b <fgets_t+0x3b>
lea    0xe8a(%rip),%rdi
call   1070 <perror@plt>
jmp    11b9 <fgets_t+0x69>
lea    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0x3c,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     11b0 <fgets_t+0x60>
lea    -0x50(%rbp),%rdi
call   1030 <puts@plt>
mov    -0x8(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fgets_t>:
push   %rbx
sub    $0x40,%rsp
lea    0xea4(%rip),%rdi
lea    0xea6(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     119b <fgets_t+0x4b>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x3c,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     118d <fgets_t+0x3d>
mov    %rsp,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
add    $0x40,%rsp
pop    %rbx
ret
lea    0xe69(%rip),%rdi
add    $0x40,%rsp
pop    %rbx
jmp    1070 <perror@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"file.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [19 x i8] c"Error opening file\00"
define void @fgets_t() {
%1 = alloca i32*
%2 = alloca [60 x i8]
%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 ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %19
8: ; preds = %0
%9 = getelementptr inbounds [60 x i8], [60 x i8]* %2, i64 0, i64 0
%10 = load i32*, i32** %1
%11 = call i32* @fgets(i8* %9, i32 60, i32* %10)
%12 = icmp ne i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %8
%14 = getelementptr inbounds [60 x i8], [60 x i8]* %2, i64 0, i64 0
%15 = call i32 @puts(i8* %14)
br label %16
16: ; preds = %13, %8
%17 = load i32*, i32** %1
%18 = call i32 @fclose(i32* %17)
br label %19
19: ; preds = %16, %6
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"file.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [19 x i8] c"Error opening file\00"
define void @fgets_t() {
%1 = alloca [60 x i8]
%2 = getelementptr inbounds [60 x i8], [60 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 %5, label %7
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %14
7: ; preds = %0
%8 = call i32* @fgets(i8* %2, i32 60, i32* %3)
%9 = icmp eq i32* %8, null
br i1 %9, label %12, label %10
10: ; preds = %7
%11 = call i32 @puts(i8* %2)
br label %12
12: ; preds = %10, %7
%13 = call i32 @fclose(i32* %3)
br label %14
14: ; preds = %12, %5
ret void
}