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

@OrderAgentsByDistance.DistFromRef = external hidden global [3 x double], align 16
@OrderAgentsByDistance.Dist1 = external hidden global double, align 8
@OrderAgentsByDistance.Dist2 = 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

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

; Function Attrs: nounwind uwtable
define dso_local void @OrderAgentsByDistance(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
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @NullVect(ptr noundef @OrderAgentsByDistance.DistFromRef, i32 noundef 3)
  store double 2.000000e+222, ptr @OrderAgentsByDistance.Dist1, align 8, !tbaa !9
  store double 2.000000e+222, ptr @OrderAgentsByDistance.Dist2, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 1, ptr %5, align 4, !tbaa !11
  br label %7

7:                                                ; preds = %50, %2
  %8 = load i32, ptr %5, align 4, !tbaa !11
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.phase_t, ptr %9, i32 0, i32 5
  %11 = load i32, ptr %10, align 4, !tbaa !13
  %12 = icmp slt i32 %8, %11
  br i1 %12, label %13, label %53

13:                                               ; preds = %7
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  %15 = load i32, ptr %5, align 4, !tbaa !11
  call void @GetAgentsCoordinates(ptr noundef @OrderAgentsByDistance.DistFromRef, ptr noundef %14, i32 noundef %15)
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  call void @VectDifference(ptr noundef @OrderAgentsByDistance.DistFromRef, ptr noundef @OrderAgentsByDistance.DistFromRef, ptr noundef %16)
  %17 = call double @VectAbs(ptr noundef @OrderAgentsByDistance.DistFromRef)
  store double %17, ptr @OrderAgentsByDistance.Dist1, align 8, !tbaa !9
  %18 = load i32, ptr %5, align 4, !tbaa !11
  store i32 %18, ptr %6, align 4, !tbaa !11
  %19 = load ptr, ptr %3, align 8, !tbaa !5
  %20 = load i32, ptr %6, align 4, !tbaa !11
  %21 = sub nsw i32 %20, 1
  call void @GetAgentsCoordinates(ptr noundef @OrderAgentsByDistance.DistFromRef, ptr noundef %19, i32 noundef %21)
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  call void @VectDifference(ptr noundef @OrderAgentsByDistance.DistFromRef, ptr noundef @OrderAgentsByDistance.DistFromRef, ptr noundef %22)
  %23 = call double @VectAbs(ptr noundef @OrderAgentsByDistance.DistFromRef)
  store double %23, ptr @OrderAgentsByDistance.Dist2, align 8, !tbaa !9
  br label %24

24:                                               ; preds = %33, %13
  %25 = load i32, ptr %6, align 4, !tbaa !11
  %26 = icmp sgt i32 %25, 0
  br i1 %26, label %27, label %31

27:                                               ; preds = %24
  %28 = load double, ptr @OrderAgentsByDistance.Dist2, align 8, !tbaa !9
  %29 = load double, ptr @OrderAgentsByDistance.Dist1, align 8, !tbaa !9
  %30 = fcmp ogt double %28, %29
  br label %31

31:                                               ; preds = %27, %24
  %32 = phi i1 [ false, %24 ], [ %30, %27 ]
  br i1 %32, label %33, label %49

33:                                               ; preds = %31
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  %35 = load i32, ptr %6, align 4, !tbaa !11
  %36 = sub nsw i32 %35, 1
  %37 = load i32, ptr %6, align 4, !tbaa !11
  call void @SwapAgents(ptr noundef %34, i32 noundef %36, i32 noundef %37)
  %38 = load i32, ptr %6, align 4, !tbaa !11
  %39 = add nsw i32 %38, -1
  store i32 %39, ptr %6, align 4, !tbaa !11
  %40 = load ptr, ptr %3, align 8, !tbaa !5
  %41 = load i32, ptr %6, align 4, !tbaa !11
  call void @GetAgentsCoordinates(ptr noundef @OrderAgentsByDistance.DistFromRef, ptr noundef %40, i32 noundef %41)
  %42 = load ptr, ptr %4, align 8, !tbaa !5
  call void @VectDifference(ptr noundef @OrderAgentsByDistance.DistFromRef, ptr noundef @OrderAgentsByDistance.DistFromRef, ptr noundef %42)
  %43 = call double @VectAbs(ptr noundef @OrderAgentsByDistance.DistFromRef)
  store double %43, ptr @OrderAgentsByDistance.Dist1, align 8, !tbaa !9
  %44 = load ptr, ptr %3, align 8, !tbaa !5
  %45 = load i32, ptr %6, align 4, !tbaa !11
  %46 = sub nsw i32 %45, 1
  call void @GetAgentsCoordinates(ptr noundef @OrderAgentsByDistance.DistFromRef, ptr noundef %44, i32 noundef %46)
  %47 = load ptr, ptr %4, align 8, !tbaa !5
  call void @VectDifference(ptr noundef @OrderAgentsByDistance.DistFromRef, ptr noundef @OrderAgentsByDistance.DistFromRef, ptr noundef %47)
  %48 = call double @VectAbs(ptr noundef @OrderAgentsByDistance.DistFromRef)
  store double %48, ptr @OrderAgentsByDistance.Dist2, align 8, !tbaa !9
  br label %24, !llvm.loop !15

49:                                               ; preds = %31
  br label %50

50:                                               ; preds = %49
  %51 = load i32, ptr %5, align 4, !tbaa !11
  %52 = add nsw i32 %51, 1
  store i32 %52, ptr %5, align 4, !tbaa !11
  br label %7, !llvm.loop !17

53:                                               ; preds = %7
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  ret void
}

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

