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

; 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.timeval = type { i64, i64 }

@ms = external dso_local global i32, align 4
@running = external dso_local global i8, align 1
@pause = external dso_local global i8, align 1

; 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 double @timedifference_msec(i64, i64, i64, i64) #1

; Function Attrs: nounwind uwtable
declare dso_local void @physics_update() #1

; 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) #2

; Function Attrs: nounwind uwtable
define dso_local ptr @physics_run() #1 {
  %1 = alloca %struct.timeval, align 8
  %2 = alloca %struct.timeval, align 8
  %3 = alloca double, align 8
  store i8 1, ptr @running, align 1, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 16, ptr %1) #4
  call void @llvm.lifetime.start.p0(i64 16, ptr %2) #4
  %4 = call i32 @gettimeofday(ptr noundef %1, ptr noundef null) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  store double 0.000000e+00, ptr %3, align 8, !tbaa !9
  br label %5

5:                                                ; preds = %35, %0
  %6 = load i8, ptr @running, align 1, !tbaa !5, !range !11, !noundef !12
  %7 = trunc i8 %6 to i1
  br i1 %7, label %8, label %36

8:                                                ; preds = %5
  %9 = load i8, ptr @pause, align 1, !tbaa !5, !range !11, !noundef !12
  %10 = trunc i8 %9 to i1
  br i1 %10, label %33, label %11

11:                                               ; preds = %8
  %12 = call i32 @gettimeofday(ptr noundef %2, ptr noundef null) #4
  %13 = getelementptr inbounds { i64, i64 }, ptr %1, i32 0, i32 0
  %14 = load i64, ptr %13, align 8
  %15 = getelementptr inbounds { i64, i64 }, ptr %1, i32 0, i32 1
  %16 = load i64, ptr %15, align 8
  %17 = getelementptr inbounds { i64, i64 }, ptr %2, i32 0, i32 0
  %18 = load i64, ptr %17, align 8
  %19 = getelementptr inbounds { i64, i64 }, ptr %2, i32 0, i32 1
  %20 = load i64, ptr %19, align 8
  %21 = call double @timedifference_msec(i64 %14, i64 %16, i64 %18, i64 %20)
  %22 = load i32, ptr @ms, align 4, !tbaa !13
  %23 = sitofp i32 %22 to double
  %24 = fdiv double %21, %23
  %25 = load double, ptr %3, align 8, !tbaa !9
  %26 = fadd double %25, %24
  store double %26, ptr %3, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %1, ptr align 8 %2, i64 16, i1 false), !tbaa.struct !15
  %27 = load double, ptr %3, align 8, !tbaa !9
  %28 = fcmp oge double %27, 1.000000e+00
  br i1 %28, label %29, label %32

29:                                               ; preds = %11
  call void @physics_update()
  %30 = load double, ptr %3, align 8, !tbaa !9
  %31 = fadd double %30, -1.000000e+00
  store double %31, ptr %3, align 8, !tbaa !9
  br label %32

32:                                               ; preds = %29, %11
  br label %35

33:                                               ; preds = %8
  %34 = call i32 @gettimeofday(ptr noundef %1, ptr noundef null) #4
  br label %35

35:                                               ; preds = %33, %32
  br label %5, !llvm.loop !18

36:                                               ; preds = %5
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  call void @llvm.lifetime.end.p0(i64 16, ptr %2) #4
  call void @llvm.lifetime.end.p0(i64 16, ptr %1) #4
  ret ptr null
}

; Function Attrs: nounwind
declare i32 @gettimeofday(ptr noundef, ptr noundef) #3

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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = { 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 = !{!"_Bool", !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 = !{i8 0, i8 2}
!12 = !{}
!13 = !{!14, !14, i64 0}
!14 = !{!"int", !7, i64 0}
!15 = !{i64 0, i64 8, !16, i64 8, i64 8, !16}
!16 = !{!17, !17, i64 0}
!17 = !{!"long", !7, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"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.timeval = type { i64, i64 }

@ms = external dso_local local_unnamed_addr global i32, align 4
@running = external dso_local local_unnamed_addr global i8, align 1
@pause = external dso_local local_unnamed_addr global i8, align 1

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

; Function Attrs: mustprogress 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 double @timedifference_msec(i64, i64, i64, i64) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @physics_update() local_unnamed_addr #1

; Function Attrs: mustprogress 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) #2

; Function Attrs: nounwind uwtable
define dso_local noalias noundef ptr @physics_run() local_unnamed_addr #1 {
  %1 = alloca %struct.timeval, align 8
  %2 = alloca %struct.timeval, align 8
  store i8 1, ptr @running, align 1, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %1) #4
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %2) #4
  %3 = call i32 @gettimeofday(ptr noundef nonnull %1, ptr noundef null) #4
  %4 = load i8, ptr @running, align 1, !tbaa !5, !range !9, !noundef !10
  %5 = trunc nuw i8 %4 to i1
  br i1 %5, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %0
  %6 = getelementptr inbounds i8, ptr %1, i64 8
  %7 = getelementptr inbounds i8, ptr %2, i64 8
  br label %8

8:                                                ; preds = %.lr.ph, %27
  %.05 = phi double [ 0.000000e+00, %.lr.ph ], [ %.1, %27 ]
  %9 = load i8, ptr @pause, align 1, !tbaa !5, !range !9, !noundef !10
  %10 = trunc nuw i8 %9 to i1
  br i1 %10, label %25, label %11

11:                                               ; preds = %8
  %12 = call i32 @gettimeofday(ptr noundef nonnull %2, ptr noundef null) #4
  %13 = load i64, ptr %1, align 8
  %14 = load i64, ptr %6, align 8
  %15 = load i64, ptr %2, align 8
  %16 = load i64, ptr %7, align 8
  %17 = tail call double @timedifference_msec(i64 %13, i64 %14, i64 %15, i64 %16)
  %18 = load i32, ptr @ms, align 4, !tbaa !11
  %19 = sitofp i32 %18 to double
  %20 = fdiv double %17, %19
  %21 = fadd double %.05, %20
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %1, ptr noundef nonnull align 8 dereferenceable(16) %2, i64 16, i1 false), !tbaa.struct !13
  %22 = fcmp ult double %21, 1.000000e+00
  br i1 %22, label %27, label %23

23:                                               ; preds = %11
  tail call void @physics_update()
  %24 = fadd double %21, -1.000000e+00
  br label %27

25:                                               ; preds = %8
  %26 = call i32 @gettimeofday(ptr noundef nonnull %1, ptr noundef null) #4
  br label %27

27:                                               ; preds = %11, %23, %25
  %.1 = phi double [ %.05, %25 ], [ %24, %23 ], [ %21, %11 ]
  %28 = load i8, ptr @running, align 1, !tbaa !5, !range !9, !noundef !10
  %29 = trunc nuw i8 %28 to i1
  br i1 %29, label %8, label %._crit_edge, !llvm.loop !16

._crit_edge:                                      ; preds = %27, %0
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %2) #4
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %1) #4
  ret ptr null
}

; Function Attrs: nofree nounwind
declare noundef i32 @gettimeofday(ptr nocapture noundef, ptr nocapture noundef) local_unnamed_addr #3

attributes #0 = { mustprogress 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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 #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 = !{!"_Bool", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{i8 0, i8 2}
!10 = !{}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{i64 0, i64 8, !14, i64 8, i64 8, !14}
!14 = !{!15, !15, i64 0}
!15 = !{!"long", !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


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