; LLVM IR from sbischoff-ai/simple-molecular-dynamics :: SimpleMolecularDynamics/SimpleMolecularDynamics/mat.ll (ZN3mat7get_maxEv)

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

%class.mat = type { ptr, i32, i32 }
%class.vec = type <{ ptr, i32, [4 x i8] }>

; 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

declare noundef nonnull align 8 dereferenceable(8) ptr @_ZN3vecixEi(ptr noundef nonnull align 8 dereferenceable(12), i32 noundef) #1

; Function Attrs: mustprogress uwtable
define dso_local noundef double @_ZN3mat7get_maxEv(ptr noundef nonnull align 8 dereferenceable(16) %0) #2 align 2 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca double, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  %6 = load ptr, ptr %2, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %7 = getelementptr inbounds %class.mat, ptr %6, i32 0, i32 0
  %8 = load ptr, ptr %7, align 8, !tbaa !9
  %9 = getelementptr inbounds %class.vec, ptr %8, i64 0
  %10 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZN3vecixEi(ptr noundef nonnull align 8 dereferenceable(12) %9, i32 noundef 1)
  %11 = load double, ptr %10, align 8, !tbaa !12
  store double %11, ptr %5, align 8, !tbaa !12
  store i32 0, ptr %3, align 4, !tbaa !14
  br label %12

12:                                               ; preds = %48, %1
  %13 = load i32, ptr %3, align 4, !tbaa !14
  %14 = getelementptr inbounds %class.mat, ptr %6, i32 0, i32 1
  %15 = load i32, ptr %14, align 8, !tbaa !15
  %16 = icmp slt i32 %13, %15
  br i1 %16, label %17, label %51

17:                                               ; preds = %12
  store i32 1, ptr %4, align 4, !tbaa !14
  br label %18

18:                                               ; preds = %44, %17
  %19 = load i32, ptr %4, align 4, !tbaa !14
  %20 = getelementptr inbounds %class.mat, ptr %6, i32 0, i32 2
  %21 = load i32, ptr %20, align 4, !tbaa !16
  %22 = icmp sle i32 %19, %21
  br i1 %22, label %23, label %47

23:                                               ; preds = %18
  %24 = getelementptr inbounds %class.mat, ptr %6, i32 0, i32 0
  %25 = load ptr, ptr %24, align 8, !tbaa !9
  %26 = load i32, ptr %3, align 4, !tbaa !14
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds %class.vec, ptr %25, i64 %27
  %29 = load i32, ptr %4, align 4, !tbaa !14
  %30 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZN3vecixEi(ptr noundef nonnull align 8 dereferenceable(12) %28, i32 noundef %29)
  %31 = load double, ptr %30, align 8, !tbaa !12
  %32 = load double, ptr %5, align 8, !tbaa !12
  %33 = fcmp ogt double %31, %32
  br i1 %33, label %34, label %43

34:                                               ; preds = %23
  %35 = getelementptr inbounds %class.mat, ptr %6, i32 0, i32 0
  %36 = load ptr, ptr %35, align 8, !tbaa !9
  %37 = load i32, ptr %3, align 4, !tbaa !14
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds %class.vec, ptr %36, i64 %38
  %40 = load i32, ptr %4, align 4, !tbaa !14
  %41 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZN3vecixEi(ptr noundef nonnull align 8 dereferenceable(12) %39, i32 noundef %40)
  %42 = load double, ptr %41, align 8, !tbaa !12
  store double %42, ptr %5, align 8, !tbaa !12
  br label %43

43:                                               ; preds = %34, %23
  br label %44

44:                                               ; preds = %43
  %45 = load i32, ptr %4, align 4, !tbaa !14
  %46 = add nsw i32 %45, 1
  store i32 %46, ptr %4, align 4, !tbaa !14
  br label %18, !llvm.loop !17

47:                                               ; preds = %18
  br label %48

48:                                               ; preds = %47
  %49 = load i32, ptr %3, align 4, !tbaa !14
  %50 = add nsw i32 %49, 1
  store i32 %50, ptr %3, align 4, !tbaa !14
  br label %12, !llvm.loop !19

