#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 */
typedef int uint32_t;

/* Variables and functions */
uint32_t _bitscan(uint32_t dword) {
  for (uint32_t i = 0; i < 32; i++) {
    if (1 & dword) {
      return i;
    }
    dword = dword >> 1;
  }
  return (uint32_t)-1;
}


x86 assembly (clang, -O0):
<_bitscan>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x20,-0xc(%rbp)
jge    1149 <_bitscan+0x49>
mov    -0x8(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1132 <_bitscan+0x32>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1150 <_bitscan+0x50>
mov    -0x8(%rbp),%eax
sar    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    110e <_bitscan+0xe>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<_bitscan>:
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
test   $0x1,%dil
jne    113c <_bitscan+0x3c>
test   $0x2,%dil
jne    1139 <_bitscan+0x39>
test   $0x4,%dil
jne    113d <_bitscan+0x3d>
test   $0x8,%dil
jne    1141 <_bitscan+0x41>
sar    $0x4,%edi
add    $0x4,%eax
cmp    $0x20,%eax
jne    1110 <_bitscan+0x10>
mov    $0xffffffff,%eax
ret
add    $0x1,%eax
ret
add    $0x2,%eax
ret
add    $0x3,%eax
ret

LLVM IR (-O0):
define i32 @_bitscan(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
store i32 0, i32* %4
br label %5
5: ; preds = %17, %1
%6 = load i32, i32* %4
%7 = icmp slt i32 %6, 32
br i1 %7, label %8, label %20
8: ; preds = %5
%9 = load i32, i32* %3
%10 = and i32 1, %9
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = load i32, i32* %4
store i32 %13, i32* %2
br label %21
14: ; preds = %8
%15 = load i32, i32* %3
%16 = ashr i32 %15, 1
store i32 %16, i32* %3
br label %17
17: ; preds = %14
%18 = load i32, i32* %4
%19 = add nsw i32 %18, 1
store i32 %19, i32* %4
br label %5
20: ; preds = %5
store i32 -1, i32* %2
br label %21
21: ; preds = %20, %12
%22 = load i32, i32* %2
ret i32 %22
}

LLVM IR (-O2):
define i32 @_bitscan(i32 %0) {
br label %2
2: ; preds = %16, %1
%3 = phi i32 [ 0, %1 ], [ %18, %16 ]
%4 = phi i32 [ %0, %1 ], [ %17, %16 ]
%5 = and i32 %4, 1
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %26
7: ; preds = %2
%8 = and i32 %4, 2
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %24
10: ; preds = %7
%11 = and i32 %4, 4
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %22
13: ; preds = %10
%14 = and i32 %4, 8
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = ashr i32 %4, 4
%18 = add nuw nsw i32 %3, 4
%19 = icmp eq i32 %18, 32
br i1 %19, label %26, label %2
20: ; preds = %13
%21 = or i32 %3, 3
br label %26
22: ; preds = %10
%23 = or i32 %3, 2
br label %26
24: ; preds = %7
%25 = or i32 %3, 1
br label %26
26: ; preds = %2, %16, %24, %22, %20
%27 = phi i32 [ %21, %20 ], [ %23, %22 ], [ %25, %24 ], [ -1, %16 ], [ %3, %2 ]
ret i32 %27
}