unsigned int keyboard_get_scancode(void) {
  unsigned int scancode, k;
  volatile unsigned int flag __attribute__((unused));
  unsigned int result = 0;
  while (1) {
    flag = *(volatile unsigned int *)((void *)((char *)(0x04000000 << 2) +
                                               (1 << 2)));
    scancode = *(volatile unsigned int *)((void *)((char *)(0x04000000 << 2) +
                                                   (0 << 2)));
    if (scancode & 0x100) {
      k = scancode & 0xff;
      result = (result << 8) | k;
      if (k == 0xe0 || k == 0xf0) {
        continue;
      }
      return result;
    } else if (!result) {
      return result;
    }
  }
}


x86 assembly (clang, -O0):
<keyboard_get_scancode>:
push   %rbp
mov    %rsp,%rbp
movl   $0x0,-0x14(%rbp)
mov    $0x10000000,%eax
mov    0x4(%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x10000000,%eax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
and    $0x100,%eax
cmp    $0x0,%eax
je     1172 <keyboard_get_scancode+0x72>
mov    -0x8(%rbp),%eax
and    $0xff,%eax
mov    %eax,-0xc(%rbp)
mov    -0x14(%rbp),%eax
shl    $0x8,%eax
or     -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0xe0,-0xc(%rbp)
je     1162 <keyboard_get_scancode+0x62>
cmpl   $0xf0,-0xc(%rbp)
jne    1167 <keyboard_get_scancode+0x67>
jmp    110b <keyboard_get_scancode+0xb>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1191 <keyboard_get_scancode+0x91>
cmpl   $0x0,-0x14(%rbp)
jne    1187 <keyboard_get_scancode+0x87>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1191 <keyboard_get_scancode+0x91>
jmp    118c <keyboard_get_scancode+0x8c>
jmp    110b <keyboard_get_scancode+0xb>
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<keyboard_get_scancode>:
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x10000004,%ecx
mov    %ecx,-0x4(%rsp)
mov    0x10000000,%ecx
test   $0x100,%ecx
jne    1130 <keyboard_get_scancode+0x30>
test   %eax,%eax
jne    1110 <keyboard_get_scancode+0x10>
jmp    1148 <keyboard_get_scancode+0x48>
movzbl %cl,%edx
shl    $0x8,%eax
or     %edx,%eax
and    $0xef,%ecx
cmp    $0xe0,%ecx
je     1110 <keyboard_get_scancode+0x10>
jmp    114a <keyboard_get_scancode+0x4a>
xor    %eax,%eax
ret

LLVM IR (-O0):
define i32 @keyboard_get_scancode() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %5
br label %6
6: ; preds = %0, %24, %33
%7 = load volatile i32, i32* bitcast (i8* getelementptr inbounds (i8, i8* inttoptr (i64 268435456 to i8*), i64 4) to i32*)
store volatile i32 %7, i32* %4
%8 = load volatile i32, i32* inttoptr (i64 268435456 to i32*)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = and i32 %9, 256
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %27
12: ; preds = %6
%13 = load i32, i32* %2
%14 = and i32 %13, 255
store i32 %14, i32* %3
%15 = load i32, i32* %5
%16 = shl i32 %15, 8
%17 = load i32, i32* %3
%18 = or i32 %16, %17
store i32 %18, i32* %5
%19 = load i32, i32* %3
%20 = icmp eq i32 %19, 224
br i1 %20, label %24, label %21
21: ; preds = %12
%22 = load i32, i32* %3
%23 = icmp eq i32 %22, 240
br i1 %23, label %24, label %25
24: ; preds = %21, %12
br label %6
25: ; preds = %21
%26 = load i32, i32* %5
store i32 %26, i32* %1
br label %34
27: ; preds = %6
%28 = load i32, i32* %5
%29 = icmp ne i32 %28, 0
br i1 %29, label %32, label %30
30: ; preds = %27
%31 = load i32, i32* %5
store i32 %31, i32* %1
br label %34
32: ; preds = %27
br label %33
33: ; preds = %32
br label %6
34: ; preds = %30, %25
%35 = load i32, i32* %1
ret i32 %35
}

LLVM IR (-O2):
define i32 @keyboard_get_scancode() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
br label %3
3: ; preds = %11, %0
%4 = phi i32 [ %14, %11 ], [ 0, %0 ]
%5 = icmp eq i32 %4, 0
br label %6
6: ; preds = %3, %17
%7 = load volatile i32, i32* inttoptr (i64 268435460 to i32*)
store volatile i32 %7, i32* %1
%8 = load volatile i32, i32* inttoptr (i64 268435456 to i32*)
%9 = and i32 %8, 256
%10 = icmp eq i32 %9, 0
br i1 %10, label %17, label %11
11: ; preds = %6
%12 = and i32 %8, 255
%13 = shl i32 %4, 8
%14 = or i32 %12, %13
%15 = and i32 %8, 239
%16 = icmp eq i32 %15, 224
br i1 %16, label %3, label %18
17: ; preds = %6
br i1 %5, label %18, label %6
18: ; preds = %11, %17
%19 = phi i32 [ 0, %17 ], [ %14, %11 ]
ret i32 %19
}