; LLVM IR from iostapyshyn/gravity :: physics (particle_remove)

; 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.particle = type { double, double, double, double, double, [3 x double] }
%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }

@array = external dso_local global <{ %struct.particle, %struct.particle, %struct.particle, [117 x %struct.particle] }>, align 16
@lock = external dso_local global %union.pthread_mutex_t, align 8

; Function Attrs: nounwind uwtable
declare dso_local void @update() #0

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

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

; Function Attrs: nounwind
declare double @sqrt(double noundef) #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.fmuladd.f64(double, double, double) #3

; Function Attrs: nounwind
declare i32 @pthread_mutex_lock(ptr noundef) #2

; Function Attrs: nounwind
declare i32 @pthread_mutex_unlock(ptr noundef) #2

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

14:                                               ; preds = %55, %2
  %15 = load i32, ptr %7, align 4, !tbaa !9
  %16 = icmp slt i32 %15, 120
  br i1 %16, label %18, label %17

17:                                               ; preds = %14
  store i32 2, ptr %8, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  br label %58

18:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %19 = load i32, ptr %7, align 4, !tbaa !9
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [120 x %struct.particle], ptr @array, i64 0, i64 %20
  store ptr %21, ptr %9, align 8, !tbaa !11
  %22 = load ptr, ptr %9, align 8, !tbaa !11
  %23 = getelementptr inbounds %struct.particle, ptr %22, i32 0, i32 4
  %24 = load double, ptr %23, align 8, !tbaa !13
  %25 = fcmp une double %24, 0.000000e+00
  br i1 %25, label %27, label %26

26:                                               ; preds = %18
  store i32 4, ptr %8, align 4
  br label %52

27:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  %28 = load ptr, ptr %9, align 8, !tbaa !11
  %29 = call double @particle_getcoords(ptr noundef byval(%struct.particle) align 8 %28, ptr noundef %10, ptr noundef %11)
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #4
  %30 = load double, ptr %10, align 8, !tbaa !5
  %31 = load double, ptr %3, align 8, !tbaa !5
  %32 = fsub double %30, %31
  %33 = load double, ptr %10, align 8, !tbaa !5
  %34 = load double, ptr %3, align 8, !tbaa !5
  %35 = fsub double %33, %34
  %36 = load double, ptr %11, align 8, !tbaa !5
  %37 = load double, ptr %4, align 8, !tbaa !5
  %38 = fsub double %36, %37
  %39 = load double, ptr %11, align 8, !tbaa !5
  %40 = load double, ptr %4, align 8, !tbaa !5
  %41 = fsub double %39, %40
  %42 = fmul double %38, %41
  %43 = call double @llvm.fmuladd.f64(double %32, double %35, double %42)
  %44 = call double @sqrt(double noundef %43) #4
  store double %44, ptr %12, align 8, !tbaa !5
  %45 = load double, ptr %12, align 8, !tbaa !5
  %46 = load double, ptr %6, align 8, !tbaa !5
  %47 = fcmp olt double %45, %46
  br i1 %47, label %48, label %51

48:                                               ; preds = %27
  %49 = load double, ptr %12, align 8, !tbaa !5
  store double %49, ptr %6, align 8, !tbaa !5
  %50 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %50, ptr %5, align 4, !tbaa !9
  br label %51

51:                                               ; preds = %48, %27
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  store i32 0, ptr %8, align 4
  br label %52

52:                                               ; preds = %51, %26
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  %53 = load i32, ptr %8, align 4
  switch i32 %53, label %64 [
    i32 0, label %54
    i32 4, label %55
  ]

54:                                               ; preds = %52
  br label %55

55:                                               ; preds = %54, %52
  %56 = load i32, ptr %7, align 4, !tbaa !9
  %57 = add nsw i32 %56, 1
  store i32 %57, ptr %7, align 4, !tbaa !9
  br label %14, !llvm.loop !15

58:                                               ; preds = %17
  %59 = load i32, ptr %5, align 4, !tbaa !9
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [120 x %struct.particle], ptr @array, i64 0, i64 %60
  %62 = getelementptr inbounds %struct.particle, ptr %61, i32 0, i32 4
  store double 0.000000e+00, ptr %62, align 16, !tbaa !13
  %63 = call i32 @pthread_mutex_unlock(ptr noundef @lock) #4
  call void @update()
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  ret void

