void bmi2_test() {
  unsigned i;
  unsigned long long a = 0xce7ace0ce7ace0;
  unsigned long long b = 0xface;
  unsigned long long res_l, res_h;
  unsigned __int128 res, res_ref;
  for (i = 0; i < 5; ++i) {
    a = a * (i + 1);
    b = b / (i + 1);
    res_ref = calc_mul_u64(a, b);
    res_l = calc_mulx_u64(a, b, &res_h);
    res = ((unsigned __int128)res_h << 64) | res_l;
    if (res != res_ref)
      abort();
  }
}


x86 assembly (clang, -O0):
<bmi2_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movabs $0xce7ace0ce7ace0,%rax
mov    %rax,-0x10(%rbp)
movq   $0xface,-0x18(%rbp)
movl   $0x0,-0x4(%rbp)
cmpl   $0x5,-0x4(%rbp)
jae    1213 <bmi2_test+0xe3>
mov    -0x10(%rbp),%rax
mov    -0x4(%rbp),%ecx
add    $0x1,%ecx
imul   %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x4(%rbp),%ecx
add    $0x1,%ecx
xor    %edx,%edx
mov    %edx,-0x58(%rbp)
div    %rcx
mov    %rax,%rcx
mov    -0x58(%rbp),%eax
mov    %rcx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    %al,-0x51(%rbp)
call   1040 <calc_mul_u64@plt>
mov    %eax,%ecx
mov    -0x51(%rbp),%al
movslq %ecx,%rdx
mov    %rdx,%rcx
sar    $0x3f,%rcx
mov    %rdx,-0x50(%rbp)
mov    %rcx,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1050 <calc_mulx_u64@plt>
cltq
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,-0x38(%rbp)
mov    %rax,-0x40(%rbp)
movaps -0x40(%rbp),%xmm0
movaps -0x50(%rbp),%xmm1
pcmpeqb %xmm1,%xmm0
pmovmskb %xmm0,%eax
sub    $0xffff,%eax
je     1200 <bmi2_test+0xd0>
jmp    11fb <bmi2_test+0xcb>
call   1030 <abort@plt>
jmp    1205 <bmi2_test+0xd5>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    1155 <bmi2_test+0x25>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bmi2_test>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
movabs $0xce7ace0ce7ace0,%r14
mov    $0xface,%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <calc_mul_u64@plt>
movslq %eax,%r15
mov    %r15,%rbx
sar    $0x3f,%rbx
lea    0x8(%rsp),%rdx
mov    $0xface,%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <calc_mulx_u64@plt>
xor    %eax,%r15d
movslq %r15d,%rax
xor    0x8(%rsp),%rbx
or     %rax,%rbx
jne    12b2 <bmi2_test+0x182>
movabs $0x19cf59c19cf59c0,%r14
f5 9c 01
mov    $0x7d67,%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <calc_mul_u64@plt>
movslq %eax,%r15
mov    %r15,%rbx
sar    $0x3f,%rbx
lea    0x8(%rsp),%rdx
mov    $0x7d67,%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <calc_mulx_u64@plt>
xor    %eax,%r15d
movslq %r15d,%rax
xor    0x8(%rsp),%rbx
or     %rax,%rbx
jne    12b2 <bmi2_test+0x182>
movabs $0x4d6e0d44d6e0d40,%r14
e0 d6 04
mov    $0x29cd,%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <calc_mul_u64@plt>
movslq %eax,%r15
mov    %r15,%rbx
sar    $0x3f,%rbx
lea    0x8(%rsp),%rdx
mov    $0x29cd,%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <calc_mulx_u64@plt>
xor    %eax,%r15d
movslq %r15d,%rax
xor    0x8(%rsp),%rbx
or     %rax,%rbx
jne    12b2 <bmi2_test+0x182>
movabs $0x135b835135b83500,%r14
83 5b 13
mov    $0xa73,%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <calc_mul_u64@plt>
movslq %eax,%r15
mov    %r15,%rbx
sar    $0x3f,%rbx
lea    0x8(%rsp),%rdx
mov    $0xa73,%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <calc_mulx_u64@plt>
xor    %eax,%r15d
movslq %r15d,%rax
xor    0x8(%rsp),%rbx
or     %rax,%rbx
jne    12b2 <bmi2_test+0x182>
movabs $0x60c990960c990900,%r14
90 c9 60
mov    $0x217,%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <calc_mul_u64@plt>
movslq %eax,%r15
mov    %r15,%rbx
sar    $0x3f,%rbx
lea    0x8(%rsp),%rdx
mov    $0x217,%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <calc_mulx_u64@plt>
xor    %eax,%r15d
movslq %r15d,%rax
xor    0x8(%rsp),%rbx
or     %rax,%rbx
jne    12b2 <bmi2_test+0x182>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
call   1030 <abort@plt>

