#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 EOF;
scalar_t__ feof(int /*<<< orphan*/);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int fgetc(int /*<<< orphan*/);
int fputc(int, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int _echo(void) {
  int c;
  while ((c = fgetc(stdin)) != EOF) {
    if (fputc(c, stdout) == EOF)
      return feof(stdout) ? 0 : 2;
    if (c == '\n')
      fflush(stdout);
  }
  return feof(stdin) ? 0 : 2;
}


x86 assembly (clang, -O0):
<_echo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e99(%rip),%rax
mov    (%rax),%edi
call   1030 <fgetc@plt>
mov    %eax,-0x8(%rbp)
mov    0x2e80(%rip),%rcx
cmp    (%rcx),%eax
je     11cc <_echo+0x8c>
mov    -0x8(%rbp),%edi
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
call   1040 <fputc@plt>
mov    0x2e60(%rip),%rcx
cmp    (%rcx),%eax
jne    11af <_echo+0x6f>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
call   1050 <feof@plt>
mov    %rax,%rdx
mov    $0x2,%eax
xor    %ecx,%ecx
cmp    $0x0,%rdx
cmovne %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    11ee <_echo+0xae>
cmpl   $0xa,-0x8(%rbp)
jne    11c7 <_echo+0x87>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
jmp    1148 <_echo+0x8>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
call   1050 <feof@plt>
mov    %rax,%rdx
mov    $0x2,%eax
xor    %ecx,%ecx
cmp    $0x0,%rdx
cmovne %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_echo>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    0x2e99(%rip),%r14
mov    (%r14),%edi
call   1030 <fgetc@plt>
mov    0x2e82(%rip),%r15
cmp    (%r15),%eax
je     11b1 <_echo+0x71>
mov    %eax,%ebx
mov    0x2e64(%rip),%r14
mov    0x2e75(%rip),%r12
jmp    1190 <_echo+0x50>
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r12),%edi
call   1030 <fgetc@plt>
mov    %eax,%ebx
cmp    (%r15),%eax
je     11ae <_echo+0x6e>
mov    (%r14),%esi
mov    %ebx,%edi
call   1040 <fputc@plt>
cmp    (%r15),%eax
je     11b1 <_echo+0x71>
cmp    $0xa,%ebx
jne    1180 <_echo+0x40>
mov    (%r14),%edi
call   1060 <fflush@plt>
jmp    1180 <_echo+0x40>
mov    %r12,%r14
mov    (%r14),%edi
call   1050 <feof@plt>
xor    %ecx,%ecx
test   %rax,%rax
sete   %cl
add    %ecx,%ecx
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stdin = global i32 0
@EOF = global i32 0
@stdout = global i32 0
define i32 @_echo() {
%1 = alloca i32
%2 = alloca i32
br label %3
3: ; preds = %26, %0
%4 = load i32, i32* @stdin
%5 = call i32 @fgetc(i32 %4)
store i32 %5, i32* %2
%6 = load i32, i32* @EOF
%7 = icmp ne i32 %5, %6
br i1 %7, label %8, label %27
8: ; preds = %3
%9 = load i32, i32* %2
%10 = load i32, i32* @stdout
%11 = call i32 @fputc(i32 %9, i32 %10)
%12 = load i32, i32* @EOF
%13 = icmp eq i32 %11, %12
br i1 %13, label %14, label %20
14: ; preds = %8
%15 = load i32, i32* @stdout
%16 = call i64 @feof(i32 %15)
%17 = icmp ne i64 %16, 0
%18 = zext i1 %17 to i64
%19 = select i1 %17, i32 0, i32 2
store i32 %19, i32* %1
br label %33
20: ; preds = %8
%21 = load i32, i32* %2
%22 = icmp eq i32 %21, 10
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = load i32, i32* @stdout
%25 = call i32 @fflush(i32 %24)
br label %26
26: ; preds = %23, %20
br label %3
27: ; preds = %3
%28 = load i32, i32* @stdin
%29 = call i64 @feof(i32 %28)
%30 = icmp ne i64 %29, 0
%31 = zext i1 %30 to i64
%32 = select i1 %30, i32 0, i32 2
store i32 %32, i32* %1
br label %33
33: ; preds = %27, %14
%34 = load i32, i32* %1
ret i32 %34
}

LLVM IR (-O2):
@stdin = global i32 0
@EOF = global i32 0
@stdout = global i32 0
define i32 @_echo() {
%1 = load i32, i32* @stdin
%2 = tail call i32 @fgetc(i32 %1)
%3 = load i32, i32* @EOF
%4 = icmp eq i32 %2, %3
br i1 %4, label %21, label %5
5: ; preds = %0, %16
%6 = phi i32 [ %18, %16 ], [ %2, %0 ]
%7 = load i32, i32* @stdout
%8 = tail call i32 @fputc(i32 %6, i32 %7)
%9 = load i32, i32* @EOF
%10 = icmp eq i32 %8, %9
br i1 %10, label %21, label %11
11: ; preds = %5
%12 = icmp eq i32 %6, 10
br i1 %12, label %13, label %16
13: ; preds = %11
%14 = load i32, i32* @stdout
%15 = tail call i32 @fflush(i32 %14)
br label %16
16: ; preds = %13, %11
%17 = load i32, i32* @stdin
%18 = tail call i32 @fgetc(i32 %17)
%19 = load i32, i32* @EOF
%20 = icmp eq i32 %18, %19
br i1 %20, label %21, label %5
21: ; preds = %16, %5, %0
%22 = phi i32* [ @stdin, %0 ], [ @stdout, %5 ], [ @stdin, %16 ]
%23 = load i32, i32* %22
%24 = tail call i64 @feof(i32 %23)
%25 = icmp eq i64 %24, 0
%26 = select i1 %25, i32 2, i32 0
ret i32 %26
}