int __count_leading_ones__(char ch) {
  int i;
  for (i = 7; i >= 0; i--) {
    if ((ch & (1 << i)) == 0) {
      return 7 - i;
    }
  }
  return 8;
}


x86 assembly (clang, -O0):
<__count_leading_ones__>:
push   %rbp
mov    %rsp,%rbp
mov    %dil,%al
mov    %al,-0x5(%rbp)
movl   $0x7,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     1159 <__count_leading_ones__+0x59>
movsbl -0x5(%rbp),%eax
mov    -0xc(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
jne    1146 <__count_leading_ones__+0x46>
mov    $0x7,%eax
sub    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1160 <__count_leading_ones__+0x60>
jmp    114b <__count_leading_ones__+0x4b>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    1111 <__count_leading_ones__+0x11>
movl   $0x8,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<__count_leading_ones__>:
xor    %eax,%eax
test   $0x80,%dil
je     1154 <__count_leading_ones__+0x54>
mov    $0x1,%eax
test   $0x40,%dil
je     1154 <__count_leading_ones__+0x54>
mov    $0x2,%eax
test   $0x20,%dil
je     1154 <__count_leading_ones__+0x54>
mov    $0x3,%eax
test   $0x10,%dil
je     1154 <__count_leading_ones__+0x54>
mov    $0x4,%eax
test   $0x8,%dil
je     1154 <__count_leading_ones__+0x54>
mov    $0x5,%eax
test   $0x4,%dil
je     1154 <__count_leading_ones__+0x54>
mov    $0x6,%eax
test   $0x2,%dil
je     1154 <__count_leading_ones__+0x54>
movzbl %dil,%eax
and    $0x1,%eax
add    $0x7,%eax
ret

LLVM IR (-O0):
define i32 @__count_leading_ones__(i8 signext %0) {
%2 = alloca i32
%3 = alloca i8
%4 = alloca i32
store i8 %0, i8* %3
store i32 7, i32* %4
br label %5
5: ; preds = %19, %1
%6 = load i32, i32* %4
%7 = icmp sge i32 %6, 0
br i1 %7, label %8, label %22
8: ; preds = %5
%9 = load i8, i8* %3
%10 = sext i8 %9 to i32
%11 = load i32, i32* %4
%12 = shl i32 1, %11
%13 = and i32 %10, %12
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %8
%16 = load i32, i32* %4
%17 = sub nsw i32 7, %16
store i32 %17, i32* %2
br label %23
18: ; preds = %8
br label %19
19: ; preds = %18
%20 = load i32, i32* %4
%21 = add nsw i32 %20, -1
store i32 %21, i32* %4
br label %5
22: ; preds = %5
store i32 8, i32* %2
br label %23
23: ; preds = %22, %15
%24 = load i32, i32* %2
ret i32 %24
}

LLVM IR (-O2):
define i32 @__count_leading_ones__(i8 signext %0) {
%2 = zext i8 %0 to i32
%3 = and i32 %2, 128
%4 = icmp eq i32 %3, 0
br i1 %4, label %27, label %5
5: ; preds = %1
%6 = and i32 %2, 64
%7 = icmp eq i32 %6, 0
br i1 %7, label %27, label %8
8: ; preds = %5
%9 = and i32 %2, 32
%10 = icmp eq i32 %9, 0
br i1 %10, label %27, label %11
11: ; preds = %8
%12 = and i32 %2, 16
%13 = icmp eq i32 %12, 0
br i1 %13, label %27, label %14
14: ; preds = %11
%15 = and i32 %2, 8
%16 = icmp eq i32 %15, 0
br i1 %16, label %27, label %17
17: ; preds = %14
%18 = and i32 %2, 4
%19 = icmp eq i32 %18, 0
br i1 %19, label %27, label %20
20: ; preds = %17
%21 = and i32 %2, 2
%22 = icmp eq i32 %21, 0
br i1 %22, label %27, label %23
23: ; preds = %20
%24 = and i32 %2, 1
%25 = icmp eq i32 %24, 0
%26 = select i1 %25, i32 7, i32 8
br label %27
27: ; preds = %23, %1, %5, %8, %11, %14, %17, %20
%28 = phi i32 [ 0, %1 ], [ 1, %5 ], [ 2, %8 ], [ 3, %11 ], [ 4, %14 ], [ 5, %17 ], [ 6, %20 ], [ %26, %23 ]
ret i32 %28
}