#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_SUCCESS;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ feof(int /*<<< orphan*/);
char *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdin;
int main(void) {
  char s[100];
  while (1) {
    char *ret = fgets(s, 100, stdin);
    if (ret == NULL) {
      if (feof(stdin)) {
        printf("End of file/stream was reached.\n");
      } else
        perror("fgets");
      exit(EXIT_SUCCESS);
    }
    printf("Echo: %s\n", s);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x70(%rbp),%rdi
mov    0x2e7b(%rip),%rax
mov    (%rax),%edx
mov    $0x64,%esi
call   1040 <fgets@plt>
mov    %rax,-0x78(%rbp)
cmpq   $0x0,-0x78(%rbp)
jne    11cd <main+0x7d>
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
call   1050 <feof@plt>
cmp    $0x0,%rax
je     11b3 <main+0x63>
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11bf <main+0x6f>
lea    0xe67(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
lea    -0x70(%rbp),%rsi
lea    0xe4f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1162 <main+0x12>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x70,%rsp
mov    0x2e78(%rip),%r15
mov    (%r15),%edx
mov    %rsp,%rdi
mov    $0x64,%esi
call   1050 <fgets@plt>
test   %rax,%rax
je     11b2 <main+0x52>
lea    0xe7a(%rip),%r14
mov    %rsp,%rbx
nop
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%r15),%edx
mov    %rbx,%rdi
mov    $0x64,%esi
call   1050 <fgets@plt>
test   %rax,%rax
jne    1190 <main+0x30>
mov    (%r15),%edi
call   1060 <feof@plt>
test   %rax,%rax
jne    11d9 <main+0x79>
lea    0xe3a(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    0xe30(%rip),%rdi
call   1030 <puts@plt>
jmp    11cb <main+0x6b>

LLVM IR (-O0):
@stdin = global i32 0
@.str = [33 x i8] c"End of file/stream was reached.\0A\00"
@.str.1 = [6 x i8] c"fgets\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [10 x i8] c"Echo: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [100 x i8]
%3 = alloca i8*
store i32 0, i32* %1
br label %4
4: ; preds = %0, %21
%5 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%6 = load i32, i32* @stdin
%7 = call i8* @fgets(i8* %5, i32 100, i32 %6)
store i8* %7, i8** %3
%8 = load i8*, i8** %3
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %21
10: ; preds = %4
%11 = load i32, i32* @stdin
%12 = call i64 @feof(i32 %11)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %18
16: ; preds = %10
%17 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %16, %14
%19 = load i32, i32* @EXIT_SUCCESS
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %4
%22 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %22)
br label %4
}

LLVM IR (-O2):
@stdin = global i32 0
@.str.1 = [6 x i8] c"fgets\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [10 x i8] c"Echo: %s\0A\00"
@str = [32 x i8] c"End of file/stream was reached.\00"
define i32 @main() {
%1 = alloca [100 x i8]
%2 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @stdin
%4 = call i8* @fgets(i8* %2, i32 100, i32 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %17
6: ; preds = %17, %0
%7 = load i32, i32* @stdin
%8 = call i64 @feof(i32 %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %6
%11 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
br label %14
12: ; preds = %6
%13 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %10
%15 = load i32, i32* @EXIT_SUCCESS
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %0, %17
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %2)
%19 = load i32, i32* @stdin
%20 = call i8* @fgets(i8* %2, i32 100, i32 %19)
%21 = icmp eq i8* %20, null
br i1 %21, label %6, label %17
}