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

/* Variables and functions */
int EOF;
int /*<<< orphan*/ feof(int /*<<< orphan*/);
char *fgets(char *, int, int /*<<< orphan*/);
int fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int good_echo(void) {
  char s[5];
  char *ret;
  ret = fgets(s, sizeof(s), stdin);
  if (ret == NULL) {
    if (!feof(stdin))
      return -1;
    goto done;
  }
  int out;
  out = fputs(s, stdout);
  if (out == EOF)
    return -1;
done:
  return 0;
}


x86 assembly (clang, -O0):
<good_echo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    -0x9(%rbp),%rdi
mov    0x2ea5(%rip),%rax
mov    (%rax),%edx
mov    $0x5,%esi
call   1040 <fgets@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1186 <good_echo+0x56>
mov    0x2e83(%rip),%rax
mov    (%rax),%edi
call   1050 <feof@plt>
cmp    $0x0,%eax
jne    1181 <good_echo+0x51>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c5 <good_echo+0x95>
jmp    11be <good_echo+0x8e>
lea    -0x9(%rbp),%rdi
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
call   1030 <fputs@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2e3b(%rip),%rcx
cmp    (%rcx),%eax
jne    11b9 <good_echo+0x89>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c5 <good_echo+0x95>
jmp    11be <good_echo+0x8e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<good_echo>:
push   %rbx
sub    $0x10,%rsp
mov    0x2eac(%rip),%rbx
mov    (%rbx),%edx
lea    0xb(%rsp),%rdi
mov    $0x5,%esi
call   1040 <fgets@plt>
test   %rax,%rax
je     1179 <good_echo+0x49>
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
lea    0xb(%rsp),%rdi
call   1030 <fputs@plt>
mov    %eax,%ecx
mov    0x2e72(%rip),%rdx
mov    $0xffffffff,%eax
cmp    (%rdx),%ecx
jne    1184 <good_echo+0x54>
jmp    118d <good_echo+0x5d>
mov    (%rbx),%edi
call   1050 <feof@plt>
test   %eax,%eax
je     1188 <good_echo+0x58>
xor    %eax,%eax
jmp    118d <good_echo+0x5d>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@stdin = global i32 0
@stdout = global i32 0
@EOF = global i32 0
define i32 @good_echo() {
%1 = alloca i32
%2 = alloca [5 x i8]
%3 = alloca i8*
%4 = alloca i32
%5 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 0
%6 = load i32, i32* @stdin
%7 = call i8* @fgets(i8* %5, i32 5, i32 %6)
store i8* %7, i8** %3
%8 = load i8*, i8** %3
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %16
10: ; preds = %0
%11 = load i32, i32* @stdin
%12 = call i32 @feof(i32 %11)
%13 = icmp ne i32 %12, 0
br i1 %13, label %15, label %14
14: ; preds = %10
store i32 -1, i32* %1
br label %26
15: ; preds = %10
br label %25
16: ; preds = %0
%17 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 0
%18 = load i32, i32* @stdout
%19 = call i32 @fputs(i8* %17, i32 %18)
store i32 %19, i32* %4
%20 = load i32, i32* %4
%21 = load i32, i32* @EOF
%22 = icmp eq i32 %20, %21
br i1 %22, label %23, label %24
23: ; preds = %16
store i32 -1, i32* %1
br label %26
24: ; preds = %16
br label %25
25: ; preds = %24, %15
store i32 0, i32* %1
br label %26
26: ; preds = %25, %23, %14
%27 = load i32, i32* %1
ret i32 %27
}

LLVM IR (-O2):
@stdin = global i32 0
@stdout = global i32 0
@EOF = global i32 0
define i32 @good_echo() {
%1 = alloca [5 x i8]
%2 = getelementptr inbounds [5 x i8], [5 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @stdin
%4 = call i8* @fgets(i8* %2, i32 5, i32 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %10
6: ; preds = %0
%7 = load i32, i32* @stdin
%8 = call i32 @feof(i32 %7)
%9 = icmp eq i32 %8, 0
br i1 %9, label %16, label %15
10: ; preds = %0
%11 = load i32, i32* @stdout
%12 = call i32 @fputs(i8* %2, i32 %11)
%13 = load i32, i32* @EOF
%14 = icmp eq i32 %12, %13
br i1 %14, label %16, label %15
15: ; preds = %10, %6
br label %16
16: ; preds = %10, %6, %15
%17 = phi i32 [ 0, %15 ], [ -1, %6 ], [ -1, %10 ]
ret i32 %17
}