; LLVM IR from sbshah97/DS-Algo-Handbook :: labs/lab3/max_subArrayDivAndConq.ll (Z14maxCrossingSumiii)

; 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"

@arr = external dso_local global [100 x i32], align 16

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

13:                                               ; preds = %31, %3
  %14 = load i32, ptr %9, align 4, !tbaa !5
  %15 = load i32, ptr %4, align 4, !tbaa !5
  %16 = icmp sge i32 %14, %15
  br i1 %16, label %18, label %17

17:                                               ; preds = %13
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  br label %34

18:                                               ; preds = %13
  %19 = load i32, ptr %9, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [100 x i32], ptr @arr, i64 0, i64 %20
  %22 = load i32, ptr %21, align 4, !tbaa !5
  %23 = load i32, ptr %7, align 4, !tbaa !5
  %24 = add nsw i32 %23, %22
  store i32 %24, ptr %7, align 4, !tbaa !5
  %25 = load i32, ptr %7, align 4, !tbaa !5
  %26 = load i32, ptr %8, align 4, !tbaa !5
  %27 = icmp sgt i32 %25, %26
  br i1 %27, label %28, label %30

28:                                               ; preds = %18
  %29 = load i32, ptr %7, align 4, !tbaa !5
  store i32 %29, ptr %8, align 4, !tbaa !5
  br label %30

30:                                               ; preds = %28, %18
  br label %31

31:                                               ; preds = %30
  %32 = load i32, ptr %9, align 4, !tbaa !5
  %33 = add nsw i32 %32, -1
  store i32 %33, ptr %9, align 4, !tbaa !5
  br label %13, !llvm.loop !9

34:                                               ; preds = %17
  store i32 0, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  store i32 -2147483648, ptr %10, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  %35 = load i32, ptr %5, align 4, !tbaa !5
  %36 = add nsw i32 %35, 1
  store i32 %36, ptr %11, align 4, !tbaa !5
  br label %37

37:                                               ; preds = %55, %34
  %38 = load i32, ptr %11, align 4, !tbaa !5
  %39 = load i32, ptr %6, align 4, !tbaa !5
  %40 = icmp sle i32 %38, %39
  br i1 %40, label %42, label %41

41:                                               ; preds = %37
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  br label %58

42:                                               ; preds = %37
  %43 = load i32, ptr %11, align 4, !tbaa !5
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds [100 x i32], ptr @arr, i64 0, i64 %44
  %46 = load i32, ptr %45, align 4, !tbaa !5
  %47 = load i32, ptr %7, align 4, !tbaa !5
  %48 = add nsw i32 %47, %46
  store i32 %48, ptr %7, align 4, !tbaa !5
  %49 = load i32, ptr %7, align 4, !tbaa !5
  %50 = load i32, ptr %10, align 4, !tbaa !5
  %51 = icmp sgt i32 %49, %50
  br i1 %51, label %52, label %54

52:                                               ; preds = %42
  %53 = load i32, ptr %7, align 4, !tbaa !5
  store i32 %53, ptr %10, align 4, !tbaa !5
  br label %54

54:                                               ; preds = %52, %42
  br label %55

55:                                               ; preds = %54
  %56 = load i32, ptr %11, align 4, !tbaa !5
  %57 = add nsw i32 %56, 1
  store i32 %57, ptr %11, align 4, !tbaa !5
  br label %37, !llvm.loop !11

58:                                               ; preds = %41
  %59 = load i32, ptr %10, align 4, !tbaa !5
  %60 = load i32, ptr %8, align 4, !tbaa !5
  %61 = add nsw i32 %59, %60
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  ret i32 %61
}

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


; 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"

