#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ abort();
unsigned long long calc_mul_u32(unsigned int, unsigned int);
unsigned int calc_mulx_u32(unsigned int, unsigned int, unsigned int *);
void bmi2_test() {
  unsigned i;
  unsigned a = 0xce7ace0;
  unsigned b = 0xfacefff;
  unsigned res_l, res_h;
  unsigned long long res, res_ref;
  for (i = 0; i < 5; ++i) {
    a = a * (i + 1);
    b = b / (i + 1);
    res_ref = calc_mul_u32(a, b);
    res_l = calc_mulx_u32(a, b, &res_h);
    res = ((unsigned long long)res_h << 32) | res_l;
    if (res != res_ref)
      abort();
  }
}


x86 assembly (clang, -O0):
<bmi2_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0xce7ace0,-0x8(%rbp)
movl   $0xfacefff,-0xc(%rbp)
movl   $0x0,-0x4(%rbp)
cmpl   $0x5,-0x4(%rbp)
jae    11d0 <bmi2_test+0xa0>
mov    -0x8(%rbp),%eax
mov    -0x4(%rbp),%ecx
add    $0x1,%ecx
imul   %ecx,%eax
mov    %eax,-0x8(%rbp)
mov    -0xc(%rbp),%eax
mov    -0x4(%rbp),%ecx
add    $0x1,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1030 <calc_mul_u32@plt>
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
lea    -0x14(%rbp),%rdx
call   1050 <calc_mulx_u32@plt>
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
shl    $0x20,%rax
mov    -0x10(%rbp),%ecx
or     %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
je     11bd <bmi2_test+0x8d>
mov    $0x0,%al
call   1040 <abort@plt>
jmp    11c2 <bmi2_test+0x92>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    114d <bmi2_test+0x1d>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bmi2_test>:
push   %rbx
sub    $0x10,%rsp
mov    $0xce7ace0,%edi
mov    $0xfacefff,%esi
call   1030 <calc_mul_u32@plt>
mov    %rax,%rbx
lea    0xc(%rsp),%rdx
mov    $0xce7ace0,%edi
mov    $0xfacefff,%esi
call   1050 <calc_mulx_u32@plt>
mov    0xc(%rsp),%ecx
shl    $0x20,%rcx
mov    %eax,%eax
or     %rcx,%rax
cmp    %rbx,%rax
jne    125b <bmi2_test+0x12b>
mov    $0x19cf59c0,%edi
mov    $0x7d677ff,%esi
call   1030 <calc_mul_u32@plt>
mov    %rax,%rbx
lea    0xc(%rsp),%rdx
mov    $0x19cf59c0,%edi
mov    $0x7d677ff,%esi
call   1050 <calc_mulx_u32@plt>
mov    0xc(%rsp),%ecx
shl    $0x20,%rcx
mov    %eax,%eax
or     %rcx,%rax
cmp    %rbx,%rax
jne    125b <bmi2_test+0x12b>
mov    $0x4d6e0d40,%edi
mov    $0x29cd2aa,%esi
call   1030 <calc_mul_u32@plt>
mov    %rax,%rbx
lea    0xc(%rsp),%rdx
mov    $0x4d6e0d40,%edi
mov    $0x29cd2aa,%esi
call   1050 <calc_mulx_u32@plt>
mov    0xc(%rsp),%ecx
shl    $0x20,%rcx
mov    %eax,%eax
or     %rcx,%rax
cmp    %rbx,%rax
jne    125b <bmi2_test+0x12b>
mov    $0x35b83500,%edi
mov    $0xa734aa,%esi
call   1030 <calc_mul_u32@plt>
mov    %rax,%rbx
lea    0xc(%rsp),%rdx
mov    $0x35b83500,%edi
mov    $0xa734aa,%esi
call   1050 <calc_mulx_u32@plt>
mov    0xc(%rsp),%ecx
shl    $0x20,%rcx
mov    %eax,%eax
or     %rcx,%rax
cmp    %rbx,%rax
jne    125b <bmi2_test+0x12b>
mov    $0xc990900,%edi
mov    $0x2170ee,%esi
call   1030 <calc_mul_u32@plt>
mov    %rax,%rbx
lea    0xc(%rsp),%rdx
mov    $0xc990900,%edi
mov    $0x2170ee,%esi
call   1050 <calc_mulx_u32@plt>
mov    0xc(%rsp),%ecx
shl    $0x20,%rcx
mov    %eax,%eax
or     %rcx,%rax
cmp    %rbx,%rax
jne    125b <bmi2_test+0x12b>
add    $0x10,%rsp
pop    %rbx
ret
xor    %eax,%eax
call   1040 <abort@plt>

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

LLVM IR (-O2):
define void @bmi2_test() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = call i64 @calc_mul_u32(i32 216509664, i32 262991871)
%4 = call i32 @calc_mulx_u32(i32 216509664, i32 262991871, i32* %1)
%5 = load i32, i32* %1
%6 = zext i32 %5 to i64
%7 = shl nuw i64 %6, 32
%8 = zext i32 %4 to i64
%9 = or i64 %7, %8
%10 = icmp eq i64 %9, %3
br i1 %10, label %11, label %47
11: ; preds = %0
%12 = call i64 @calc_mul_u32(i32 433019328, i32 131495935)
%13 = call i32 @calc_mulx_u32(i32 433019328, i32 131495935, i32* %1)
%14 = load i32, i32* %1
%15 = zext i32 %14 to i64
%16 = shl nuw i64 %15, 32
%17 = zext i32 %13 to i64
%18 = or i64 %16, %17
%19 = icmp eq i64 %18, %12
br i1 %19, label %20, label %47
20: ; preds = %11
%21 = call i64 @calc_mul_u32(i32 1299057984, i32 43831978)
%22 = call i32 @calc_mulx_u32(i32 1299057984, i32 43831978, i32* %1)
%23 = load i32, i32* %1
%24 = zext i32 %23 to i64
%25 = shl nuw i64 %24, 32
%26 = zext i32 %22 to i64
%27 = or i64 %25, %26
%28 = icmp eq i64 %27, %21
br i1 %28, label %29, label %47
29: ; preds = %20
%30 = call i64 @calc_mul_u32(i32 901264640, i32 10957994)
%31 = call i32 @calc_mulx_u32(i32 901264640, i32 10957994, i32* %1)
%32 = load i32, i32* %1
%33 = zext i32 %32 to i64
%34 = shl nuw i64 %33, 32
%35 = zext i32 %31 to i64
%36 = or i64 %34, %35
%37 = icmp eq i64 %36, %30
br i1 %37, label %38, label %47
38: ; preds = %29
%39 = call i64 @calc_mul_u32(i32 211355904, i32 2191598)
%40 = call i32 @calc_mulx_u32(i32 211355904, i32 2191598, i32* %1)
%41 = load i32, i32* %1
%42 = zext i32 %41 to i64
%43 = shl nuw i64 %42, 32
%44 = zext i32 %40 to i64
%45 = or i64 %43, %44
%46 = icmp eq i64 %45, %39
br i1 %46, label %49, label %47
47: ; preds = %38, %29, %20, %11, %0
%48 = call i32 (...) @abort()
unreachable
49: ; preds = %38
ret void
}