; LLVM IR from radualexandrub/Optimization-Algorithms-Find-local-minimum :: 2D_Derivative_NonQuadratic_CostFunctions (Mat_add_Mat)

; 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: nounwind uwtable
define dso_local void @Mat_add_Mat(i32 noundef %0, i32 noundef %1, ptr noundef %2, ptr noundef %3, ptr noundef %4) #1 {
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  store i32 %0, ptr %6, align 4, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !5
  store ptr %2, ptr %8, align 8, !tbaa !9
  store ptr %3, ptr %9, align 8, !tbaa !9
  store ptr %4, ptr %10, align 8, !tbaa !9
  %14 = load i32, ptr %6, align 4, !tbaa !5
  %15 = zext i32 %14 to i64
  %16 = load i32, ptr %6, align 4, !tbaa !5
  %17 = zext i32 %16 to i64
  %18 = load i32, ptr %6, align 4, !tbaa !5
  %19 = zext i32 %18 to i64
  %20 = load i32, ptr %6, align 4, !tbaa !5
  %21 = zext i32 %20 to i64
  %22 = load i32, ptr %6, align 4, !tbaa !5
  %23 = zext i32 %22 to i64
  %24 = load i32, ptr %6, align 4, !tbaa !5
  %25 = zext i32 %24 to i64
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  store i32 0, ptr %11, align 4, !tbaa !5
  br label %26

26:                                               ; preds = %69, %5
  %27 = load i32, ptr %11, align 4, !tbaa !5
  %28 = load i32, ptr %6, align 4, !tbaa !5
  %29 = icmp slt i32 %27, %28
  br i1 %29, label %31, label %30

30:                                               ; preds = %26
  store i32 2, ptr %12, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  br label %72

31:                                               ; preds = %26
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #2
  store i32 0, ptr %13, align 4, !tbaa !5
  br label %32

32:                                               ; preds = %65, %31
  %33 = load i32, ptr %13, align 4, !tbaa !5
  %34 = load i32, ptr %7, align 4, !tbaa !5
  %35 = icmp slt i32 %33, %34
  br i1 %35, label %37, label %36

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

37:                                               ; preds = %32
  %38 = load ptr, ptr %8, align 8, !tbaa !9
  %39 = load i32, ptr %11, align 4, !tbaa !5
  %40 = sext i32 %39 to i64
  %41 = mul nsw i64 %40, %17
  %42 = getelementptr inbounds double, ptr %38, i64 %41
  %43 = load i32, ptr %13, align 4, !tbaa !5
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds double, ptr %42, i64 %44
  %46 = load double, ptr %45, align 8, !tbaa !11
  %47 = load ptr, ptr %9, align 8, !tbaa !9
  %48 = load i32, ptr %11, align 4, !tbaa !5
  %49 = sext i32 %48 to i64
  %50 = mul nsw i64 %49, %21
  %51 = getelementptr inbounds double, ptr %47, i64 %50
  %52 = load i32, ptr %13, align 4, !tbaa !5
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds double, ptr %51, i64 %53
  %55 = load double, ptr %54, align 8, !tbaa !11
  %56 = fadd double %46, %55
  %57 = load ptr, ptr %10, align 8, !tbaa !9
  %58 = load i32, ptr %11, align 4, !tbaa !5
  %59 = sext i32 %58 to i64
  %60 = mul nsw i64 %59, %25
  %61 = getelementptr inbounds double, ptr %57, i64 %60
  %62 = load i32, ptr %13, align 4, !tbaa !5
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds double, ptr %61, i64 %63
  store double %56, ptr %64, align 8, !tbaa !11
  br label %65

65:                                               ; preds = %37
  %66 = load i32, ptr %13, align 4, !tbaa !5
  %67 = add nsw i32 %66, 1
  store i32 %67, ptr %13, align 4, !tbaa !5
  br label %32, !llvm.loop !13

68:                                               ; preds = %36
  br label %69

69:                                               ; preds = %68
  %70 = load i32, ptr %11, align 4, !tbaa !5
  %71 = add nsw i32 %70, 1
  store i32 %71, ptr %11, align 4, !tbaa !5
  br label %26, !llvm.loop !15

72:                                               ; preds = %30
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"double", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


