#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 */
struct termios {
  int /*<<< orphan*/ c_lflag;
};

/* Variables and functions */
int /*<<< orphan*/ ECHO;
int /*<<< orphan*/ ICANON;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int getchar();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
int main(int argc, char *argv[]) {
  int c;
  struct termios tty, tty_back;
  tcgetattr(STDIN_FILENO, &tty);
  tty_back = tty;
  tty.c_lflag &= ~ICANON;
  tty.c_lflag &= ~ECHO;
  tcsetattr(STDIN_FILENO, TCSANOW, &tty);
  while (1) {
    c = getchar();
    switch (c) {
    case 'h':
      printf("[1D");
      break;
    case 'j':
      printf("[1B");
      break;
    case 'k':
      printf("[1A");
      break;
    case 'l':
      printf("[1C");
      break;
    case 'q':
      goto out;
      break;
    default:
      break;
    }
    fflush(stdout);
  }
out:
  tcsetattr(STDIN_FILENO, TCSANOW, &tty_back);
  printf("[1;1H");
  printf("[2J");
  fflush(stdout);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1060 <tcgetattr@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1070 <tcsetattr@plt>
xor    %eax,%eax
call   1040 <getchar@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0xffffff98,%eax
mov    %eax,%ecx
mov    %rcx,-0x28(%rbp)
sub    $0x9,%eax
ja     1239 <main+0xe9>
mov    -0x28(%rbp),%rax
lea    0xe19(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xe31(%rip),%rdi
call   1030 <printf@plt>
jmp    123e <main+0xee>
lea    0xe25(%rip),%rdi
call   1030 <printf@plt>
jmp    123e <main+0xee>
lea    0xe19(%rip),%rdi
call   1030 <printf@plt>
jmp    123e <main+0xee>
lea    0xe0d(%rip),%rdi
call   1030 <printf@plt>
jmp    123e <main+0xee>
jmp    1251 <main+0x101>
jmp    123e <main+0xee>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
jmp    11bd <main+0x6d>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
mov    0x2d87(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1070 <tcsetattr@plt>
lea    0xdc9(%rip),%rdi
call   1030 <printf@plt>
lea    0xdc4(%rip),%rdi
call   1030 <printf@plt>
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e63(%rip),%rbp
mov    0x0(%rbp),%edi
lea    0x8(%rsp),%rbx
mov    %rbx,%rsi
call   1060 <tcgetattr@plt>
mov    0x8(%rsp),%eax
mov    %eax,0x10(%rsp)
mov    0x2e3c(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2e53(%rip),%rdx
or     (%rdx),%ecx
not    %ecx
and    %eax,%ecx
mov    %ecx,0x8(%rsp)
mov    0x0(%rbp),%edi
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdx
call   1070 <tcsetattr@plt>
lea    0xe76(%rip),%r13
mov    0x2e17(%rip),%r12
lea    0xe40(%rip),%rbx
lea    0xe66(%rip),%rbp
lea    0xe64(%rip),%r15
lea    0xe62(%rip),%r14
jmp    11e8 <main+0x98>
mov    %r14,%rdi
call   1030 <printf@plt>
mov    (%r12),%edi
call   1050 <fflush@plt>
xor    %eax,%eax
call   1040 <getchar@plt>
add    $0xffffff98,%eax
cmp    $0x9,%eax
ja     11df <main+0x8f>
movslq (%rbx,%rax,4),%rax
add    %rbx,%rax
mov    %r13,%rdi
jmp    *%rax
mov    %rbp,%rdi
jmp    11da <main+0x8a>
mov    %r15,%rdi
jmp    11da <main+0x8a>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
call   1070 <tcsetattr@plt>
lea    0xe0c(%rip),%rdi
call   1030 <printf@plt>
lea    0xe07(%rip),%rdi
call   1030 <printf@plt>
mov    (%r12),%edi
call   1050 <fflush@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@STDIN_FILENO = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
@.str = [5 x i8] c"\1B[1D\00"
@.str.1 = [5 x i8] c"\1B[1B\00"
@.str.2 = [5 x i8] c"\1B[1A\00"
@.str.3 = [5 x i8] c"\1B[1C\00"
@stdout = global i32 0
@.str.4 = [7 x i8] c"\1B[1;1H\00"
@.str.5 = [5 x i8] c"\1B[2J\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.termios
%8 = alloca %struct.termios
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* @STDIN_FILENO
%10 = call i32 @tcgetattr(i32 %9, %struct.termios* %7)
%11 = bitcast %struct.termios* %8 to i8*
%12 = bitcast %struct.termios* %7 to i8*
%13 = load i32, i32* @ICANON
%14 = xor i32 %13, -1
%15 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 0
%16 = load i32, i32* %15
%17 = and i32 %16, %14
store i32 %17, i32* %15
%18 = load i32, i32* @ECHO
%19 = xor i32 %18, -1
%20 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 0
%21 = load i32, i32* %20
%22 = and i32 %21, %19
store i32 %22, i32* %20
%23 = load i32, i32* @STDIN_FILENO
%24 = load i32, i32* @TCSANOW
%25 = call i32 @tcsetattr(i32 %23, i32 %24, %struct.termios* %7)
br label %26
26: ; preds = %2, %39
%27 = call i32 (...) @getchar()
store i32 %27, i32* %6
%28 = load i32, i32* %6
switch i32 %28, label %38 [
i32 104, label %29
i32 106, label %31
i32 107, label %33
i32 108, label %35
i32 113, label %37
]
29: ; preds = %26
%30 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %39
31: ; preds = %26
%32 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %39
33: ; preds = %26
%34 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %39
35: ; preds = %26
%36 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %39
37: ; preds = %26
br label %42
38: ; preds = %26
br label %39
39: ; preds = %38, %35, %33, %31, %29
%40 = load i32, i32* @stdout
%41 = call i32 @fflush(i32 %40)
br label %26
42: ; preds = %37
%43 = load i32, i32* @STDIN_FILENO
%44 = load i32, i32* @TCSANOW
%45 = call i32 @tcsetattr(i32 %43, i32 %44, %struct.termios* %8)
%46 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%47 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%48 = load i32, i32* @stdout
%49 = call i32 @fflush(i32 %48)
ret i32 0
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@STDIN_FILENO = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
@.str = [5 x i8] c"\1B[1D\00"
@.str.1 = [5 x i8] c"\1B[1B\00"
@.str.2 = [5 x i8] c"\1B[1A\00"
@.str.3 = [5 x i8] c"\1B[1C\00"
@stdout = global i32 0
@.str.4 = [7 x i8] c"\1B[1;1H\00"
@.str.5 = [5 x i8] c"\1B[2J\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.termios
%4 = alloca %struct.termios
%5 = bitcast %struct.termios* %3 to i8*
%6 = bitcast %struct.termios* %4 to i8*
%7 = load i32, i32* @STDIN_FILENO
%8 = call i32 @tcgetattr(i32 %7, %struct.termios* %3)
%9 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%10 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
%11 = load i32, i32* %9
store i32 %11, i32* %10
%12 = load i32, i32* @ICANON
%13 = load i32, i32* @ECHO
%14 = or i32 %12, %13
%15 = xor i32 %14, -1
%16 = and i32 %11, %15
store i32 %16, i32* %9
%17 = load i32, i32* @STDIN_FILENO
%18 = load i32, i32* @TCSANOW
%19 = call i32 @tcsetattr(i32 %17, i32 %18, %struct.termios* %3)
br label %20
20: ; preds = %28, %2
%21 = call i32 (...) @getchar()
switch i32 %21, label %28 [
i32 104, label %25
i32 106, label %22
i32 107, label %23
i32 108, label %24
i32 113, label %31
]
22: ; preds = %20
br label %25
23: ; preds = %20
br label %25
24: ; preds = %20
br label %25
25: ; preds = %20, %22, %23, %24
%26 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), %24 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), %23 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %22 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %20 ]
%27 = call i32 @printf(i8* dereferenceable(1) %26)
br label %28
28: ; preds = %25, %20
%29 = load i32, i32* @stdout
%30 = call i32 @fflush(i32 %29)
br label %20
31: ; preds = %20
%32 = load i32, i32* @STDIN_FILENO
%33 = load i32, i32* @TCSANOW
%34 = call i32 @tcsetattr(i32 %32, i32 %33, %struct.termios* %4)
%35 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%36 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%37 = load i32, i32* @stdout
%38 = call i32 @fflush(i32 %37)
ret i32 0
}