; LLVM IR from csviragh/robotsim :: src/utilities (dynamics_utils)

; 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.phase_t = type { ptr, ptr, ptr, ptr, i32, i32 }

@GetLocalCoM.HowManyAgentsNearby = external hidden global i32, align 4
@GetLocalCoM.NeighboursPosition = external hidden global [3 x double], align 16
@GetLocalCoM.ActualAgentsPosition = external hidden global [3 x double], align 16
@GetLocalCoM.DifferenceOfPositions = external hidden global [3 x double], align 16

; 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
declare dso_local void @GetAgentsCoordinates(ptr noundef, ptr noundef, i32 noundef) #1

declare void @MultiplicateWithScalar(ptr noundef, ptr noundef, double noundef, i32 noundef) #2

; Function Attrs: nounwind
declare double @pow(double noundef, double noundef) #3

declare void @VectDifference(ptr noundef, ptr noundef, ptr noundef) #2

declare double @VectAbs(ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local void @GetLocalCoM(ptr noundef %0, ptr noundef %1, i32 noundef %2, double noundef %3) #1 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca double, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !9
  store double %3, ptr %8, align 8, !tbaa !11
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  call void @NullVect(ptr noundef %11, i32 noundef 3)
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  store i32 0, ptr @GetLocalCoM.HowManyAgentsNearby, align 4, !tbaa !9
  %12 = load ptr, ptr %6, align 8, !tbaa !5
  %13 = load i32, ptr %7, align 4, !tbaa !9
  call void @GetAgentsCoordinates(ptr noundef @GetLocalCoM.ActualAgentsPosition, ptr noundef %12, i32 noundef %13)
  store i32 0, ptr %10, align 4, !tbaa !9
  br label %14

14:                                               ; preds = %48, %4
  %15 = load i32, ptr %10, align 4, !tbaa !9
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.phase_t, ptr %16, i32 0, i32 5
  %18 = load i32, ptr %17, align 4, !tbaa !13
  %19 = icmp slt i32 %15, %18
  br i1 %19, label %20, label %51

20:                                               ; preds = %14
  %21 = load ptr, ptr %6, align 8, !tbaa !5
  %22 = load i32, ptr %10, align 4, !tbaa !9
  call void @GetAgentsCoordinates(ptr noundef @GetLocalCoM.NeighboursPosition, ptr noundef %21, i32 noundef %22)
  call void @VectDifference(ptr noundef @GetLocalCoM.DifferenceOfPositions, ptr noundef @GetLocalCoM.ActualAgentsPosition, ptr noundef @GetLocalCoM.NeighboursPosition)
  %23 = call double @VectAbs(ptr noundef @GetLocalCoM.DifferenceOfPositions)
  %24 = load double, ptr %8, align 8, !tbaa !11
  %25 = fcmp ole double %23, %24
  br i1 %25, label %26, label %47

26:                                               ; preds = %20
  store i32 0, ptr %9, align 4, !tbaa !9
  br label %27

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

30:                                               ; preds = %27
  %31 = load i32, ptr %9, align 4, !tbaa !9
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [3 x double], ptr @GetLocalCoM.NeighboursPosition, i64 0, i64 %32
  %34 = load double, ptr %33, align 8, !tbaa !11
  %35 = load ptr, ptr %5, align 8, !tbaa !5
  %36 = load i32, ptr %9, align 4, !tbaa !9
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds double, ptr %35, i64 %37
  %39 = load double, ptr %38, align 8, !tbaa !11
  %40 = fadd double %39, %34
  store double %40, ptr %38, align 8, !tbaa !11
  br label %41

41:                                               ; preds = %30
  %42 = load i32, ptr %9, align 4, !tbaa !9
  %43 = add nsw i32 %42, 1
  store i32 %43, ptr %9, align 4, !tbaa !9
  br label %27, !llvm.loop !15

44:                                               ; preds = %27
  %45 = load i32, ptr @GetLocalCoM.HowManyAgentsNearby, align 4, !tbaa !9
  %46 = add nsw i32 %45, 1
  store i32 %46, ptr @GetLocalCoM.HowManyAgentsNearby, align 4, !tbaa !9
  br label %47

47:                                               ; preds = %44, %20
  br label %48

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

51:                                               ; preds = %14
  %52 = load ptr, ptr %5, align 8, !tbaa !5
  %53 = load ptr, ptr %5, align 8, !tbaa !5
  %54 = load i32, ptr @GetLocalCoM.HowManyAgentsNearby, align 4, !tbaa !9
  %55 = sitofp i32 %54 to double
  %56 = call double @pow(double noundef %55, double noundef -1.000000e+00) #4
  call void @MultiplicateWithScalar(ptr noundef %52, ptr noundef %53, double noundef %56, i32 noundef 3)
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  ret void
}

