; LLVM IR from devparthgarg/Data-Structures-and-Algorithms :: 06. Searching/13 Count Triplet with Given Sum (13a count triplets with given sum)

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

12:                                               ; preds = %65, %3
  %13 = load i32, ptr %8, align 4, !tbaa !9
  %14 = load i32, ptr %5, align 4, !tbaa !9
  %15 = icmp slt i32 %13, %14
  br i1 %15, label %17, label %16

16:                                               ; preds = %12
  store i32 2, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  br label %68

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

20:                                               ; preds = %61, %17
  %21 = load i32, ptr %10, align 4, !tbaa !9
  %22 = load i32, ptr %5, align 4, !tbaa !9
  %23 = icmp slt i32 %21, %22
  br i1 %23, label %25, label %24

24:                                               ; preds = %20
  store i32 5, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  br label %64

25:                                               ; preds = %20
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  %26 = load i32, ptr %10, align 4, !tbaa !9
  %27 = add nsw i32 %26, 1
  store i32 %27, ptr %11, align 4, !tbaa !9
  br label %28

28:                                               ; preds = %57, %25
  %29 = load i32, ptr %11, align 4, !tbaa !9
  %30 = load i32, ptr %5, align 4, !tbaa !9
  %31 = icmp slt i32 %29, %30
  br i1 %31, label %33, label %32

32:                                               ; preds = %28
  store i32 8, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  br label %60

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

53:                                               ; preds = %33
  %54 = load i32, ptr %7, align 4, !tbaa !9
  %55 = add nsw i32 %54, 1
  store i32 %55, ptr %7, align 4, !tbaa !9
  br label %56

56:                                               ; preds = %53, %33
  br label %57

57:                                               ; preds = %56
  %58 = load i32, ptr %11, align 4, !tbaa !9
  %59 = add nsw i32 %58, 1
  store i32 %59, ptr %11, align 4, !tbaa !9
  br label %28, !llvm.loop !11

60:                                               ; preds = %32
  br label %61

61:                                               ; preds = %60
  %62 = load i32, ptr %10, align 4, !tbaa !9
  %63 = add nsw i32 %62, 1
  store i32 %63, ptr %10, align 4, !tbaa !9
  br label %20, !llvm.loop !13

64:                                               ; preds = %24
  br label %65

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

68:                                               ; preds = %16
  %69 = load i32, ptr %7, align 4, !tbaa !9
  store i32 1, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  ret i32 %69
}

