; LLVM IR from NishkarshRaj/Design-and-Analysis-of-Algorithms :: Brute Force Techniques (3)

; 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 @search(ptr noundef %0, ptr noundef %1, i32 noundef %2, i32 noundef %3) #0 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !9
  store i32 %3, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  store i32 -1, ptr %11, align 4, !tbaa !9
  store i32 0, ptr %9, align 4, !tbaa !9
  br label %12

12:                                               ; preds = %64, %4
  %13 = load i32, ptr %9, align 4, !tbaa !9
  %14 = load i32, ptr %7, align 4, !tbaa !9
  %15 = load i32, ptr %8, align 4, !tbaa !9
  %16 = sub nsw i32 %14, %15
  %17 = icmp slt i32 %13, %16
  br i1 %17, label %18, label %67

18:                                               ; preds = %12
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  %20 = load i32, ptr %9, align 4, !tbaa !9
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds i8, ptr %19, i64 %21
  %23 = load i8, ptr %22, align 1, !tbaa !11
  %24 = sext i8 %23 to i32
  %25 = load ptr, ptr %6, align 8, !tbaa !5
  %26 = getelementptr inbounds i8, ptr %25, i64 0
  %27 = load i8, ptr %26, align 1, !tbaa !11
  %28 = sext i8 %27 to i32
  %29 = icmp eq i32 %24, %28
  br i1 %29, label %30, label %57

30:                                               ; preds = %18
  store i32 0, ptr %10, align 4, !tbaa !9
  br label %31

31:                                               ; preds = %53, %30
  %32 = load i32, ptr %10, align 4, !tbaa !9
  %33 = load i32, ptr %8, align 4, !tbaa !9
  %34 = icmp slt i32 %32, %33
  br i1 %34, label %35, label %51

35:                                               ; preds = %31
  %36 = load ptr, ptr %5, align 8, !tbaa !5
  %37 = load i32, ptr %9, align 4, !tbaa !9
  %38 = load i32, ptr %10, align 4, !tbaa !9
  %39 = add nsw i32 %37, %38
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds i8, ptr %36, i64 %40
  %42 = load i8, ptr %41, align 1, !tbaa !11
  %43 = sext i8 %42 to i32
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  %45 = load i32, ptr %10, align 4, !tbaa !9
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds i8, ptr %44, i64 %46
  %48 = load i8, ptr %47, align 1, !tbaa !11
  %49 = sext i8 %48 to i32
  %50 = icmp eq i32 %43, %49
  br label %51

51:                                               ; preds = %35, %31
  %52 = phi i1 [ false, %31 ], [ %50, %35 ]
  br i1 %52, label %53, label %56

53:                                               ; preds = %51
  %54 = load i32, ptr %10, align 4, !tbaa !9
  %55 = add nsw i32 %54, 1
  store i32 %55, ptr %10, align 4, !tbaa !9
  br label %31, !llvm.loop !12

56:                                               ; preds = %51
  br label %57

57:                                               ; preds = %56, %18
  %58 = load i32, ptr %10, align 4, !tbaa !9
  %59 = load i32, ptr %8, align 4, !tbaa !9
  %60 = icmp eq i32 %58, %59
  br i1 %60, label %61, label %63

61:                                               ; preds = %57
  %62 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %62, ptr %11, align 4, !tbaa !9
  br label %63

63:                                               ; preds = %61, %57
  br label %64

64:                                               ; preds = %63
  %65 = load i32, ptr %9, align 4, !tbaa !9
  %66 = add nsw i32 %65, 1
  store i32 %66, ptr %9, align 4, !tbaa !9
  br label %12, !llvm.loop !14

67:                                               ; preds = %12
  %68 = load i32, ptr %11, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  ret i32 %68
}

; 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}