; 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: readwrite) uwtable
define dso_local void @Mat_add_Mat(i32 noundef %0, i32 noundef %1, ptr nocapture noundef readonly %2, ptr nocapture noundef readonly %3, ptr nocapture noundef writeonly %4) local_unnamed_addr #0 {
  %6 = zext i32 %0 to i64
  %7 = icmp sgt i32 %0, 0
  %8 = icmp sgt i32 %1, 0
  %or.cond = and i1 %7, %8
  br i1 %or.cond, label %.preheader.us.preheader, label %._crit_edge23

.preheader.us.preheader:                          ; preds = %5
  %9 = ptrtoint ptr %4 to i64
  %10 = ptrtoint ptr %2 to i64
  %11 = ptrtoint ptr %3 to i64
  %wide.trip.count = zext nneg i32 %1 to i64
  %12 = sub i64 %9, %10
  %13 = sub i64 %9, %11
  %min.iters.check = icmp ult i32 %1, 4
  %diff.check = icmp ult i64 %12, 32
  %diff.check31 = icmp ult i64 %13, 32
  %conflict.rdx = or i1 %diff.check, %diff.check31
  %n.vec = and i64 %wide.trip.count, 2147483644
  %cmp.n = icmp eq i64 %n.vec, %wide.trip.count
  %xtraiter = and i64 %wide.trip.count, 3
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br label %.preheader.us

.preheader.us:                                    ; preds = %.preheader.us.preheader, %._crit_edge.us
  %indvars.iv26 = phi i64 [ 0, %.preheader.us.preheader ], [ %indvars.iv.next27, %._crit_edge.us ]
  %14 = mul nuw nsw i64 %indvars.iv26, %6
  %15 = getelementptr inbounds double, ptr %2, i64 %14
  %16 = getelementptr inbounds double, ptr %3, i64 %14
  %17 = getelementptr inbounds double, ptr %4, i64 %14
  %brmerge = or i1 %min.iters.check, %conflict.rdx
  br i1 %brmerge, label %scalar.ph.preheader, label %vector.body

vector.body:                                      ; preds = %.preheader.us, %vector.body
  %index = phi i64 [ %index.next, %vector.body ], [ 0, %.preheader.us ]
  %18 = getelementptr inbounds double, ptr %15, i64 %index
  %19 = getelementptr inbounds i8, ptr %18, i64 16
  %wide.load = load <2 x double>, ptr %18, align 8, !tbaa !5
  %wide.load32 = load <2 x double>, ptr %19, align 8, !tbaa !5
  %20 = getelementptr inbounds double, ptr %16, i64 %index
  %21 = getelementptr inbounds i8, ptr %20, i64 16
  %wide.load33 = load <2 x double>, ptr %20, align 8, !tbaa !5
  %wide.load34 = load <2 x double>, ptr %21, align 8, !tbaa !5
  %22 = fadd <2 x double> %wide.load, %wide.load33
  %23 = fadd <2 x double> %wide.load32, %wide.load34
  %24 = getelementptr inbounds double, ptr %17, i64 %index
  %25 = getelementptr inbounds i8, ptr %24, i64 16
  store <2 x double> %22, ptr %24, align 8, !tbaa !5
  store <2 x double> %23, ptr %25, align 8, !tbaa !5
  %index.next = add nuw i64 %index, 4
  %26 = icmp eq i64 %index.next, %n.vec
  br i1 %26, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  br i1 %cmp.n, label %._crit_edge.us, label %scalar.ph.preheader

scalar.ph.preheader:                              ; preds = %.preheader.us, %middle.block
  %indvars.iv.ph = phi i64 [ 0, %.preheader.us ], [ %n.vec, %middle.block ]
  br i1 %lcmp.mod.not, label %scalar.ph.prol.loopexit, label %scalar.ph.prol

