; LLVM IR from buresjan/optim_lbm :: nomad/ext/sgtelib/src/Matrix.ll (ZNK7SGTELIB6Matrix4rmseEv)

; 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.SGTELIB::Matrix" = type { ptr, %"class.std::__cxx11::basic_string", i32, i32, ptr }
%"class.std::__cxx11::basic_string" = type { %"struct.std::__cxx11::basic_string<char>::_Alloc_hider", i64, %union.anon }
%"struct.std::__cxx11::basic_string<char>::_Alloc_hider" = type { ptr }
%union.anon = type { i64, [8 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

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

; Function Attrs: nounwind
declare double @sqrt(double noundef) #2

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef double @_ZNK7SGTELIB6Matrix4rmseEv(ptr noundef nonnull align 8 dereferenceable(56) %0) #3 align 2 {
  %2 = alloca ptr, align 8
  %3 = alloca double, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca double, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  %7 = load ptr, ptr %2, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  store double 0.000000e+00, ptr %3, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  store i32 0, ptr %4, align 4, !tbaa !11
  br label %8

8:                                                ; preds = %38, %1
  %9 = load i32, ptr %4, align 4, !tbaa !11
  %10 = getelementptr inbounds %"class.SGTELIB::Matrix", ptr %7, i32 0, i32 2
  %11 = load i32, ptr %10, align 8, !tbaa !13
  %12 = icmp slt i32 %9, %11
  br i1 %12, label %13, label %41

13:                                               ; preds = %8
  store i32 0, ptr %5, align 4, !tbaa !11
  br label %14

14:                                               ; preds = %34, %13
  %15 = load i32, ptr %5, align 4, !tbaa !11
  %16 = getelementptr inbounds %"class.SGTELIB::Matrix", ptr %7, i32 0, i32 3
  %17 = load i32, ptr %16, align 4, !tbaa !18
  %18 = icmp slt i32 %15, %17
  br i1 %18, label %19, label %37

19:                                               ; preds = %14
  %20 = getelementptr inbounds %"class.SGTELIB::Matrix", ptr %7, i32 0, i32 4
  %21 = load ptr, ptr %20, align 8, !tbaa !19
  %22 = load i32, ptr %4, align 4, !tbaa !11
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds ptr, ptr %21, i64 %23
  %25 = load ptr, ptr %24, align 8, !tbaa !5
  %26 = load i32, ptr %5, align 4, !tbaa !11
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds double, ptr %25, i64 %27
  %29 = load double, ptr %28, align 8, !tbaa !9
  store double %29, ptr %6, align 8, !tbaa !9
  %30 = load double, ptr %6, align 8, !tbaa !9
  %31 = load double, ptr %6, align 8, !tbaa !9
  %32 = load double, ptr %3, align 8, !tbaa !9
  %33 = call double @llvm.fmuladd.f64(double %30, double %31, double %32)
  store double %33, ptr %3, align 8, !tbaa !9
  br label %34

34:                                               ; preds = %19
  %35 = load i32, ptr %5, align 4, !tbaa !11
  %36 = add nsw i32 %35, 1
  store i32 %36, ptr %5, align 4, !tbaa !11
  br label %14, !llvm.loop !20

37:                                               ; preds = %14
  br label %38

38:                                               ; preds = %37
  %39 = load i32, ptr %4, align 4, !tbaa !11
  %40 = add nsw i32 %39, 1
  store i32 %40, ptr %4, align 4, !tbaa !11
  br label %8, !llvm.loop !22

41:                                               ; preds = %8
  %42 = getelementptr inbounds %"class.SGTELIB::Matrix", ptr %7, i32 0, i32 2
  %43 = load i32, ptr %42, align 8, !tbaa !13
  %44 = getelementptr inbounds %"class.SGTELIB::Matrix", ptr %7, i32 0, i32 3
  %45 = load i32, ptr %44, align 4, !tbaa !18
  %46 = mul nsw i32 %43, %45
  %47 = sitofp i32 %46 to double
  %48 = load double, ptr %3, align 8, !tbaa !9
  %49 = fdiv double %48, %47
  store double %49, ptr %3, align 8, !tbaa !9
  %50 = load double, ptr %3, align 8, !tbaa !9
  %51 = call double @sqrt(double noundef %50) #4
  store double %51, ptr %3, align 8, !tbaa !9
  %52 = load double, ptr %3, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  ret double %52
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #2 = { nounwind "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 = { 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 #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 = !{!"double", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !12, i64 40}
!14 = !{!"_ZTSN7SGTELIB6MatrixE", !15, i64 8, !12, i64 40, !12, i64 44, !6, i64 48}
!15 = !{!"_ZTSNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE", !16, i64 0, !17, i64 8, !7, i64 16}
!16 = !{!"_ZTSNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_Alloc_hiderE", !6, i64 0}
!17 = !{!"long", !7, i64 0}
!18 = !{!14, !12, i64 44}
!19 = !{!14, !6, i64 48}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}
!22 = distinct !{!22, !21}


; 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 nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.fmuladd.f64(double, double, double) #0

; Function Attrs: mustprogress nofree nounwind willreturn memory(write)
declare double @sqrt(double noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nofree nounwind memory(readwrite, inaccessiblemem: write) uwtable
define dso_local noundef double @_ZNK7SGTELIB6Matrix4rmseEv(ptr nocapture noundef nonnull readonly align 8 dereferenceable(56) %0) local_unnamed_addr #2 align 2 {
  %2 = getelementptr inbounds i8, ptr %0, i64 40
  %3 = load i32, ptr %2, align 8, !tbaa !5
  %4 = icmp sgt i32 %3, 0
  %5 = getelementptr inbounds i8, ptr %0, i64 44
  %6 = load i32, ptr %5, align 4, !tbaa !14
  br i1 %4, label %.preheader.lr.ph, label %._crit_edge19

.preheader.lr.ph:                                 ; preds = %1
  %7 = icmp sgt i32 %6, 0
  %8 = getelementptr inbounds i8, ptr %0, i64 48
  %9 = load ptr, ptr %8, align 8
  br i1 %7, label %.preheader.us.preheader, label %._crit_edge19

.preheader.us.preheader:                          ; preds = %.preheader.lr.ph
  %wide.trip.count28 = zext nneg i32 %3 to i64
  %wide.trip.count = zext nneg i32 %6 to i64
  %xtraiter = and i64 %wide.trip.count, 3
  %10 = icmp ult i32 %6, 4
  %unroll_iter = and i64 %wide.trip.count, 2147483644
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br label %.preheader.us

.preheader.us:                                    ; preds = %.preheader.us.preheader, %._crit_edge.us
  %indvars.iv25 = phi i64 [ 0, %.preheader.us.preheader ], [ %indvars.iv.next26, %._crit_edge.us ]
  %.018.us = phi double [ 0.000000e+00, %.preheader.us.preheader ], [ %.lcssa, %._crit_edge.us ]
  %11 = getelementptr inbounds ptr, ptr %9, i64 %indvars.iv25
  %12 = load ptr, ptr %11, align 8, !tbaa !15
  br i1 %10, label %._crit_edge.us.unr-lcssa, label %.preheader.us.new

.preheader.us.new:                                ; preds = %.preheader.us, %.preheader.us.new
  %indvars.iv = phi i64 [ %indvars.iv.next.3, %.preheader.us.new ], [ 0, %.preheader.us ]
  %.116.us = phi double [ %24, %.preheader.us.new ], [ %.018.us, %.preheader.us ]
  %niter = phi i64 [ %niter.next.3, %.preheader.us.new ], [ 0, %.preheader.us ]
  %13 = getelementptr inbounds double, ptr %12, i64 %indvars.iv
  %14 = load double, ptr %13, align 8, !tbaa !16
  %15 = tail call double @llvm.fmuladd.f64(double %14, double %14, double %.116.us)
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %16 = getelementptr inbounds double, ptr %12, i64 %indvars.iv.next
  %17 = load double, ptr %16, align 8, !tbaa !16
  %18 = tail call double @llvm.fmuladd.f64(double %17, double %17, double %15)
  %indvars.iv.next.1 = or disjoint i64 %indvars.iv, 2
  %19 = getelementptr inbounds double, ptr %12, i64 %indvars.iv.next.1
  %20 = load double, ptr %19, align 8, !tbaa !16
  %21 = tail call double @llvm.fmuladd.f64(double %20, double %20, double %18)
  %indvars.iv.next.2 = or disjoint i64 %indvars.iv, 3
  %22 = getelementptr inbounds double, ptr %12, i64 %indvars.iv.next.2
  %23 = load double, ptr %22, align 8, !tbaa !16
  %24 = tail call double @llvm.fmuladd.f64(double %23, double %23, double %21)
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %niter.next.3 = add i64 %niter, 4
  %niter.ncmp.3 = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %._crit_edge.us.unr-lcssa, label %.preheader.us.new, !llvm.loop !18

._crit_edge.us.unr-lcssa:                         ; preds = %.preheader.us.new, %.preheader.us
  %.lcssa.ph = phi double [ poison, %.preheader.us ], [ %24, %.preheader.us.new ]
  %indvars.iv.unr = phi i64 [ 0, %.preheader.us ], [ %indvars.iv.next.3, %.preheader.us.new ]
  %.116.us.unr = phi double [ %.018.us, %.preheader.us ], [ %24, %.preheader.us.new ]
  br i1 %lcmp.mod.not, label %._crit_edge.us, label %.epil.preheader

.epil.preheader:                                  ; preds = %._crit_edge.us.unr-lcssa, %.epil.preheader
  %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %.epil.preheader ], [ %indvars.iv.unr, %._crit_edge.us.unr-lcssa ]
  %.116.us.epil = phi double [ %27, %.epil.preheader ], [ %.116.us.unr, %._crit_edge.us.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.epil.preheader ], [ 0, %._crit_edge.us.unr-lcssa ]
  %25 = getelementptr inbounds double, ptr %12, i64 %indvars.iv.epil
  %26 = load double, ptr %25, align 8, !tbaa !16
  %27 = tail call double @llvm.fmuladd.f64(double %26, double %26, double %.116.us.epil)
  %indvars.iv.next.epil = add nuw 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 %._crit_edge.us, label %.epil.preheader, !llvm.loop !20

._crit_edge.us:                                   ; preds = %.epil.preheader, %._crit_edge.us.unr-lcssa
  %.lcssa = phi double [ %.lcssa.ph, %._crit_edge.us.unr-lcssa ], [ %27, %.epil.preheader ]
  %indvars.iv.next26 = add nuw nsw i64 %indvars.iv25, 1
  %exitcond29.not = icmp eq i64 %indvars.iv.next26, %wide.trip.count28
  br i1 %exitcond29.not, label %._crit_edge19, label %.preheader.us, !llvm.loop !22

._crit_edge19:                                    ; preds = %._crit_edge.us, %1, %.preheader.lr.ph
  %.0.lcssa = phi double [ 0.000000e+00, %.preheader.lr.ph ], [ 0.000000e+00, %1 ], [ %.lcssa, %._crit_edge.us ]
  %28 = mul nsw i32 %6, %3
  %29 = sitofp i32 %28 to double
  %30 = fdiv double %.0.lcssa, %29
  %31 = tail call double @sqrt(double noundef %30) #3
  ret double %31
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #1 = { mustprogress nofree nounwind willreturn memory(write) "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 nofree nounwind memory(readwrite, inaccessiblemem: write) 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, !13, i64 40}
!6 = !{!"_ZTSN7SGTELIB6MatrixE", !7, i64 8, !13, i64 40, !13, i64 44, !9, i64 48}
!7 = !{!"_ZTSNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE", !8, i64 0, !12, i64 8, !10, i64 16}
!8 = !{!"_ZTSNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_Alloc_hiderE", !9, i64 0}
!9 = !{!"any pointer", !10, i64 0}
!10 = !{!"omnipotent char", !11, i64 0}
!11 = !{!"Simple C++ TBAA"}
!12 = !{!"long", !10, i64 0}
!13 = !{!"int", !10, i64 0}
!14 = !{!6, !13, i64 44}
!15 = !{!9, !9, i64 0}
!16 = !{!17, !17, i64 0}
!17 = !{!"double", !10, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.unroll.disable"}
!22 = distinct !{!22, !19}


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