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

@SelectNearbyVisibleAgents.DistFromRef = external hidden global [3 x double], align 16
@SelectNearbyVisibleAgents.Dist = external hidden global double, align 8

; 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 @VectDifference(ptr noundef, ptr noundef, ptr noundef) #2

declare double @VectAbs(ptr noundef) #2

declare void @NullVect(ptr noundef, i32 noundef) #2

declare double @randomizeDouble(double noundef, double noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local void @SwapAgents(ptr noundef, i32 noundef, i32 noundef) #1

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

16:                                               ; preds = %60, %4
  %17 = load i32, ptr %9, align 4, !tbaa !11
  %18 = load i32, ptr %11, align 4, !tbaa !11
  %19 = icmp sge i32 %17, %18
  br i1 %19, label %20, label %63

20:                                               ; preds = %16
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = load i32, ptr %9, align 4, !tbaa !11
  call void @GetAgentsCoordinates(ptr noundef @SelectNearbyVisibleAgents.DistFromRef, ptr noundef %21, i32 noundef %22)
  %23 = load ptr, ptr %6, align 8, !tbaa !5
  call void @VectDifference(ptr noundef @SelectNearbyVisibleAgents.DistFromRef, ptr noundef @SelectNearbyVisibleAgents.DistFromRef, ptr noundef %23)
  %24 = call double @VectAbs(ptr noundef @SelectNearbyVisibleAgents.DistFromRef)
  store double %24, ptr @SelectNearbyVisibleAgents.Dist, align 8, !tbaa !9
  %25 = call double @randomizeDouble(double noundef 0.000000e+00, double noundef 1.000000e+00)
  %26 = load double, ptr @SelectNearbyVisibleAgents.Dist, align 8, !tbaa !9
  %27 = load double, ptr @SelectNearbyVisibleAgents.Dist, align 8, !tbaa !9
  %28 = fmul double %26, %27
  %29 = load double, ptr %8, align 8, !tbaa !9
  %30 = fmul double %28, %29
  %31 = fcmp olt double %25, %30
  %32 = zext i1 %31 to i8
  store i8 %32, ptr %10, align 1, !tbaa !15
  %33 = load double, ptr @SelectNearbyVisibleAgents.Dist, align 8, !tbaa !9
  %34 = fcmp une double %33, 0.000000e+00
  br i1 %34, label %35, label %50

35:                                               ; preds = %20
  %36 = load double, ptr @SelectNearbyVisibleAgents.Dist, align 8, !tbaa !9
  %37 = load double, ptr %7, align 8, !tbaa !9
  %38 = fcmp ole double %36, %37
  br i1 %38, label %39, label %50

39:                                               ; preds = %35
  %40 = load i8, ptr %10, align 1, !tbaa !15, !range !17, !noundef !18
  %41 = trunc i8 %40 to i1
  br i1 %41, label %50, label %42

42:                                               ; preds = %39
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  %44 = load i32, ptr %9, align 4, !tbaa !11
  %45 = load i32, ptr %11, align 4, !tbaa !11
  call void @SwapAgents(ptr noundef %43, i32 noundef %44, i32 noundef %45)
  %46 = load i32, ptr %11, align 4, !tbaa !11
  %47 = add nsw i32 %46, 1
  store i32 %47, ptr %11, align 4, !tbaa !11
  %48 = load i32, ptr %9, align 4, !tbaa !11
  %49 = add nsw i32 %48, 1
  store i32 %49, ptr %9, align 4, !tbaa !11
  br label %59

50:                                               ; preds = %39, %35, %20
  %51 = load double, ptr @SelectNearbyVisibleAgents.Dist, align 8, !tbaa !9
  %52 = fcmp oeq double %51, 0.000000e+00
  br i1 %52, label %53, label %58

53:                                               ; preds = %50
  %54 = load ptr, ptr %5, align 8, !tbaa !5
  %55 = load i32, ptr %9, align 4, !tbaa !11
  call void @SwapAgents(ptr noundef %54, i32 noundef %55, i32 noundef 0)
  %56 = load i32, ptr %9, align 4, !tbaa !11
  %57 = add nsw i32 %56, 1
  store i32 %57, ptr %9, align 4, !tbaa !11
  br label %58

58:                                               ; preds = %53, %50
  br label %59

59:                                               ; preds = %58, %42
  br label %60

60:                                               ; preds = %59
  %61 = load i32, ptr %9, align 4, !tbaa !11
  %62 = add nsw i32 %61, -1
  store i32 %62, ptr %9, align 4, !tbaa !11
  br label %16, !llvm.loop !19

63:                                               ; preds = %16
  %64 = load i32, ptr %11, align 4, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  ret i32 %64
}

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 = !{!"double", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !12, i64 36}
!14 = !{!"", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !12, i64 32, !12, i64 36}
!15 = !{!16, !16, i64 0}
!16 = !{!"_Bool", !7, i64 0}
!17 = !{i8 0, i8 2}
!18 = !{}
!19 = distinct !{!19, !20}
!20 = !{!"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"

@SelectNearbyVisibleAgents.DistFromRef = external hidden global [3 x double], align 16
@SelectNearbyVisibleAgents.Dist = external hidden local_unnamed_addr global double, align 8

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

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

declare double @VectAbs(ptr noundef) local_unnamed_addr #1

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

declare double @randomizeDouble(double noundef, double noundef) local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 -2147483647, -2147483648) i32 @SelectNearbyVisibleAgents(ptr noundef %0, ptr noundef %1, double noundef %2, double noundef %3) local_unnamed_addr #0 {
  tail call void @NullVect(ptr noundef nonnull @SelectNearbyVisibleAgents.DistFromRef, i32 noundef 3) #2
  %5 = getelementptr inbounds i8, ptr %0, i64 36
  %6 = load i32, ptr %5, align 4, !tbaa !5
  %.not.not22 = icmp sgt i32 %6, 1
  br i1 %.not.not22, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %4, %19
  %.024 = phi i32 [ %.1, %19 ], [ 1, %4 ]
  %.018.in23 = phi i32 [ %.119, %19 ], [ %6, %4 ]
  %.01825 = add nsw i32 %.018.in23, -1
  tail call void @GetAgentsCoordinates(ptr noundef nonnull @SelectNearbyVisibleAgents.DistFromRef, ptr noundef %0, i32 noundef %.01825)
  tail call void @VectDifference(ptr noundef nonnull @SelectNearbyVisibleAgents.DistFromRef, ptr noundef nonnull @SelectNearbyVisibleAgents.DistFromRef, ptr noundef %1) #2
  %7 = tail call double @VectAbs(ptr noundef nonnull @SelectNearbyVisibleAgents.DistFromRef) #2
  store double %7, ptr @SelectNearbyVisibleAgents.Dist, align 8, !tbaa !11
  %8 = tail call double @randomizeDouble(double noundef 0.000000e+00, double noundef 1.000000e+00) #2
  %9 = load double, ptr @SelectNearbyVisibleAgents.Dist, align 8, !tbaa !11
  %10 = fmul double %9, %9
  %11 = fmul double %10, %3
  %12 = fcmp olt double %8, %11
  %13 = fcmp oeq double %9, 0.000000e+00
  %14 = fcmp ugt double %9, %2
  %or.cond = or i1 %13, %14
  %brmerge = select i1 %or.cond, i1 true, i1 %12
  br i1 %brmerge, label %17, label %15

15:                                               ; preds = %.lr.ph
  tail call void @SwapAgents(ptr noundef %0, i32 noundef %.01825, i32 noundef %.024)
  %16 = add nsw i32 %.024, 1
  br label %19

17:                                               ; preds = %.lr.ph
  br i1 %13, label %18, label %19

18:                                               ; preds = %17
  tail call void @SwapAgents(ptr noundef %0, i32 noundef %.01825, i32 noundef 0)
  br label %19

19:                                               ; preds = %15, %18, %17
  %.119 = phi i32 [ %.018.in23, %18 ], [ %.01825, %17 ], [ %.018.in23, %15 ]
  %.1 = phi i32 [ %.024, %18 ], [ %.024, %17 ], [ %16, %15 ]
  %.not.not = icmp sgt i32 %.119, %.1
  br i1 %.not.not, label %.lr.ph, label %._crit_edge, !llvm.loop !13

._crit_edge:                                      ; preds = %19, %4
  %.0.lcssa = phi i32 [ 1, %4 ], [ %.1, %19 ]
  ret i32 %.0.lcssa
}

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, !10, i64 36}
!6 = !{!"", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !10, i64 32, !10, i64 36}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"double", !8, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


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