#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*/ green(char *);
int /*<<< orphan*/ printf(char *, char *);
int read_console();
size_t read_tty(char *, int);
int /*<<< orphan*/ setup_con();
int /*<<< orphan*/ write_tty(char *, int);
void run_terminal() {
  green(("Work in terminal mode without echo\n"));
  int rb;
  char buf[1024];
  size_t l;
  setup_con();
  while (1) {
    if ((l = read_tty(buf, 1024 - 1))) {
      buf[l] = 0;
      printf("%s", buf);
    }
    if ((rb = read_console())) {
      buf[0] = (char)rb;
      write_tty(buf, 1);
    }
  }
}


x86 assembly (clang, -O0):
<run_terminal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
lea    0xe8e(%rip),%rdi
call   1040 <green@plt>
mov    $0x0,%al
call   1050 <setup_con@plt>
lea    -0x410(%rbp),%rdi
mov    $0x3ff,%esi
call   1070 <read_tty@plt>
mov    %rax,-0x418(%rbp)
cmp    $0x0,%rax
je     11c2 <run_terminal+0x62>
mov    -0x418(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
lea    -0x410(%rbp),%rsi
lea    0xe67(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1080 <read_console@plt>
mov    %eax,-0x4(%rbp)
cmp    $0x0,%eax
je     11ef <run_terminal+0x8f>
mov    -0x4(%rbp),%eax
mov    %al,-0x410(%rbp)
lea    -0x410(%rbp),%rdi
mov    $0x1,%esi
call   1060 <write_tty@plt>
jmp    117e <run_terminal+0x1e>

x86 assembly (clang, -O2):
<run_terminal>:
push   %r14
push   %rbx
sub    $0x408,%rsp
lea    0xe8f(%rip),%rdi
call   1040 <green@plt>
xor    %eax,%eax
call   1050 <setup_con@plt>
mov    %rsp,%rbx
lea    0xe9d(%rip),%r14
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x3ff,%esi
call   1070 <read_tty@plt>
test   %rax,%rax
je     11b1 <run_terminal+0x51>
movb   $0x0,(%rsp,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
xor    %eax,%eax
call   1080 <read_console@plt>
test   %eax,%eax
je     1190 <run_terminal+0x30>
mov    %al,(%rsp)
mov    %rbx,%rdi
mov    $0x1,%esi
call   1060 <write_tty@plt>
jmp    1190 <run_terminal+0x30>

LLVM IR (-O0):
@.str = [36 x i8] c"Work in terminal mode without echo\0A\00"
@.str.1 = [3 x i8] c"%s\00"
define void @run_terminal() {
%1 = alloca i32
%2 = alloca [1024 x i8]
%3 = alloca i64
%4 = call i32 @green(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%5 = call i32 (...) @setup_con()
br label %6
6: ; preds = %0, %24
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%8 = call i64 @read_tty(i8* %7, i32 1023)
store i64 %8, i64* %3
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %6
%11 = load i64, i64* %3
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %11
store i8 0, i8* %12
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%14 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %13)
br label %15
15: ; preds = %10, %6
%16 = call i32 (...) @read_console()
store i32 %16, i32* %1
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %15
%19 = load i32, i32* %1
%20 = trunc i32 %19 to i8
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
store i8 %20, i8* %21
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%23 = call i32 @write_tty(i8* %22, i32 1)
br label %24
24: ; preds = %18, %15
br label %6
}

LLVM IR (-O2):
@.str = [36 x i8] c"Work in terminal mode without echo\0A\00"
@.str.1 = [3 x i8] c"%s\00"
define void @run_terminal() {
%1 = alloca [1024 x i8]
%2 = tail call i32 @green(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%4 = tail call i32 (...) @setup_con()
br label %5
5: ; preds = %17, %0
%6 = call i64 @read_tty(i8* %3, i32 1023)
%7 = icmp eq i64 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %5
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 %6
store i8 0, i8* %9
%10 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %3)
br label %11
11: ; preds = %8, %5
%12 = call i32 (...) @read_console()
%13 = icmp eq i32 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %11
%15 = trunc i32 %12 to i8
store i8 %15, i8* %3
%16 = call i32 @write_tty(i8* %3, i32 1)
br label %17
17: ; preds = %14, %11
br label %5
}