51:                                               ; preds = %12
  %52 = load double, ptr %5, align 8, !tbaa !12
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  ret double %52
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 = { 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 #3 = { 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, !6, i64 0}
!10 = !{!"_ZTS3mat", !6, i64 0, !11, i64 8, !11, i64 12}
!11 = !{!"int", !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"double", !7, i64 0}
!14 = !{!11, !11, i64 0}
!15 = !{!10, !11, i64 8}
!16 = !{!10, !11, i64 12}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = distinct !{!19, !18}


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

%class.vec = type <{ ptr, i32, [4 x i8] }>

declare noundef nonnull align 8 dereferenceable(8) ptr @_ZN3vecixEi(ptr noundef nonnull align 8 dereferenceable(12), i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local noundef double @_ZN3mat7get_maxEv(ptr nocapture noundef nonnull readonly align 8 dereferenceable(16) %0) local_unnamed_addr #1 align 2 {
  %2 = load ptr, ptr %0, align 8, !tbaa !5
  %3 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZN3vecixEi(ptr noundef nonnull align 8 dereferenceable(12) %2, i32 noundef 1)
  %4 = load double, ptr %3, align 8, !tbaa !11
  %5 = getelementptr inbounds i8, ptr %0, i64 8
  %6 = load i32, ptr %5, align 8, !tbaa !13
  %7 = icmp sgt i32 %6, 0
  br i1 %7, label %.preheader.lr.ph, label %._crit_edge17

.preheader.lr.ph:                                 ; preds = %1
  %8 = getelementptr inbounds i8, ptr %0, i64 12
  %9 = load i32, ptr %8, align 4, !tbaa !14
  %10 = icmp slt i32 %9, 1
  br i1 %10, label %._crit_edge17, label %.preheader

.preheader:                                       ; preds = %.preheader.lr.ph, %._crit_edge
  %11 = phi i32 [ %26, %._crit_edge ], [ %6, %.preheader.lr.ph ]
  %12 = phi i32 [ %27, %._crit_edge ], [ %9, %.preheader.lr.ph ]
  %indvars.iv = phi i64 [ %indvars.iv.next, %._crit_edge ], [ 0, %.preheader.lr.ph ]
  %.016 = phi double [ %.1.lcssa, %._crit_edge ], [ %4, %.preheader.lr.ph ]
  %.not12 = icmp slt i32 %12, 1
  br i1 %.not12, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %23
  %.114 = phi double [ %.2, %23 ], [ %.016, %.preheader ]
  %.01013 = phi i32 [ %24, %23 ], [ 1, %.preheader ]
  %13 = load ptr, ptr %0, align 8, !tbaa !5
  %14 = getelementptr inbounds %class.vec, ptr %13, i64 %indvars.iv
  %15 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZN3vecixEi(ptr noundef nonnull align 8 dereferenceable(12) %14, i32 noundef %.01013)
  %16 = load double, ptr %15, align 8, !tbaa !11
  %17 = fcmp ogt double %16, %.114
  br i1 %17, label %18, label %23

18:                                               ; preds = %.lr.ph
  %19 = load ptr, ptr %0, align 8, !tbaa !5
  %20 = getelementptr inbounds %class.vec, ptr %19, i64 %indvars.iv
  %21 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZN3vecixEi(ptr noundef nonnull align 8 dereferenceable(12) %20, i32 noundef %.01013)
  %22 = load double, ptr %21, align 8, !tbaa !11
  br label %23

23:                                               ; preds = %.lr.ph, %18
  %.2 = phi double [ %22, %18 ], [ %.114, %.lr.ph ]
  %24 = add nuw nsw i32 %.01013, 1
  %25 = load i32, ptr %8, align 4, !tbaa !14
  %.not.not = icmp slt i32 %.01013, %25
  br i1 %.not.not, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !15

._crit_edge.loopexit:                             ; preds = %23
  %.pre = load i32, ptr %5, align 8, !tbaa !13
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %.preheader
  %26 = phi i32 [ %11, %.preheader ], [ %.pre, %._crit_edge.loopexit ]
  %27 = phi i32 [ %12, %.preheader ], [ %25, %._crit_edge.loopexit ]
  %.1.lcssa = phi double [ %.016, %.preheader ], [ %.2, %._crit_edge.loopexit ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %28 = sext i32 %26 to i64
  %29 = icmp slt i64 %indvars.iv.next, %28
  br i1 %29, label %.preheader, label %._crit_edge17, !llvm.loop !17

._crit_edge17:                                    ; preds = %._crit_edge, %.preheader.lr.ph, %1
  %.0.lcssa = phi double [ %4, %1 ], [ %4, %.preheader.lr.ph ], [ %.1.lcssa, %._crit_edge ]
  ret double %.0.lcssa
}

attributes #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 = { 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" }

!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, !7, i64 0}
!6 = !{!"_ZTS3mat", !7, i64 0, !10, i64 8, !10, i64 12}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"double", !8, i64 0}
!13 = !{!6, !10, i64 8}
!14 = !{!6, !10, i64 12}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !16, !18}
!18 = !{!"llvm.loop.unswitch.partial.disable"}


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