unsigned int keymap_modkey(unsigned int scancode, unsigned int modifier) {
  switch (scancode) {
  case 0x1D:
    return modifier | 4;
  case 0x9D:
    return modifier & ~4;
  case 0x2A:
  case 0x36:
    return modifier | 1;
  case 0xAA:
  case 0xB6:
    return modifier & ~1;
  case 0x38:
    return modifier | 2;
  case 0xB8:
    return modifier & ~2;
  }
  return modifier;
}


x86 assembly (clang, -O0):
<keymap_modkey>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
sub    $0x1d,%eax
je     119d <keymap_modkey+0x9d>
jmp    111e <keymap_modkey+0x1e>
mov    -0x10(%rbp),%eax
sub    $0x2a,%eax
je     11b9 <keymap_modkey+0xb9>
jmp    112f <keymap_modkey+0x2f>
mov    -0x10(%rbp),%eax
sub    $0x36,%eax
je     11b9 <keymap_modkey+0xb9>
jmp    1140 <keymap_modkey+0x40>
mov    -0x10(%rbp),%eax
sub    $0x38,%eax
je     11d5 <keymap_modkey+0xd5>
jmp    1151 <keymap_modkey+0x51>
mov    -0x10(%rbp),%eax
sub    $0x9d,%eax
je     11ab <keymap_modkey+0xab>
jmp    1164 <keymap_modkey+0x64>
mov    -0x10(%rbp),%eax
sub    $0xaa,%eax
je     11c7 <keymap_modkey+0xc7>
jmp    1177 <keymap_modkey+0x77>
mov    -0x10(%rbp),%eax
sub    $0xb6,%eax
je     11c7 <keymap_modkey+0xc7>
jmp    118a <keymap_modkey+0x8a>
mov    -0x10(%rbp),%eax
sub    $0xb8,%eax
je     11e3 <keymap_modkey+0xe3>
jmp    11f1 <keymap_modkey+0xf1>
mov    -0xc(%rbp),%eax
or     $0x4,%eax
mov    %eax,-0x4(%rbp)
jmp    11f7 <keymap_modkey+0xf7>
mov    -0xc(%rbp),%eax
and    $0xfffffffb,%eax
mov    %eax,-0x4(%rbp)
jmp    11f7 <keymap_modkey+0xf7>
mov    -0xc(%rbp),%eax
or     $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    11f7 <keymap_modkey+0xf7>
mov    -0xc(%rbp),%eax
and    $0xfffffffe,%eax
mov    %eax,-0x4(%rbp)
jmp    11f7 <keymap_modkey+0xf7>
mov    -0xc(%rbp),%eax
or     $0x2,%eax
mov    %eax,-0x4(%rbp)
jmp    11f7 <keymap_modkey+0xf7>
mov    -0xc(%rbp),%eax
and    $0xfffffffd,%eax
mov    %eax,-0x4(%rbp)
jmp    11f7 <keymap_modkey+0xf7>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<keymap_modkey>:
mov    %esi,%eax
lea    -0x1d(%rdi),%ecx
cmp    $0x1b,%ecx
jbe    1129 <keymap_modkey+0x29>
add    $0xffffff63,%edi
cmp    $0x1b,%edi
ja     1140 <keymap_modkey+0x40>
lea    0xf54(%rip),%rcx
movslq (%rcx,%rdi,4),%rdx
add    %rcx,%rdx
jmp    *%rdx
and    $0xfffffffe,%eax
ret
lea    0xed0(%rip),%rdx
movslq (%rdx,%rcx,4),%rcx
add    %rdx,%rcx
jmp    *%rcx
or     $0x1,%eax
ret
and    $0xfffffffd,%eax
ret
or     $0x4,%eax
ret
or     $0x2,%eax
ret
and    $0xfffffffb,%eax
ret

LLVM IR (-O0):
define i32 @keymap_modkey(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%6 = load i32, i32* %4
switch i32 %6, label %25 [
i32 29, label %7
i32 157, label %10
i32 42, label %13
i32 54, label %13
i32 170, label %16
i32 182, label %16
i32 56, label %19
i32 184, label %22
]
7: ; preds = %2
%8 = load i32, i32* %5
%9 = or i32 %8, 4
store i32 %9, i32* %3
br label %27
10: ; preds = %2
%11 = load i32, i32* %5
%12 = and i32 %11, -5
store i32 %12, i32* %3
br label %27
13: ; preds = %2, %2
%14 = load i32, i32* %5
%15 = or i32 %14, 1
store i32 %15, i32* %3
br label %27
16: ; preds = %2, %2
%17 = load i32, i32* %5
%18 = and i32 %17, -2
store i32 %18, i32* %3
br label %27
19: ; preds = %2
%20 = load i32, i32* %5
%21 = or i32 %20, 2
store i32 %21, i32* %3
br label %27
22: ; preds = %2
%23 = load i32, i32* %5
%24 = and i32 %23, -3
store i32 %24, i32* %3
br label %27
25: ; preds = %2
%26 = load i32, i32* %5
store i32 %26, i32* %3
br label %27
27: ; preds = %25, %22, %19, %16, %13, %10, %7
%28 = load i32, i32* %3
ret i32 %28
}

LLVM IR (-O2):
define i32 @keymap_modkey(i32 %0, i32 %1) {
switch i32 %0, label %15 [
i32 29, label %3
i32 157, label %5
i32 42, label %7
i32 54, label %7
i32 170, label %9
i32 182, label %9
i32 56, label %11
i32 184, label %13
]
3: ; preds = %2
%4 = or i32 %1, 4
br label %15
5: ; preds = %2
%6 = and i32 %1, -5
br label %15
7: ; preds = %2, %2
%8 = or i32 %1, 1
br label %15
9: ; preds = %2, %2
%10 = and i32 %1, -2
br label %15
11: ; preds = %2
%12 = or i32 %1, 2
br label %15
13: ; preds = %2
%14 = and i32 %1, -3
br label %15
15: ; preds = %2, %13, %11, %9, %7, %5, %3
%16 = phi i32 [ %14, %13 ], [ %12, %11 ], [ %10, %9 ], [ %8, %7 ], [ %6, %5 ], [ %4, %3 ], [ %1, %2 ]
ret i32 %16
}