; LLVM IR from VTIvanov20/eight-ball-pool :: src (main)

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

@_ZL10usedMemory = external hidden global i32, align 4
@_ZL6bodies = external hidden global [64 x ptr], align 16
@_ZL18physicsBodiesCount = external hidden global i32, align 4
@_ZL21physicsManifoldsCount = external hidden global i32, align 4
@_ZL8contacts = external hidden global [4096 x ptr], align 16
@.str.13 = external hidden unnamed_addr constant [46 x i8], align 1

declare i32 @printf(ptr noundef, ...) #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 void @free(ptr noundef) #2

; Function Attrs: mustprogress uwtable
define dso_local void @ResetPhysics() #3 {
  %1 = alloca i32, align 4
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = load i32, ptr @_ZL18physicsBodiesCount, align 4, !tbaa !5
  %6 = icmp ugt i32 %5, 0
  br i1 %6, label %7, label %35

7:                                                ; preds = %0
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #4
  %8 = load i32, ptr @_ZL18physicsBodiesCount, align 4, !tbaa !5
  %9 = sub i32 %8, 1
  store i32 %9, ptr %1, align 4, !tbaa !5
  br label %10

10:                                               ; preds = %31, %7
  %11 = load i32, ptr %1, align 4, !tbaa !5
  %12 = icmp sge i32 %11, 0
  br i1 %12, label %14, label %13

13:                                               ; preds = %10
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #4
  br label %34

14:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #4
  %15 = load i32, ptr %1, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [64 x ptr], ptr @_ZL6bodies, i64 0, i64 %16
  %18 = load ptr, ptr %17, align 8, !tbaa !9
  store ptr %18, ptr %2, align 8, !tbaa !9
  %19 = load ptr, ptr %2, align 8, !tbaa !9
  %20 = icmp ne ptr %19, null
  br i1 %20, label %21, label %30

21:                                               ; preds = %14
  %22 = load ptr, ptr %2, align 8, !tbaa !9
  call void @free(ptr noundef %22) #4
  %23 = load i32, ptr %1, align 4, !tbaa !5
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [64 x ptr], ptr @_ZL6bodies, i64 0, i64 %24
  store ptr null, ptr %25, align 8, !tbaa !9
  %26 = load i32, ptr @_ZL10usedMemory, align 4, !tbaa !5
  %27 = zext i32 %26 to i64
  %28 = sub i64 %27, 504
  %29 = trunc i64 %28 to i32
  store i32 %29, ptr @_ZL10usedMemory, align 4, !tbaa !5
  br label %30

30:                                               ; preds = %21, %14
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #4
  br label %31

31:                                               ; preds = %30
  %32 = load i32, ptr %1, align 4, !tbaa !5
  %33 = add nsw i32 %32, -1
  store i32 %33, ptr %1, align 4, !tbaa !5
  br label %10, !llvm.loop !11

34:                                               ; preds = %13
  store i32 0, ptr @_ZL18physicsBodiesCount, align 4, !tbaa !5
  br label %35

35:                                               ; preds = %34, %0
  %36 = load i32, ptr @_ZL21physicsManifoldsCount, align 4, !tbaa !5
  %37 = icmp ugt i32 %36, 0
  br i1 %37, label %38, label %66

38:                                               ; preds = %35
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  %39 = load i32, ptr @_ZL21physicsManifoldsCount, align 4, !tbaa !5
  %40 = sub i32 %39, 1
  store i32 %40, ptr %3, align 4, !tbaa !5
  br label %41

41:                                               ; preds = %62, %38
  %42 = load i32, ptr %3, align 4, !tbaa !5
  %43 = icmp sge i32 %42, 0
  br i1 %43, label %45, label %44

44:                                               ; preds = %41
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  br label %65

45:                                               ; preds = %41
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  %46 = load i32, ptr %3, align 4, !tbaa !5
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds [4096 x ptr], ptr @_ZL8contacts, i64 0, i64 %47
  %49 = load ptr, ptr %48, align 8, !tbaa !9
  store ptr %49, ptr %4, align 8, !tbaa !9
  %50 = load ptr, ptr %4, align 8, !tbaa !9
  %51 = icmp ne ptr %50, null
  br i1 %51, label %52, label %61

52:                                               ; preds = %45
  %53 = load ptr, ptr %4, align 8, !tbaa !9
  call void @free(ptr noundef %53) #4
  %54 = load i32, ptr %3, align 4, !tbaa !5
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds [4096 x ptr], ptr @_ZL8contacts, i64 0, i64 %55
  store ptr null, ptr %56, align 8, !tbaa !9
  %57 = load i32, ptr @_ZL10usedMemory, align 4, !tbaa !5
  %58 = zext i32 %57 to i64
  %59 = sub i64 %58, 72
  %60 = trunc i64 %59 to i32
  store i32 %60, ptr @_ZL10usedMemory, align 4, !tbaa !5
  br label %61

61:                                               ; preds = %52, %45
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  br label %62

