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

; 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 @fls(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 %26

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 = %20, %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 %25

14:                                               ; preds = %10
  %15 = load i32, ptr %4, align 4, !tbaa !5
  %16 = icmp eq i32 %15, 1
  br i1 %16, label %17, label %19

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

19:                                               ; preds = %14
  br label %20

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

25:                                               ; preds = %17, %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 %26

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

; 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 @fls(i32 noundef %0) local_unnamed_addr #0 {
  %switch = icmp ult i32 %0, 2
  br i1 %switch, label %.loopexit, label %.preheader.1

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

.loopexit:                                        ; preds = %1, %.preheader.30, %.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 = phi i32 [ 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 ], [ %0, %1 ]
  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