64:                                               ; preds = %52
  unreachable
}

; Function Attrs: nounwind uwtable
declare dso_local double @particle_getcoords(ptr noundef byval(%struct.particle) align 8, ptr noundef, ptr noundef) #0

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = !{!"double", !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 = !{!"any pointer", !7, i64 0}
!13 = !{!14, !6, i64 32}
!14 = !{!"particle", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !7, i64 40}
!15 = distinct !{!15, !16}
!16 = !{!"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"

%struct.particle = type { double, double, double, double, double, [3 x double] }
%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }

@array = external dso_local global <{ %struct.particle, %struct.particle, %struct.particle, [117 x %struct.particle] }>, align 16
@lock = external dso_local global %union.pthread_mutex_t, align 8

; Function Attrs: nounwind uwtable
declare dso_local void @update() local_unnamed_addr #0

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

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

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

; Function Attrs: nounwind
declare i32 @pthread_mutex_lock(ptr noundef) local_unnamed_addr #3

; Function Attrs: nounwind
declare i32 @pthread_mutex_unlock(ptr noundef) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
define dso_local void @particle_remove(double noundef %0, double noundef %1) local_unnamed_addr #0 {
  %3 = alloca double, align 8
  %4 = alloca double, align 8
  %5 = tail call i32 @pthread_mutex_lock(ptr noundef nonnull @lock) #5
  br label %10

6:                                                ; preds = %25
  %7 = sext i32 %.2 to i64
  %8 = getelementptr inbounds [120 x %struct.particle], ptr @array, i64 0, i64 %7, i32 4
  store double 0.000000e+00, ptr %8, align 16, !tbaa !5
  %9 = call i32 @pthread_mutex_unlock(ptr noundef nonnull @lock) #5
  call void @update()
  ret void

10:                                               ; preds = %2, %25
  %indvars.iv = phi i64 [ 0, %2 ], [ %indvars.iv.next, %25 ]
  %.020 = phi i32 [ 0, %2 ], [ %.2, %25 ]
  %.01319 = phi double [ 0x7FF0000000000000, %2 ], [ %.215, %25 ]
  %11 = getelementptr inbounds [120 x %struct.particle], ptr @array, i64 0, i64 %indvars.iv
  %12 = getelementptr inbounds i8, ptr %11, i64 32
  %13 = load double, ptr %12, align 16, !tbaa !5
  %14 = fcmp une double %13, 0.000000e+00
  br i1 %14, label %15, label %25

15:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %4) #5
  %16 = call double @particle_getcoords(ptr noundef nonnull byval(%struct.particle) align 8 %11, ptr noundef nonnull %3, ptr noundef nonnull %4)
  %17 = load double, ptr %3, align 8, !tbaa !10
  %18 = fsub double %17, %0
  %19 = load double, ptr %4, align 8, !tbaa !10
  %20 = fsub double %19, %1
  %21 = fmul double %20, %20
  %22 = call double @llvm.fmuladd.f64(double %18, double %18, double %21)
  %sqrt = call double @llvm.sqrt.f64(double %22)
  %23 = fcmp olt double %sqrt, %.01319
  %.114 = select i1 %23, double %sqrt, double %.01319
  %24 = trunc nuw nsw i64 %indvars.iv to i32
  %.1 = select i1 %23, i32 %24, i32 %.020
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %4) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #5
  br label %25

25:                                               ; preds = %10, %15
  %.215 = phi double [ %.114, %15 ], [ %.01319, %10 ]
  %.2 = phi i32 [ %.1, %15 ], [ %.020, %10 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, 120
  br i1 %exitcond.not, label %6, label %10, !llvm.loop !11
}

; Function Attrs: nounwind uwtable
declare dso_local double @particle_getcoords(ptr noundef byval(%struct.particle) align 8, ptr noundef, ptr noundef) local_unnamed_addr #0

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

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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #5 = { 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, !7, i64 32}
!6 = !{!"particle", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !7, i64 32, !8, i64 40}
!7 = !{!"double", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!7, !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: AggressiveInstCombinePass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopRotatePass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass