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


x86 assembly (clang, -O0):
<bmi_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0xfacec0ff,-0xc(%rbp)
movl   $0x0,-0x4(%rbp)
cmpl   $0x5,-0x4(%rbp)
jae    11c8 <bmi_test+0x98>
imul   $0x7bf,-0x4(%rbp),%eax
and    $0x1f,%eax
mov    %al,-0x5(%rbp)
mov    -0x4(%rbp),%eax
imul   $0x7bf,-0x4(%rbp),%ecx
add    %ecx,%eax
and    $0x1f,%eax
mov    %al,-0x6(%rbp)
imul   $0x3,-0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
movzbl -0x5(%rbp),%eax
movzbl -0x6(%rbp),%ecx
shl    $0x8,%ecx
or     %ecx,%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1040 <calc_bextr_u32@plt>
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1050 <__bextr_u32@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     11b5 <bmi_test+0x85>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    11ba <bmi_test+0x8a>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    1146 <bmi_test+0x16>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bmi_test>:
push   %rbx
mov    $0xf06c42fd,%edi
xor    %esi,%esi
call   1040 <calc_bextr_u32@plt>
mov    %eax,%ebx
mov    $0xf06c42fd,%edi
xor    %esi,%esi
call   1050 <__bextr_u32@plt>
cmp    %ebx,%eax
jne    11e5 <bmi_test+0xb5>
mov    $0xd144c8f7,%edi
mov    $0x1f,%esi
call   1040 <calc_bextr_u32@plt>
mov    %eax,%ebx
mov    $0xd144c8f7,%edi
mov    $0x1f,%esi
call   1050 <__bextr_u32@plt>
cmp    %ebx,%eax
jne    11e5 <bmi_test+0xb5>
mov    $0x73ce5ae5,%edi
mov    $0x1e,%esi
call   1040 <calc_bextr_u32@plt>
mov    %eax,%ebx
mov    $0x73ce5ae5,%edi
mov    $0x1e,%esi
call   1050 <__bextr_u32@plt>
cmp    %ebx,%eax
jne    11e5 <bmi_test+0xb5>
mov    $0x5b6b10af,%edi
mov    $0x1d,%esi
call   1040 <calc_bextr_u32@plt>
mov    %eax,%ebx
mov    $0x5b6b10af,%edi
mov    $0x1d,%esi
call   1050 <__bextr_u32@plt>
cmp    %ebx,%eax
jne    11e5 <bmi_test+0xb5>
mov    $0x1241320d,%edi
mov    $0x1c,%esi
call   1040 <calc_bextr_u32@plt>
mov    %eax,%ebx
mov    $0x1241320d,%edi
mov    $0x1c,%esi
call   1050 <__bextr_u32@plt>
cmp    %ebx,%eax
jne    11e5 <bmi_test+0xb5>
pop    %rbx
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 i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 -87113473, i32* %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, 32
%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, 32
%21 = trunc i32 %20 to i8
store i8 %21, i8* %3
%22 = load i32, i32* %4
%23 = mul i32 %22, 3
store i32 %23, i32* %4
%24 = load i8, i8* %2
%25 = zext i8 %24 to i32
%26 = load i8, i8* %3
%27 = zext i8 %26 to i32
%28 = shl i32 %27, 8
%29 = or i32 %25, %28
store i32 %29, i32* %7
%30 = load i32, i32* %4
%31 = load i32, i32* %7
%32 = call i32 @calc_bextr_u32(i32 %30, i32 %31)
store i32 %32, i32* %6
%33 = load i32, i32* %4
%34 = load i32, i32* %7
%35 = call i32 @__bextr_u32(i32 %33, i32 %34)
store i32 %35, i32* %5
%36 = load i32, i32* %5
%37 = load i32, i32* %6
%38 = icmp ne i32 %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 i32 @calc_bextr_u32(i32 -261340419, i32 0)
%2 = tail call i32 @__bextr_u32(i32 -261340419, i32 0)
%3 = icmp eq i32 %2, %1
br i1 %3, label %4, label %21
4: ; preds = %0
%5 = tail call i32 @calc_bextr_u32(i32 -784021257, i32 31)
%6 = tail call i32 @__bextr_u32(i32 -784021257, i32 31)
%7 = icmp eq i32 %6, %5
br i1 %7, label %8, label %21
8: ; preds = %4
%9 = tail call i32 @calc_bextr_u32(i32 1942903525, i32 30)
%10 = tail call i32 @__bextr_u32(i32 1942903525, i32 30)
%11 = icmp eq i32 %10, %9
br i1 %11, label %12, label %21
12: ; preds = %8
%13 = tail call i32 @calc_bextr_u32(i32 1533743279, i32 29)
%14 = tail call i32 @__bextr_u32(i32 1533743279, i32 29)
%15 = icmp eq i32 %14, %13
br i1 %15, label %16, label %21
16: ; preds = %12
%17 = tail call i32 @calc_bextr_u32(i32 306262541, i32 28)
%18 = tail call i32 @__bextr_u32(i32 306262541, i32 28)
%19 = icmp eq i32 %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
}