; 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 = { 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 #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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}
!14 = distinct !{!14, !12}


; 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(argmem: read) uwtable
define dso_local noundef i32 @_Z8threeSumPiii(ptr nocapture noundef readonly %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = icmp sgt i32 %1, 0
  br i1 %4, label %.lr.ph32.preheader, label %._crit_edge

.lr.ph32.preheader:                               ; preds = %3
  %5 = zext nneg i32 %1 to i64
  %wide.trip.count47 = zext nneg i32 %1 to i64
  %6 = add nsw i32 %1, -3
  %broadcast.splatinsert51 = insertelement <4 x i32> poison, i32 %2, i64 0
  %broadcast.splat52 = shufflevector <4 x i32> %broadcast.splatinsert51, <4 x i32> poison, <4 x i32> zeroinitializer
  br label %.lr.ph32

.loopexit23:                                      ; preds = %.loopexit, %.lr.ph32
  %.1.lcssa = phi i32 [ %.02130, %.lr.ph32 ], [ %.2.lcssa, %.loopexit ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %indvars.iv.next40 = add nuw nsw i64 %indvars.iv39, 1
  %exitcond48.not = icmp eq i64 %indvars.iv.next45, %wide.trip.count47
  br i1 %exitcond48.not, label %._crit_edge, label %.lr.ph32, !llvm.loop !5

._crit_edge:                                      ; preds = %.loopexit23, %3
  %.021.lcssa = phi i32 [ 0, %3 ], [ %.1.lcssa, %.loopexit23 ]
  ret i32 %.021.lcssa

.lr.ph32:                                         ; preds = %.lr.ph32.preheader, %.loopexit23
  %indvars.iv44 = phi i64 [ 0, %.lr.ph32.preheader ], [ %indvars.iv.next45, %.loopexit23 ]
  %indvars.iv39 = phi i64 [ 1, %.lr.ph32.preheader ], [ %indvars.iv.next40, %.loopexit23 ]
  %indvars.iv = phi i64 [ 2, %.lr.ph32.preheader ], [ %indvars.iv.next, %.loopexit23 ]
  %.02130 = phi i32 [ 0, %.lr.ph32.preheader ], [ %.1.lcssa, %.loopexit23 ]
  %7 = trunc i64 %indvars.iv44 to i32
  %indvars.iv.next45 = add nuw nsw i64 %indvars.iv44, 1
  %8 = icmp ult i64 %indvars.iv.next45, %5
  br i1 %8, label %.lr.ph28, label %.loopexit23

.lr.ph28:                                         ; preds = %.lr.ph32
  %9 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv44
  br label %10

.loopexit:                                        ; preds = %scalar.ph, %middle.block, %10
  %.2.lcssa = phi i32 [ %.126, %10 ], [ %34, %middle.block ], [ %spec.select, %scalar.ph ]
  %indvars.iv.next35 = add nuw nsw i64 %indvars.iv34, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next42, %wide.trip.count47
  %indvar.next = add i32 %indvar, 1
  br i1 %exitcond.not, label %.loopexit23, label %10, !llvm.loop !7

10:                                               ; preds = %.lr.ph28, %.loopexit
  %indvar = phi i32 [ 0, %.lr.ph28 ], [ %indvar.next, %.loopexit ]
  %indvars.iv41 = phi i64 [ %indvars.iv39, %.lr.ph28 ], [ %indvars.iv.next42, %.loopexit ]
  %indvars.iv34 = phi i64 [ %indvars.iv, %.lr.ph28 ], [ %indvars.iv.next35, %.loopexit ]
  %.126 = phi i32 [ %.02130, %.lr.ph28 ], [ %.2.lcssa, %.loopexit ]
  %11 = add i32 %indvar, %7
  %12 = sub i32 %6, %11
  %13 = zext i32 %12 to i64
  %14 = add nuw nsw i64 %13, 1
  %indvars.iv.next42 = add nuw nsw i64 %indvars.iv41, 1
  %15 = trunc nuw i64 %indvars.iv.next42 to i32
  %16 = icmp slt i32 %15, %1
  br i1 %16, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %10
  %17 = load i32, ptr %9, align 4, !tbaa !8
  %18 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv41
  %19 = load i32, ptr %18, align 4, !tbaa !8
  %20 = add nsw i32 %19, %17
  %min.iters.check = icmp ult i32 %12, 7
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph
  %n.vec = and i64 %14, 8589934584
  %ind.end = add nuw i64 %indvars.iv34, %n.vec
  %21 = insertelement <4 x i32> <i32 poison, i32 0, i32 0, i32 0>, i32 %.126, i64 0
  %broadcast.splatinsert = insertelement <4 x i32> poison, i32 %20, i64 0
  %broadcast.splat = shufflevector <4 x i32> %broadcast.splatinsert, <4 x i32> poison, <4 x i32> zeroinitializer
  %22 = getelementptr i32, ptr %0, i64 %indvars.iv34
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <4 x i32> [ %21, %vector.ph ], [ %31, %vector.body ]
  %vec.phi49 = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %32, %vector.body ]
  %23 = getelementptr i32, ptr %22, i64 %index
  %24 = getelementptr inbounds i8, ptr %23, i64 16
  %wide.load = load <4 x i32>, ptr %23, align 4, !tbaa !8
  %wide.load50 = load <4 x i32>, ptr %24, align 4, !tbaa !8
  %25 = add nsw <4 x i32> %broadcast.splat, %wide.load
  %26 = add nsw <4 x i32> %broadcast.splat, %wide.load50
  %27 = icmp eq <4 x i32> %25, %broadcast.splat52
  %28 = icmp eq <4 x i32> %26, %broadcast.splat52
  %29 = zext <4 x i1> %27 to <4 x i32>
  %30 = zext <4 x i1> %28 to <4 x i32>
  %31 = add <4 x i32> %vec.phi, %29
  %32 = add <4 x i32> %vec.phi49, %30
  %index.next = add nuw i64 %index, 8
  %33 = icmp eq i64 %index.next, %n.vec
  br i1 %33, label %middle.block, label %vector.body, !llvm.loop !12

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <4 x i32> %32, %31
  %34 = tail call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx)
  %cmp.n = icmp eq i64 %14, %n.vec
  br i1 %cmp.n, label %.loopexit, label %scalar.ph.preheader

scalar.ph.preheader:                              ; preds = %middle.block, %.lr.ph
  %indvars.iv36.ph = phi i64 [ %indvars.iv34, %.lr.ph ], [ %ind.end, %middle.block ]
  %.224.ph = phi i32 [ %.126, %.lr.ph ], [ %34, %middle.block ]
  br label %scalar.ph

scalar.ph:                                        ; preds = %scalar.ph.preheader, %scalar.ph
  %indvars.iv36 = phi i64 [ %indvars.iv.next37, %scalar.ph ], [ %indvars.iv36.ph, %scalar.ph.preheader ]
  %.224 = phi i32 [ %spec.select, %scalar.ph ], [ %.224.ph, %scalar.ph.preheader ]
  %35 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv36
  %36 = load i32, ptr %35, align 4, !tbaa !8
  %37 = add nsw i32 %20, %36
  %38 = icmp eq i32 %37, %2
  %39 = zext i1 %38 to i32
  %spec.select = add nsw i32 %.224, %39
  %indvars.iv.next37 = add nuw nsw i64 %indvars.iv36, 1
  %40 = trunc nuw i64 %indvars.iv.next37 to i32
  %41 = icmp slt i32 %40, %1
  br i1 %41, label %scalar.ph, label %.loopexit, !llvm.loop !15
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.add.v4i32(<4 x i32>) #1

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn 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" }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = distinct !{!7, !6}
!8 = !{!9, !9, i64 0}
!9 = !{!"int", !10, i64 0}
!10 = !{!"omnipotent char", !11, i64 0}
!11 = !{!"Simple C++ TBAA"}
!12 = distinct !{!12, !6, !13, !14}
!13 = !{!"llvm.loop.isvectorized", i32 1}
!14 = !{!"llvm.loop.unroll.runtime.disable"}
!15 = distinct !{!15, !6, !14, !13}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass