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

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

@next_m = external dso_local global i32, align 4
@next_color = external dso_local global [3 x double], align 16
@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 ptr @new_color() #0

; 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 uwtable
declare dso_local zeroext i1 @check() #0

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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

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

; Function Attrs: nounwind uwtable
define dso_local void @particle_add(double noundef %0, double noundef %1, double noundef %2, double noundef %3) #0 {
  %5 = alloca double, align 8
  %6 = alloca double, align 8
  %7 = alloca double, align 8
  %8 = alloca double, align 8
  %9 = alloca i32, align 4
  %10 = alloca %struct.particle, align 8
  store double %0, ptr %5, align 8, !tbaa !5
  store double %1, ptr %6, align 8, !tbaa !5
  store double %2, ptr %7, align 8, !tbaa !5
  store double %3, ptr %8, align 8, !tbaa !5
  %11 = call i32 @pthread_mutex_lock(ptr noundef @lock) #4
  %12 = call zeroext i1 @check()
  br i1 %12, label %13, label %46

13:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  store i32 0, ptr %9, align 4, !tbaa !9
  br label %14

14:                                               ; preds = %22, %13
  %15 = load i32, ptr %9, align 4, !tbaa !9
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [120 x %struct.particle], ptr @array, i64 0, i64 %16
  %18 = getelementptr inbounds %struct.particle, ptr %17, i32 0, i32 4
  %19 = load double, ptr %18, align 16, !tbaa !11
  %20 = fcmp une double %19, 0.000000e+00
  br i1 %20, label %21, label %25

21:                                               ; preds = %14
  br label %22

22:                                               ; preds = %21
  %23 = load i32, ptr %9, align 4, !tbaa !9
  %24 = add nsw i32 %23, 1
  store i32 %24, ptr %9, align 4, !tbaa !9
  br label %14, !llvm.loop !13

25:                                               ; preds = %14
  %26 = load i32, ptr %9, align 4, !tbaa !9
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [120 x %struct.particle], ptr @array, i64 0, i64 %27
  %29 = getelementptr inbounds %struct.particle, ptr %10, i32 0, i32 0
  %30 = load double, ptr %5, align 8, !tbaa !5
  store double %30, ptr %29, align 8, !tbaa !15
  %31 = getelementptr inbounds %struct.particle, ptr %10, i32 0, i32 1
  %32 = load double, ptr %6, align 8, !tbaa !5
  store double %32, ptr %31, align 8, !tbaa !16
  %33 = getelementptr inbounds %struct.particle, ptr %10, i32 0, i32 2
  %34 = load double, ptr %7, align 8, !tbaa !5
  store double %34, ptr %33, align 8, !tbaa !17
  %35 = getelementptr inbounds %struct.particle, ptr %10, i32 0, i32 3
  %36 = load double, ptr %8, align 8, !tbaa !5
  store double %36, ptr %35, align 8, !tbaa !18
  %37 = getelementptr inbounds %struct.particle, ptr %10, i32 0, i32 4
  %38 = load i32, ptr @next_m, align 4, !tbaa !9
  %39 = sitofp i32 %38 to double
  store double %39, ptr %37, align 8, !tbaa !11
  %40 = getelementptr inbounds %struct.particle, ptr %10, i32 0, i32 5
  %41 = load double, ptr @next_color, align 16, !tbaa !5
  store double %41, ptr %40, align 8, !tbaa !5
  %42 = getelementptr inbounds double, ptr %40, i64 1
  %43 = load double, ptr getelementptr inbounds ([3 x double], ptr @next_color, i64 0, i64 1), align 8, !tbaa !5
  store double %43, ptr %42, align 8, !tbaa !5
  %44 = getelementptr inbounds double, ptr %40, i64 2
  %45 = load double, ptr getelementptr inbounds ([3 x double], ptr @next_color, i64 0, i64 2), align 16, !tbaa !5
  store double %45, ptr %44, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %28, ptr align 8 %10, i64 64, i1 false), !tbaa.struct !19
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  br label %46

