; LLVM IR from sp-nitech/SPTK :: third_party/REAPER/wave/codec_riff.ll (ZNK4sptk6reaper9UlawCodec13SegmentSearchEsPKsi)

; 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef signext i16 @_ZNK4sptk6reaper9UlawCodec13SegmentSearchEsPKsi(ptr noundef nonnull align 1 dereferenceable(1) %0, i16 noundef signext %1, ptr noundef %2, i32 noundef %3) #1 align 2 {
  %5 = alloca i16, align 2
  %6 = alloca ptr, align 8
  %7 = alloca i16, align 2
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i16 %1, ptr %7, align 2, !tbaa !9
  store ptr %2, ptr %8, align 8, !tbaa !5
  store i32 %3, ptr %9, align 4, !tbaa !11
  %12 = load ptr, ptr %6, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  store i32 0, ptr %10, align 4, !tbaa !11
  br label %13

13:                                               ; preds = %30, %4
  %14 = load i32, ptr %10, align 4, !tbaa !11
  %15 = load i32, ptr %9, align 4, !tbaa !11
  %16 = icmp slt i32 %14, %15
  br i1 %16, label %18, label %17

17:                                               ; preds = %13
  store i32 2, ptr %11, align 4
  br label %33

18:                                               ; preds = %13
  %19 = load i16, ptr %7, align 2, !tbaa !9
  %20 = sext i16 %19 to i32
  %21 = load ptr, ptr %8, align 8, !tbaa !5
  %22 = getelementptr inbounds i16, ptr %21, i32 1
  store ptr %22, ptr %8, align 8, !tbaa !5
  %23 = load i16, ptr %21, align 2, !tbaa !9
  %24 = sext i16 %23 to i32
  %25 = icmp sle i32 %20, %24
  br i1 %25, label %26, label %29

26:                                               ; preds = %18
  %27 = load i32, ptr %10, align 4, !tbaa !11
  %28 = trunc i32 %27 to i16
  store i16 %28, ptr %5, align 2
  store i32 1, ptr %11, align 4
  br label %33

29:                                               ; preds = %18
  br label %30

30:                                               ; preds = %29
  %31 = load i32, ptr %10, align 4, !tbaa !11
  %32 = add nsw i32 %31, 1
  store i32 %32, ptr %10, align 4, !tbaa !11
  br label %13, !llvm.loop !13

33:                                               ; preds = %26, %17
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  %34 = load i32, ptr %11, align 4
  switch i32 %34, label %40 [
    i32 2, label %35
    i32 1, label %38
  ]

35:                                               ; preds = %33
  %36 = load i32, ptr %9, align 4, !tbaa !11
  %37 = trunc i32 %36 to i16
  store i16 %37, ptr %5, align 2
  br label %38

38:                                               ; preds = %35, %33
  %39 = load i16, ptr %5, align 2
  ret i16 %39

40:                                               ; preds = %33
  unreachable
}

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 #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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"short", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"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(read, inaccessiblemem: none) uwtable
define dso_local noundef signext i16 @_ZNK4sptk6reaper9UlawCodec13SegmentSearchEsPKsi(ptr nocapture noundef nonnull readnone align 1 dereferenceable(1) %0, i16 noundef signext %1, ptr nocapture noundef readonly %2, i32 noundef %3) local_unnamed_addr #0 align 2 {
  %.not1216 = icmp sgt i32 %3, 0
  br i1 %.not1216, label %.lr.ph.preheader, label %.loopexit.thread

.lr.ph.preheader:                                 ; preds = %4
  %5 = load i16, ptr %2, align 2, !tbaa !5
  %.not31 = icmp slt i16 %5, %1
  br i1 %.not31, label %.lr.ph34, label %10

.loopexit.thread:                                 ; preds = %4
  %6 = trunc i32 %3 to i16
  br label %14

.lr.ph:                                           ; preds = %.lr.ph34
  %7 = getelementptr inbounds i8, ptr %.0111733, i64 2
  %8 = load i16, ptr %7, align 2, !tbaa !5
  %.not = icmp slt i16 %8, %1
  br i1 %.not, label %.lr.ph34, label %.lr.ph._crit_edge, !llvm.loop !9

.lr.ph._crit_edge:                                ; preds = %.lr.ph
  %.not12 = icmp slt i32 %12, %3
  %9 = trunc i32 %12 to i16
  br label %10

10:                                               ; preds = %.lr.ph._crit_edge, %.lr.ph.preheader
  %.not1219.lcssa = phi i1 [ %.not12, %.lr.ph._crit_edge ], [ true, %.lr.ph.preheader ]
  %.0918.lcssa = phi i16 [ %9, %.lr.ph._crit_edge ], [ 0, %.lr.ph.preheader ]
  %11 = trunc i32 %3 to i16
  %spec.select = select i1 %.not1219.lcssa, i16 %.0918.lcssa, i16 %11
  br label %14

.lr.ph34:                                         ; preds = %.lr.ph.preheader, %.lr.ph
  %.0111733 = phi ptr [ %7, %.lr.ph ], [ %2, %.lr.ph.preheader ]
  %.091832 = phi i32 [ %12, %.lr.ph ], [ 0, %.lr.ph.preheader ]
  %12 = add i32 %.091832, 1
  %exitcond.not = icmp eq i32 %12, %3
  br i1 %exitcond.not, label %.loopexit, label %.lr.ph, !llvm.loop !9

.loopexit:                                        ; preds = %.lr.ph34
  %13 = trunc i32 %3 to i16
  br label %14

14:                                               ; preds = %10, %.loopexit, %.loopexit.thread
  %15 = phi i16 [ %13, %.loopexit ], [ %6, %.loopexit.thread ], [ %spec.select, %10 ]
  ret i16 %15
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, inaccessiblemem: 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)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"short", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass