#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 uint8_t;
typedef int uint64_t;

/* Variables and functions */
__attribute__((always_)) uint8_t iwbits_find_last_sbit64(uint64_t x) {
  uint8_t num = 63;
  if ((x & 0xffffffff00000000ULL) == 0) {
    num -= 32;
    x <<= 32;
  }
  if ((x & 0xffff000000000000ULL) == 0) {
    num -= 16;
    x <<= 16;
  }
  if ((x & 0xff00000000000000ULL) == 0) {
    num -= 8;
    x <<= 8;
  }
  if ((x & 0xf000000000000000ULL) == 0) {
    num -= 4;
    x <<= 4;
  }
  if ((x & 0xc000000000000000ULL) == 0) {
    num -= 2;
    x <<= 2;
  }
  if ((x & 0x8000000000000000ULL) == 0) {
    num -= 1;
  }
  return num;
}


x86 assembly (clang, -O0):
<iwbits_find_last_sbit64>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
movl   $0x3f,-0x8(%rbp)
movslq -0x4(%rbp),%rax
movabs $0xffffffff00000000,%rcx
ff ff ff
and    %rcx,%rax
cmp    $0x0,%rax
jne    113f <iwbits_find_last_sbit64+0x3f>
mov    -0x8(%rbp),%eax
sub    $0x20,%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x20,%ecx
shl    %cl,%eax
mov    %eax,-0x4(%rbp)
movslq -0x4(%rbp),%rax
movabs $0xffff000000000000,%rcx
and    %rcx,%rax
cmp    $0x0,%rax
jne    116c <iwbits_find_last_sbit64+0x6c>
mov    -0x8(%rbp),%eax
sub    $0x10,%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%eax
shl    $0x10,%eax
mov    %eax,-0x4(%rbp)
movslq -0x4(%rbp),%rax
movabs $0xff00000000000000,%rcx
and    %rcx,%rax
cmp    $0x0,%rax
jne    1199 <iwbits_find_last_sbit64+0x99>
mov    -0x8(%rbp),%eax
sub    $0x8,%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%eax
shl    $0x8,%eax
mov    %eax,-0x4(%rbp)
movslq -0x4(%rbp),%rax
movabs $0xf000000000000000,%rcx
and    %rcx,%rax
cmp    $0x0,%rax
jne    11c6 <iwbits_find_last_sbit64+0xc6>
mov    -0x8(%rbp),%eax
sub    $0x4,%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%eax
shl    $0x4,%eax
mov    %eax,-0x4(%rbp)
movslq -0x4(%rbp),%rax
movabs $0xc000000000000000,%rcx
and    %rcx,%rax
cmp    $0x0,%rax
jne    11f3 <iwbits_find_last_sbit64+0xf3>
mov    -0x8(%rbp),%eax
sub    $0x2,%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%eax
shl    $0x2,%eax
mov    %eax,-0x4(%rbp)
movslq -0x4(%rbp),%rax
movabs $0x8000000000000000,%rcx
and    %rcx,%rax
cmp    $0x0,%rax
jne    1217 <iwbits_find_last_sbit64+0x117>
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<iwbits_find_last_sbit64>:
mov    %edi,%eax
shl    $0x10,%eax
test   %edi,%edi
cmovs  %edi,%eax
mov    $0xf,%ecx
mov    $0x3f,%edx
cmovns %ecx,%edx
lea    -0x8(%rdx),%ecx
mov    %eax,%esi
shl    $0x8,%esi
test   %eax,%eax
cmovs  %eax,%esi
cmovs  %edx,%ecx
lea    -0x4(%rcx),%edx
mov    %esi,%edi
shl    $0x4,%edi
test   %esi,%esi
cmovs  %esi,%edi
cmovs  %ecx,%edx
lea    -0x2(%rdx),%ecx
lea    0x0(,%rdi,4),%eax
test   %edi,%edi
cmovs  %edi,%eax
cmovs  %edx,%ecx
sar    $0x1f,%eax
not    %eax
add    %ecx,%eax
ret

LLVM IR (-O0):
define i32 @iwbits_find_last_sbit64(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
store i32 63, i32* %3
%4 = load i32, i32* %2
%5 = sext i32 %4 to i64
%6 = and i64 %5, -4294967296
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %1
%9 = load i32, i32* %3
%10 = sub nsw i32 %9, 32
store i32 %10, i32* %3
%11 = load i32, i32* %2
%12 = shl i32 %11, 32
store i32 %12, i32* %2
br label %13
13: ; preds = %8, %1
%14 = load i32, i32* %2
%15 = sext i32 %14 to i64
%16 = and i64 %15, -281474976710656
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %13
%19 = load i32, i32* %3
%20 = sub nsw i32 %19, 16
store i32 %20, i32* %3
%21 = load i32, i32* %2
%22 = shl i32 %21, 16
store i32 %22, i32* %2
br label %23
23: ; preds = %18, %13
%24 = load i32, i32* %2
%25 = sext i32 %24 to i64
%26 = and i64 %25, -72057594037927936
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %23
%29 = load i32, i32* %3
%30 = sub nsw i32 %29, 8
store i32 %30, i32* %3
%31 = load i32, i32* %2
%32 = shl i32 %31, 8
store i32 %32, i32* %2
br label %33
33: ; preds = %28, %23
%34 = load i32, i32* %2
%35 = sext i32 %34 to i64
%36 = and i64 %35, -1152921504606846976
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %43
38: ; preds = %33
%39 = load i32, i32* %3
%40 = sub nsw i32 %39, 4
store i32 %40, i32* %3
%41 = load i32, i32* %2
%42 = shl i32 %41, 4
store i32 %42, i32* %2
br label %43
43: ; preds = %38, %33
%44 = load i32, i32* %2
%45 = sext i32 %44 to i64
%46 = and i64 %45, -4611686018427387904
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %53
48: ; preds = %43
%49 = load i32, i32* %3
%50 = sub nsw i32 %49, 2
store i32 %50, i32* %3
%51 = load i32, i32* %2
%52 = shl i32 %51, 2
store i32 %52, i32* %2
br label %53
53: ; preds = %48, %43
%54 = load i32, i32* %2
%55 = sext i32 %54 to i64
%56 = and i64 %55, -9223372036854775808
%57 = icmp eq i64 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %53
%59 = load i32, i32* %3
%60 = sub nsw i32 %59, 1
store i32 %60, i32* %3
br label %61
61: ; preds = %58, %53
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
define i32 @iwbits_find_last_sbit64(i32 %0) {
%2 = icmp sgt i32 %0, -1
%3 = shl i32 %0, 16
%4 = select i1 %2, i32 %3, i32 %0
%5 = select i1 %2, i32 15, i32 63
%6 = icmp sgt i32 %4, -1
%7 = add nsw i32 %5, -8
%8 = shl i32 %4, 8
%9 = select i1 %6, i32 %8, i32 %4
%10 = select i1 %6, i32 %7, i32 %5
%11 = icmp sgt i32 %9, -1
%12 = add nsw i32 %10, -4
%13 = shl i32 %9, 4
%14 = select i1 %11, i32 %13, i32 %9
%15 = select i1 %11, i32 %12, i32 %10
%16 = icmp sgt i32 %14, -1
%17 = add nsw i32 %15, -2
%18 = shl i32 %14, 2
%19 = select i1 %16, i32 %18, i32 %14
%20 = select i1 %16, i32 %17, i32 %15
%21 = ashr i32 %19, 31
%22 = xor i32 %21, -1
%23 = add nsw i32 %20, %22
ret i32 %23
}