; LLVM IR from blat-blatnik/Painted-Portal-Ray-Tracing :: src/utils.ll (Z17checkTrackedFilesv)

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

@_ZL15numTrackedFiles = external hidden global i32, align 4
@_ZL12trackedFiles = external hidden global ptr, 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: mustprogress nounwind uwtable
declare hidden noundef i64 @_ZL11getFileTimePKc(ptr noundef) #1

; Function Attrs: mustprogress uwtable
define dso_local void @_Z17checkTrackedFilesv() #2 {
  %1 = alloca i32, align 4
  %2 = alloca ptr, align 8
  %3 = alloca i64, align 8
  %4 = alloca i8, align 1
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #5
  store i32 0, ptr %1, align 4, !tbaa !5
  br label %5

5:                                                ; preds = %74, %0
  %6 = load i32, ptr %1, align 4, !tbaa !5
  %7 = load i32, ptr @_ZL15numTrackedFiles, align 4, !tbaa !5
  %8 = icmp slt i32 %6, %7
  br i1 %8, label %10, label %9

9:                                                ; preds = %5
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #5
  br label %77

10:                                               ; preds = %5
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #5
  %11 = load ptr, ptr @_ZL12trackedFiles, align 8, !tbaa !9
  %12 = load i32, ptr %1, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds %struct.FileTrackData, ptr %11, i64 %13
  store ptr %14, ptr %2, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #5
  %15 = load ptr, ptr %2, align 8, !tbaa !9
  %16 = getelementptr inbounds %struct.FileTrackData, ptr %15, i32 0, i32 0
  %17 = load ptr, ptr %16, align 8, !tbaa !11
  %18 = call noundef i64 @_ZL11getFileTimePKc(ptr noundef %17)
  store i64 %18, ptr %3, align 8, !tbaa !14
  %19 = load i64, ptr %3, align 8, !tbaa !14
  %20 = load ptr, ptr %2, align 8, !tbaa !9
  %21 = getelementptr inbounds %struct.FileTrackData, ptr %20, i32 0, i32 3
  %22 = load i64, ptr %21, align 8, !tbaa !15
  %23 = icmp ne i64 %19, %22
  br i1 %23, label %24, label %73

24:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 1, ptr %4) #5
  %25 = load ptr, ptr %2, align 8, !tbaa !9
  %26 = getelementptr inbounds %struct.FileTrackData, ptr %25, i32 0, i32 2
  %27 = load ptr, ptr %26, align 8, !tbaa !16
  %28 = load ptr, ptr %2, align 8, !tbaa !9
  %29 = getelementptr inbounds %struct.FileTrackData, ptr %28, i32 0, i32 0
  %30 = load ptr, ptr %29, align 8, !tbaa !11
  %31 = load ptr, ptr %2, align 8, !tbaa !9
  %32 = getelementptr inbounds %struct.FileTrackData, ptr %31, i32 0, i32 1
  %33 = load ptr, ptr %32, align 8, !tbaa !17
  %34 = call noundef zeroext i1 %27(ptr noundef %30, ptr noundef %33)
  %35 = zext i1 %34 to i8
  store i8 %35, ptr %4, align 1, !tbaa !18
  %36 = load i8, ptr %4, align 1, !tbaa !18, !range !20, !noundef !21
  %37 = trunc i8 %36 to i1
  br i1 %37, label %38, label %45

38:                                               ; preds = %24
  %39 = load ptr, ptr %2, align 8, !tbaa !9
  %40 = getelementptr inbounds %struct.FileTrackData, ptr %39, i32 0, i32 0
  %41 = load ptr, ptr %40, align 8, !tbaa !11
  %42 = call noundef i64 @_ZL11getFileTimePKc(ptr noundef %41)
  %43 = load ptr, ptr %2, align 8, !tbaa !9
  %44 = getelementptr inbounds %struct.FileTrackData, ptr %43, i32 0, i32 3
  store i64 %42, ptr %44, align 8, !tbaa !15
  br label %72

