; LLVM IR from apache/nuttx :: libs/libc/string (lib_ffs)

; Unoptimized IR (clang -disable-llvm-passes):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: nounwind uwtable
define dso_local i32 @ffs(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  store i32 0, ptr %3, align 4, !tbaa !5
  %6 = load i32, ptr %2, align 4, !tbaa !5
  %7 = icmp ne i32 %6, 0
  br i1 %7, label %8, label %27

8:                                                ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  %9 = load i32, ptr %2, align 4, !tbaa !5
  store i32 %9, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  store i32 1, ptr %5, align 4, !tbaa !5
  br label %10

10:                                               ; preds = %21, %8
  %11 = load i32, ptr %5, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = icmp ule i64 %12, 32
  br i1 %13, label %14, label %26

14:                                               ; preds = %10
  %15 = load i32, ptr %4, align 4, !tbaa !5
  %16 = and i32 %15, 1
  %17 = icmp ne i32 %16, 0
  br i1 %17, label %18, label %20

18:                                               ; preds = %14
  %19 = load i32, ptr %5, align 4, !tbaa !5
  store i32 %19, ptr %3, align 4, !tbaa !5
  br label %26

20:                                               ; preds = %14
  br label %21

21:                                               ; preds = %20
  %22 = load i32, ptr %5, align 4, !tbaa !5
  %23 = add nsw i32 %22, 1
  store i32 %23, ptr %5, align 4, !tbaa !5
  %24 = load i32, ptr %4, align 4, !tbaa !5
  %25 = lshr i32 %24, 1
  store i32 %25, ptr %4, align 4, !tbaa !5
  br label %10, !llvm.loop !9

26:                                               ; preds = %18, %10
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  br label %27

27:                                               ; preds = %26, %1
  %28 = load i32, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  ret i32 %28
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

attributes #0 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nounwind }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


; Optimized IR (opt -passes='default<O3>'):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none) uwtable
define dso_local noundef i32 @ffs(i32 noundef %0) local_unnamed_addr #0 {
  %.not = icmp eq i32 %0, 0
  br i1 %.not, label %.loopexit, label %.preheader.preheader

.preheader.preheader:                             ; preds = %1
  %2 = and i32 %0, 1
  %.not11 = icmp eq i32 %2, 0
  br i1 %.not11, label %.preheader.1, label %.loopexit

.preheader.1:                                     ; preds = %.preheader.preheader
  %3 = and i32 %0, 2
  %.not11.1 = icmp eq i32 %3, 0
  br i1 %.not11.1, label %.preheader.2, label %.loopexit

.preheader.2:                                     ; preds = %.preheader.1
  %4 = and i32 %0, 4
  %.not11.2 = icmp eq i32 %4, 0
  br i1 %.not11.2, label %.preheader.3, label %.loopexit

.preheader.3:                                     ; preds = %.preheader.2
  %5 = and i32 %0, 8
  %.not11.3 = icmp eq i32 %5, 0
  br i1 %.not11.3, label %.preheader.4, label %.loopexit

.preheader.4:                                     ; preds = %.preheader.3
  %6 = and i32 %0, 16
  %.not11.4 = icmp eq i32 %6, 0
  br i1 %.not11.4, label %.preheader.5, label %.loopexit

.preheader.5:                                     ; preds = %.preheader.4
  %7 = and i32 %0, 32
  %.not11.5 = icmp eq i32 %7, 0
  br i1 %.not11.5, label %.preheader.6, label %.loopexit

.preheader.6:                                     ; preds = %.preheader.5
  %8 = and i32 %0, 64
  %.not11.6 = icmp eq i32 %8, 0
  br i1 %.not11.6, label %.preheader.7, label %.loopexit

.preheader.7:                                     ; preds = %.preheader.6
  %9 = and i32 %0, 128
  %.not11.7 = icmp eq i32 %9, 0
  br i1 %.not11.7, label %.preheader.8, label %.loopexit

.preheader.8:                                     ; preds = %.preheader.7
  %10 = and i32 %0, 256
  %.not11.8 = icmp eq i32 %10, 0
  br i1 %.not11.8, label %.preheader.9, label %.loopexit

.preheader.9:                                     ; preds = %.preheader.8
  %11 = and i32 %0, 512
  %.not11.9 = icmp eq i32 %11, 0
  br i1 %.not11.9, label %.preheader.10, label %.loopexit

.preheader.10:                                    ; preds = %.preheader.9
  %12 = and i32 %0, 1024
  %.not11.10 = icmp eq i32 %12, 0
  br i1 %.not11.10, label %.preheader.11, label %.loopexit

.preheader.11:                                    ; preds = %.preheader.10
  %13 = and i32 %0, 2048
  %.not11.11 = icmp eq i32 %13, 0
  br i1 %.not11.11, label %.preheader.12, label %.loopexit

.preheader.12:                                    ; preds = %.preheader.11
  %14 = and i32 %0, 4096
  %.not11.12 = icmp eq i32 %14, 0
  br i1 %.not11.12, label %.preheader.13, label %.loopexit

.preheader.13:                                    ; preds = %.preheader.12
  %15 = and i32 %0, 8192
  %.not11.13 = icmp eq i32 %15, 0
  br i1 %.not11.13, label %.preheader.14, label %.loopexit

.preheader.14:                                    ; preds = %.preheader.13
  %16 = and i32 %0, 16384
  %.not11.14 = icmp eq i32 %16, 0
  br i1 %.not11.14, label %.preheader.15, label %.loopexit

