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

@NumberOfNearbyUnits.NumberOfNeighbours = external hidden global i32, align 4
@NumberOfNearbyUnits.Temp2 = 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

; Function Attrs: nounwind uwtable
define dso_local i32 @NumberOfNearbyUnits(ptr noundef %0, i32 noundef %1, double noundef %2) #1 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca double, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store double %2, ptr %6, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 0, ptr @NumberOfNearbyUnits.NumberOfNeighbours, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.phase_t, ptr %9, i32 0, i32 0
  %11 = load ptr, ptr %10, align 8, !tbaa !13
  %12 = load i32, ptr %5, align 4, !tbaa !9
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds ptr, ptr %11, i64 %13
  %15 = load ptr, ptr %14, align 8, !tbaa !5
  store ptr %15, ptr %8, align 8, !tbaa !5
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %16

16:                                               ; preds = %33, %3
  %17 = load i32, ptr %7, align 4, !tbaa !9
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.phase_t, ptr %18, i32 0, i32 5
  %20 = load i32, ptr %19, align 4, !tbaa !15
  %21 = icmp slt i32 %17, %20
  br i1 %21, label %22, label %36

22:                                               ; preds = %16
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = load i32, ptr %7, align 4, !tbaa !9
  call void @GetAgentsCoordinates(ptr noundef @NumberOfNearbyUnits.Temp2, ptr noundef %23, i32 noundef %24)
  %25 = load ptr, ptr %8, align 8, !tbaa !5
  call void @VectDifference(ptr noundef @NumberOfNearbyUnits.Temp2, ptr noundef @NumberOfNearbyUnits.Temp2, ptr noundef %25)
  %26 = call double @VectAbs(ptr noundef @NumberOfNearbyUnits.Temp2)
  %27 = load double, ptr %6, align 8, !tbaa !11
  %28 = fcmp ole double %26, %27
  br i1 %28, label %29, label %32

29:                                               ; preds = %22
  %30 = load i32, ptr @NumberOfNearbyUnits.NumberOfNeighbours, align 4, !tbaa !9
  %31 = add nsw i32 %30, 1
  store i32 %31, ptr @NumberOfNearbyUnits.NumberOfNeighbours, align 4, !tbaa !9
  br label %32

32:                                               ; preds = %29, %22
  br label %33

33:                                               ; preds = %32
  %34 = load i32, ptr %7, align 4, !tbaa !9
  %35 = add nsw i32 %34, 1
  store i32 %35, ptr %7, align 4, !tbaa !9
  br label %16, !llvm.loop !16

36:                                               ; preds = %16
  %37 = load i32, ptr @NumberOfNearbyUnits.NumberOfNeighbours, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  ret i32 %37
}

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

declare double @VectAbs(ptr 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 }

!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, !6, i64 0}
!14 = !{!"", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !10, i64 32, !10, i64 36}
!15 = !{!14, !10, i64 36}
!16 = distinct !{!16, !17}
!17 = !{!"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"

@NumberOfNearbyUnits.NumberOfNeighbours = external hidden local_unnamed_addr global i32, align 4
@NumberOfNearbyUnits.Temp2 = 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

; Function Attrs: nounwind uwtable
define dso_local i32 @NumberOfNearbyUnits(ptr noundef %0, i32 noundef %1, double noundef %2) local_unnamed_addr #0 {
  store i32 0, ptr @NumberOfNearbyUnits.NumberOfNeighbours, align 4, !tbaa !5
  %4 = load ptr, ptr %0, align 8, !tbaa !9
  %5 = sext i32 %1 to i64
  %6 = getelementptr inbounds ptr, ptr %4, i64 %5
  %7 = load ptr, ptr %6, align 8, !tbaa !12
  %8 = getelementptr inbounds i8, ptr %0, i64 36
  %9 = load i32, ptr %8, align 4, !tbaa !13
  %10 = icmp sgt i32 %9, 0
  br i1 %10, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %3, %16
  %.08 = phi i32 [ %17, %16 ], [ 0, %3 ]
  tail call void @GetAgentsCoordinates(ptr noundef nonnull @NumberOfNearbyUnits.Temp2, ptr noundef nonnull %0, i32 noundef %.08)
  tail call void @VectDifference(ptr noundef nonnull @NumberOfNearbyUnits.Temp2, ptr noundef nonnull @NumberOfNearbyUnits.Temp2, ptr noundef %7) #2
  %11 = tail call double @VectAbs(ptr noundef nonnull @NumberOfNearbyUnits.Temp2) #2
  %12 = fcmp ugt double %11, %2
  br i1 %12, label %16, label %13

13:                                               ; preds = %.lr.ph
  %14 = load i32, ptr @NumberOfNearbyUnits.NumberOfNeighbours, align 4, !tbaa !5
  %15 = add nsw i32 %14, 1
  store i32 %15, ptr @NumberOfNearbyUnits.NumberOfNeighbours, align 4, !tbaa !5
  br label %16

16:                                               ; preds = %.lr.ph, %13
  %17 = add nuw nsw i32 %.08, 1
  %18 = load i32, ptr %8, align 4, !tbaa !13
  %19 = icmp slt i32 %17, %18
  br i1 %19, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !14

._crit_edge.loopexit:                             ; preds = %16
  %.pre = load i32, ptr @NumberOfNearbyUnits.NumberOfNeighbours, align 4, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %3
  %20 = phi i32 [ %.pre, %._crit_edge.loopexit ], [ 0, %3 ]
  ret i32 %20
}

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

declare double @VectAbs(ptr 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, !11, i64 0}
!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 = !{!11, !11, i64 0}
!13 = !{!10, !6, i64 36}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass