void test_cdq(int x) {
  volatile int a, d, newa, newd;
  a = x;
  d = 0xaabbccdd;
  __asm__ __volatile__("mov %2, %%eax\n\t"
                       "mov %3, %%edx\n\t"
                       "cltd\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 (!(newd == (a < 0 ? -1 : 0)))
      asm volatile(".byte 0xd6" : : "a"(1));
  } while (0);
}


x86 assembly (clang, -O0):
<test_cdq>:
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
cltd
mov    %eax,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%eax
mov    -0x8(%rbp),%ecx
cmp    %ecx,%eax
je     1135 <test_cdq+0x35>
mov    $0x1,%eax
(bad)
jmp    113a <test_cdq+0x3a>
jmp    113f <test_cdq+0x3f>
mov    -0x14(%rbp),%eax
mov    -0x8(%rbp),%esi
xor    %ecx,%ecx
mov    $0xffffffff,%edx
cmp    $0x0,%esi
cmovl  %edx,%ecx
cmp    %ecx,%eax
je     1160 <test_cdq+0x60>
mov    $0x1,%eax
(bad)
jmp    1165 <test_cdq+0x65>
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_cdq>:
mov    %edi,-0x10(%rsp)
movl   $0xaabbccdd,-0x4(%rsp)
aa
mov    -0x10(%rsp),%eax
mov    -0x4(%rsp),%edx
cltd
mov    %eax,-0x8(%rsp)
mov    %edx,-0xc(%rsp)
mov    -0x8(%rsp),%eax
cmp    -0x10(%rsp),%eax
je     112d <test_cdq+0x2d>
mov    $0x1,%eax
(bad)
mov    -0xc(%rsp),%eax
mov    -0x10(%rsp),%ecx
sar    $0x1f,%ecx
cmp    %ecx,%eax
je     1142 <test_cdq+0x42>
mov    $0x1,%eax
(bad)
ret

LLVM IR (-O0):
define void @test_cdq(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 = load volatile i32, i32* %3
%18 = icmp slt i32 %17, 0
%19 = zext i1 %18 to i64
%20 = select i1 %18, i32 -1, i32 0
%21 = icmp eq i32 %16, %20
br i1 %21, label %23, label %22
22: ; preds = %15
br label %23
23: ; preds = %22, %15
br label %24
24: ; preds = %23
ret void
}

LLVM IR (-O2):
define void @test_cdq(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 = load volatile i32, i32* %2
%17 = ashr i32 %16, 31
%18 = icmp eq i32 %15, %17
br i1 %18, label %20, label %19
19: ; preds = %14
br label %20
20: ; preds = %19, %14
ret void
}