int cnt_trailing_cleared_bits(int num) {
  int i;
  int cnt = 0;
  for (i = 0; (i <= 31) && ((~(num >> i) & 1)); i++) {
    cnt++;
  }
  return cnt;
}


x86 assembly (clang, -O0):
<cnt_trailing_cleared_bits>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x8(%rbp)
xor    %eax,%eax
cmpl   $0x1f,-0x8(%rbp)
mov    %al,-0xd(%rbp)
jg     113b <cnt_trailing_cleared_bits+0x3b>
mov    -0x4(%rbp),%eax
mov    -0x8(%rbp),%ecx
sar    %cl,%eax
xor    $0xffffffff,%eax
and    $0x1,%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0xd(%rbp)
mov    -0xd(%rbp),%al
test   $0x1,%al
jne    114b <cnt_trailing_cleared_bits+0x4b>
jmp    1162 <cnt_trailing_cleared_bits+0x62>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1115 <cnt_trailing_cleared_bits+0x15>
mov    -0xc(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<cnt_trailing_cleared_bits>:
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
bt     %eax,%edi
jb     114b <cnt_trailing_cleared_bits+0x4b>
lea    0x1(%rax),%ecx
movzbl %cl,%ecx
bt     %ecx,%edi
jb     1144 <cnt_trailing_cleared_bits+0x44>
lea    0x2(%rax),%ecx
movzbl %cl,%ecx
bt     %ecx,%edi
jb     1148 <cnt_trailing_cleared_bits+0x48>
lea    0x3(%rax),%ecx
movzbl %cl,%ecx
bt     %ecx,%edi
jb     114c <cnt_trailing_cleared_bits+0x4c>
add    $0x4,%eax
cmp    $0x20,%eax
jne    1110 <cnt_trailing_cleared_bits+0x10>
mov    $0x20,%eax
ret
add    $0x1,%eax
ret
add    $0x2,%eax
ret
add    $0x3,%eax
ret

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

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