#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 __bextr_u64(unsigned long long, unsigned long long);
int /*<<< orphan*/ abort();
unsigned long long calc_bextr_u64(unsigned long long, unsigned long long);
void bmi_test() {
  unsigned i;
  unsigned char start, len;
  unsigned long long src1 = 0xfacec0ffeefacec0;
  unsigned long long res, res_ref, src2;
  for (i = 0; i < 5; ++i) {
    start = (i * 1983) % 64;
    len = (i + (i * 1983)) % 64;
    src1 = src1 * 3;
    src2 = start | (((unsigned long long)len) << 8);
    res_ref = calc_bextr_u64(src1, src2);
    res = __bextr_u64(src1, src2);
    if (res != res_ref)
      abort();
  }
}


x86 assembly (clang, -O0):
<bmi_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movabs $0xfacec0ffeefacec0,%rax
c0 ce fa
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x4(%rbp)
cmpl   $0x5,-0x4(%rbp)
jae    11dc <bmi_test+0xac>
imul   $0x7bf,-0x4(%rbp),%eax
and    $0x3f,%eax
mov    %al,-0x5(%rbp)
mov    -0x4(%rbp),%eax
imul   $0x7bf,-0x4(%rbp),%ecx
add    %ecx,%eax
and    $0x3f,%eax
mov    %al,-0x6(%rbp)
imul   $0x3,-0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
movzbl -0x5(%rbp),%eax
movzbl -0x6(%rbp),%ecx
shl    $0x8,%rcx
or     %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1050 <calc_bextr_u64@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <__bextr_u64@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     11c9 <bmi_test+0x99>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    11ce <bmi_test+0x9e>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    114d <bmi_test+0x1d>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bmi_test>:
push   %r14
push   %rbx
push   %rax
movabs $0xf06c42ffccf06c40,%r14
42 6c f0
mov    %r14,%rdi
xor    %esi,%esi
call   1050 <calc_bextr_u64@plt>
mov    %rax,%rbx
mov    %r14,%rdi
xor    %esi,%esi
call   1040 <__bextr_u64@plt>
cmp    %rbx,%rax
jne    121a <bmi_test+0xea>
movabs $0xd144c8ff66d144c0,%r14
c8 44 d1
mov    $0x3f,%esi
mov    %r14,%rdi
call   1050 <calc_bextr_u64@plt>
mov    %rax,%rbx
mov    $0x3f,%esi
mov    %r14,%rdi
call   1040 <__bextr_u64@plt>
cmp    %rbx,%rax
jne    121a <bmi_test+0xea>
movabs $0x73ce5afe3473ce40,%r14
5a ce 73
mov    $0x3e,%esi
mov    %r14,%rdi
call   1050 <calc_bextr_u64@plt>
mov    %rax,%rbx
mov    $0x3e,%esi
mov    %r14,%rdi
call   1040 <__bextr_u64@plt>
cmp    %rbx,%rax
jne    121a <bmi_test+0xea>
movabs $0x5b6b10fa9d5b6ac0,%r14
10 6b 5b
mov    $0x3d,%esi
mov    %r14,%rdi
call   1050 <calc_bextr_u64@plt>
mov    %rax,%rbx
mov    $0x3d,%esi
mov    %r14,%rdi
call   1040 <__bextr_u64@plt>
cmp    %rbx,%rax
jne    121a <bmi_test+0xea>
movabs $0x124132efd8124040,%r14
32 41 12
mov    $0x3c,%esi
mov    %r14,%rdi
call   1050 <calc_bextr_u64@plt>
mov    %rax,%rbx
mov    $0x3c,%esi
mov    %r14,%rdi
call   1040 <__bextr_u64@plt>
cmp    %rbx,%rax
jne    121a <bmi_test+0xea>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
xor    %eax,%eax
call   1030 <abort@plt>

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

LLVM IR (-O2):
define void @bmi_test() {
%1 = tail call i64 @calc_bextr_u64(i64 -1122448540699694016, i64 0)
%2 = tail call i64 @__bextr_u64(i64 -1122448540699694016, i64 0)
%3 = icmp eq i64 %2, %1
br i1 %3, label %4, label %21
4: ; preds = %0
%5 = tail call i64 @calc_bextr_u64(i64 -3367345622099082048, i64 63)
%6 = tail call i64 @__bextr_u64(i64 -3367345622099082048, i64 63)
%7 = icmp eq i64 %6, %5
br i1 %7, label %8, label %21
8: ; preds = %4
%9 = tail call i64 @calc_bextr_u64(i64 8344707207412305472, i64 62)
%10 = tail call i64 @__bextr_u64(i64 8344707207412305472, i64 62)
%11 = icmp eq i64 %10, %9
br i1 %11, label %12, label %21
12: ; preds = %8
%13 = tail call i64 @calc_bextr_u64(i64 6587377548527364800, i64 61)
%14 = tail call i64 @__bextr_u64(i64 6587377548527364800, i64 61)
%15 = icmp eq i64 %14, %13
br i1 %15, label %16, label %21
16: ; preds = %12
%17 = tail call i64 @calc_bextr_u64(i64 1315388571872542784, i64 60)
%18 = tail call i64 @__bextr_u64(i64 1315388571872542784, i64 60)
%19 = icmp eq i64 %18, %17
br i1 %19, label %20, label %21
20: ; preds = %16
ret void
21: ; preds = %16, %12, %8, %4, %0
%22 = tail call i32 (...) @abort()
unreachable
}