void test_fbcd(double a) {
  unsigned short bcd[5];
  double b;
  asm("fbstp %0" : "=m"(bcd[0]) : "t"(a) : "st");
  asm("fbld %1" : "=t"(b) : "m"(bcd[0]));
  xxprintf("a=%f bcd=%04x%04x%04x%04x%04x b=%f\n", a, bcd[4], bcd[3], bcd[2],
           bcd[1], bcd[0], b);
}


x86 assembly (clang, -O0):
<test_fbcd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x8(%rbp)
lea    -0x12(%rbp),%rax
movsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
fldl   -0x30(%rbp)
fbstp  (%rax)
lea    -0x12(%rbp),%rax
fbld   (%rax)
fstpl  -0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x8(%rbp),%xmm0
movzwl -0xa(%rbp),%esi
movzwl -0xc(%rbp),%edx
movzwl -0xe(%rbp),%ecx
movzwl -0x10(%rbp),%r8d
movzwl -0x12(%rbp),%r9d
movsd  -0x20(%rbp),%xmm1
lea    0xe96(%rip),%rdi
mov    $0x2,%al
call   1030 <xxprintf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_fbcd>:
sub    $0x28,%rsp
movsd  %xmm0,0x20(%rsp)
fldl   0x20(%rsp)
fbstp  0xe(%rsp)
fbld   0xe(%rsp)
fstpl  0x18(%rsp)
movsd  0x18(%rsp),%xmm1
movzwl 0x16(%rsp),%esi
movzwl 0x14(%rsp),%edx
movzwl 0x12(%rsp),%ecx
movzwl 0x10(%rsp),%r8d
movzwl 0xe(%rsp),%r9d
lea    0xeae(%rip),%rdi
mov    $0x2,%al
call   1030 <xxprintf@plt>
add    $0x28,%rsp
ret

LLVM IR (-O0):
@.str = [36 x i8] c"a=%f bcd=%04x%04x%04x%04x%04x b=%f\0A\00"
define void @test_fbcd(double %0) {
%2 = alloca double
%3 = alloca [5 x i16]
%4 = alloca double
store double %0, double* %2
%5 = getelementptr inbounds [5 x i16], [5 x i16]* %3, i64 0, i64 0
%6 = load double, double* %2
%7 = getelementptr inbounds [5 x i16], [5 x i16]* %3, i64 0, i64 0
%8 = call double asm "fbld $1", "={st},*m,~{dirflag},~{fpsr},~{flags}"(i16* elementtype(i16) %7)
store double %8, double* %4
%9 = load double, double* %2
%10 = getelementptr inbounds [5 x i16], [5 x i16]* %3, i64 0, i64 4
%11 = load i16, i16* %10
%12 = zext i16 %11 to i32
%13 = getelementptr inbounds [5 x i16], [5 x i16]* %3, i64 0, i64 3
%14 = load i16, i16* %13
%15 = zext i16 %14 to i32
%16 = getelementptr inbounds [5 x i16], [5 x i16]* %3, i64 0, i64 2
%17 = load i16, i16* %16
%18 = zext i16 %17 to i32
%19 = getelementptr inbounds [5 x i16], [5 x i16]* %3, i64 0, i64 1
%20 = load i16, i16* %19
%21 = zext i16 %20 to i32
%22 = getelementptr inbounds [5 x i16], [5 x i16]* %3, i64 0, i64 0
%23 = load i16, i16* %22
%24 = zext i16 %23 to i32
%25 = load double, double* %4
%26 = call i32 (i8*, double, i32, i32, i32, i32, i32, double, ...) bitcast (i32 (...)* @xxprintf to i32 (i8*, double, i32, i32, i32, i32, i32, double, ...)*)(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), double %9, i32 %12, i32 %15, i32 %18, i32 %21, i32 %24, double %25)
ret void
}

LLVM IR (-O2):
@.str = [36 x i8] c"a=%f bcd=%04x%04x%04x%04x%04x b=%f\0A\00"
define void @test_fbcd(double %0) {
%2 = alloca [5 x i16]
%3 = bitcast [5 x i16]* %2 to i8*
%4 = getelementptr inbounds [5 x i16], [5 x i16]* %2, i64 0, i64 0
%5 = call double asm "fbld $1", "={st},*m,~{dirflag},~{fpsr},~{flags}"(i16* elementtype(i16) %4)
%6 = getelementptr inbounds [5 x i16], [5 x i16]* %2, i64 0, i64 4
%7 = load i16, i16* %6
%8 = zext i16 %7 to i32
%9 = getelementptr inbounds [5 x i16], [5 x i16]* %2, i64 0, i64 3
%10 = load i16, i16* %9
%11 = zext i16 %10 to i32
%12 = getelementptr inbounds [5 x i16], [5 x i16]* %2, i64 0, i64 2
%13 = load i16, i16* %12
%14 = zext i16 %13 to i32
%15 = getelementptr inbounds [5 x i16], [5 x i16]* %2, i64 0, i64 1
%16 = load i16, i16* %15
%17 = zext i16 %16 to i32
%18 = load i16, i16* %4
%19 = zext i16 %18 to i32
%20 = call i32 (i8*, double, i32, i32, i32, i32, i32, double, ...) bitcast (i32 (...)* @xxprintf to i32 (i8*, double, i32, i32, i32, i32, i32, double, ...)*)(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), double %0, i32 %8, i32 %11, i32 %14, i32 %17, i32 %19, double %5)
ret void
}