#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 */

/* Variables and functions */
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ feof(int /*<<< orphan*/);
char *fgets(char *, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stdin;
int strlen(char *);
char *fgets_eof(char *buf, size_t len) {
  char *retval = fgets(buf, len, stdin);
  if (feof(stdin)) {
    printf("EOF reached\n");
    exit(0);
  }
  if (strlen(buf) >= 1 && buf[strlen(buf) - 1] == '\n')
    buf[strlen(buf) - 1] = '\x00';
  return retval;
}


x86 assembly (clang, -O0):
<fgets_eof>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2e79(%rip),%rax
mov    (%rax),%edx
call   1050 <fgets@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e67(%rip),%rax
mov    (%rax),%edi
call   1060 <feof@plt>
cmp    $0x0,%rax
je     11a5 <fgets_eof+0x55>
lea    0xe67(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x1,%eax
jl     1202 <fgets_eof+0xb2>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1202 <fgets_eof+0xb2>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fgets_eof>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
mov    0x2e89(%rip),%r15
mov    (%r15),%edx
call   1050 <fgets@plt>
mov    %rax,%r14
mov    (%r15),%edi
call   1060 <feof@plt>
test   %rax,%rax
jne    11ac <fgets_eof+0x5c>
mov    %rbx,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
jle    11a3 <fgets_eof+0x53>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    11a3 <fgets_eof+0x53>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe4d(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stdin = global i32 0
@.str = [13 x i8] c"EOF reached\0A\00"
define i8* @fgets_eof(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
store i8* %0, i8** %3
store i64 %1, i64* %4
%6 = load i8*, i8** %3
%7 = load i64, i64* %4
%8 = load i32, i32* @stdin
%9 = call i8* @fgets(i8* %6, i64 %7, i32 %8)
store i8* %9, i8** %5
%10 = load i32, i32* @stdin
%11 = call i64 @feof(i32 %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 0)
unreachable
16: ; preds = %2
%17 = load i8*, i8** %3
%18 = call i32 @strlen(i8* %17)
%19 = icmp sge i32 %18, 1
br i1 %19, label %20, label %37
20: ; preds = %16
%21 = load i8*, i8** %3
%22 = load i8*, i8** %3
%23 = call i32 @strlen(i8* %22)
%24 = sub nsw i32 %23, 1
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %21, i64 %25
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 10
br i1 %29, label %30, label %37
30: ; preds = %20
%31 = load i8*, i8** %3
%32 = load i8*, i8** %3
%33 = call i32 @strlen(i8* %32)
%34 = sub nsw i32 %33, 1
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %31, i64 %35
store i8 0, i8* %36
br label %37
37: ; preds = %30, %20, %16
%38 = load i8*, i8** %5
ret i8* %38
}

LLVM IR (-O2):
@stdin = global i32 0
@str = [12 x i8] c"EOF reached\00"
define i8* @fgets_eof(i8* %0, i64 %1) {
%3 = load i32, i32* @stdin
%4 = tail call i8* @fgets(i8* %0, i64 %1, i32 %3)
%5 = load i32, i32* @stdin
%6 = tail call i64 @feof(i32 %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 0)
unreachable
11: ; preds = %2
%12 = tail call i32 @strlen(i8* %0)
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %26
14: ; preds = %11
%15 = tail call i32 @strlen(i8* %0)
%16 = add nsw i32 %15, -1
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %0, i64 %17
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 10
br i1 %20, label %21, label %26
21: ; preds = %14
%22 = tail call i32 @strlen(i8* %0)
%23 = add nsw i32 %22, -1
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %0, i64 %24
store i8 0, i8* %25
br label %26
26: ; preds = %21, %14, %11
ret i8* %4
}