unsigned short read_chan(unsigned char chan) {
  unsigned int res = 0x800;
  (*(volatile unsigned int *)(((((unsigned int)0x40000000) + 0x10000) +
                               0x2000) +
                              0x34)) = (chan << (0 * 5));
  while (res == 0x800) {
    if (chan == 0x10 || chan == 0x11)
      (*(volatile unsigned int *)((((((unsigned int)0x40000000) + 0x10000) +
                                    0x2000) +
                                   0x300) +
                                  0x4)) |= (1 << 23);
    else if (chan == 0x12)
      (*(volatile unsigned int *)((((((unsigned int)0x40000000) + 0x10000) +
                                    0x2000) +
                                   0x300) +
                                  0x4)) |= (1 << 22);
    (*(volatile unsigned int *)(((((unsigned int)0x40000000) + 0x10000) +
                                 0x2000) +
                                0x08)) |= (1 << 0);
    (*(volatile unsigned int *)(((((unsigned int)0x40000000) + 0x10000) +
                                 0x2000) +
                                0x08)) |= (1 << 30);
    while (
        ((*(volatile unsigned int *)(((((unsigned int)0x40000000) + 0x10000) +
                                      0x2000) +
                                     0x00)) &
         (1 << 1)) == 0)
      ;
    res = (*(volatile unsigned int *)(((((unsigned int)0x40000000) + 0x10000) +
                                       0x2000) +
                                      0x4c));
    (*(volatile unsigned int *)(((((unsigned int)0x40000000) + 0x10000) +
                                 0x2000) +
                                0x08)) &= ~(1 << 30);
    (*(volatile unsigned int *)(((((unsigned int)0x40000000) + 0x10000) +
                                 0x2000) +
                                0x08)) &= ~(1 << 0);
    (*(volatile unsigned int *)((((((unsigned int)0x40000000) + 0x10000) +
                                  0x2000) +
                                 0x300) +
                                0x4)) &= ~(1 << 23);
  }
  return res;
}


x86 assembly (clang, -O0):
<read_chan>:
push   %rbp
mov    %rsp,%rbp
mov    %dil,%al
mov    %al,-0x1(%rbp)
movl   $0x800,-0x8(%rbp)
movzbl -0x1(%rbp),%ecx
shl    $0x0,%ecx
mov    $0x40012034,%eax
mov    %ecx,(%rax)
cmpl   $0x800,-0x8(%rbp)
jne    120a <read_chan+0x10a>
movzbl -0x1(%rbp),%eax
cmp    $0x10,%eax
je     1146 <read_chan+0x46>
movzbl -0x1(%rbp),%eax
cmp    $0x11,%eax
jne    115f <read_chan+0x5f>
mov    $0x40012304,%eax
mov    (%rax),%ecx
or     $0x800000,%ecx
mov    $0x40012304,%eax
mov    %ecx,(%rax)
jmp    1185 <read_chan+0x85>
movzbl -0x1(%rbp),%eax
cmp    $0x12,%eax
jne    1180 <read_chan+0x80>
mov    $0x40012304,%eax
mov    (%rax),%ecx
or     $0x400000,%ecx
mov    $0x40012304,%eax
mov    %ecx,(%rax)
jmp    1185 <read_chan+0x85>
mov    $0x40012008,%eax
mov    (%rax),%ecx
or     $0x1,%ecx
mov    $0x40012008,%eax
mov    %ecx,(%rax)
mov    $0x40012008,%eax
mov    (%rax),%ecx
or     $0x40000000,%ecx
mov    $0x40012008,%eax
mov    %ecx,(%rax)
mov    $0x40012000,%eax
mov    (%rax),%eax
and    $0x2,%eax
cmp    $0x0,%eax
jne    11c2 <read_chan+0xc2>
jmp    11aa <read_chan+0xaa>
mov    $0x4001204c,%eax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    $0x40012008,%eax
mov    (%rax),%ecx
and    $0xbfffffff,%ecx
mov    $0x40012008,%eax
mov    %ecx,(%rax)
mov    $0x40012008,%eax
mov    (%rax),%ecx
and    $0xfffffffe,%ecx
mov    $0x40012008,%eax
mov    %ecx,(%rax)
mov    $0x40012304,%eax
mov    (%rax),%ecx
and    $0xff7fffff,%ecx
mov    $0x40012304,%eax
mov    %ecx,(%rax)
jmp    111f <read_chan+0x1f>
mov    -0x8(%rbp),%eax
movzwl %ax,%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_chan>:
mov    %edi,0x40012034
lea    -0x10(%rdi),%ecx
mov    %edi,%eax
and    $0xfe,%al
xor    %edx,%edx
cmp    $0x10,%al
sete   %dl
shl    $0x16,%edx
add    $0x400000,%edx
xchg   %ax,%ax
cmp    $0x2,%cl
ja     112c <read_chan+0x2c>
or     %edx,0x40012304
orl    $0x1,0x40012008
01
orl    $0x40000000,0x40012008
nop
testl  $0x2,0x40012000
je     1140 <read_chan+0x40>
mov    0x4001204c,%eax
andl   $0xbfffffff,0x40012008
ff ff ff bf
andl   $0xfffffffe,0x40012008
fe
andl   $0xff7fffff,0x40012304
ff ff 7f ff
cmp    $0x800,%eax
je     1120 <read_chan+0x20>
ret