@arr = external dso_local local_unnamed_addr global [100 x i32], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_Z14maxCrossingSumiii(i32 noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %.not30 = icmp slt i32 %1, %0
  br i1 %.not30, label %.preheader, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %3
  %4 = sext i32 %1 to i64
  %5 = sext i32 %0 to i64
  %6 = add nsw i64 %4, 1
  %7 = sub nsw i64 %6, %5
  %8 = sub nsw i64 %4, %5
  %xtraiter = and i64 %7, 3
  %9 = icmp ult i64 %8, 3
  br i1 %9, label %.preheader.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %7, -4
  br label %.lr.ph

.preheader.loopexit.unr-lcssa:                    ; preds = %.lr.ph, %.lr.ph.preheader
  %spec.select.lcssa.ph = phi i32 [ poison, %.lr.ph.preheader ], [ %spec.select.3, %.lr.ph ]
  %indvars.iv.unr = phi i64 [ %4, %.lr.ph.preheader ], [ %indvars.iv.next.3, %.lr.ph ]
  %.02232.unr = phi i32 [ -2147483648, %.lr.ph.preheader ], [ %spec.select.3, %.lr.ph ]
  %.02431.unr = phi i32 [ 0, %.lr.ph.preheader ], [ %28, %.lr.ph ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.preheader, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %.preheader.loopexit.unr-lcssa, %.lr.ph.epil
  %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %.lr.ph.epil ], [ %indvars.iv.unr, %.preheader.loopexit.unr-lcssa ]
  %.02232.epil = phi i32 [ %spec.select.epil, %.lr.ph.epil ], [ %.02232.unr, %.preheader.loopexit.unr-lcssa ]
  %.02431.epil = phi i32 [ %12, %.lr.ph.epil ], [ %.02431.unr, %.preheader.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph.epil ], [ 0, %.preheader.loopexit.unr-lcssa ]
  %10 = getelementptr inbounds [100 x i32], ptr @arr, i64 0, i64 %indvars.iv.epil
  %11 = load i32, ptr %10, align 4, !tbaa !5
  %12 = add nsw i32 %11, %.02431.epil
  %spec.select.epil = tail call i32 @llvm.smax.i32(i32 %12, i32 %.02232.epil)
  %indvars.iv.next.epil = add 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 %.preheader, label %.lr.ph.epil, !llvm.loop !9

.preheader:                                       ; preds = %.preheader.loopexit.unr-lcssa, %.lr.ph.epil, %3
  %.022.lcssa = phi i32 [ -2147483648, %3 ], [ %spec.select.lcssa.ph, %.preheader.loopexit.unr-lcssa ], [ %spec.select.epil, %.lr.ph.epil ]
  %.not28.not34 = icmp slt i32 %1, %2
  br i1 %.not28.not34, label %.lr.ph38.preheader, label %._crit_edge

.lr.ph38.preheader:                               ; preds = %.preheader
  %13 = sext i32 %1 to i64
  %wide.trip.count = sext i32 %2 to i64
  %14 = sub nsw i64 %wide.trip.count, %13
  %xtraiter45 = and i64 %14, 3
  %15 = sub nsw i64 %13, %wide.trip.count
  %16 = icmp ugt i64 %15, -4
  br i1 %16, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph38.preheader.new

.lr.ph38.preheader.new:                           ; preds = %.lr.ph38.preheader
  %unroll_iter49 = and i64 %14, -4
  br label %.lr.ph38

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %indvars.iv = phi i64 [ %4, %.lr.ph.preheader.new ], [ %indvars.iv.next.3, %.lr.ph ]
  %.02232 = phi i32 [ -2147483648, %.lr.ph.preheader.new ], [ %spec.select.3, %.lr.ph ]
  %.02431 = phi i32 [ 0, %.lr.ph.preheader.new ], [ %28, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.3, %.lr.ph ]
  %17 = getelementptr inbounds [100 x i32], ptr @arr, i64 0, i64 %indvars.iv
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = add nsw i32 %18, %.02431
  %spec.select = tail call i32 @llvm.smax.i32(i32 %19, i32 %.02232)
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %20 = getelementptr inbounds [100 x i32], ptr @arr, i64 0, i64 %indvars.iv.next
  %21 = load i32, ptr %20, align 4, !tbaa !5
  %22 = add nsw i32 %21, %19
  %spec.select.1 = tail call i32 @llvm.smax.i32(i32 %22, i32 %spec.select)
  %indvars.iv.next.1 = add nsw i64 %indvars.iv, -2
  %23 = getelementptr inbounds [100 x i32], ptr @arr, i64 0, i64 %indvars.iv.next.1
  %24 = load i32, ptr %23, align 4, !tbaa !5
  %25 = add nsw i32 %24, %22
  %spec.select.2 = tail call i32 @llvm.smax.i32(i32 %25, i32 %spec.select.1)
  %indvars.iv.next.2 = add nsw i64 %indvars.iv, -3
  %26 = getelementptr inbounds [100 x i32], ptr @arr, i64 0, i64 %indvars.iv.next.2
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %28 = add nsw i32 %27, %25
  %spec.select.3 = tail call i32 @llvm.smax.i32(i32 %28, i32 %spec.select.2)
  %indvars.iv.next.3 = add nsw i64 %indvars.iv, -4
  %niter.next.3 = add i64 %niter, 4
  %niter.ncmp.3.not = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3.not, label %.preheader.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !11

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.lr.ph38, %.lr.ph38.preheader
  %spec.select29.lcssa.ph = phi i32 [ poison, %.lr.ph38.preheader ], [ %spec.select29.3, %.lr.ph38 ]
  %indvars.iv41.unr = phi i64 [ %13, %.lr.ph38.preheader ], [ %indvars.iv.next42.3, %.lr.ph38 ]
  %.02036.unr = phi i32 [ -2147483648, %.lr.ph38.preheader ], [ %spec.select29.3, %.lr.ph38 ]
  %.12535.unr = phi i32 [ 0, %.lr.ph38.preheader ], [ %44, %.lr.ph38 ]
  %lcmp.mod47.not = icmp eq i64 %xtraiter45, 0
  br i1 %lcmp.mod47.not, label %._crit_edge, label %.lr.ph38.epil

.lr.ph38.epil:                                    ; preds = %._crit_edge.loopexit.unr-lcssa, %.lr.ph38.epil
  %indvars.iv41.epil = phi i64 [ %indvars.iv.next42.epil, %.lr.ph38.epil ], [ %indvars.iv41.unr, %._crit_edge.loopexit.unr-lcssa ]
  %.02036.epil = phi i32 [ %spec.select29.epil, %.lr.ph38.epil ], [ %.02036.unr, %._crit_edge.loopexit.unr-lcssa ]
  %.12535.epil = phi i32 [ %31, %.lr.ph38.epil ], [ %.12535.unr, %._crit_edge.loopexit.unr-lcssa ]
  %epil.iter46 = phi i64 [ %epil.iter46.next, %.lr.ph38.epil ], [ 0, %._crit_edge.loopexit.unr-lcssa ]
  %indvars.iv.next42.epil = add nsw i64 %indvars.iv41.epil, 1
  %29 = getelementptr inbounds [100 x i32], ptr @arr, i64 0, i64 %indvars.iv.next42.epil
  %30 = load i32, ptr %29, align 4, !tbaa !5
  %31 = add nsw i32 %30, %.12535.epil
  %spec.select29.epil = tail call i32 @llvm.smax.i32(i32 %31, i32 %.02036.epil)
  %epil.iter46.next = add i64 %epil.iter46, 1
  %epil.iter46.cmp.not = icmp eq i64 %epil.iter46.next, %xtraiter45
  br i1 %epil.iter46.cmp.not, label %._crit_edge, label %.lr.ph38.epil, !llvm.loop !13

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %.lr.ph38.epil, %.preheader
  %.020.lcssa = phi i32 [ -2147483648, %.preheader ], [ %spec.select29.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %spec.select29.epil, %.lr.ph38.epil ]
  %32 = add nsw i32 %.020.lcssa, %.022.lcssa
  ret i32 %32

.lr.ph38:                                         ; preds = %.lr.ph38, %.lr.ph38.preheader.new
  %indvars.iv41 = phi i64 [ %13, %.lr.ph38.preheader.new ], [ %indvars.iv.next42.3, %.lr.ph38 ]
  %.02036 = phi i32 [ -2147483648, %.lr.ph38.preheader.new ], [ %spec.select29.3, %.lr.ph38 ]
  %.12535 = phi i32 [ 0, %.lr.ph38.preheader.new ], [ %44, %.lr.ph38 ]
  %niter50 = phi i64 [ 0, %.lr.ph38.preheader.new ], [ %niter50.next.3, %.lr.ph38 ]
  %indvars.iv.next42 = add nsw i64 %indvars.iv41, 1
  %33 = getelementptr inbounds [100 x i32], ptr @arr, i64 0, i64 %indvars.iv.next42
  %34 = load i32, ptr %33, align 4, !tbaa !5
  %35 = add nsw i32 %34, %.12535
  %spec.select29 = tail call i32 @llvm.smax.i32(i32 %35, i32 %.02036)
  %indvars.iv.next42.1 = add nsw i64 %indvars.iv41, 2
  %36 = getelementptr inbounds [100 x i32], ptr @arr, i64 0, i64 %indvars.iv.next42.1
  %37 = load i32, ptr %36, align 4, !tbaa !5
  %38 = add nsw i32 %37, %35
  %spec.select29.1 = tail call i32 @llvm.smax.i32(i32 %38, i32 %spec.select29)
  %indvars.iv.next42.2 = add nsw i64 %indvars.iv41, 3
  %39 = getelementptr inbounds [100 x i32], ptr @arr, i64 0, i64 %indvars.iv.next42.2
  %40 = load i32, ptr %39, align 4, !tbaa !5
  %41 = add nsw i32 %40, %38
  %spec.select29.2 = tail call i32 @llvm.smax.i32(i32 %41, i32 %spec.select29.1)
  %indvars.iv.next42.3 = add nsw i64 %indvars.iv41, 4
  %42 = getelementptr inbounds [100 x i32], ptr @arr, i64 0, i64 %indvars.iv.next42.3
  %43 = load i32, ptr %42, align 4, !tbaa !5
  %44 = add nsw i32 %43, %41
  %spec.select29.3 = tail call i32 @llvm.smax.i32(i32 %44, i32 %spec.select29.2)
  %niter50.next.3 = add i64 %niter50, 4
  %niter50.ncmp.3 = icmp eq i64 %niter50.next.3, %unroll_iter49
  br i1 %niter50.ncmp.3, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph38, !llvm.loop !14
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #1

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


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