@OrderAgentsByDistance.DistFromRef = external hidden global [3 x double], align 16
@OrderAgentsByDistance.Dist1 = external hidden local_unnamed_addr global double, align 8
@OrderAgentsByDistance.Dist2 = 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

; 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 void @OrderAgentsByDistance(ptr noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  tail call void @NullVect(ptr noundef nonnull @OrderAgentsByDistance.DistFromRef, i32 noundef 3) #2
  store double 2.000000e+222, ptr @OrderAgentsByDistance.Dist1, align 8, !tbaa !5
  store double 2.000000e+222, ptr @OrderAgentsByDistance.Dist2, align 8, !tbaa !5
  %3 = getelementptr inbounds i8, ptr %0, i64 36
  %4 = load i32, ptr %3, align 4, !tbaa !9
  %5 = icmp sgt i32 %4, 1
  br i1 %5, label %.lr.ph24, label %._crit_edge25

.lr.ph24:                                         ; preds = %2, %._crit_edge
  %.02022 = phi i32 [ %19, %._crit_edge ], [ 1, %2 ]
  tail call void @GetAgentsCoordinates(ptr noundef nonnull @OrderAgentsByDistance.DistFromRef, ptr noundef nonnull %0, i32 noundef %.02022)
  tail call void @VectDifference(ptr noundef nonnull @OrderAgentsByDistance.DistFromRef, ptr noundef nonnull @OrderAgentsByDistance.DistFromRef, ptr noundef %1) #2
  %6 = tail call double @VectAbs(ptr noundef nonnull @OrderAgentsByDistance.DistFromRef) #2
  store double %6, ptr @OrderAgentsByDistance.Dist1, align 8, !tbaa !5
  %7 = add nsw i32 %.02022, -1
  tail call void @GetAgentsCoordinates(ptr noundef nonnull @OrderAgentsByDistance.DistFromRef, ptr noundef nonnull %0, i32 noundef %7)
  tail call void @VectDifference(ptr noundef nonnull @OrderAgentsByDistance.DistFromRef, ptr noundef nonnull @OrderAgentsByDistance.DistFromRef, ptr noundef %1) #2
  %8 = tail call double @VectAbs(ptr noundef nonnull @OrderAgentsByDistance.DistFromRef) #2
  store double %8, ptr @OrderAgentsByDistance.Dist2, align 8, !tbaa !5
  %9 = load double, ptr @OrderAgentsByDistance.Dist1, align 8
  %10 = fcmp ogt double %8, %9
  br i1 %10, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %.lr.ph24, %.lr.ph
  %.021 = phi i32 [ %11, %.lr.ph ], [ %.02022, %.lr.ph24 ]
  %11 = add nsw i32 %.021, -1
  tail call void @SwapAgents(ptr noundef %0, i32 noundef %11, i32 noundef %.021)
  tail call void @GetAgentsCoordinates(ptr noundef nonnull @OrderAgentsByDistance.DistFromRef, ptr noundef %0, i32 noundef %11)
  tail call void @VectDifference(ptr noundef nonnull @OrderAgentsByDistance.DistFromRef, ptr noundef nonnull @OrderAgentsByDistance.DistFromRef, ptr noundef %1) #2
  %12 = tail call double @VectAbs(ptr noundef nonnull @OrderAgentsByDistance.DistFromRef) #2
  store double %12, ptr @OrderAgentsByDistance.Dist1, align 8, !tbaa !5
  %13 = add nsw i32 %.021, -2
  tail call void @GetAgentsCoordinates(ptr noundef nonnull @OrderAgentsByDistance.DistFromRef, ptr noundef %0, i32 noundef %13)
  tail call void @VectDifference(ptr noundef nonnull @OrderAgentsByDistance.DistFromRef, ptr noundef nonnull @OrderAgentsByDistance.DistFromRef, ptr noundef %1) #2
  %14 = tail call double @VectAbs(ptr noundef nonnull @OrderAgentsByDistance.DistFromRef) #2
  store double %14, ptr @OrderAgentsByDistance.Dist2, align 8, !tbaa !5
  %15 = icmp sgt i32 %.021, 1
  %16 = load double, ptr @OrderAgentsByDistance.Dist1, align 8
  %17 = fcmp ogt double %14, %16
  %18 = select i1 %15, i1 %17, i1 false
  br i1 %18, label %.lr.ph, label %._crit_edge, !llvm.loop !13

._crit_edge:                                      ; preds = %.lr.ph, %.lr.ph24
  %19 = add nuw nsw i32 %.02022, 1
  %20 = load i32, ptr %3, align 4, !tbaa !9
  %21 = icmp slt i32 %19, %20
  br i1 %21, label %.lr.ph24, label %._crit_edge25, !llvm.loop !15

._crit_edge25:                                    ; preds = %._crit_edge, %2
  ret void
}

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


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