; LLVM IR from ritikbanger/Hacktoberfest2022-DSA :: 07. Matrix/max_rectangle.ll (Z12sortedMatrixPA10_ii)

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

; Function Attrs: mustprogress uwtable
define dso_local void @_Z12sortedMatrixPA10_ii(ptr noundef %0, i32 noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  store i32 0, ptr %5, align 4, !tbaa !9
  %14 = load i32, ptr %4, align 4, !tbaa !9
  %15 = load i32, ptr %4, align 4, !tbaa !9
  %16 = mul nsw i32 %14, %15
  %17 = zext i32 %16 to i64
  %18 = call ptr @llvm.stacksave.p0()
  store ptr %18, ptr %6, align 8
  %19 = alloca i32, i64 %17, align 16
  store i64 %17, ptr %7, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  store i32 0, ptr %8, align 4, !tbaa !9
  br label %20

20:                                               ; preds = %48, %2
  %21 = load i32, ptr %8, align 4, !tbaa !9
  %22 = load i32, ptr %4, align 4, !tbaa !9
  %23 = icmp slt i32 %21, %22
  br i1 %23, label %25, label %24

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

25:                                               ; preds = %20
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  store i32 0, ptr %10, align 4, !tbaa !9
  br label %26

26:                                               ; preds = %44, %25
  %27 = load i32, ptr %10, align 4, !tbaa !9
  %28 = load i32, ptr %4, align 4, !tbaa !9
  %29 = icmp slt i32 %27, %28
  br i1 %29, label %31, label %30

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

31:                                               ; preds = %26
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = load i32, ptr %8, align 4, !tbaa !9
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [10 x i32], ptr %32, i64 %34
  %36 = load i32, ptr %10, align 4, !tbaa !9
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [10 x i32], ptr %35, i64 0, i64 %37
  %39 = load i32, ptr %38, align 4, !tbaa !9
  %40 = load i32, ptr %5, align 4, !tbaa !9
  %41 = add nsw i32 %40, 1
  store i32 %41, ptr %5, align 4, !tbaa !9
  %42 = sext i32 %40 to i64
  %43 = getelementptr inbounds i32, ptr %19, i64 %42
  store i32 %39, ptr %43, align 4, !tbaa !9
  br label %44

44:                                               ; preds = %31
  %45 = load i32, ptr %10, align 4, !tbaa !9
  %46 = add nsw i32 %45, 1
  store i32 %46, ptr %10, align 4, !tbaa !9
  br label %26, !llvm.loop !11

47:                                               ; preds = %30
  br label %48

48:                                               ; preds = %47
  %49 = load i32, ptr %8, align 4, !tbaa !9
  %50 = add nsw i32 %49, 1
  store i32 %50, ptr %8, align 4, !tbaa !9
  br label %20, !llvm.loop !13

51:                                               ; preds = %24
  %52 = load i32, ptr %4, align 4, !tbaa !9
  %53 = load i32, ptr %4, align 4, !tbaa !9
  %54 = mul nsw i32 %52, %53
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds i32, ptr %19, i64 %55
  call void @_ZSt4sortIPiEvT_S1_(ptr noundef %19, ptr noundef %56)
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  store i32 0, ptr %11, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #4
  store i32 0, ptr %12, align 4, !tbaa !9
  br label %57

57:                                               ; preds = %85, %51
  %58 = load i32, ptr %12, align 4, !tbaa !9
  %59 = load i32, ptr %4, align 4, !tbaa !9
  %60 = icmp slt i32 %58, %59
  br i1 %60, label %62, label %61

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

62:                                               ; preds = %57
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #4
  store i32 0, ptr %13, align 4, !tbaa !9
  br label %63

63:                                               ; preds = %81, %62
  %64 = load i32, ptr %13, align 4, !tbaa !9
  %65 = load i32, ptr %4, align 4, !tbaa !9
  %66 = icmp slt i32 %64, %65
  br i1 %66, label %68, label %67

67:                                               ; preds = %63
  store i32 11, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #4
  br label %84

68:                                               ; preds = %63
  %69 = load i32, ptr %11, align 4, !tbaa !9
  %70 = add nsw i32 %69, 1
  store i32 %70, ptr %11, align 4, !tbaa !9
  %71 = sext i32 %69 to i64
  %72 = getelementptr inbounds i32, ptr %19, i64 %71
  %73 = load i32, ptr %72, align 4, !tbaa !9
  %74 = load ptr, ptr %3, align 8, !tbaa !5
  %75 = load i32, ptr %12, align 4, !tbaa !9
  %76 = sext i32 %75 to i64
  %77 = getelementptr inbounds [10 x i32], ptr %74, i64 %76
  %78 = load i32, ptr %13, align 4, !tbaa !9
  %79 = sext i32 %78 to i64
  %80 = getelementptr inbounds [10 x i32], ptr %77, i64 0, i64 %79
  store i32 %73, ptr %80, align 4, !tbaa !9
  br label %81

81:                                               ; preds = %68
  %82 = load i32, ptr %13, align 4, !tbaa !9
  %83 = add nsw i32 %82, 1
  store i32 %83, ptr %13, align 4, !tbaa !9
  br label %63, !llvm.loop !14

84:                                               ; preds = %67
  br label %85

85:                                               ; preds = %84
  %86 = load i32, ptr %12, align 4, !tbaa !9
  %87 = add nsw i32 %86, 1
  store i32 %87, ptr %12, align 4, !tbaa !9
  br label %57, !llvm.loop !15

88:                                               ; preds = %61
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  %89 = load ptr, ptr %6, align 8
  call void @llvm.stackrestore.p0(ptr %89)
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #2

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZSt4sortIPiEvT_S1_(ptr noundef, ptr noundef) #3

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 = { nocallback nofree nosync nounwind willreturn }
attributes #3 = { inlinehint mustprogress 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 #4 = { 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}
!15 = distinct !{!15, !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 uwtable
define dso_local void @_Z12sortedMatrixPA10_ii(ptr nocapture noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = mul nsw i32 %1, %1
  %4 = zext nneg i32 %3 to i64
  %5 = alloca i32, i64 %4, align 16
  %6 = icmp sgt i32 %1, 0
  br i1 %6, label %.preheader32.us.preheader, label %._crit_edge37.thread

._crit_edge37.thread:                             ; preds = %2
  %7 = getelementptr inbounds i32, ptr %5, i64 %4
  call void @_ZSt4sortIPiEvT_S1_(ptr noundef nonnull %5, ptr noundef nonnull %7)
  br label %._crit_edge42

.preheader32.us.preheader:                        ; preds = %2
  %8 = zext nneg i32 %1 to i64
  %9 = shl nuw nsw i64 %8, 2
  %10 = zext nneg i32 %1 to i64
  %11 = add nsw i64 %8, -1
  %xtraiter = and i64 %8, 1
  %12 = icmp eq i64 %11, 0
  br i1 %12, label %._crit_edge37.unr-lcssa, label %.preheader32.us.preheader.new

.preheader32.us.preheader.new:                    ; preds = %.preheader32.us.preheader
  %unroll_iter = and i64 %8, 2147483646
  %invariant.gep = getelementptr i8, ptr %0, i64 40
  br label %.preheader32.us

.preheader32.us:                                  ; preds = %.preheader32.us, %.preheader32.us.preheader.new
  %indvar = phi i64 [ 0, %.preheader32.us.preheader.new ], [ %indvar.next.1, %.preheader32.us ]
  %.02835.us = phi i64 [ 0, %.preheader32.us.preheader.new ], [ %19, %.preheader32.us ]
  %niter = phi i64 [ 0, %.preheader32.us.preheader.new ], [ %niter.next.1, %.preheader32.us ]
  %13 = mul nuw nsw i64 %indvar, 40
  %scevgep46 = getelementptr i8, ptr %0, i64 %13
  %sext = shl i64 %.02835.us, 32
  %14 = ashr exact i64 %sext, 30
  %scevgep = getelementptr i8, ptr %5, i64 %14
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %scevgep, ptr align 4 %scevgep46, i64 %9, i1 false), !tbaa !5
  %15 = add nsw i64 %.02835.us, %10
  %16 = mul nuw i64 %indvar, 40
  %gep = getelementptr i8, ptr %invariant.gep, i64 %16
  %sext.1 = shl i64 %15, 32
  %17 = ashr exact i64 %sext.1, 32
  %18 = ashr exact i64 %sext.1, 30
  %scevgep.1 = getelementptr i8, ptr %5, i64 %18
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %scevgep.1, ptr align 4 %gep, i64 %9, i1 false), !tbaa !5
  %19 = add nsw i64 %17, %10
  %indvar.next.1 = add nuw nsw i64 %indvar, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge37.unr-lcssa.loopexit, label %.preheader32.us, !llvm.loop !9

._crit_edge37.unr-lcssa.loopexit:                 ; preds = %.preheader32.us
  %20 = mul nuw nsw i64 %indvar.next.1, 40
  %21 = shl i64 %19, 32
  %22 = ashr exact i64 %21, 30
  br label %._crit_edge37.unr-lcssa

._crit_edge37.unr-lcssa:                          ; preds = %._crit_edge37.unr-lcssa.loopexit, %.preheader32.us.preheader
  %indvar.unr = phi i64 [ 0, %.preheader32.us.preheader ], [ %20, %._crit_edge37.unr-lcssa.loopexit ]
  %.02835.us.unr = phi i64 [ 0, %.preheader32.us.preheader ], [ %22, %._crit_edge37.unr-lcssa.loopexit ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge37, label %.preheader32.us.epil

.preheader32.us.epil:                             ; preds = %._crit_edge37.unr-lcssa
  %scevgep46.epil = getelementptr i8, ptr %0, i64 %indvar.unr
  %scevgep.epil = getelementptr i8, ptr %5, i64 %.02835.us.unr
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %scevgep.epil, ptr align 4 %scevgep46.epil, i64 %9, i1 false), !tbaa !5
  br label %._crit_edge37

._crit_edge37:                                    ; preds = %._crit_edge37.unr-lcssa, %.preheader32.us.epil
  %23 = getelementptr inbounds i32, ptr %5, i64 %4
  call void @_ZSt4sortIPiEvT_S1_(ptr noundef nonnull %5, ptr noundef nonnull %23)
  br i1 %6, label %.preheader.us.preheader, label %._crit_edge42

.preheader.us.preheader:                          ; preds = %._crit_edge37
  %24 = zext nneg i32 %1 to i64
  %25 = shl nuw nsw i64 %24, 2
  %26 = zext nneg i32 %1 to i64
  %xtraiter69 = and i64 %8, 1
  %27 = icmp eq i64 %11, 0
  br i1 %27, label %._crit_edge42.loopexit.unr-lcssa, label %.preheader.us.preheader.new

.preheader.us.preheader.new:                      ; preds = %.preheader.us.preheader
  %unroll_iter71 = and i64 %8, 2147483646
  %invariant.gep74 = getelementptr i8, ptr %0, i64 40
  br label %.preheader.us

.preheader.us:                                    ; preds = %.preheader.us, %.preheader.us.preheader.new
  %indvar54 = phi i64 [ 0, %.preheader.us.preheader.new ], [ %indvar.next55.1, %.preheader.us ]
  %.02540.us = phi i64 [ 0, %.preheader.us.preheader.new ], [ %34, %.preheader.us ]
  %niter72 = phi i64 [ 0, %.preheader.us.preheader.new ], [ %niter72.next.1, %.preheader.us ]
  %28 = mul nuw nsw i64 %indvar54, 40
  %scevgep56 = getelementptr i8, ptr %0, i64 %28
  %sext68 = shl i64 %.02540.us, 32
  %29 = ashr exact i64 %sext68, 30
  %scevgep57 = getelementptr i8, ptr %5, i64 %29
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %scevgep56, ptr align 4 %scevgep57, i64 %25, i1 false), !tbaa !5
  %30 = add nsw i64 %.02540.us, %26
  %31 = mul nuw i64 %indvar54, 40
  %gep75 = getelementptr i8, ptr %invariant.gep74, i64 %31
  %sext68.1 = shl i64 %30, 32
  %32 = ashr exact i64 %sext68.1, 32
  %33 = ashr exact i64 %sext68.1, 30
  %scevgep57.1 = getelementptr i8, ptr %5, i64 %33
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %gep75, ptr align 4 %scevgep57.1, i64 %25, i1 false), !tbaa !5
  %34 = add nsw i64 %32, %26
  %indvar.next55.1 = add nuw nsw i64 %indvar54, 2
  %niter72.next.1 = add i64 %niter72, 2
  %niter72.ncmp.1 = icmp eq i64 %niter72.next.1, %unroll_iter71
  br i1 %niter72.ncmp.1, label %._crit_edge42.loopexit.unr-lcssa.loopexit, label %.preheader.us, !llvm.loop !11

