#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_iflag;
  int c_cflag;
  int *c_cc;
  scalar_t__ c_lflag;
  scalar_t__ c_oflag;
};

/* Variables and functions */
int CREAD;
int CS8;
int IGNBRK;
int IGNPAR;
int ISTRIP;
int /*<<< orphan*/ KDSKBMODE;
int /*<<< orphan*/ K_RAW;
int /*<<< orphan*/ K_XLATE;
int /*<<< orphan*/ O_RDONLY;
int PARMRK;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ cfsetispeed(struct termios *, int);
int /*<<< orphan*/ cfsetospeed(struct termios *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, unsigned char);
int read(int, unsigned char *, int);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int main(int argc, char *argv[]) {
  int kbdFd = open("/dev/tty0", O_RDONLY, 0);
  if (kbdFd < 0) {
    return 1;
  }
  struct termios origTermData;
  struct termios termData;
  tcgetattr(kbdFd, &origTermData);
  tcgetattr(kbdFd, &termData);
  ioctl(kbdFd, KDSKBMODE, K_RAW);
  termData.c_iflag = (IGNPAR | IGNBRK) & (~PARMRK) & (~ISTRIP);
  termData.c_oflag = 0;
  termData.c_cflag = CREAD | CS8;
  termData.c_lflag = 0;
  termData.c_cc[VTIME] = 0;
  termData.c_cc[VMIN] = 1;
  cfsetispeed(&termData, 9600);
  cfsetospeed(&termData, 9600);
  tcsetattr(kbdFd, TCSANOW, &termData);
  unsigned char code;
  int n = read(kbdFd, &code, 1);
  if (n == 1)
    printf("%02x\n", code);
  ioctl(kbdFd, KDSKBMODE, K_XLATE);
  tcsetattr(kbdFd, TCSANOW, &origTermData);
  close(kbdFd);
  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    0x2e2b(%rip),%rax
mov    (%rax),%esi
lea    0xe4a(%rip),%rdi
xor    %edx,%edx
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11d6 <main+0x46>
movl   $0x1,-0x4(%rbp)
jmp    1325 <main+0x195>
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1080 <tcgetattr@plt>
mov    -0x14(%rbp),%edi
lea    -0x58(%rbp),%rsi
call   1080 <tcgetattr@plt>
mov    -0x14(%rbp),%edi
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
mov    0x2db7(%rip),%rax
mov    (%rax),%edx
call   1050 <ioctl@plt>
mov    0x2d71(%rip),%rax
mov    (%rax),%eax
mov    0x2d78(%rip),%rcx
or     (%rcx),%eax
mov    0x2dcf(%rip),%rcx
mov    (%rcx),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    0x2d69(%rip),%rcx
mov    (%rcx),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    %eax,-0x58(%rbp)
movq   $0x0,-0x40(%rbp)
mov    0x2d78(%rip),%rax
mov    (%rax),%eax
mov    0x2d5f(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x54(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x50(%rbp),%rax
mov    0x2d1f(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x50(%rbp),%rax
mov    0x2d2a(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
lea    -0x58(%rbp),%rdi
mov    $0x2580,%esi
call   10b0 <cfsetispeed@plt>
lea    -0x58(%rbp),%rdi
mov    $0x2580,%esi
call   1030 <cfsetospeed@plt>
mov    -0x14(%rbp),%edi
mov    0x2d1a(%rip),%rax
mov    (%rax),%esi
lea    -0x58(%rbp),%rdx
call   1090 <tcsetattr@plt>
mov    -0x14(%rbp),%edi
lea    -0x59(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    %eax,-0x60(%rbp)
cmpl   $0x1,-0x60(%rbp)
jne    12e7 <main+0x157>
lea    0xd2c(%rip),%rdi
movzbl -0x59(%rbp),%esi
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
mov    0x2cdf(%rip),%rax
mov    (%rax),%esi
mov    0x2ce6(%rip),%rax
mov    (%rax),%edx
call   1050 <ioctl@plt>
mov    -0x14(%rbp),%edi
mov    0x2cbd(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1090 <tcsetattr@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
lea    0xe55(%rip),%rdi
xor    %edx,%edx
call   10a0 <open@plt>
test   %eax,%eax
js     12e6 <main+0x156>
mov    %eax,%ebx
lea    0x28(%rsp),%rsi
mov    %eax,%edi
call   1080 <tcgetattr@plt>
lea    0x8(%rsp),%r14
mov    %ebx,%edi
mov    %r14,%rsi
call   1080 <tcgetattr@plt>
mov    0x2df2(%rip),%r15
mov    (%r15),%esi
mov    0x2dd0(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
call   1050 <ioctl@plt>
mov    0x2d88(%rip),%rax
mov    0x2d91(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2de6(%rip),%rax
mov    0x2d87(%rip),%rdx
mov    (%rdx),%edx
or     (%rax),%edx
not    %edx
and    %ecx,%edx
mov    %edx,0x8(%rsp)
mov    0x2d9c(%rip),%rax
mov    0x2d85(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,0xc(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x18(%rsp)
mov    0x10(%rsp),%rax
mov    0x2d41(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2d50(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    %r14,%rdi
mov    $0x2580,%esi
call   10b0 <cfsetispeed@plt>
mov    %r14,%rdi
mov    $0x2580,%esi
call   1030 <cfsetospeed@plt>
mov    0x2d45(%rip),%r12
mov    (%r12),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1090 <tcsetattr@plt>
lea    0x7(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1070 <read@plt>
cmp    $0x1,%eax
jne    12b8 <main+0x128>
movzbl 0x7(%rsp),%esi
lea    0xd57(%rip),%rdi
call   1040 <printf@plt>
mov    (%r15),%esi
mov    0x2d1e(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
call   1050 <ioctl@plt>
mov    (%r12),%esi
lea    0x28(%rsp),%rdx
mov    %ebx,%edi
call   1090 <tcsetattr@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
jmp    12eb <main+0x15b>
mov    $0x1,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32, i32*, i64, i64 }
@.str = [10 x i8] c"/dev/tty0\00"
@O_RDONLY = global i32 0
@KDSKBMODE = global i32 0
@K_RAW = global i32 0
@IGNPAR = global i32 0
@IGNBRK = global i32 0
@PARMRK = global i32 0
@ISTRIP = global i32 0
@CREAD = global i32 0
@CS8 = global i32 0
@VTIME = global i64 0
@VMIN = global i64 0
@TCSANOW = global i32 0
@.str.1 = [6 x i8] c"%02x\0A\00"
@K_XLATE = global i32 0
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
%9 = alloca i8
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %11, i32 0)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 1, i32* %3
br label %71
16: ; preds = %2
%17 = load i32, i32* %6
%18 = call i32 @tcgetattr(i32 %17, %struct.termios* %7)
%19 = load i32, i32* %6
%20 = call i32 @tcgetattr(i32 %19, %struct.termios* %8)
%21 = load i32, i32* %6
%22 = load i32, i32* @KDSKBMODE
%23 = load i32, i32* @K_RAW
%24 = call i32 @ioctl(i32 %21, i32 %22, i32 %23)
%25 = load i32, i32* @IGNPAR
%26 = load i32, i32* @IGNBRK
%27 = or i32 %25, %26
%28 = load i32, i32* @PARMRK
%29 = xor i32 %28, -1
%30 = and i32 %27, %29
%31 = load i32, i32* @ISTRIP
%32 = xor i32 %31, -1
%33 = and i32 %30, %32
%34 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 0
store i32 %33, i32* %34
%35 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 4
store i64 0, i64* %35
%36 = load i32, i32* @CREAD
%37 = load i32, i32* @CS8
%38 = or i32 %36, %37
%39 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 1
store i32 %38, i32* %39
%40 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 3
store i64 0, i64* %40
%41 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 2
%42 = load i32*, i32** %41
%43 = load i64, i64* @VTIME
%44 = getelementptr inbounds i32, i32* %42, i64 %43
store i32 0, i32* %44
%45 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 2
%46 = load i32*, i32** %45
%47 = load i64, i64* @VMIN
%48 = getelementptr inbounds i32, i32* %46, i64 %47
store i32 1, i32* %48
%49 = call i32 @cfsetispeed(%struct.termios* %8, i32 9600)
%50 = call i32 @cfsetospeed(%struct.termios* %8, i32 9600)
%51 = load i32, i32* %6
%52 = load i32, i32* @TCSANOW
%53 = call i32 @tcsetattr(i32 %51, i32 %52, %struct.termios* %8)
%54 = load i32, i32* %6
%55 = call i32 @read(i32 %54, i8* %9, i32 1)
store i32 %55, i32* %10
%56 = load i32, i32* %10
%57 = icmp eq i32 %56, 1
br i1 %57, label %58, label %61
58: ; preds = %16
%59 = load i8, i8* %9
%60 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %59)
br label %61
61: ; preds = %58, %16
%62 = load i32, i32* %6
%63 = load i32, i32* @KDSKBMODE
%64 = load i32, i32* @K_XLATE
%65 = call i32 @ioctl(i32 %62, i32 %63, i32 %64)
%66 = load i32, i32* %6
%67 = load i32, i32* @TCSANOW
%68 = call i32 @tcsetattr(i32 %66, i32 %67, %struct.termios* %7)
%69 = load i32, i32* %6
%70 = call i32 @close(i32 %69)
store i32 0, i32* %3
br label %71
71: ; preds = %61, %15
%72 = load i32, i32* %3
ret i32 %72
}

LLVM IR (-O2):
%struct.termios = type { i32, i32, i32*, i64, i64 }
@.str = [10 x i8] c"/dev/tty0\00"
@O_RDONLY = global i32 0
@KDSKBMODE = global i32 0
@K_RAW = global i32 0
@IGNPAR = global i32 0
@IGNBRK = global i32 0
@PARMRK = global i32 0
@ISTRIP = global i32 0
@CREAD = global i32 0
@CS8 = global i32 0
@VTIME = global i64 0
@VMIN = global i64 0
@TCSANOW = global i32 0
@.str.1 = [6 x i8] c"%02x\0A\00"
@K_XLATE = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.termios
%4 = alloca %struct.termios
%5 = alloca i8
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %54, label %9
9: ; preds = %2
%10 = bitcast %struct.termios* %3 to i8*
%11 = bitcast %struct.termios* %4 to i8*
%12 = call i32 @tcgetattr(i32 %7, %struct.termios* %3)
%13 = call i32 @tcgetattr(i32 %7, %struct.termios* %4)
%14 = load i32, i32* @KDSKBMODE
%15 = load i32, i32* @K_RAW
%16 = call i32 @ioctl(i32 %7, i32 %14, i32 %15)
%17 = load i32, i32* @IGNPAR
%18 = load i32, i32* @IGNBRK
%19 = or i32 %18, %17
%20 = load i32, i32* @PARMRK
%21 = load i32, i32* @ISTRIP
%22 = or i32 %21, %20
%23 = xor i32 %22, -1
%24 = and i32 %19, %23
%25 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
store i32 %24, i32* %25
%26 = load i32, i32* @CREAD
%27 = load i32, i32* @CS8
%28 = or i32 %27, %26
%29 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 1
store i32 %28, i32* %29
%30 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 3
%31 = bitcast i64* %30 to <2 x i64>*
store <2 x i64> zeroinitializer, <2 x i64>* %31
%32 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 2
%33 = load i32*, i32** %32
%34 = load i64, i64* @VTIME
%35 = getelementptr inbounds i32, i32* %33, i64 %34
store i32 0, i32* %35
%36 = load i64, i64* @VMIN
%37 = getelementptr inbounds i32, i32* %33, i64 %36
store i32 1, i32* %37
%38 = call i32 @cfsetispeed(%struct.termios* %4, i32 9600)
%39 = call i32 @cfsetospeed(%struct.termios* %4, i32 9600)
%40 = load i32, i32* @TCSANOW
%41 = call i32 @tcsetattr(i32 %7, i32 %40, %struct.termios* %4)
%42 = call i32 @read(i32 %7, i8* %5, i32 1)
%43 = icmp eq i32 %42, 1
br i1 %43, label %44, label %47
44: ; preds = %9
%45 = load i8, i8* %5
%46 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %45)
br label %47
47: ; preds = %44, %9
%48 = load i32, i32* @KDSKBMODE
%49 = load i32, i32* @K_XLATE
%50 = call i32 @ioctl(i32 %7, i32 %48, i32 %49)
%51 = load i32, i32* @TCSANOW
%52 = call i32 @tcsetattr(i32 %7, i32 %51, %struct.termios* %3)
%53 = call i32 @close(i32 %7)
br label %54
54: ; preds = %2, %47
%55 = phi i32 [ 0, %47 ], [ 1, %2 ]
ret i32 %55
}