#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 */
unsigned long long _bzhi_u64(unsigned long long, unsigned int);
int /*<<< orphan*/ abort();
unsigned long long calc_bzhi_u64(unsigned long long, unsigned int);
void bmi2_test() {
  unsigned i;
  unsigned long long src = 0xce7ace0ce7ace0ff;
  unsigned long long res, res_ref;
  for (i = 0; i < 5; ++i) {
    src = src * (i + 1);
    res_ref = calc_bzhi_u64(src, i * 2);
    res = _bzhi_u64(src, i * 2);
    if (res != res_ref)
      abort();
  }
}


x86 assembly (clang, -O0):
<bmi2_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movabs $0xce7ace0ce7ace0ff,%rax
ce 7a ce
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x4(%rbp)
cmpl   $0x5,-0x4(%rbp)
jae    11b9 <bmi2_test+0x89>
mov    -0x10(%rbp),%rax
mov    -0x4(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,%ecx
imul   %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%esi
shl    $0x1,%esi
call   1040 <calc_bzhi_u64@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%esi
shl    $0x1,%esi
call   1050 <_bzhi_u64@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     11a6 <bmi2_test+0x76>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    11ab <bmi2_test+0x7b>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    114d <bmi2_test+0x1d>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bmi2_test>:
push   %r14
push   %rbx
push   %rax
movabs $0xce7ace0ce7ace0ff,%r14
ce 7a ce
mov    %r14,%rdi
xor    %esi,%esi
call   1040 <calc_bzhi_u64@plt>
mov    %rax,%rbx
mov    %r14,%rdi
xor    %esi,%esi
call   1050 <_bzhi_u64@plt>
cmp    %rbx,%rax
jne    121a <bmi2_test+0xea>
movabs $0x9cf59c19cf59c1fe,%r14
9c f5 9c
mov    %r14,%rdi
mov    $0x2,%esi
call   1040 <calc_bzhi_u64@plt>
mov    %rax,%rbx
mov    %r14,%rdi
mov    $0x2,%esi
call   1050 <_bzhi_u64@plt>
cmp    %rbx,%rax
jne    121a <bmi2_test+0xea>
movabs $0xd6e0d44d6e0d45fa,%r14
d4 e0 d6
mov    %r14,%rdi
mov    $0x4,%esi
call   1040 <calc_bzhi_u64@plt>
mov    %rax,%rbx
mov    %r14,%rdi
mov    $0x4,%esi
call   1050 <_bzhi_u64@plt>
cmp    %rbx,%rax
jne    121a <bmi2_test+0xea>
movabs $0x5b835135b83517e8,%r14
51 83 5b
mov    %r14,%rdi
mov    $0x6,%esi
call   1040 <calc_bzhi_u64@plt>
mov    %rax,%rbx
mov    %r14,%rdi
mov    $0x6,%esi
call   1050 <_bzhi_u64@plt>
cmp    %rbx,%rax
jne    121a <bmi2_test+0xea>
movabs $0xc990960c99097788,%r14
96 90 c9
mov    %r14,%rdi
mov    $0x8,%esi
call   1040 <calc_bzhi_u64@plt>
mov    %rax,%rbx
mov    %r14,%rdi
mov    $0x8,%esi
call   1050 <_bzhi_u64@plt>
cmp    %rbx,%rax
jne    121a <bmi2_test+0xea>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
define void @bmi2_test() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
store i64 -3568313199915900673, i64* %2
store i32 0, i32* %1
br label %5
5: ; preds = %28, %0
%6 = load i32, i32* %1
%7 = icmp ult i32 %6, 5
br i1 %7, label %8, label %31
8: ; preds = %5
%9 = load i64, i64* %2
%10 = load i32, i32* %1
%11 = add i32 %10, 1
%12 = zext i32 %11 to i64
%13 = mul i64 %9, %12
store i64 %13, i64* %2
%14 = load i64, i64* %2
%15 = load i32, i32* %1
%16 = mul i32 %15, 2
%17 = call i64 @calc_bzhi_u64(i64 %14, i32 %16)
store i64 %17, i64* %4
%18 = load i64, i64* %2
%19 = load i32, i32* %1
%20 = mul i32 %19, 2
%21 = call i64 @_bzhi_u64(i64 %18, i32 %20)
store i64 %21, i64* %3
%22 = load i64, i64* %3
%23 = load i64, i64* %4
%24 = icmp ne i64 %22, %23
br i1 %24, label %25, label %27
25: ; preds = %8
%26 = call i32 (...) @abort()
unreachable
27: ; preds = %8
br label %28
28: ; preds = %27
%29 = load i32, i32* %1
%30 = add i32 %29, 1
store i32 %30, i32* %1
br label %5
31: ; preds = %5
ret void
}

LLVM IR (-O2):
define void @bmi2_test() {
%1 = tail call i64 @calc_bzhi_u64(i64 -3568313199915900673, i32 0)
%2 = tail call i64 @_bzhi_u64(i64 -3568313199915900673, i32 0)
%3 = icmp eq i64 %2, %1
br i1 %3, label %4, label %20
4: ; preds = %0
%5 = tail call i64 @calc_bzhi_u64(i64 -7136626399831801346, i32 2)
%6 = tail call i64 @_bzhi_u64(i64 -7136626399831801346, i32 2)
%7 = icmp eq i64 %6, %5
br i1 %7, label %8, label %20
8: ; preds = %4
%9 = tail call i64 @calc_bzhi_u64(i64 -2963135125785852422, i32 4)
%10 = tail call i64 @_bzhi_u64(i64 -2963135125785852422, i32 4)
%11 = icmp eq i64 %10, %9
br i1 %11, label %12, label %20
12: ; preds = %8
%13 = tail call i64 @calc_bzhi_u64(i64 6594203570566141928, i32 6)
%14 = tail call i64 @_bzhi_u64(i64 6594203570566141928, i32 6)
%15 = icmp eq i64 %14, %13
br i1 %15, label %16, label %20
16: ; preds = %12
%17 = tail call i64 @calc_bzhi_u64(i64 -3922470294588393592, i32 8)
%18 = tail call i64 @_bzhi_u64(i64 -3922470294588393592, i32 8)
%19 = icmp eq i64 %18, %17
br i1 %19, label %22, label %20
20: ; preds = %16, %12, %8, %4, %0
%21 = tail call i32 (...) @abort()
unreachable
22: ; preds = %16
ret void
}