int main() {
  volatile int a = 0x00000001;
  if (__builtin_ffs(a) != 1) {
    return 1;
  }
  volatile int b = 0x6000beef;
  if (__builtin_ffs(b) != 1) {
    return 1;
  }
  volatile int c = 0x8abc0000;
  if (__builtin_ffs(c) != 19) {
    return 1;
  }
  volatile int d = 0x00000000;
  if (__builtin_ffs(d) != 0) {
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x8(%rbp)
mov    -0x8(%rbp),%edx
bsf    %edx,%eax
add    $0x1,%eax
xor    %ecx,%ecx
cmp    $0x0,%edx
cmove  %ecx,%eax
cmp    $0x1,%eax
je     1138 <main+0x38>
movl   $0x1,-0x4(%rbp)
jmp    11c6 <main+0xc6>
movl   $0x6000beef,-0xc(%rbp)
mov    -0xc(%rbp),%edx
bsf    %edx,%eax
add    $0x1,%eax
xor    %ecx,%ecx
cmp    $0x0,%edx
cmove  %ecx,%eax
cmp    $0x1,%eax
je     1165 <main+0x65>
movl   $0x1,-0x4(%rbp)
jmp    11c6 <main+0xc6>
movl   $0x8abc0000,-0x10(%rbp)
mov    -0x10(%rbp),%edx
bsf    %edx,%eax
add    $0x1,%eax
xor    %ecx,%ecx
cmp    $0x0,%edx
cmove  %ecx,%eax
cmp    $0x13,%eax
je     1192 <main+0x92>
movl   $0x1,-0x4(%rbp)
jmp    11c6 <main+0xc6>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%edx
bsf    %edx,%eax
add    $0x1,%eax
xor    %ecx,%ecx
cmp    $0x0,%edx
cmove  %ecx,%eax
cmp    $0x0,%eax
je     11bf <main+0xbf>
movl   $0x1,-0x4(%rbp)
jmp    11c6 <main+0xc6>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
movl   $0x1,-0x4(%rsp)
mov    $0x1,%eax
testl  $0x1,-0x4(%rsp)
je     115e <main+0x5e>
movl   $0x6000beef,-0x8(%rsp)
60
mov    $0x1,%eax
testl  $0x1,-0x8(%rsp)
je     115e <main+0x5e>
movl   $0x8abc0000,-0xc(%rsp)
8a
mov    $0x7ffff,%ecx
and    -0xc(%rsp),%ecx
mov    $0x1,%eax
cmp    $0x40000,%ecx
jne    115e <main+0x5e>
movl   $0x0,-0x10(%rsp)
xor    %eax,%eax
cmpl   $0x0,-0x10(%rsp)
setne  %al
ret

LLVM IR (-O0):
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
store volatile i32 1, i32* %2
%6 = load volatile i32, i32* %2
%7 = call i32 @llvm.cttz.i32(i32 %6, i1 true)
%8 = add i32 %7, 1
%9 = icmp eq i32 %6, 0
%10 = select i1 %9, i32 0, i32 %8
%11 = icmp ne i32 %10, 1
br i1 %11, label %12, label %13
12: ; preds = %0
store i32 1, i32* %1
br label %38
13: ; preds = %0
store volatile i32 1610661615, i32* %3
%14 = load volatile i32, i32* %3
%15 = call i32 @llvm.cttz.i32(i32 %14, i1 true)
%16 = add i32 %15, 1
%17 = icmp eq i32 %14, 0
%18 = select i1 %17, i32 0, i32 %16
%19 = icmp ne i32 %18, 1
br i1 %19, label %20, label %21
20: ; preds = %13
store i32 1, i32* %1
br label %38
21: ; preds = %13
store volatile i32 -1967390720, i32* %4
%22 = load volatile i32, i32* %4
%23 = call i32 @llvm.cttz.i32(i32 %22, i1 true)
%24 = add i32 %23, 1
%25 = icmp eq i32 %22, 0
%26 = select i1 %25, i32 0, i32 %24
%27 = icmp ne i32 %26, 19
br i1 %27, label %28, label %29
28: ; preds = %21
store i32 1, i32* %1
br label %38
29: ; preds = %21
store volatile i32 0, i32* %5
%30 = load volatile i32, i32* %5
%31 = call i32 @llvm.cttz.i32(i32 %30, i1 true)
%32 = add i32 %31, 1
%33 = icmp eq i32 %30, 0
%34 = select i1 %33, i32 0, i32 %32
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %29
store i32 1, i32* %1
br label %38
37: ; preds = %29
store i32 0, i32* %1
br label %38
38: ; preds = %37, %36, %28, %20, %12
%39 = load i32, i32* %1
ret i32 %39
}

LLVM IR (-O2):
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %1 to i8*
store volatile i32 1, i32* %1
%6 = load volatile i32, i32* %1
%7 = and i32 %6, 1
%8 = icmp eq i32 %7, 0
br i1 %8, label %28, label %9
9: ; preds = %0
%10 = bitcast i32* %2 to i8*
store volatile i32 1610661615, i32* %2
%11 = load volatile i32, i32* %2
%12 = and i32 %11, 1
%13 = icmp eq i32 %12, 0
br i1 %13, label %26, label %14
14: ; preds = %9
%15 = bitcast i32* %3 to i8*
store volatile i32 -1967390720, i32* %3
%16 = load volatile i32, i32* %3
%17 = and i32 %16, 524287
%18 = icmp eq i32 %17, 262144
br i1 %18, label %19, label %24
19: ; preds = %14
%20 = bitcast i32* %4 to i8*
store volatile i32 0, i32* %4
%21 = load volatile i32, i32* %4
%22 = icmp ne i32 %21, 0
%23 = zext i1 %22 to i32
br label %24
24: ; preds = %14, %19
%25 = phi i32 [ %23, %19 ], [ 1, %14 ]
br label %26
26: ; preds = %9, %24
%27 = phi i32 [ %25, %24 ], [ 1, %9 ]
br label %28
28: ; preds = %0, %26
%29 = phi i32 [ %27, %26 ], [ 1, %0 ]
ret i32 %29
}