.preheader.15:                                    ; preds = %.preheader.14
  %17 = and i32 %0, 32768
  %.not11.15 = icmp eq i32 %17, 0
  br i1 %.not11.15, label %.preheader.16, label %.loopexit

.preheader.16:                                    ; preds = %.preheader.15
  %18 = and i32 %0, 65536
  %.not11.16 = icmp eq i32 %18, 0
  br i1 %.not11.16, label %.preheader.17, label %.loopexit

.preheader.17:                                    ; preds = %.preheader.16
  %19 = and i32 %0, 131072
  %.not11.17 = icmp eq i32 %19, 0
  br i1 %.not11.17, label %.preheader.18, label %.loopexit

.preheader.18:                                    ; preds = %.preheader.17
  %20 = and i32 %0, 262144
  %.not11.18 = icmp eq i32 %20, 0
  br i1 %.not11.18, label %.preheader.19, label %.loopexit

.preheader.19:                                    ; preds = %.preheader.18
  %21 = and i32 %0, 524288
  %.not11.19 = icmp eq i32 %21, 0
  br i1 %.not11.19, label %.preheader.20, label %.loopexit

.preheader.20:                                    ; preds = %.preheader.19
  %22 = and i32 %0, 1048576
  %.not11.20 = icmp eq i32 %22, 0
  br i1 %.not11.20, label %.preheader.21, label %.loopexit

.preheader.21:                                    ; preds = %.preheader.20
  %23 = and i32 %0, 2097152
  %.not11.21 = icmp eq i32 %23, 0
  br i1 %.not11.21, label %.preheader.22, label %.loopexit

.preheader.22:                                    ; preds = %.preheader.21
  %24 = and i32 %0, 4194304
  %.not11.22 = icmp eq i32 %24, 0
  br i1 %.not11.22, label %.preheader.23, label %.loopexit

.preheader.23:                                    ; preds = %.preheader.22
  %25 = and i32 %0, 8388608
  %.not11.23 = icmp eq i32 %25, 0
  br i1 %.not11.23, label %.preheader.24, label %.loopexit

.preheader.24:                                    ; preds = %.preheader.23
  %26 = and i32 %0, 16777216
  %.not11.24 = icmp eq i32 %26, 0
  br i1 %.not11.24, label %.preheader.25, label %.loopexit

.preheader.25:                                    ; preds = %.preheader.24
  %27 = and i32 %0, 33554432
  %.not11.25 = icmp eq i32 %27, 0
  br i1 %.not11.25, label %.preheader.26, label %.loopexit

.preheader.26:                                    ; preds = %.preheader.25
  %28 = and i32 %0, 67108864
  %.not11.26 = icmp eq i32 %28, 0
  br i1 %.not11.26, label %.preheader.27, label %.loopexit

.preheader.27:                                    ; preds = %.preheader.26
  %29 = and i32 %0, 134217728
  %.not11.27 = icmp eq i32 %29, 0
  br i1 %.not11.27, label %.preheader.28, label %.loopexit

.preheader.28:                                    ; preds = %.preheader.27
  %30 = and i32 %0, 268435456
  %.not11.28 = icmp eq i32 %30, 0
  br i1 %.not11.28, label %.preheader.29, label %.loopexit

.preheader.29:                                    ; preds = %.preheader.28
  %31 = and i32 %0, 536870912
  %.not11.29 = icmp eq i32 %31, 0
  br i1 %.not11.29, label %.preheader.30, label %.loopexit

.preheader.30:                                    ; preds = %.preheader.29
  %32 = and i32 %0, 1073741824
  %.not11.30 = icmp eq i32 %32, 0
  %spec.select = select i1 %.not11.30, i32 32, i32 31
  br label %.loopexit

.loopexit:                                        ; preds = %.preheader.30, %.preheader.preheader, %.preheader.1, %.preheader.2, %.preheader.3, %.preheader.4, %.preheader.5, %.preheader.6, %.preheader.7, %.preheader.8, %.preheader.9, %.preheader.10, %.preheader.11, %.preheader.12, %.preheader.13, %.preheader.14, %.preheader.15, %.preheader.16, %.preheader.17, %.preheader.18, %.preheader.19, %.preheader.20, %.preheader.21, %.preheader.22, %.preheader.23, %.preheader.24, %.preheader.25, %.preheader.26, %.preheader.27, %.preheader.28, %.preheader.29, %1
  %.1 = phi i32 [ 0, %1 ], [ 1, %.preheader.preheader ], [ 2, %.preheader.1 ], [ 3, %.preheader.2 ], [ 4, %.preheader.3 ], [ 5, %.preheader.4 ], [ 6, %.preheader.5 ], [ 7, %.preheader.6 ], [ 8, %.preheader.7 ], [ 9, %.preheader.8 ], [ 10, %.preheader.9 ], [ 11, %.preheader.10 ], [ 12, %.preheader.11 ], [ 13, %.preheader.12 ], [ 14, %.preheader.13 ], [ 15, %.preheader.14 ], [ 16, %.preheader.15 ], [ 17, %.preheader.16 ], [ 18, %.preheader.17 ], [ 19, %.preheader.18 ], [ 20, %.preheader.19 ], [ 21, %.preheader.20 ], [ 22, %.preheader.21 ], [ 23, %.preheader.22 ], [ 24, %.preheader.23 ], [ 25, %.preheader.24 ], [ 26, %.preheader.25 ], [ 27, %.preheader.26 ], [ 28, %.preheader.27 ], [ 29, %.preheader.28 ], [ 30, %.preheader.29 ], [ %spec.select, %.preheader.30 ]
  ret i32 %.1
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(none) uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}


; Effective O3 passes: GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass