long extract_bdm(unsigned long insn, int dialect, int *invalid) {
  if ((dialect & 0x4000) == 0) {
    if (((insn & (1 << 21)) == 0) != ((insn & (1 << 15)) == 0))
      *invalid = 1;
  } else {
    if ((insn & (0x17 << 21)) != (0x06 << 21) &&
        (insn & (0x1d << 21)) != (0x18 << 21))
      *invalid = 1;
  }
  return ((insn & 0xfffc) ^ 0x8000) - 0x8000;
}


x86 assembly (clang, -O0):
<extract_bdm>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0xc(%rbp),%eax
and    $0x4000,%eax
cmp    $0x0,%eax
jne    1165 <extract_bdm+0x65>
mov    -0x8(%rbp),%rax
and    $0x200000,%rax
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    -0x8(%rbp),%rcx
and    $0x8000,%rcx
cmp    $0x0,%rcx
sete   %cl
and    $0x1,%cl
movzbl %cl,%ecx
cmp    %ecx,%eax
je     1160 <extract_bdm+0x60>
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
jmp    11a0 <extract_bdm+0xa0>
mov    -0x8(%rbp),%rax
and    $0x2e00000,%rax
cmp    $0xc00000,%rax
je     119b <extract_bdm+0x9b>
mov    -0x8(%rbp),%rax
and    $0x3a00000,%rax
cmp    $0x3000000,%rax
je     119b <extract_bdm+0x9b>
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
jmp    11a0 <extract_bdm+0xa0>
mov    -0x8(%rbp),%rax
and    $0xfffc,%rax
xor    $0x8000,%rax
sub    $0x8000,%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<extract_bdm>:
mov    %edi,%eax
test   $0x4000,%esi
jne    111b <extract_bdm+0x1b>
shr    $0x15,%eax
mov    %edi,%ecx
shr    $0xf,%ecx
xor    %eax,%ecx
test   $0x1,%cl
jne    1135 <extract_bdm+0x35>
jmp    113b <extract_bdm+0x3b>
and    $0x2e00000,%eax
cmp    $0xc00000,%eax
je     113b <extract_bdm+0x3b>
mov    %edi,%eax
and    $0x3a00000,%eax
cmp    $0x3000000,%eax
je     113b <extract_bdm+0x3b>
movl   $0x1,(%rdx)
movswq %di,%rax
and    $0xfffffffffffffffc,%rax
ret

LLVM IR (-O0):
define i64 @extract_bdm(i64 %0, i32 %1, i32* %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32*
store i64 %0, i64* %4
store i32 %1, i32* %5
store i32* %2, i32** %6
%7 = load i32, i32* %5
%8 = and i32 %7, 16384
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %23
10: ; preds = %3
%11 = load i64, i64* %4
%12 = and i64 %11, 2097152
%13 = icmp eq i64 %12, 0
%14 = zext i1 %13 to i32
%15 = load i64, i64* %4
%16 = and i64 %15, 32768
%17 = icmp eq i64 %16, 0
%18 = zext i1 %17 to i32
%19 = icmp ne i32 %14, %18
br i1 %19, label %20, label %22
20: ; preds = %10
%21 = load i32*, i32** %6
store i32 1, i32* %21
br label %22
22: ; preds = %20, %10
br label %34
23: ; preds = %3
%24 = load i64, i64* %4
%25 = and i64 %24, 48234496
%26 = icmp ne i64 %25, 12582912
br i1 %26, label %27, label %33
27: ; preds = %23
%28 = load i64, i64* %4
%29 = and i64 %28, 60817408
%30 = icmp ne i64 %29, 50331648
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = load i32*, i32** %6
store i32 1, i32* %32
br label %33
33: ; preds = %31, %27, %23
br label %34
34: ; preds = %33, %22
%35 = load i64, i64* %4
%36 = and i64 %35, 65532
%37 = xor i64 %36, 32768
%38 = sub i64 %37, 32768
ret i64 %38
}

LLVM IR (-O2):
define i64 @extract_bdm(i64 %0, i32 %1, i32* nocapture writeonly %2) {
%4 = and i32 %1, 16384
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %13
6: ; preds = %3
%7 = trunc i64 %0 to i32
%8 = lshr i32 %7, 21
%9 = lshr i32 %7, 15
%10 = xor i32 %8, %9
%11 = and i32 %10, 1
%12 = icmp eq i32 %11, 0
br i1 %12, label %20, label %19
13: ; preds = %3
%14 = and i64 %0, 48234496
%15 = icmp eq i64 %14, 12582912
%16 = and i64 %0, 60817408
%17 = icmp eq i64 %16, 50331648
%18 = or i1 %15, %17
br i1 %18, label %20, label %19
19: ; preds = %13, %6
store i32 1, i32* %2
br label %20
20: ; preds = %19, %13, %6
%21 = shl i64 %0, 48
%22 = ashr exact i64 %21, 48
%23 = and i64 %22, -4
ret i64 %23
}