void test_cwd(int x) {
  volatile int a, d, newa, newd;
  a = x;
  d = 0xaabbccdd;
  __asm__ __volatile__("mov %2, %%eax\n\t"
                       "mov %3, %%edx\n\t"
                       "cwtd\n\t"
                       "mov %%eax, %0\n\t"
                       "mov %%edx, %1\n\t"
                       : "=m"(newa), "=m"(newd)
                       : "m"(a), "m"(d)
                       : "eax", "edx");
  do {
    if (!(newa == a))
      asm volatile(".byte 0xd6" : : "a"(1));
  } while (0);
  do {
    if (!(((short)newd) == (((short)a) < 0 ? -1 : 0)))
      asm volatile(".byte 0xd6" : : "a"(1));
  } while (0);
  do {
    if (!((((unsigned)newd) >> 16) == 0xaabb))
      asm volatile(".byte 0xd6" : : "a"(1));
  } while (0);
}


x86 assembly (clang, -O0):
<test_cwd>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x8(%rbp)
movl   $0xaabbccdd,-0xc(%rbp)
mov    -0x8(%rbp),%eax
mov    -0xc(%rbp),%edx
cwtd
mov    %eax,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%eax
mov    -0x8(%rbp),%ecx
cmp    %ecx,%eax
je     1136 <test_cwd+0x36>
mov    $0x1,%eax
(bad)
jmp    113b <test_cwd+0x3b>
jmp    1140 <test_cwd+0x40>
mov    -0x14(%rbp),%eax
cwtl
mov    -0x8(%rbp),%ecx
movswl %cx,%esi
xor    %ecx,%ecx
mov    $0xffffffff,%edx
cmp    $0x0,%esi
cmovl  %edx,%ecx
cmp    %ecx,%eax
je     1165 <test_cwd+0x65>
mov    $0x1,%eax
(bad)
jmp    116a <test_cwd+0x6a>
jmp    116f <test_cwd+0x6f>
mov    -0x14(%rbp),%eax
shr    $0x10,%eax
cmp    $0xaabb,%eax
je     1186 <test_cwd+0x86>
mov    $0x1,%eax
(bad)
jmp    118b <test_cwd+0x8b>
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_cwd>:
mov    %edi,-0x10(%rsp)
movl   $0xaabbccdd,-0x4(%rsp)
aa
mov    -0x10(%rsp),%eax
mov    -0x4(%rsp),%edx
cwtd
mov    %eax,-0x8(%rsp)
mov    %edx,-0xc(%rsp)
mov    -0x8(%rsp),%eax
cmp    -0x10(%rsp),%eax
je     112e <test_cwd+0x2e>
mov    $0x1,%eax
(bad)
mov    -0xc(%rsp),%eax
cwtl
mov    -0x10(%rsp),%ecx
movswl %cx,%ecx
sar    $0xf,%ecx
cmp    %ecx,%eax
je     1147 <test_cwd+0x47>
mov    $0x1,%eax
(bad)
mov    $0xffff0000,%eax
and    -0xc(%rsp),%eax
cmp    $0xaabb0000,%eax
je     115d <test_cwd+0x5d>
mov    $0x1,%eax
(bad)
ret

LLVM IR (-O0):
define void @test_cwd(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %2
%7 = load i32, i32* %2
store volatile i32 %7, i32* %3
store volatile i32 -1430532899, i32* %4
br label %8
8: ; preds = %1
%9 = load volatile i32, i32* %5
%10 = load volatile i32, i32* %3
%11 = icmp eq i32 %9, %10
br i1 %11, label %13, label %12
12: ; preds = %8
br label %13
13: ; preds = %12, %8
br label %14
14: ; preds = %13
br label %15
15: ; preds = %14
%16 = load volatile i32, i32* %6
%17 = trunc i32 %16 to i16
%18 = sext i16 %17 to i32
%19 = load volatile i32, i32* %3
%20 = trunc i32 %19 to i16
%21 = sext i16 %20 to i32
%22 = icmp slt i32 %21, 0
%23 = zext i1 %22 to i64
%24 = select i1 %22, i32 -1, i32 0
%25 = icmp eq i32 %18, %24
br i1 %25, label %27, label %26
26: ; preds = %15
br label %27
27: ; preds = %26, %15
br label %28
28: ; preds = %27
br label %29
29: ; preds = %28
%30 = load volatile i32, i32* %6
%31 = lshr i32 %30, 16
%32 = icmp eq i32 %31, 43707
br i1 %32, label %34, label %33
33: ; preds = %29
br label %34
34: ; preds = %33, %29
br label %35
35: ; preds = %34
ret void
}

LLVM IR (-O2):
define void @test_cwd(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i32* %2 to i8*
%7 = bitcast i32* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = bitcast i32* %5 to i8*
store volatile i32 %0, i32* %2
store volatile i32 -1430532899, i32* %3
%10 = load volatile i32, i32* %4
%11 = load volatile i32, i32* %2
%12 = icmp eq i32 %10, %11
br i1 %12, label %14, label %13
13: ; preds = %1
br label %14
14: ; preds = %1, %13
%15 = load volatile i32, i32* %5
%16 = shl i32 %15, 16
%17 = ashr exact i32 %16, 16
%18 = load volatile i32, i32* %2
%19 = shl i32 %18, 16
%20 = ashr i32 %19, 31
%21 = icmp eq i32 %17, %20
br i1 %21, label %23, label %22
22: ; preds = %14
br label %23
23: ; preds = %14, %22
%24 = load volatile i32, i32* %5
%25 = and i32 %24, -65536
%26 = icmp eq i32 %25, -1430585344
br i1 %26, label %28, label %27
27: ; preds = %23
br label %28
28: ; preds = %27, %23
ret void
}