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

/* Variables and functions */
int /*<<< orphan*/ printf(char *);
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ term_set_raw_mode();
int /*<<< orphan*/ write(int, char *, int);
void echo_read(void) {
  int ret;
  char buf[16];
  printf("echo_read()\n");
  term_set_raw_mode();
  while (1) {
    ret = read(0, buf, sizeof(buf));
    write(1, buf, ret);
    if (ret == 1 && (buf[0] == '\n' || buf[0] == '\r'))
      break;
  }
}


x86 assembly (clang, -O0):
<echo_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xeb1(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <term_set_raw_mode@plt>
lea    -0x20(%rbp),%rsi
xor    %edi,%edi
mov    $0x10,%edx
call   1060 <read@plt>
mov    %eax,-0x4(%rbp)
lea    -0x20(%rbp),%rsi
mov    -0x4(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
cmpl   $0x1,-0x4(%rbp)
jne    11a8 <echo_read+0x68>
movsbl -0x20(%rbp),%eax
cmp    $0xa,%eax
je     11a3 <echo_read+0x63>
movsbl -0x20(%rbp),%eax
cmp    $0xd,%eax
jne    11a8 <echo_read+0x68>
jmp    11ad <echo_read+0x6d>
jmp    115b <echo_read+0x1b>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<echo_read>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
lea    0xeb3(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1050 <term_set_raw_mode@plt>
mov    %rsp,%rbx
nopl   0x0(%rax)
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x10,%edx
call   1060 <read@plt>
mov    %eax,%ebp
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    $0x1,%ebp
jne    1160 <echo_read+0x20>
movzbl (%rsp),%eax
cmp    $0xd,%al
je     1191 <echo_read+0x51>
cmp    $0xa,%al
jne    1160 <echo_read+0x20>
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"echo_read()\0A\00"
define void @echo_read() {
%1 = alloca i32
%2 = alloca [16 x i8]
%3 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%4 = call i32 (...) @term_set_raw_mode()
br label %5
5: ; preds = %0, %24
%6 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%7 = call i32 @read(i32 0, i8* %6, i32 16)
store i32 %7, i32* %1
%8 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%9 = load i32, i32* %1
%10 = call i32 @write(i32 1, i8* %8, i32 %9)
%11 = load i32, i32* %1
%12 = icmp eq i32 %11, 1
br i1 %12, label %13, label %24
13: ; preds = %5
%14 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp eq i32 %16, 10
br i1 %17, label %23, label %18
18: ; preds = %13
%19 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp eq i32 %21, 13
br i1 %22, label %23, label %24
23: ; preds = %18, %13
br label %25
24: ; preds = %18, %5
br label %5
25: ; preds = %23
ret void
}

LLVM IR (-O2):
@str = [12 x i8] c"echo_read()\00"
define void @echo_read() {
%1 = alloca [16 x i8]
%2 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 (...) @term_set_raw_mode()
br label %5
5: ; preds = %11, %0
%6 = call i32 @read(i32 0, i8* %2, i32 16)
%7 = call i32 @write(i32 1, i8* %2, i32 %6)
%8 = icmp eq i32 %6, 1
br i1 %8, label %9, label %11
9: ; preds = %5
%10 = load i8, i8* %2
switch i8 %10, label %11 [
i8 13, label %12
i8 10, label %12
]
11: ; preds = %9, %5
br label %5
12: ; preds = %9, %9
ret void
}