46:                                               ; preds = %25, %4
  %47 = call i32 @pthread_mutex_unlock(ptr noundef @lock) #4
  call void @update()
  %48 = call ptr @new_color()
  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 = { 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 nounwind willreturn memory(argmem: readwrite) }
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, !6, i64 32}
!12 = !{!"particle", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !7, i64 40}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!12, !6, i64 0}
!16 = !{!12, !6, i64 8}
!17 = !{!12, !6, i64 16}
!18 = !{!12, !6, i64 24}
!19 = !{i64 0, i64 8, !5, i64 8, i64 8, !5, i64 16, i64 8, !5, i64 24, i64 8, !5, i64 32, i64 8, !5, i64 40, i64 24, !20}
!20 = !{!7, !7, i64 0}


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

@next_m = external dso_local local_unnamed_addr global i32, align 4
@next_color = external dso_local local_unnamed_addr global [3 x double], align 16
@array = external dso_local local_unnamed_addr 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 ptr @new_color() local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @check() local_unnamed_addr #0

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

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

; Function Attrs: nounwind uwtable
define dso_local void @particle_add(double noundef %0, double noundef %1, double noundef %2, double noundef %3) local_unnamed_addr #0 {
  %5 = tail call i32 @pthread_mutex_lock(ptr noundef nonnull @lock) #2
  %6 = tail call zeroext i1 @check()
  br i1 %6, label %.preheader, label %17

.preheader:                                       ; preds = %4, %.preheader
  %indvars.iv = phi i64 [ %indvars.iv.next, %.preheader ], [ 0, %4 ]
  %7 = getelementptr inbounds [120 x %struct.particle], ptr @array, i64 0, i64 %indvars.iv
  %8 = getelementptr inbounds i8, ptr %7, i64 32
  %9 = load double, ptr %8, align 16, !tbaa !5
  %10 = fcmp une double %9, 0.000000e+00
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  br i1 %10, label %.preheader, label %11, !llvm.loop !10

11:                                               ; preds = %.preheader
  %12 = getelementptr inbounds i8, ptr %7, i64 32
  %13 = load i32, ptr @next_m, align 4, !tbaa !12
  %14 = sitofp i32 %13 to double
  %15 = load double, ptr getelementptr inbounds (i8, ptr @next_color, i64 16), align 16, !tbaa !14
  store double %0, ptr %7, align 16, !tbaa !14
  %.sroa.2.0..sroa_idx = getelementptr inbounds i8, ptr %7, i64 8
  store double %1, ptr %.sroa.2.0..sroa_idx, align 8, !tbaa !14
  %.sroa.3.0..sroa_idx = getelementptr inbounds i8, ptr %7, i64 16
  store double %2, ptr %.sroa.3.0..sroa_idx, align 16, !tbaa !14
  %.sroa.4.0..sroa_idx = getelementptr inbounds i8, ptr %7, i64 24
  store double %3, ptr %.sroa.4.0..sroa_idx, align 8, !tbaa !14
  store double %14, ptr %12, align 16, !tbaa !14
  %.sroa.6.0..sroa_idx = getelementptr inbounds i8, ptr %7, i64 40
  %16 = load <2 x double>, ptr @next_color, align 16, !tbaa !14
  store <2 x double> %16, ptr %.sroa.6.0..sroa_idx, align 8
  %.sroa.8.0..sroa_idx = getelementptr inbounds i8, ptr %7, i64 56
  store double %15, ptr %.sroa.8.0..sroa_idx, align 8, !tbaa !15
  br label %17

17:                                               ; preds = %11, %4
  %18 = tail call i32 @pthread_mutex_unlock(ptr noundef nonnull @lock) #2
  tail call void @update()
  %19 = tail call ptr @new_color()
  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 = { 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 #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, !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 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = !{!13, !13, i64 0}
!13 = !{!"int", !8, i64 0}
!14 = !{!7, !7, i64 0}
!15 = !{!8, !8, i64 0}


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