#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 c_cflag;
  int c_lflag;
  int *c_cc;
  int /*<<< orphan*/ c_iflag;
};

/* Variables and functions */
int CS8;
int CSIZE;
int ECHO;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int HUPCL;
int ICANON;
int /*<<< orphan*/ ISTRIP;
unsigned long O_NONBLOCK;
int /*<<< orphan*/ TCSADRAIN;
size_t VMIN;
int /*<<< orphan*/ _IONBF;
unsigned long fcntl(int /*<<< orphan*/, int /*<<< orphan*/, unsigned long);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
scalar_t__ isprint(unsigned char);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
int read(int /*<<< orphan*/, unsigned char *, int);
int /*<<< orphan*/ setvbuf(int /*<<< orphan*/, int /*<<< orphan*/ *,
                           int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
int main(int argc, char *argv[]) {
  struct termios tio, tio_s;
  int nc;
  unsigned long flags;
  unsigned char c;
  fflush(stdin);
  setvbuf(stdin, NULL, _IONBF, 0);
  flags = fcntl(0, F_GETFL, 0);
  fcntl(0, F_SETFL, flags | O_NONBLOCK);
  tcgetattr(0, &tio);
  tio_s = tio;
  tio.c_iflag &= ~ISTRIP;
  tio.c_cflag &= ~(CSIZE | HUPCL);
  tio.c_cflag |= CS8;
  tio.c_lflag &= ~(ICANON | ECHO);
  tio.c_cc[VMIN] = 1;
  tcsetattr(0, TCSADRAIN, &tio);
  printf("Keycodes. X to exit\n");
  puts("\033[?1000h");
  do {
    nc = read(0, &c, 1);
    if (nc == 1)
      printf("nc = %d, symbol = [%c], code = %3u, %02xx\n", nc,
             isprint(c) ? c : ' ', c, c);
  } while (c != 'X');
  tcsetattr(0, TCSADRAIN, &tio_s);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
mov    0x2e0c(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
xor    %ecx,%ecx
call   1080 <setvbuf@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <fcntl@plt>
mov    %rax,-0x50(%rbp)
mov    0x2db8(%rip),%rax
mov    (%rax),%esi
mov    -0x50(%rbp),%rdx
mov    0x2deb(%rip),%rax
or     (%rax),%rdx
xor    %edi,%edi
call   1040 <fcntl@plt>
xor    %edi,%edi
lea    -0x28(%rbp),%rsi
call   10a0 <tcgetattr@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2d5f(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d75(%rip),%rax
mov    (%rax),%eax
mov    0x2d3c(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d6a(%rip),%rax
mov    (%rax),%eax
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d13(%rip),%rax
mov    (%rax),%eax
mov    0x2d62(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2d34(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2d03(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x28(%rbp),%rdx
call   10b0 <tcsetattr@plt>
lea    0xd4f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xd56(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
lea    -0x51(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x1,-0x44(%rbp)
jne    1330 <main+0x1a0>
mov    -0x44(%rbp),%eax
mov    %eax,-0x58(%rbp)
movzbl -0x51(%rbp),%edi
call   1090 <isprint@plt>
cmp    $0x0,%rax
je     1306 <main+0x176>
movzbl -0x51(%rbp),%eax
mov    %eax,-0x5c(%rbp)
jmp    1313 <main+0x183>
mov    $0x20,%eax
mov    %eax,-0x5c(%rbp)
jmp    1313 <main+0x183>
mov    -0x58(%rbp),%esi
mov    -0x5c(%rbp),%edx
movzbl -0x51(%rbp),%ecx
movzbl -0x51(%rbp),%r8d
lea    0xcf5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1335 <main+0x1a5>
movzbl -0x51(%rbp),%eax
cmp    $0x58,%eax
jne    12c4 <main+0x134>
mov    0x2c57(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x40(%rbp),%rdx
call   10b0 <tcsetattr@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
mov    0x2e3d(%rip),%rbx
mov    (%rbx),%edi
call   1070 <fflush@plt>
mov    (%rbx),%edi
mov    0x2e1d(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
xor    %ecx,%ecx
call   1080 <setvbuf@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    0x2dd1(%rip),%rcx
mov    (%rcx),%esi
mov    0x2e08(%rip),%rcx
or     (%rcx),%rax
xor    %edi,%edi
mov    %rax,%rdx
call   1040 <fcntl@plt>
lea    0x8(%rsp),%rbx
xor    %edi,%edi
mov    %rbx,%rsi
call   10a0 <tcgetattr@plt>
movups 0x8(%rsp),%xmm0
mov    0x18(%rsp),%rax
mov    0x2d83(%rip),%rcx
mov    (%rcx),%ecx
not    %ecx
and    %ecx,0x18(%rsp)
mov    0x2d9c(%rip),%rcx
mov    0x2d65(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
not    %edx
and    0x8(%rsp),%edx
mov    0x2d94(%rip),%rcx
or     (%rcx),%edx
mov    %edx,0x8(%rsp)
mov    0x2d3f(%rip),%rcx
mov    0x2d90(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
not    %edx
and    %edx,0xc(%rsp)
movaps %xmm0,0x20(%rsp)
mov    %rax,0x30(%rsp)
mov    0x10(%rsp),%rax
mov    0x2d58(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2d27(%rip),%r15
mov    (%r15),%esi
xor    %edi,%edi
mov    %rbx,%rdx
call   10b0 <tcsetattr@plt>
lea    0xda7(%rip),%rdi
call   1030 <puts@plt>
lea    0xd67(%rip),%rdi
call   1030 <puts@plt>
lea    0x7(%rsp),%rbx
mov    $0x20,%r12d
lea    0xd59(%rip),%r14
jmp    12c6 <main+0x136>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
cmp    $0x58,%bpl
je     1305 <main+0x175>
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
movzbl 0x7(%rsp),%ebp
cmp    $0x1,%eax
jne    12c0 <main+0x130>
mov    %ebp,%edi
call   1090 <isprint@plt>
test   %rax,%rax
mov    %ebp,%edx
cmove  %r12d,%edx
mov    %r14,%rdi
mov    $0x1,%esi
mov    %ebp,%ecx
mov    %ebp,%r8d
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12c0 <main+0x130>
mov    (%r15),%esi
lea    0x20(%rsp),%rdx
xor    %edi,%edi
call   10b0 <tcsetattr@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32, i32*, i32 }
@stdin = global i32 0
@_IONBF = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i64 0
@ISTRIP = global i32 0
@CSIZE = global i32 0
@HUPCL = global i32 0
@CS8 = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@VMIN = global i64 0
@TCSADRAIN = global i32 0
@.str = [21 x i8] c"Keycodes. X to exit\0A\00"
@.str.1 = [9 x i8] c"\1B[?1000h\00"
@.str.2 = [43 x i8] c"nc = %d, symbol = [%c], code = %3u, %02xx\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.termios
%7 = alloca %struct.termios
%8 = alloca i32
%9 = alloca i64
%10 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* @stdin
%12 = call i32 @fflush(i32 %11)
%13 = load i32, i32* @stdin
%14 = load i32, i32* @_IONBF
%15 = call i32 @setvbuf(i32 %13, i32* null, i32 %14, i32 0)
%16 = load i32, i32* @F_GETFL
%17 = call i64 @fcntl(i32 0, i32 %16, i64 0)
store i64 %17, i64* %9
%18 = load i32, i32* @F_SETFL
%19 = load i64, i64* %9
%20 = load i64, i64* @O_NONBLOCK
%21 = or i64 %19, %20
%22 = call i64 @fcntl(i32 0, i32 %18, i64 %21)
%23 = call i32 @tcgetattr(i32 0, %struct.termios* %6)
%24 = bitcast %struct.termios* %7 to i8*
%25 = bitcast %struct.termios* %6 to i8*
%26 = load i32, i32* @ISTRIP
%27 = xor i32 %26, -1
%28 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 3
%29 = load i32, i32* %28
%30 = and i32 %29, %27
store i32 %30, i32* %28
%31 = load i32, i32* @CSIZE
%32 = load i32, i32* @HUPCL
%33 = or i32 %31, %32
%34 = xor i32 %33, -1
%35 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%36 = load i32, i32* %35
%37 = and i32 %36, %34
store i32 %37, i32* %35
%38 = load i32, i32* @CS8
%39 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%40 = load i32, i32* %39
%41 = or i32 %40, %38
store i32 %41, i32* %39
%42 = load i32, i32* @ICANON
%43 = load i32, i32* @ECHO
%44 = or i32 %42, %43
%45 = xor i32 %44, -1
%46 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 1
%47 = load i32, i32* %46
%48 = and i32 %47, %45
store i32 %48, i32* %46
%49 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 2
%50 = load i32*, i32** %49
%51 = load i64, i64* @VMIN
%52 = getelementptr inbounds i32, i32* %50, i64 %51
store i32 1, i32* %52
%53 = load i32, i32* @TCSADRAIN
%54 = call i32 @tcsetattr(i32 0, i32 %53, %struct.termios* %6)
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%56 = call i32 @puts(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %57
57: ; preds = %78, %2
%58 = call i32 @read(i32 0, i8* %10, i32 1)
store i32 %58, i32* %8
%59 = load i32, i32* %8
%60 = icmp eq i32 %59, 1
br i1 %60, label %61, label %77
61: ; preds = %57
%62 = load i32, i32* %8
%63 = load i8, i8* %10
%64 = call i64 @isprint(i8 zeroext %63)
%65 = icmp ne i64 %64, 0
br i1 %65, label %66, label %69
66: ; preds = %61
%67 = load i8, i8* %10
%68 = zext i8 %67 to i32
br label %70
69: ; preds = %61
br label %70
70: ; preds = %69, %66
%71 = phi i32 [ %68, %66 ], [ 32, %69 ]
%72 = load i8, i8* %10
%73 = zext i8 %72 to i32
%74 = load i8, i8* %10
%75 = zext i8 %74 to i32
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i32 %62, i32 %71, i32 %73, i32 %75)
br label %77
77: ; preds = %70, %57
br label %78
78: ; preds = %77
%79 = load i8, i8* %10
%80 = zext i8 %79 to i32
%81 = icmp ne i32 %80, 88
br i1 %81, label %57, label %82
82: ; preds = %78
%83 = load i32, i32* @TCSADRAIN
%84 = call i32 @tcsetattr(i32 0, i32 %83, %struct.termios* %7)
ret i32 0
}

LLVM IR (-O2):
%struct.termios = type { i32, i32, i32*, i32 }
@stdin = global i32 0
@_IONBF = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i64 0
@ISTRIP = global i32 0
@CSIZE = global i32 0
@HUPCL = global i32 0
@CS8 = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@VMIN = global i64 0
@TCSADRAIN = global i32 0
@.str.1 = [9 x i8] c"\1B[?1000h\00"
@.str.2 = [43 x i8] c"nc = %d, symbol = [%c], code = %3u, %02xx\0A\00"
@str = [20 x i8] c"Keycodes. X to exit\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.termios
%4 = alloca %struct.termios
%5 = alloca i8
%6 = bitcast %struct.termios* %3 to i8*
%7 = bitcast %struct.termios* %4 to i8*
%8 = load i32, i32* @stdin
%9 = tail call i32 @fflush(i32 %8)
%10 = load i32, i32* @stdin
%11 = load i32, i32* @_IONBF
%12 = tail call i32 @setvbuf(i32 %10, i32* null, i32 %11, i32 0)
%13 = load i32, i32* @F_GETFL
%14 = tail call i64 @fcntl(i32 0, i32 %13, i64 0)
%15 = load i32, i32* @F_SETFL
%16 = load i64, i64* @O_NONBLOCK
%17 = or i64 %16, %14
%18 = tail call i64 @fcntl(i32 0, i32 %15, i64 %17)
%19 = call i32 @tcgetattr(i32 0, %struct.termios* %3)
%20 = load i32, i32* @ISTRIP
%21 = xor i32 %20, -1
%22 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 3
%23 = load i32, i32* %22
%24 = and i32 %23, %21
store i32 %24, i32* %22
%25 = load i32, i32* @CSIZE
%26 = load i32, i32* @HUPCL
%27 = or i32 %26, %25
%28 = xor i32 %27, -1
%29 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%30 = load i32, i32* %29
%31 = and i32 %30, %28
%32 = load i32, i32* @CS8
%33 = or i32 %32, %31
store i32 %33, i32* %29
%34 = load i32, i32* @ICANON
%35 = load i32, i32* @ECHO
%36 = or i32 %35, %34
%37 = xor i32 %36, -1
%38 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 1
%39 = load i32, i32* %38
%40 = and i32 %39, %37
store i32 %40, i32* %38
%41 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 2
%42 = load i32*, i32** %41
%43 = load i64, i64* @VMIN
%44 = getelementptr inbounds i32, i32* %42, i64 %43
store i32 1, i32* %44
%45 = load i32, i32* @TCSADRAIN
%46 = call i32 @tcsetattr(i32 0, i32 %45, %struct.termios* %3)
%47 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%48 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %49
49: ; preds = %59, %2
%50 = call i32 @read(i32 0, i8* %5, i32 1)
%51 = icmp eq i32 %50, 1
%52 = load i8, i8* %5
br i1 %51, label %53, label %59
53: ; preds = %49
%54 = call i64 @isprint(i8 zeroext %52)
%55 = icmp eq i64 %54, 0
%56 = zext i8 %52 to i32
%57 = select i1 %55, i32 32, i32 %56
%58 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i32 1, i32 %57, i32 %56, i32 %56)
br label %59
59: ; preds = %49, %53
%60 = icmp eq i8 %52, 88
br i1 %60, label %61, label %49
61: ; preds = %59
%62 = load i32, i32* @TCSADRAIN
%63 = call i32 @tcsetattr(i32 0, i32 %62, %struct.termios* %4)
ret i32 0
}