#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*/ KDSKBMODE;
int /*<<< orphan*/ K_MEDIUMRAW;
scalar_t__ ioctl(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ term_set_raw_mode();
void medium_raw_read(void) {
  char c;
  int rc;
  if (ioctl(0, KDSKBMODE, K_MEDIUMRAW) != 0) {
    printf("Unable to set mediumraw mode.\r\n");
    return;
  }
  term_set_raw_mode();
  do {
    rc = read(0, &c, 1);
    if (!rc)
      break;
    if (c & 0x80)
      printf("released 0x%x", (unsigned char)(c & ~0x80));
    else
      printf("PRESSED 0x%x", (unsigned char)(c & ~0x80));
    printf("\r\n");
  } while (c != 0x10);
}


x86 assembly (clang, -O0):
<medium_raw_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e99(%rip),%rax
mov    (%rax),%esi
mov    0x2e88(%rip),%rax
mov    (%rax),%edx
xor    %edi,%edi
call   1050 <ioctl@plt>
cmp    $0x0,%rax
je     117e <medium_raw_read+0x3e>
lea    0xe8e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    120d <medium_raw_read+0xcd>
mov    $0x0,%al
call   1040 <term_set_raw_mode@plt>
xor    %edi,%edi
lea    -0x1(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    11a7 <medium_raw_read+0x67>
jmp    120d <medium_raw_read+0xcd>
movsbl -0x1(%rbp),%eax
and    $0x80,%eax
cmp    $0x0,%eax
je     11d8 <medium_raw_read+0x98>
movsbl -0x1(%rbp),%eax
and    $0xffffff7f,%eax
movzbl %al,%esi
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11f2 <medium_raw_read+0xb2>
movsbl -0x1(%rbp),%eax
and    $0xffffff7f,%eax
movzbl %al,%esi
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movsbl -0x1(%rbp),%eax
cmp    $0x10,%eax
jne    1185 <medium_raw_read+0x45>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<medium_raw_read>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
mov    0x2e74(%rip),%rax
mov    (%rax),%edx
xor    %edi,%edi
call   1060 <ioctl@plt>
test   %rax,%rax
je     1192 <medium_raw_read+0x42>
lea    0xe9a(%rip),%rdi
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <puts@plt>
xor    %eax,%eax
call   1050 <term_set_raw_mode@plt>
lea    0xf(%rsp),%r14
lea    0xe69(%rip),%r15
lea    0xe54(%rip),%r12
lea    0xe85(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %edi,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
test   %eax,%eax
je     11fc <medium_raw_read+0xac>
movzbl 0xf(%rsp),%ebp
mov    %ebp,%esi
and    $0x7f,%esi
test   %bpl,%bpl
mov    %r12,%rdi
cmovns %r15,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
cmp    $0x10,%bpl
jne    11c0 <medium_raw_read+0x70>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@KDSKBMODE = global i32 0
@K_MEDIUMRAW = global i32 0
@.str = [32 x i8] c"Unable to set mediumraw mode.\0D\0A\00"
@.str.1 = [14 x i8] c"released 0x%x\00"
@.str.2 = [13 x i8] c"PRESSED 0x%x\00"
@.str.3 = [3 x i8] c"\0D\0A\00"
define void @medium_raw_read() {
%1 = alloca i8
%2 = alloca i32
%3 = load i32, i32* @KDSKBMODE
%4 = load i32, i32* @K_MEDIUMRAW
%5 = call i64 @ioctl(i32 0, i32 %3, i32 %4)
%6 = icmp ne i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %41
9: ; preds = %0
%10 = call i32 (...) @term_set_raw_mode()
br label %11
11: ; preds = %37, %9
%12 = call i32 @read(i32 0, i8* %1, i32 1)
store i32 %12, i32* %2
%13 = load i32, i32* %2
%14 = icmp ne i32 %13, 0
br i1 %14, label %16, label %15
15: ; preds = %11
br label %41
16: ; preds = %11
%17 = load i8, i8* %1
%18 = sext i8 %17 to i32
%19 = and i32 %18, 128
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %28
21: ; preds = %16
%22 = load i8, i8* %1
%23 = sext i8 %22 to i32
%24 = and i32 %23, -129
%25 = trunc i32 %24 to i8
%26 = zext i8 %25 to i32
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %26)
br label %35
28: ; preds = %16
%29 = load i8, i8* %1
%30 = sext i8 %29 to i32
%31 = and i32 %30, -129
%32 = trunc i32 %31 to i8
%33 = zext i8 %32 to i32
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %33)
br label %35
35: ; preds = %28, %21
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
br label %37
37: ; preds = %35
%38 = load i8, i8* %1
%39 = sext i8 %38 to i32
%40 = icmp ne i32 %39, 16
br i1 %40, label %11, label %41
41: ; preds = %7, %37, %15
ret void
}

LLVM IR (-O2):
@KDSKBMODE = global i32 0
@K_MEDIUMRAW = global i32 0
@.str.1 = [14 x i8] c"released 0x%x\00"
@.str.2 = [13 x i8] c"PRESSED 0x%x\00"
@str = [2 x i8] c"\0D\00"
@str.4 = [31 x i8] c"Unable to set mediumraw mode.\0D\00"
define void @medium_raw_read() {
%1 = alloca i8
%2 = load i32, i32* @KDSKBMODE
%3 = load i32, i32* @K_MEDIUMRAW
%4 = tail call i64 @ioctl(i32 0, i32 %2, i32 %3)
%5 = icmp eq i64 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.4, i64 0, i64 0))
br label %22
8: ; preds = %0
%9 = tail call i32 (...) @term_set_raw_mode()
br label %10
10: ; preds = %13, %8
%11 = call i32 @read(i32 0, i8* %1, i32 1)
%12 = icmp eq i32 %11, 0
br i1 %12, label %22, label %13
13: ; preds = %10
%14 = load i8, i8* %1
%15 = icmp sgt i8 %14, -1
%16 = and i8 %14, 127
%17 = select i1 %15, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0)
%18 = zext i8 %16 to i32
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) %17, i32 %18)
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str, i64 0, i64 0))
%21 = icmp eq i8 %14, 16
br i1 %21, label %22, label %10
22: ; preds = %13, %10, %6
ret void
}