._crit_edge42.loopexit.unr-lcssa.loopexit:        ; preds = %.preheader.us
  %35 = mul nuw nsw i64 %indvar.next55.1, 40
  %36 = shl i64 %34, 32
  %37 = ashr exact i64 %36, 30
  br label %._crit_edge42.loopexit.unr-lcssa

._crit_edge42.loopexit.unr-lcssa:                 ; preds = %._crit_edge42.loopexit.unr-lcssa.loopexit, %.preheader.us.preheader
  %indvar54.unr = phi i64 [ 0, %.preheader.us.preheader ], [ %35, %._crit_edge42.loopexit.unr-lcssa.loopexit ]
  %.02540.us.unr = phi i64 [ 0, %.preheader.us.preheader ], [ %37, %._crit_edge42.loopexit.unr-lcssa.loopexit ]
  %lcmp.mod70.not = icmp eq i64 %xtraiter69, 0
  br i1 %lcmp.mod70.not, label %._crit_edge42, label %.preheader.us.epil

.preheader.us.epil:                               ; preds = %._crit_edge42.loopexit.unr-lcssa
  %scevgep56.epil = getelementptr i8, ptr %0, i64 %indvar54.unr
  %scevgep57.epil = getelementptr i8, ptr %5, i64 %.02540.us.unr
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %scevgep56.epil, ptr align 4 %scevgep57.epil, i64 %25, i1 false), !tbaa !5
  br label %._crit_edge42

._crit_edge42:                                    ; preds = %.preheader.us.epil, %._crit_edge42.loopexit.unr-lcssa, %._crit_edge37.thread, %._crit_edge37
  ret void
}

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZSt4sortIPiEvT_S1_(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

attributes #0 = { mustprogress 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 = { inlinehint mustprogress 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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }

!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}


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