declare void @NullVect(ptr noundef, i32 noundef) #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 = { "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 "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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"double", !7, i64 0}
!13 = !{!14, !10, i64 36}
!14 = !{!"", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !10, i64 32, !10, i64 36}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !16}


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

@GetLocalCoM.HowManyAgentsNearby = external hidden local_unnamed_addr global i32, align 4
@GetLocalCoM.NeighboursPosition = external hidden global [3 x double], align 16
@GetLocalCoM.ActualAgentsPosition = external hidden global [3 x double], align 16
@GetLocalCoM.DifferenceOfPositions = external hidden global [3 x double], align 16

; Function Attrs: nounwind uwtable
declare dso_local void @GetAgentsCoordinates(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #0

declare void @MultiplicateWithScalar(ptr noundef, ptr noundef, double noundef, i32 noundef) local_unnamed_addr #1

declare void @VectDifference(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

declare double @VectAbs(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @GetLocalCoM(ptr noundef %0, ptr noundef %1, i32 noundef %2, double noundef %3) local_unnamed_addr #0 {
  tail call void @NullVect(ptr noundef %0, i32 noundef 3) #2
  store i32 0, ptr @GetLocalCoM.HowManyAgentsNearby, align 4, !tbaa !5
  tail call void @GetAgentsCoordinates(ptr noundef nonnull @GetLocalCoM.ActualAgentsPosition, ptr noundef %1, i32 noundef %2)
  %5 = getelementptr inbounds i8, ptr %1, i64 36
  %6 = load i32, ptr %5, align 4, !tbaa !9
  %7 = icmp sgt i32 %6, 0
  br i1 %7, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %4
  %8 = getelementptr inbounds i8, ptr %0, i64 8
  %9 = getelementptr inbounds i8, ptr %0, i64 16
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %23
  %.017 = phi i32 [ %24, %23 ], [ 0, %.lr.ph.preheader ]
  tail call void @GetAgentsCoordinates(ptr noundef nonnull @GetLocalCoM.NeighboursPosition, ptr noundef nonnull %1, i32 noundef %.017)
  tail call void @VectDifference(ptr noundef nonnull @GetLocalCoM.DifferenceOfPositions, ptr noundef nonnull @GetLocalCoM.ActualAgentsPosition, ptr noundef nonnull @GetLocalCoM.NeighboursPosition) #2
  %10 = tail call double @VectAbs(ptr noundef nonnull @GetLocalCoM.DifferenceOfPositions) #2
  %11 = fcmp ugt double %10, %3
  br i1 %11, label %23, label %.preheader.preheader

.preheader.preheader:                             ; preds = %.lr.ph
  %12 = load double, ptr @GetLocalCoM.NeighboursPosition, align 16, !tbaa !12
  %13 = load double, ptr %0, align 8, !tbaa !12
  %14 = fadd double %12, %13
  store double %14, ptr %0, align 8, !tbaa !12
  %15 = load double, ptr getelementptr inbounds (i8, ptr @GetLocalCoM.NeighboursPosition, i64 8), align 8, !tbaa !12
  %16 = load double, ptr %8, align 8, !tbaa !12
  %17 = fadd double %15, %16
  store double %17, ptr %8, align 8, !tbaa !12
  %18 = load double, ptr getelementptr inbounds (i8, ptr @GetLocalCoM.NeighboursPosition, i64 16), align 16, !tbaa !12
  %19 = load double, ptr %9, align 8, !tbaa !12
  %20 = fadd double %18, %19
  store double %20, ptr %9, align 8, !tbaa !12
  %21 = load i32, ptr @GetLocalCoM.HowManyAgentsNearby, align 4, !tbaa !5
  %22 = add nsw i32 %21, 1
  store i32 %22, ptr @GetLocalCoM.HowManyAgentsNearby, align 4, !tbaa !5
  br label %23

23:                                               ; preds = %.lr.ph, %.preheader.preheader
  %24 = add nuw nsw i32 %.017, 1
  %25 = load i32, ptr %5, align 4, !tbaa !9
  %26 = icmp slt i32 %24, %25
  br i1 %26, label %.lr.ph, label %._crit_edge, !llvm.loop !14

._crit_edge:                                      ; preds = %23, %4
  %27 = load i32, ptr @GetLocalCoM.HowManyAgentsNearby, align 4, !tbaa !5
  %28 = sitofp i32 %27 to double
  %reciprocal = fdiv double 1.000000e+00, %28
  tail call void @MultiplicateWithScalar(ptr noundef %0, ptr noundef %0, double noundef %reciprocal, i32 noundef 3) #2
  ret void
}

declare void @NullVect(ptr noundef, i32 noundef) local_unnamed_addr #1

attributes #0 = { 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 #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 = { 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, !6, i64 36}
!10 = !{!"", !11, i64 0, !11, i64 8, !11, i64 16, !11, i64 24, !6, i64 32, !6, i64 36}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"double", !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


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