; 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: nofree norecurse nosync nounwind memory(argmem: read) uwtable
define dso_local i32 @search(ptr nocapture noundef readonly %0, ptr nocapture noundef readonly %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = sub nsw i32 %2, %3
  %6 = icmp sgt i32 %5, 0
  br i1 %6, label %.lr.ph27, label %._crit_edge

.lr.ph27:                                         ; preds = %4
  %7 = load i8, ptr %1, align 1, !tbaa !5
  %8 = icmp sgt i32 %3, 0
  %wide.trip.count40 = zext nneg i32 %5 to i64
  br i1 %8, label %.lr.ph27.split.us.preheader, label %.lr.ph27.split.preheader

.lr.ph27.split.preheader:                         ; preds = %.lr.ph27
  %9 = icmp eq i32 %3, 0
  %xtraiter = and i64 %wide.trip.count40, 7
  %10 = icmp ult i32 %5, 8
  br i1 %10, label %._crit_edge.loopexit48.unr-lcssa, label %.lr.ph27.split.preheader.new

.lr.ph27.split.preheader.new:                     ; preds = %.lr.ph27.split.preheader
  %unroll_iter = and i64 %wide.trip.count40, 2147483640
  br label %.lr.ph27.split

.lr.ph27.split.us.preheader:                      ; preds = %.lr.ph27
  %wide.trip.count35 = zext nneg i32 %3 to i64
  br label %.lr.ph27.split.us

.lr.ph27.split.us:                                ; preds = %.lr.ph27.split.us.preheader, %.critedge.us
  %indvars.iv37 = phi i64 [ 0, %.lr.ph27.split.us.preheader ], [ %indvars.iv.next38, %.critedge.us ]
  %.026.us = phi i32 [ -1, %.lr.ph27.split.us.preheader ], [ %spec.select.us, %.critedge.us ]
  %.01825.us = phi i32 [ undef, %.lr.ph27.split.us.preheader ], [ %.2.us, %.critedge.us ]
  %11 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv37
  %12 = load i8, ptr %11, align 1, !tbaa !5
  %13 = icmp eq i8 %12, %7
  br i1 %13, label %.preheader.us.preheader, label %.critedge.us

.preheader.us.preheader:                          ; preds = %.lr.ph27.split.us
  %invariant.gep = getelementptr inbounds i8, ptr %0, i64 %indvars.iv37
  br label %.preheader.us

.preheader.us:                                    ; preds = %.preheader.us.preheader, %21
  %indvars.iv32 = phi i64 [ 0, %.preheader.us.preheader ], [ %indvars.iv.next33, %21 ]
  %gep = getelementptr inbounds i8, ptr %invariant.gep, i64 %indvars.iv32
  %14 = load i8, ptr %gep, align 1, !tbaa !5
  %15 = getelementptr inbounds i8, ptr %1, i64 %indvars.iv32
  %16 = load i8, ptr %15, align 1, !tbaa !5
  %17 = icmp eq i8 %14, %16
  br i1 %17, label %21, label %.critedge.us.loopexit.split.loop.exit43

.critedge.us.loopexit.split.loop.exit43:          ; preds = %.preheader.us
  %18 = trunc nuw nsw i64 %indvars.iv32 to i32
  br label %.critedge.us

.critedge.us:                                     ; preds = %21, %.critedge.us.loopexit.split.loop.exit43, %.lr.ph27.split.us
  %.2.us = phi i32 [ %.01825.us, %.lr.ph27.split.us ], [ %18, %.critedge.us.loopexit.split.loop.exit43 ], [ %3, %21 ]
  %19 = icmp eq i32 %.2.us, %3
  %20 = trunc nuw nsw i64 %indvars.iv37 to i32
  %spec.select.us = select i1 %19, i32 %20, i32 %.026.us
  %indvars.iv.next38 = add nuw nsw i64 %indvars.iv37, 1
  %exitcond41.not = icmp eq i64 %indvars.iv.next38, %wide.trip.count40
  br i1 %exitcond41.not, label %._crit_edge, label %.lr.ph27.split.us, !llvm.loop !8

21:                                               ; preds = %.preheader.us
  %indvars.iv.next33 = add nuw nsw i64 %indvars.iv32, 1
  %exitcond36.not = icmp eq i64 %indvars.iv.next33, %wide.trip.count35
  br i1 %exitcond36.not, label %.critedge.us, label %.preheader.us, !llvm.loop !10

.lr.ph27.split:                                   ; preds = %.lr.ph27.split, %.lr.ph27.split.preheader.new
  %indvars.iv = phi i64 [ 0, %.lr.ph27.split.preheader.new ], [ %indvars.iv.next.7, %.lr.ph27.split ]
  %.026 = phi i32 [ -1, %.lr.ph27.split.preheader.new ], [ %spec.select.7, %.lr.ph27.split ]
  %niter = phi i64 [ 0, %.lr.ph27.split.preheader.new ], [ %niter.next.7, %.lr.ph27.split ]
  %22 = trunc i64 %indvars.iv to i32
  %23 = or disjoint i32 %22, 7
  %spec.select.7 = select i1 %9, i32 %23, i32 %.026
  %indvars.iv.next.7 = add nuw nsw i64 %indvars.iv, 8
  %niter.next.7 = add i64 %niter, 8
  %niter.ncmp.7 = icmp eq i64 %niter.next.7, %unroll_iter
  br i1 %niter.ncmp.7, label %._crit_edge.loopexit48.unr-lcssa, label %.lr.ph27.split, !llvm.loop !8

._crit_edge.loopexit48.unr-lcssa:                 ; preds = %.lr.ph27.split, %.lr.ph27.split.preheader
  %spec.select.lcssa.ph = phi i32 [ poison, %.lr.ph27.split.preheader ], [ %spec.select.7, %.lr.ph27.split ]
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph27.split.preheader ], [ %indvars.iv.next.7, %.lr.ph27.split ]
  %.026.unr = phi i32 [ -1, %.lr.ph27.split.preheader ], [ %spec.select.7, %.lr.ph27.split ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph27.split.epil

.lr.ph27.split.epil:                              ; preds = %._crit_edge.loopexit48.unr-lcssa, %.lr.ph27.split.epil
  %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %.lr.ph27.split.epil ], [ %indvars.iv.unr, %._crit_edge.loopexit48.unr-lcssa ]
  %.026.epil = phi i32 [ %spec.select.epil, %.lr.ph27.split.epil ], [ %.026.unr, %._crit_edge.loopexit48.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph27.split.epil ], [ 0, %._crit_edge.loopexit48.unr-lcssa ]
  %24 = trunc nuw nsw i64 %indvars.iv.epil to i32
  %spec.select.epil = select i1 %9, i32 %24, i32 %.026.epil
  %indvars.iv.next.epil = add nuw nsw i64 %indvars.iv.epil, 1
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge, label %.lr.ph27.split.epil, !llvm.loop !11

._crit_edge:                                      ; preds = %._crit_edge.loopexit48.unr-lcssa, %.lr.ph27.split.epil, %.critedge.us, %4
  %.0.lcssa = phi i32 [ -1, %4 ], [ %spec.select.us, %.critedge.us ], [ %spec.select.lcssa.ph, %._crit_edge.loopexit48.unr-lcssa ], [ %spec.select.epil, %.lr.ph27.split.epil ]
  ret i32 %.0.lcssa
}

attributes #0 = { nofree norecurse nosync nounwind memory(argmem: read) 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}
!10 = distinct !{!10, !9}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.unroll.disable"}


; Effective O3 passes: BDCEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SCCPPass,SROAPass,SimplifyCFGPass