scalar.ph.prol:                                   ; preds = %scalar.ph.preheader, %scalar.ph.prol
  %indvars.iv.prol = phi i64 [ %indvars.iv.next.prol, %scalar.ph.prol ], [ %indvars.iv.ph, %scalar.ph.preheader ]
  %prol.iter = phi i64 [ %prol.iter.next, %scalar.ph.prol ], [ 0, %scalar.ph.preheader ]
  %27 = getelementptr inbounds double, ptr %15, i64 %indvars.iv.prol
  %28 = load double, ptr %27, align 8, !tbaa !5
  %29 = getelementptr inbounds double, ptr %16, i64 %indvars.iv.prol
  %30 = load double, ptr %29, align 8, !tbaa !5
  %31 = fadd double %28, %30
  %32 = getelementptr inbounds double, ptr %17, i64 %indvars.iv.prol
  store double %31, ptr %32, align 8, !tbaa !5
  %indvars.iv.next.prol = add nuw nsw i64 %indvars.iv.prol, 1
  %prol.iter.next = add i64 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i64 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %scalar.ph.prol.loopexit, label %scalar.ph.prol, !llvm.loop !13

scalar.ph.prol.loopexit:                          ; preds = %scalar.ph.prol, %scalar.ph.preheader
  %indvars.iv.unr = phi i64 [ %indvars.iv.ph, %scalar.ph.preheader ], [ %indvars.iv.next.prol, %scalar.ph.prol ]
  %33 = sub nsw i64 %indvars.iv.ph, %wide.trip.count
  %34 = icmp ugt i64 %33, -4
  br i1 %34, label %._crit_edge.us, label %scalar.ph

scalar.ph:                                        ; preds = %scalar.ph.prol.loopexit, %scalar.ph
  %indvars.iv = phi i64 [ %indvars.iv.next.3, %scalar.ph ], [ %indvars.iv.unr, %scalar.ph.prol.loopexit ]
  %35 = getelementptr inbounds double, ptr %15, i64 %indvars.iv
  %36 = load double, ptr %35, align 8, !tbaa !5
  %37 = getelementptr inbounds double, ptr %16, i64 %indvars.iv
  %38 = load double, ptr %37, align 8, !tbaa !5
  %39 = fadd double %36, %38
  %40 = getelementptr inbounds double, ptr %17, i64 %indvars.iv
  store double %39, ptr %40, align 8, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %41 = getelementptr inbounds double, ptr %15, i64 %indvars.iv.next
  %42 = load double, ptr %41, align 8, !tbaa !5
  %43 = getelementptr inbounds double, ptr %16, i64 %indvars.iv.next
  %44 = load double, ptr %43, align 8, !tbaa !5
  %45 = fadd double %42, %44
  %46 = getelementptr inbounds double, ptr %17, i64 %indvars.iv.next
  store double %45, ptr %46, align 8, !tbaa !5
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %47 = getelementptr inbounds double, ptr %15, i64 %indvars.iv.next.1
  %48 = load double, ptr %47, align 8, !tbaa !5
  %49 = getelementptr inbounds double, ptr %16, i64 %indvars.iv.next.1
  %50 = load double, ptr %49, align 8, !tbaa !5
  %51 = fadd double %48, %50
  %52 = getelementptr inbounds double, ptr %17, i64 %indvars.iv.next.1
  store double %51, ptr %52, align 8, !tbaa !5
  %indvars.iv.next.2 = add nuw nsw i64 %indvars.iv, 3
  %53 = getelementptr inbounds double, ptr %15, i64 %indvars.iv.next.2
  %54 = load double, ptr %53, align 8, !tbaa !5
  %55 = getelementptr inbounds double, ptr %16, i64 %indvars.iv.next.2
  %56 = load double, ptr %55, align 8, !tbaa !5
  %57 = fadd double %54, %56
  %58 = getelementptr inbounds double, ptr %17, i64 %indvars.iv.next.2
  store double %57, ptr %58, align 8, !tbaa !5
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %exitcond.not.3 = icmp eq i64 %indvars.iv.next.3, %wide.trip.count
  br i1 %exitcond.not.3, label %._crit_edge.us, label %scalar.ph, !llvm.loop !15

._crit_edge.us:                                   ; preds = %scalar.ph.prol.loopexit, %scalar.ph, %middle.block
  %indvars.iv.next27 = add nuw nsw i64 %indvars.iv26, 1
  %exitcond30.not = icmp eq i64 %indvars.iv.next27, %6
  br i1 %exitcond30.not, label %._crit_edge23, label %.preheader.us, !llvm.loop !16

._crit_edge23:                                    ; preds = %._crit_edge.us, %5
  ret void
}

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


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