LLVM IR (-O0):
define void @bmi2_test() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i128
%7 = alloca i128
store i64 58118870600756448, i64* %2
store i64 64206, i64* %3
store i32 0, i32* %1
br label %8
8: ; preds = %41, %0
%9 = load i32, i32* %1
%10 = icmp ult i32 %9, 5
br i1 %10, label %11, label %44
11: ; preds = %8
%12 = load i64, i64* %2
%13 = load i32, i32* %1
%14 = add i32 %13, 1
%15 = zext i32 %14 to i64
%16 = mul i64 %12, %15
store i64 %16, i64* %2
%17 = load i64, i64* %3
%18 = load i32, i32* %1
%19 = add i32 %18, 1
%20 = zext i32 %19 to i64
%21 = udiv i64 %17, %20
store i64 %21, i64* %3
%22 = load i64, i64* %2
%23 = load i64, i64* %3
%24 = call i32 (i64, i64, ...) bitcast (i32 (...)* @calc_mul_u64 to i32 (i64, i64, ...)*)(i64 %22, i64 %23)
%25 = sext i32 %24 to i128
store i128 %25, i128* %7
%26 = load i64, i64* %2
%27 = load i64, i64* %3
%28 = call i32 (i64, i64, i64*, ...) bitcast (i32 (...)* @calc_mulx_u64 to i32 (i64, i64, i64*, ...)*)(i64 %26, i64 %27, i64* %5)
%29 = sext i32 %28 to i64
store i64 %29, i64* %4
%30 = load i64, i64* %5
%31 = zext i64 %30 to i128
%32 = shl i128 %31, 64
%33 = load i64, i64* %4
%34 = zext i64 %33 to i128
%35 = or i128 %32, %34
store i128 %35, i128* %6
%36 = load i128, i128* %6
%37 = load i128, i128* %7
%38 = icmp ne i128 %36, %37
br i1 %38, label %39, label %40
39: ; preds = %11
unreachable
40: ; preds = %11
br label %41
41: ; preds = %40
%42 = load i32, i32* %1
%43 = add i32 %42, 1
store i32 %43, i32* %1
br label %8
44: ; preds = %8
ret void
}

LLVM IR (-O2):
define void @bmi2_test() {
%1 = alloca i64
%2 = bitcast i64* %1 to i8*
%3 = call i32 (i64, i64, ...) bitcast (i32 (...)* @calc_mul_u64 to i32 (i64, i64, ...)*)(i64 58118870600756448, i64 64206)
%4 = sext i32 %3 to i128
%5 = call i32 (i64, i64, i64*, ...) bitcast (i32 (...)* @calc_mulx_u64 to i32 (i64, i64, i64*, ...)*)(i64 58118870600756448, i64 64206, i64* %1)
%6 = sext i32 %5 to i64
%7 = load i64, i64* %1
%8 = zext i64 %7 to i128
%9 = shl nuw i128 %8, 64
%10 = zext i64 %6 to i128
%11 = or i128 %9, %10
%12 = icmp eq i128 %11, %4
br i1 %12, label %13, label %57
13: ; preds = %0
%14 = call i32 (i64, i64, ...) bitcast (i32 (...)* @calc_mul_u64 to i32 (i64, i64, ...)*)(i64 116237741201512896, i64 32103)
%15 = sext i32 %14 to i128
%16 = call i32 (i64, i64, i64*, ...) bitcast (i32 (...)* @calc_mulx_u64 to i32 (i64, i64, i64*, ...)*)(i64 116237741201512896, i64 32103, i64* %1)
%17 = sext i32 %16 to i64
%18 = load i64, i64* %1
%19 = zext i64 %18 to i128
%20 = shl nuw i128 %19, 64
%21 = zext i64 %17 to i128
%22 = or i128 %20, %21
%23 = icmp eq i128 %22, %15
br i1 %23, label %24, label %57
24: ; preds = %13
%25 = call i32 (i64, i64, ...) bitcast (i32 (...)* @calc_mul_u64 to i32 (i64, i64, ...)*)(i64 348713223604538688, i64 10701)
%26 = sext i32 %25 to i128
%27 = call i32 (i64, i64, i64*, ...) bitcast (i32 (...)* @calc_mulx_u64 to i32 (i64, i64, i64*, ...)*)(i64 348713223604538688, i64 10701, i64* %1)
%28 = sext i32 %27 to i64
%29 = load i64, i64* %1
%30 = zext i64 %29 to i128
%31 = shl nuw i128 %30, 64
%32 = zext i64 %28 to i128
%33 = or i128 %31, %32
%34 = icmp eq i128 %33, %26
br i1 %34, label %35, label %57
35: ; preds = %24
%36 = call i32 (i64, i64, ...) bitcast (i32 (...)* @calc_mul_u64 to i32 (i64, i64, ...)*)(i64 1394852894418154752, i64 2675)
%37 = sext i32 %36 to i128
%38 = call i32 (i64, i64, i64*, ...) bitcast (i32 (...)* @calc_mulx_u64 to i32 (i64, i64, i64*, ...)*)(i64 1394852894418154752, i64 2675, i64* %1)
%39 = sext i32 %38 to i64
%40 = load i64, i64* %1
%41 = zext i64 %40 to i128
%42 = shl nuw i128 %41, 64
%43 = zext i64 %39 to i128
%44 = or i128 %42, %43
%45 = icmp eq i128 %44, %37
br i1 %45, label %46, label %57
46: ; preds = %35
%47 = call i32 (i64, i64, ...) bitcast (i32 (...)* @calc_mul_u64 to i32 (i64, i64, ...)*)(i64 6974264472090773760, i64 535)
%48 = sext i32 %47 to i128
%49 = call i32 (i64, i64, i64*, ...) bitcast (i32 (...)* @calc_mulx_u64 to i32 (i64, i64, i64*, ...)*)(i64 6974264472090773760, i64 535, i64* %1)
%50 = sext i32 %49 to i64
%51 = load i64, i64* %1
%52 = zext i64 %51 to i128
%53 = shl nuw i128 %52, 64
%54 = zext i64 %50 to i128
%55 = or i128 %53, %54
%56 = icmp eq i128 %55, %48
br i1 %56, label %58, label %57
57: ; preds = %46, %35, %24, %13, %0
unreachable
58: ; preds = %46
ret void
}