62:                                               ; preds = %61
  %63 = load i32, ptr %3, align 4, !tbaa !5
  %64 = add nsw i32 %63, -1
  store i32 %64, ptr %3, align 4, !tbaa !5
  br label %41, !llvm.loop !13

65:                                               ; preds = %44
  store i32 0, ptr @_ZL21physicsManifoldsCount, align 4, !tbaa !5
  br label %66

66:                                               ; preds = %65, %35
  %67 = call i32 (ptr, ...) @printf(ptr noundef @.str.13)
  ret void
}

attributes #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 = { mustprogress 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 #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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


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

@_ZL10usedMemory = external hidden local_unnamed_addr global i32, align 4
@_ZL6bodies = external hidden local_unnamed_addr global [64 x ptr], align 16
@_ZL18physicsBodiesCount = external hidden local_unnamed_addr global i32, align 4
@_ZL21physicsManifoldsCount = external hidden local_unnamed_addr global i32, align 4
@_ZL8contacts = external hidden local_unnamed_addr global [4096 x ptr], align 16
@.str.13 = external hidden unnamed_addr constant [46 x i8], align 1

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @ResetPhysics() local_unnamed_addr #2 {
  %1 = load i32, ptr @_ZL18physicsBodiesCount, align 4, !tbaa !5
  %.not = icmp eq i32 %1, 0
  br i1 %.not, label %11, label %.preheader19

.preheader19:                                     ; preds = %0
  %.020 = add i32 %1, -1
  %2 = icmp sgt i32 %.020, -1
  br i1 %2, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %.preheader19
  %3 = zext nneg i32 %.020 to i64
  br label %.lr.ph

._crit_edge:                                      ; preds = %9, %.preheader19
  store i32 0, ptr @_ZL18physicsBodiesCount, align 4, !tbaa !5
  br label %11

.lr.ph:                                           ; preds = %.lr.ph.preheader, %9
  %indvars.iv = phi i64 [ %3, %.lr.ph.preheader ], [ %indvars.iv.next, %9 ]
  %4 = getelementptr inbounds [64 x ptr], ptr @_ZL6bodies, i64 0, i64 %indvars.iv
  %5 = load ptr, ptr %4, align 8, !tbaa !9
  %.not18 = icmp eq ptr %5, null
  br i1 %.not18, label %9, label %6

6:                                                ; preds = %.lr.ph
  tail call void @free(ptr noundef nonnull %5) #3
  store ptr null, ptr %4, align 8, !tbaa !9
  %7 = load i32, ptr @_ZL10usedMemory, align 4, !tbaa !5
  %8 = add i32 %7, -504
  store i32 %8, ptr @_ZL10usedMemory, align 4, !tbaa !5
  br label %9

9:                                                ; preds = %6, %.lr.ph
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %10 = icmp sgt i64 %indvars.iv, 0
  br i1 %10, label %.lr.ph, label %._crit_edge, !llvm.loop !11

11:                                               ; preds = %._crit_edge, %0
  %12 = load i32, ptr @_ZL21physicsManifoldsCount, align 4, !tbaa !5
  %.not16 = icmp eq i32 %12, 0
  br i1 %.not16, label %22, label %.preheader

.preheader:                                       ; preds = %11
  %.01122 = add i32 %12, -1
  %13 = icmp sgt i32 %.01122, -1
  br i1 %13, label %.lr.ph24.preheader, label %._crit_edge25

.lr.ph24.preheader:                               ; preds = %.preheader
  %14 = zext nneg i32 %.01122 to i64
  br label %.lr.ph24

._crit_edge25:                                    ; preds = %20, %.preheader
  store i32 0, ptr @_ZL21physicsManifoldsCount, align 4, !tbaa !5
  br label %22

.lr.ph24:                                         ; preds = %.lr.ph24.preheader, %20
  %indvars.iv27 = phi i64 [ %14, %.lr.ph24.preheader ], [ %indvars.iv.next28, %20 ]
  %15 = getelementptr inbounds [4096 x ptr], ptr @_ZL8contacts, i64 0, i64 %indvars.iv27
  %16 = load ptr, ptr %15, align 8, !tbaa !9
  %.not17 = icmp eq ptr %16, null
  br i1 %.not17, label %20, label %17

17:                                               ; preds = %.lr.ph24
  tail call void @free(ptr noundef nonnull %16) #3
  store ptr null, ptr %15, align 8, !tbaa !9
  %18 = load i32, ptr @_ZL10usedMemory, align 4, !tbaa !5
  %19 = add i32 %18, -72
  store i32 %19, ptr @_ZL10usedMemory, align 4, !tbaa !5
  br label %20

20:                                               ; preds = %17, %.lr.ph24
  %indvars.iv.next28 = add nsw i64 %indvars.iv27, -1
  %21 = icmp sgt i64 %indvars.iv27, 0
  br i1 %21, label %.lr.ph24, label %._crit_edge25, !llvm.loop !13

22:                                               ; preds = %._crit_edge25, %11
  %23 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.13)
  ret void
}

attributes #0 = { nofree 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 #1 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { mustprogress 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 #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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


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