int HwMdReadMouse(int port) {
  unsigned int mouse1, mouse2;
  while ((*(volatile unsigned short *)0x20004020))
    ;
  (*(volatile unsigned short *)0x20004020) = 0x0300 | port;
  while ((*(volatile unsigned short *)0x20004020) == (0x0300 | port))
    ;
  mouse1 = (*(volatile unsigned short *)0x20004020);
  mouse2 = (*(volatile unsigned short *)0x20004022);
  (*(volatile unsigned short *)0x20004020) = 0;
  return (int)((mouse1 << 16) | mouse2);
}


x86 assembly (clang, -O0):
<HwMdReadMouse>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    $0x20004020,%eax
mov    (%rax),%ax
cmp    $0x0,%ax
je     111e <HwMdReadMouse+0x1e>
jmp    1107 <HwMdReadMouse+0x7>
mov    -0x4(%rbp),%eax
or     $0x300,%eax
mov    %ax,%cx
mov    $0x20004020,%eax
mov    %cx,(%rax)
mov    $0x20004020,%eax
mov    (%rax),%ax
movzwl %ax,%eax
mov    -0x4(%rbp),%ecx
or     $0x300,%ecx
cmp    %ecx,%eax
jne    1152 <HwMdReadMouse+0x52>
jmp    1131 <HwMdReadMouse+0x31>
mov    $0x20004020,%eax
mov    (%rax),%ax
movzwl %ax,%eax
mov    %eax,-0x8(%rbp)
mov    $0x20004022,%eax
mov    (%rax),%ax
movzwl %ax,%eax
mov    %eax,-0xc(%rbp)
mov    $0x20004020,%eax
movw   $0x0,(%rax)
mov    -0x8(%rbp),%eax
shl    $0x10,%eax
or     -0xc(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<HwMdReadMouse>:
cmpw   $0x0,0x20004020
jne    1100 <HwMdReadMouse>
or     $0x300,%edi
mov    %di,0x20004020
20
nopl   0x0(%rax)
movzwl 0x20004020,%eax
20
cmp    %eax,%edi
je     1120 <HwMdReadMouse+0x20>
movzwl 0x20004020,%eax
20
movzwl 0x20004022,%ecx
20
movw   $0x0,0x20004020
shl    $0x10,%eax
or     %ecx,%eax
ret

LLVM IR (-O0):
define i32 @HwMdReadMouse(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
br label %5
5: ; preds = %8, %1
%6 = load volatile i16, i16* inttoptr (i64 536887328 to i16*)
%7 = icmp ne i16 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %5
br label %5
9: ; preds = %5
%10 = load i32, i32* %2
%11 = or i32 768, %10
%12 = trunc i32 %11 to i16
store volatile i16 %12, i16* inttoptr (i64 536887328 to i16*)
br label %13
13: ; preds = %19, %9
%14 = load volatile i16, i16* inttoptr (i64 536887328 to i16*)
%15 = zext i16 %14 to i32
%16 = load i32, i32* %2
%17 = or i32 768, %16
%18 = icmp eq i32 %15, %17
br i1 %18, label %19, label %20
19: ; preds = %13
br label %13
20: ; preds = %13
%21 = load volatile i16, i16* inttoptr (i64 536887328 to i16*)
%22 = zext i16 %21 to i32
store i32 %22, i32* %3
%23 = load volatile i16, i16* inttoptr (i64 536887330 to i16*)
%24 = zext i16 %23 to i32
store i32 %24, i32* %4
store volatile i16 0, i16* inttoptr (i64 536887328 to i16*)
%25 = load i32, i32* %3
%26 = shl i32 %25, 16
%27 = load i32, i32* %4
%28 = or i32 %26, %27
ret i32 %28
}

LLVM IR (-O2):
define i32 @HwMdReadMouse(i32 %0) {
br label %2
2: ; preds = %2, %1
%3 = load volatile i16, i16* inttoptr (i64 536887328 to i16*)
%4 = icmp eq i16 %3, 0
br i1 %4, label %5, label %2
5: ; preds = %2
%6 = or i32 %0, 768
%7 = trunc i32 %6 to i16
store volatile i16 %7, i16* inttoptr (i64 536887328 to i16*)
br label %8
8: ; preds = %8, %5
%9 = load volatile i16, i16* inttoptr (i64 536887328 to i16*)
%10 = zext i16 %9 to i32
%11 = icmp eq i32 %6, %10
br i1 %11, label %8, label %12
12: ; preds = %8
%13 = load volatile i16, i16* inttoptr (i64 536887328 to i16*)
%14 = zext i16 %13 to i32
%15 = load volatile i16, i16* inttoptr (i64 536887330 to i16*)
%16 = zext i16 %15 to i32
store volatile i16 0, i16* inttoptr (i64 536887328 to i16*)
%17 = shl nuw i32 %14, 16
%18 = or i32 %17, %16
ret i32 %18
}