45:                                               ; preds = %24
  %46 = load ptr, ptr %2, align 8, !tbaa !9
  %47 = getelementptr inbounds %struct.FileTrackData, ptr %46, i32 0, i32 0
  %48 = load ptr, ptr %47, align 8, !tbaa !11
  call void @free(ptr noundef %48) #5
  %49 = load i32, ptr %1, align 4, !tbaa !5
  %50 = load i32, ptr @_ZL15numTrackedFiles, align 4, !tbaa !5
  %51 = sub nsw i32 %50, 1
  %52 = icmp slt i32 %49, %51
  br i1 %52, label %53, label %69

53:                                               ; preds = %45
  %54 = load ptr, ptr @_ZL12trackedFiles, align 8, !tbaa !9
  %55 = load i32, ptr %1, align 4, !tbaa !5
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds %struct.FileTrackData, ptr %54, i64 %56
  %58 = load ptr, ptr @_ZL12trackedFiles, align 8, !tbaa !9
  %59 = load i32, ptr %1, align 4, !tbaa !5
  %60 = add nsw i32 %59, 1
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds %struct.FileTrackData, ptr %58, i64 %61
  %63 = load i32, ptr @_ZL15numTrackedFiles, align 4, !tbaa !5
  %64 = sext i32 %63 to i64
  %65 = load i32, ptr %1, align 4, !tbaa !5
  %66 = sext i32 %65 to i64
  %67 = sub i64 %64, %66
  %68 = sub i64 %67, 1
  call void @llvm.memmove.p0.p0.i64(ptr align 8 %57, ptr align 8 %62, i64 %68, i1 false)
  br label %69

69:                                               ; preds = %53, %45
  %70 = load i32, ptr @_ZL15numTrackedFiles, align 4, !tbaa !5
  %71 = add nsw i32 %70, -1
  store i32 %71, ptr @_ZL15numTrackedFiles, align 4, !tbaa !5
  br label %72

72:                                               ; preds = %69, %38
  call void @llvm.lifetime.end.p0(i64 1, ptr %4) #5
  br label %73

73:                                               ; preds = %72, %10
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #5
  br label %74

74:                                               ; preds = %73
  %75 = load i32, ptr %1, align 4, !tbaa !5
  %76 = add nsw i32 %75, 1
  store i32 %76, ptr %1, align 4, !tbaa !5
  br label %5, !llvm.loop !22

77:                                               ; preds = %9
  ret void
}

