#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 char, unsigned char);
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 * 4;
    len = i * 3;
    src1 = src1 * 3;
    src2 = (start & 0xff) | ((len & 0xff) << 8);
    res_ref = calc_bextr_u64(src1, src2);
    res = _bextr_u64(src1, start, len);
    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    11de <bmi_test+0xae>
mov    -0x4(%rbp),%eax
shl    $0x2,%eax
mov    %al,-0x5(%rbp)
imul   $0x3,-0x4(%rbp),%eax
mov    %al,-0x6(%rbp)
imul   $0x3,-0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
movzbl -0x5(%rbp),%eax
and    $0xff,%eax
movzbl -0x6(%rbp),%ecx
and    $0xff,%ecx
shl    $0x8,%ecx
or     %ecx,%eax
cltq
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    -0x5(%rbp),%al
movzbl %al,%esi
movzbl -0x6(%rbp),%edx
call   1040 <_bextr_u64@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     11cb <bmi_test+0x9b>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    11d0 <bmi_test+0xa0>
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
xor    %edx,%edx
call   1040 <_bextr_u64@plt>
cmp    %rbx,%rax
jne    1230 <bmi_test+0x100>
movabs $0xd144c8ff66d144c0,%r14
c8 44 d1
mov    $0x304,%esi
mov    %r14,%rdi
call   1050 <calc_bextr_u64@plt>
mov    %rax,%rbx
mov    %r14,%rdi
mov    $0x4,%esi
mov    $0x3,%edx
call   1040 <_bextr_u64@plt>
cmp    %rbx,%rax
jne    1230 <bmi_test+0x100>
movabs $0x73ce5afe3473ce40,%r14
5a ce 73
mov    $0x608,%esi
mov    %r14,%rdi
call   1050 <calc_bextr_u64@plt>
mov    %rax,%rbx
mov    %r14,%rdi
mov    $0x8,%esi
mov    $0x6,%edx
call   1040 <_bextr_u64@plt>
cmp    %rbx,%rax
jne    1230 <bmi_test+0x100>
movabs $0x5b6b10fa9d5b6ac0,%r14
10 6b 5b
mov    $0x90c,%esi
mov    %r14,%rdi
call   1050 <calc_bextr_u64@plt>
mov    %rax,%rbx
mov    %r14,%rdi
mov    $0xc,%esi
mov    $0x9,%edx
call   1040 <_bextr_u64@plt>
cmp    %rbx,%rax
jne    1230 <bmi_test+0x100>
movabs $0x124132efd8124040,%r14
32 41 12
mov    $0xc10,%esi
mov    %r14,%rdi
call   1050 <calc_bextr_u64@plt>
mov    %rax,%rbx
mov    %r14,%rdi
mov    $0x10,%esi
mov    $0xc,%edx
call   1040 <_bextr_u64@plt>
cmp    %rbx,%rax
jne    1230 <bmi_test+0x100>
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, 4
%14 = trunc i32 %13 to i8
store i8 %14, i8* %2
%15 = load i32, i32* %1
%16 = mul i32 %15, 3
%17 = trunc i32 %16 to i8
store i8 %17, i8* %3
%18 = load i64, i64* %4
%19 = mul i64 %18, 3
store i64 %19, i64* %4
%20 = load i8, i8* %2
%21 = zext i8 %20 to i32
%22 = and i32 %21, 255
%23 = load i8, i8* %3
%24 = zext i8 %23 to i32
%25 = and i32 %24, 255
%26 = shl i32 %25, 8
%27 = or i32 %22, %26
%28 = sext i32 %27 to i64
store i64 %28, i64* %7
%29 = load i64, i64* %4
%30 = load i64, i64* %7
%31 = call i64 @calc_bextr_u64(i64 %29, i64 %30)
store i64 %31, i64* %6
%32 = load i64, i64* %4
%33 = load i8, i8* %2
%34 = load i8, i8* %3
%35 = call i64 @_bextr_u64(i64 %32, i8 zeroext %33, i8 zeroext %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, i8 zeroext 0, i8 zeroext 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 772)
%6 = tail call i64 @_bextr_u64(i64 -3367345622099082048, i8 zeroext 4, i8 zeroext 3)
%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 1544)
%10 = tail call i64 @_bextr_u64(i64 8344707207412305472, i8 zeroext 8, i8 zeroext 6)
%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 2316)
%14 = tail call i64 @_bextr_u64(i64 6587377548527364800, i8 zeroext 12, i8 zeroext 9)
%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 3088)
%18 = tail call i64 @_bextr_u64(i64 1315388571872542784, i8 zeroext 16, i8 zeroext 12)
%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
}