LLVM IR (-O0):
define zeroext i16 @read_chan(i8 zeroext %0) {
%2 = alloca i8
%3 = alloca i32
store i8 %0, i8* %2
store i32 2048, i32* %3
%4 = load i8, i8* %2
%5 = zext i8 %4 to i32
%6 = shl i32 %5, 0
store volatile i32 %6, i32* inttoptr (i64 1073815604 to i32*)
br label %7
7: ; preds = %39, %1
%8 = load i32, i32* %3
%9 = icmp eq i32 %8, 2048
br i1 %9, label %10, label %47
10: ; preds = %7
%11 = load i8, i8* %2
%12 = zext i8 %11 to i32
%13 = icmp eq i32 %12, 16
br i1 %13, label %18, label %14
14: ; preds = %10
%15 = load i8, i8* %2
%16 = zext i8 %15 to i32
%17 = icmp eq i32 %16, 17
br i1 %17, label %18, label %21
18: ; preds = %14, %10
%19 = load volatile i32, i32* inttoptr (i64 1073816324 to i32*)
%20 = or i32 %19, 8388608
store volatile i32 %20, i32* inttoptr (i64 1073816324 to i32*)
br label %29
21: ; preds = %14
%22 = load i8, i8* %2
%23 = zext i8 %22 to i32
%24 = icmp eq i32 %23, 18
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = load volatile i32, i32* inttoptr (i64 1073816324 to i32*)
%27 = or i32 %26, 4194304
store volatile i32 %27, i32* inttoptr (i64 1073816324 to i32*)
br label %28
28: ; preds = %25, %21
br label %29
29: ; preds = %28, %18
%30 = load volatile i32, i32* inttoptr (i64 1073815560 to i32*)
%31 = or i32 %30, 1
store volatile i32 %31, i32* inttoptr (i64 1073815560 to i32*)
%32 = load volatile i32, i32* inttoptr (i64 1073815560 to i32*)
%33 = or i32 %32, 1073741824
store volatile i32 %33, i32* inttoptr (i64 1073815560 to i32*)
br label %34
34: ; preds = %38, %29
%35 = load volatile i32, i32* inttoptr (i64 1073815552 to i32*)
%36 = and i32 %35, 2
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %34
br label %34
39: ; preds = %34
%40 = load volatile i32, i32* inttoptr (i64 1073815628 to i32*)
store i32 %40, i32* %3
%41 = load volatile i32, i32* inttoptr (i64 1073815560 to i32*)
%42 = and i32 %41, -1073741825
store volatile i32 %42, i32* inttoptr (i64 1073815560 to i32*)
%43 = load volatile i32, i32* inttoptr (i64 1073815560 to i32*)
%44 = and i32 %43, -2
store volatile i32 %44, i32* inttoptr (i64 1073815560 to i32*)
%45 = load volatile i32, i32* inttoptr (i64 1073816324 to i32*)
%46 = and i32 %45, -8388609
store volatile i32 %46, i32* inttoptr (i64 1073816324 to i32*)
br label %7
47: ; preds = %7
%48 = load i32, i32* %3
%49 = trunc i32 %48 to i16
ret i16 %49
}

LLVM IR (-O2):
define zeroext i16 @read_chan(i8 zeroext %0) {
%2 = zext i8 %0 to i32
store volatile i32 %2, i32* inttoptr (i64 1073815604 to i32*)
%3 = and i8 %0, -2
%4 = icmp eq i8 %3, 16
%5 = add i8 %0, -16
%6 = icmp ult i8 %5, 3
%7 = select i1 %4, i32 8388608, i32 4194304
br label %8
8: ; preds = %1, %21
br i1 %6, label %9, label %12
9: ; preds = %8
%10 = load volatile i32, i32* inttoptr (i64 1073816324 to i32*)
%11 = or i32 %10, %7
store volatile i32 %11, i32* inttoptr (i64 1073816324 to i32*)
br label %12
12: ; preds = %8, %9
%13 = load volatile i32, i32* inttoptr (i64 1073815560 to i32*)
%14 = or i32 %13, 1
store volatile i32 %14, i32* inttoptr (i64 1073815560 to i32*)
%15 = load volatile i32, i32* inttoptr (i64 1073815560 to i32*)
%16 = or i32 %15, 1073741824
store volatile i32 %16, i32* inttoptr (i64 1073815560 to i32*)
br label %17
17: ; preds = %17, %12
%18 = load volatile i32, i32* inttoptr (i64 1073815552 to i32*)
%19 = and i32 %18, 2
%20 = icmp eq i32 %19, 0
br i1 %20, label %17, label %21
21: ; preds = %17
%22 = load volatile i32, i32* inttoptr (i64 1073815628 to i32*)
%23 = load volatile i32, i32* inttoptr (i64 1073815560 to i32*)
%24 = and i32 %23, -1073741825
store volatile i32 %24, i32* inttoptr (i64 1073815560 to i32*)
%25 = load volatile i32, i32* inttoptr (i64 1073815560 to i32*)
%26 = and i32 %25, -2
store volatile i32 %26, i32* inttoptr (i64 1073815560 to i32*)
%27 = load volatile i32, i32* inttoptr (i64 1073816324 to i32*)
%28 = and i32 %27, -8388609
store volatile i32 %28, i32* inttoptr (i64 1073816324 to i32*)
%29 = icmp eq i32 %22, 2048
br i1 %29, label %8, label %30
30: ; preds = %21
%31 = trunc i32 %22 to i16
ret i16 %31
}