; Function Attrs: nounwind
declare void @free(ptr noundef) #3

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { 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 #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 nounwind willreturn memory(argmem: readwrite) }
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, !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 = !{!12, !10, i64 0}
!12 = !{!"_ZTS13FileTrackData", !10, i64 0, !10, i64 8, !10, i64 16, !13, i64 24}
!13 = !{!"long", !7, i64 0}
!14 = !{!13, !13, i64 0}
!15 = !{!12, !13, i64 24}
!16 = !{!12, !10, i64 16}
!17 = !{!12, !10, i64 8}
!18 = !{!19, !19, i64 0}
!19 = !{!"bool", !7, i64 0}
!20 = !{i8 0, i8 2}
!21 = !{}
!22 = distinct !{!22, !23}
!23 = !{!"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.FileTrackData = type { ptr, ptr, ptr, i64 }

@_ZL15numTrackedFiles = external hidden local_unnamed_addr global i32, align 4
@_ZL12trackedFiles = external hidden local_unnamed_addr global ptr, align 8

; Function Attrs: mustprogress nounwind uwtable
declare hidden noundef i64 @_ZL11getFileTimePKc(ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local void @_Z17checkTrackedFilesv() local_unnamed_addr #1 {
  %1 = load i32, ptr @_ZL15numTrackedFiles, align 4, !tbaa !5
  %2 = icmp sgt i32 %1, 0
  br i1 %2, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %32, %0
  ret void

.lr.ph:                                           ; preds = %0, %32
  %indvars.iv = phi i64 [ %indvars.iv.next, %32 ], [ 0, %0 ]
  %3 = load ptr, ptr @_ZL12trackedFiles, align 8, !tbaa !9
  %4 = getelementptr inbounds %struct.FileTrackData, ptr %3, i64 %indvars.iv
  %5 = load ptr, ptr %4, align 8, !tbaa !11
  %6 = tail call noundef i64 @_ZL11getFileTimePKc(ptr noundef %5)
  %7 = getelementptr inbounds i8, ptr %4, i64 24
  %8 = load i64, ptr %7, align 8, !tbaa !14
  %.not = icmp eq i64 %6, %8
  br i1 %.not, label %32, label %9

9:                                                ; preds = %.lr.ph
  %10 = getelementptr inbounds i8, ptr %4, i64 16
  %11 = load ptr, ptr %10, align 8, !tbaa !15
  %12 = load ptr, ptr %4, align 8, !tbaa !11
  %13 = getelementptr inbounds i8, ptr %4, i64 8
  %14 = load ptr, ptr %13, align 8, !tbaa !16
  %15 = tail call noundef zeroext i1 %11(ptr noundef %12, ptr noundef %14)
  %16 = load ptr, ptr %4, align 8, !tbaa !11
  br i1 %15, label %17, label %19

17:                                               ; preds = %9
  %18 = tail call noundef i64 @_ZL11getFileTimePKc(ptr noundef %16)
  store i64 %18, ptr %7, align 8, !tbaa !14
  br label %32

19:                                               ; preds = %9
  tail call void @free(ptr noundef %16) #4
  %20 = load i32, ptr @_ZL15numTrackedFiles, align 4, !tbaa !5
  %21 = add nsw i32 %20, -1
  %22 = sext i32 %21 to i64
  %23 = icmp slt i64 %indvars.iv, %22
  br i1 %23, label %24, label %31

24:                                               ; preds = %19
  %25 = load ptr, ptr @_ZL12trackedFiles, align 8, !tbaa !9
  %26 = getelementptr inbounds %struct.FileTrackData, ptr %25, i64 %indvars.iv
  %27 = getelementptr inbounds i8, ptr %26, i64 32
  %28 = sext i32 %20 to i64
  %29 = xor i64 %indvars.iv, -1
  %30 = add nsw i64 %28, %29
  tail call void @llvm.memmove.p0.p0.i64(ptr align 8 %26, ptr nonnull align 8 %27, i64 %30, i1 false)
  %.pre = load i32, ptr @_ZL15numTrackedFiles, align 4, !tbaa !5
  %.pre18 = add nsw i32 %.pre, -1
  br label %31

31:                                               ; preds = %24, %19
  %.pre-phi = phi i32 [ %.pre18, %24 ], [ %21, %19 ]
  store i32 %.pre-phi, ptr @_ZL15numTrackedFiles, align 4, !tbaa !5
  br label %32

32:                                               ; preds = %17, %31, %.lr.ph
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %33 = load i32, ptr @_ZL15numTrackedFiles, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = icmp slt i64 %indvars.iv.next, %34
  br i1 %35, label %.lr.ph, label %._crit_edge, !llvm.loop !17
}

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

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

attributes #0 = { 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 #1 = { 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 #2 = { 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 #3 = { mustprogress 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 = !{!"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 = !{!12, !10, i64 0}
!12 = !{!"_ZTS13FileTrackData", !10, i64 0, !10, i64 8, !10, i64 16, !13, i64 24}
!13 = !{!"long", !7, i64 0}
!14 = !{!12, !13, i64 24}
!15 = !{!12, !10, i64 16}
!16 = !{!12, !10, i64 8}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: BDCEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass