; LLVM IR from bonamarton/phys_num :: multi_dim_fitt (multiDimFitt)

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

%struct.tensor = type { ptr, i32, i32, ptr }

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind uwtable
define dso_local double @loss(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca double, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.tensor, ptr %8, i32 0, i32 2
  %10 = load i32, ptr %9, align 4, !tbaa !9
  %11 = sub nsw i32 %10, 2
  store i32 %11, ptr %6, align 4, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  store double 0.000000e+00, ptr %7, align 8, !tbaa !13
  store i32 0, ptr %5, align 4, !tbaa !12
  br label %12

12:                                               ; preds = %58, %2
  %13 = load i32, ptr %5, align 4, !tbaa !12
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.tensor, ptr %14, i32 0, i32 1
  %16 = load i32, ptr %15, align 8, !tbaa !15
  %17 = icmp slt i32 %13, %16
  br i1 %17, label %18, label %61

18:                                               ; preds = %12
  %19 = load ptr, ptr %3, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.tensor, ptr %19, i32 0, i32 0
  %21 = load ptr, ptr %20, align 8, !tbaa !16
  %22 = load i32, ptr %5, align 4, !tbaa !12
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.tensor, ptr %23, i32 0, i32 2
  %25 = load i32, ptr %24, align 4, !tbaa !9
  %26 = mul nsw i32 %22, %25
  %27 = load i32, ptr %6, align 4, !tbaa !12
  %28 = add nsw i32 %26, %27
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds double, ptr %21, i64 %29
  %31 = load double, ptr %30, align 8, !tbaa !13
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.tensor, ptr %32, i32 0, i32 0
  %34 = load ptr, ptr %33, align 8, !tbaa !16
  %35 = load i32, ptr %5, align 4, !tbaa !12
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds double, ptr %34, i64 %36
  %38 = load double, ptr %37, align 8, !tbaa !13
  %39 = fsub double %31, %38
  %40 = call double @llvm.fabs.f64(double %39)
  %41 = load ptr, ptr %3, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.tensor, ptr %41, i32 0, i32 0
  %43 = load ptr, ptr %42, align 8, !tbaa !16
  %44 = load i32, ptr %5, align 4, !tbaa !12
  %45 = load ptr, ptr %3, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.tensor, ptr %45, i32 0, i32 2
  %47 = load i32, ptr %46, align 4, !tbaa !9
  %48 = mul nsw i32 %44, %47
  %49 = load i32, ptr %6, align 4, !tbaa !12
  %50 = add nsw i32 %48, %49
  %51 = add nsw i32 %50, 1
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds double, ptr %43, i64 %52
  %54 = load double, ptr %53, align 8, !tbaa !13
  %55 = fdiv double %40, %54
  %56 = load double, ptr %7, align 8, !tbaa !13
  %57 = fadd double %56, %55
  store double %57, ptr %7, align 8, !tbaa !13
  br label %58

58:                                               ; preds = %18
  %59 = load i32, ptr %5, align 4, !tbaa !12
  %60 = add nsw i32 %59, 1
  store i32 %60, ptr %5, align 4, !tbaa !12
  br label %12, !llvm.loop !17

61:                                               ; preds = %12
  %62 = load double, ptr %7, align 8, !tbaa !13
  %63 = fdiv double %62, 1.000000e+03
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  ret double %63
}

; 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.fabs.f64(double) #2

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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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/C++ TBAA"}
!9 = !{!10, !11, i64 12}
!10 = !{!"tensor", !6, i64 0, !11, i64 8, !11, i64 12, !6, i64 16}
!11 = !{!"int", !7, i64 0}
!12 = !{!11, !11, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"double", !7, i64 0}
!15 = !{!10, !11, i64 8}
!16 = !{!10, !6, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


; 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(read, inaccessiblemem: none) uwtable
define dso_local double @loss(ptr nocapture noundef readonly %0, ptr nocapture noundef readonly %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 8
  %4 = load i32, ptr %3, align 8, !tbaa !5
  %5 = icmp sgt i32 %4, 0
  br i1 %5, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %2
  %6 = getelementptr inbounds i8, ptr %0, i64 12
  %7 = load i32, ptr %6, align 4, !tbaa !11
  %8 = load ptr, ptr %0, align 8, !tbaa !12
  %9 = load ptr, ptr %1, align 8, !tbaa !12
  %10 = sext i32 %7 to i64
  %wide.trip.count = zext nneg i32 %4 to i64
  %11 = sext i32 %7 to i64
  %invariant.gep = getelementptr double, ptr %8, i64 %11
  %xtraiter = and i64 %wide.trip.count, 1
  %12 = icmp eq i32 %4, 1
  br i1 %12, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.new

.lr.ph.new:                                       ; preds = %.lr.ph
  %unroll_iter = and i64 %wide.trip.count, 2147483646
  br label %13

13:                                               ; preds = %13, %.lr.ph.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.new ], [ %indvars.iv.next.1, %13 ]
  %.017 = phi double [ 0.000000e+00, %.lr.ph.new ], [ %35, %13 ]
  %niter = phi i64 [ 0, %.lr.ph.new ], [ %niter.next.1, %13 ]
  %14 = mul nsw i64 %indvars.iv, %10
  %gep = getelementptr double, ptr %invariant.gep, i64 %14
  %15 = getelementptr i8, ptr %gep, i64 -16
  %16 = load double, ptr %15, align 8, !tbaa !13
  %17 = getelementptr inbounds double, ptr %9, i64 %indvars.iv
  %18 = load double, ptr %17, align 8, !tbaa !13
  %19 = fsub double %16, %18
  %20 = tail call double @llvm.fabs.f64(double %19)
  %21 = getelementptr i8, ptr %gep, i64 -8
  %22 = load double, ptr %21, align 8, !tbaa !13
  %23 = fdiv double %20, %22
  %24 = fadd double %.017, %23
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %25 = mul nsw i64 %indvars.iv.next, %10
  %gep.1 = getelementptr double, ptr %invariant.gep, i64 %25
  %26 = getelementptr i8, ptr %gep.1, i64 -16
  %27 = load double, ptr %26, align 8, !tbaa !13
  %28 = getelementptr inbounds double, ptr %9, i64 %indvars.iv.next
  %29 = load double, ptr %28, align 8, !tbaa !13
  %30 = fsub double %27, %29
  %31 = tail call double @llvm.fabs.f64(double %30)
  %32 = getelementptr i8, ptr %gep.1, i64 -8
  %33 = load double, ptr %32, align 8, !tbaa !13
  %34 = fdiv double %31, %33
  %35 = fadd double %24, %34
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 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_edge.loopexit.unr-lcssa, label %13, !llvm.loop !15

._crit_edge.loopexit.unr-lcssa:                   ; preds = %13, %.lr.ph
  %.lcssa.ph = phi double [ poison, %.lr.ph ], [ %35, %13 ]
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next.1, %13 ]
  %.017.unr = phi double [ 0.000000e+00, %.lr.ph ], [ %35, %13 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %._crit_edge.loopexit.epilog-lcssa

._crit_edge.loopexit.epilog-lcssa:                ; preds = %._crit_edge.loopexit.unr-lcssa
  %36 = mul nsw i64 %indvars.iv.unr, %10
  %gep.epil = getelementptr double, ptr %invariant.gep, i64 %36
  %37 = getelementptr i8, ptr %gep.epil, i64 -16
  %38 = load double, ptr %37, align 8, !tbaa !13
  %39 = getelementptr inbounds double, ptr %9, i64 %indvars.iv.unr
  %40 = load double, ptr %39, align 8, !tbaa !13
  %41 = fsub double %38, %40
  %42 = tail call double @llvm.fabs.f64(double %41)
  %43 = getelementptr i8, ptr %gep.epil, i64 -8
  %44 = load double, ptr %43, align 8, !tbaa !13
  %45 = fdiv double %42, %44
  %46 = fadd double %.017.unr, %45
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit.epilog-lcssa, %._crit_edge.loopexit.unr-lcssa, %2
  %.0.lcssa = phi double [ 0.000000e+00, %2 ], [ %.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %46, %._crit_edge.loopexit.epilog-lcssa ]
  %47 = fdiv double %.0.lcssa, 1.000000e+03
  ret double %47
}

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

attributes #0 = { nofree norecurse nosync nounwind memory(read, 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 = { mustprogress 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, !10, i64 8}
!6 = !{!"tensor", !7, i64 0, !10, i64 8, !10, i64 12, !7, i64 16}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!6, !10, i64 12}
!12 = !{!6, !7, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"double", !8, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


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