; LLVM IR from tsotchke/quantum_geometric_tensor :: src/quantum_geometric/core (cache_manager)

; 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.anon = type { [1024 x ptr], %struct.quantum_cache_t, i64, i64, i8, i32, ptr }
%struct.quantum_cache_t = type { i32, ptr, ptr, i64, %struct.quantum_protection_t }
%struct.quantum_protection_t = type { ptr, i64, float, i8 }
%struct.cache_entry = type { ptr, i64, i64, i8, i8, i64, %struct.quantum_protection_t, ptr }

@cache_state = external hidden global %struct.anon, 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

declare ptr @quantum_system_create(i64 noundef, i32 noundef) #1

declare ptr @quantum_create_cache_circuit(i64 noundef, i32 noundef) #1

declare void @quantum_circuit_destroy(ptr noundef) #1

declare void @quantum_system_destroy(ptr noundef) #1

declare void @quantum_free_cache_entry(ptr noundef, ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @qg_cache_cleanup() #2 {
  %1 = alloca ptr, align 8
  %2 = alloca ptr, align 8
  %3 = alloca i64, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = load i8, ptr getelementptr inbounds (%struct.anon, ptr @cache_state, i32 0, i32 4), align 8, !tbaa !5, !range !16, !noundef !17
  %7 = trunc i8 %6 to i1
  br i1 %7, label %9, label %8

8:                                                ; preds = %0
  br label %39

9:                                                ; preds = %0
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #3
  %10 = call ptr @quantum_system_create(i64 noundef 1024, i32 noundef 3)
  store ptr %10, ptr %1, align 8, !tbaa !18
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #3
  %11 = call ptr @quantum_create_cache_circuit(i64 noundef 1024, i32 noundef 1)
  store ptr %11, ptr %2, align 8, !tbaa !18
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  store i64 0, ptr %3, align 8, !tbaa !19
  br label %12

12:                                               ; preds = %33, %9
  %13 = load i64, ptr %3, align 8, !tbaa !19
  %14 = icmp ult i64 %13, 1024
  br i1 %14, label %16, label %15

15:                                               ; preds = %12
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  br label %36

16:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %17 = load i64, ptr %3, align 8, !tbaa !19
  %18 = getelementptr inbounds [1024 x ptr], ptr @cache_state, i64 0, i64 %17
  %19 = load ptr, ptr %18, align 8, !tbaa !18
  store ptr %19, ptr %4, align 8, !tbaa !18
  br label %20

20:                                               ; preds = %23, %16
  %21 = load ptr, ptr %4, align 8, !tbaa !18
  %22 = icmp ne ptr %21, null
  br i1 %22, label %23, label %30

23:                                               ; preds = %20
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %24 = load ptr, ptr %4, align 8, !tbaa !18
  %25 = getelementptr inbounds %struct.cache_entry, ptr %24, i32 0, i32 7
  %26 = load ptr, ptr %25, align 8, !tbaa !20
  store ptr %26, ptr %5, align 8, !tbaa !18
  %27 = load ptr, ptr %4, align 8, !tbaa !18
  %28 = load ptr, ptr %1, align 8, !tbaa !18
  call void @quantum_free_cache_entry(ptr noundef %27, ptr noundef getelementptr inbounds (%struct.anon, ptr @cache_state, i32 0, i32 1), ptr noundef %28)
  %29 = load ptr, ptr %5, align 8, !tbaa !18
  store ptr %29, ptr %4, align 8, !tbaa !18
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  br label %20, !llvm.loop !22

30:                                               ; preds = %20
  %31 = load i64, ptr %3, align 8, !tbaa !19
  %32 = getelementptr inbounds [1024 x ptr], ptr @cache_state, i64 0, i64 %31
  store ptr null, ptr %32, align 8, !tbaa !18
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  br label %33

33:                                               ; preds = %30
  %34 = load i64, ptr %3, align 8, !tbaa !19
  %35 = add i64 %34, 1
  store i64 %35, ptr %3, align 8, !tbaa !19
  br label %12, !llvm.loop !24

36:                                               ; preds = %15
  call void @quantum_cache_destroy(ptr noundef byval(%struct.quantum_cache_t) align 8 getelementptr inbounds (%struct.anon, ptr @cache_state, i32 0, i32 1))
  %37 = load ptr, ptr %2, align 8, !tbaa !18
  call void @quantum_circuit_destroy(ptr noundef %37)
  %38 = load ptr, ptr %1, align 8, !tbaa !18
  call void @quantum_system_destroy(ptr noundef %38)
  store i8 0, ptr getelementptr inbounds (%struct.anon, ptr @cache_state, i32 0, i32 4), align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #3
  br label %39

39:                                               ; preds = %36, %8
  ret void
}

declare void @quantum_cache_destroy(ptr noundef byval(%struct.quantum_cache_t) align 8) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 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, !15, i64 8264}
!6 = !{!"", !7, i64 0, !9, i64 8192, !12, i64 8248, !12, i64 8256, !15, i64 8264, !10, i64 8268, !11, i64 8272}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"", !10, i64 0, !11, i64 8, !11, i64 16, !12, i64 24, !13, i64 32}
!10 = !{!"int", !7, i64 0}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!"", !11, i64 0, !12, i64 8, !14, i64 16, !15, i64 20}
!14 = !{!"float", !7, i64 0}
!15 = !{!"_Bool", !7, i64 0}
!16 = !{i8 0, i8 2}
!17 = !{}
!18 = !{!11, !11, i64 0}
!19 = !{!12, !12, i64 0}
!20 = !{!21, !11, i64 64}
!21 = !{!"cache_entry", !11, i64 0, !12, i64 8, !12, i64 16, !15, i64 24, !15, i64 25, !12, i64 32, !13, i64 40, !11, i64 64}
!22 = distinct !{!22, !23}
!23 = !{!"llvm.loop.mustprogress"}
!24 = distinct !{!24, !23}


; 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.anon = type { [1024 x ptr], %struct.quantum_cache_t, i64, i64, i8, i32, ptr }
%struct.quantum_cache_t = type { i32, ptr, ptr, i64, %struct.quantum_protection_t }
%struct.quantum_protection_t = type { ptr, i64, float, i8 }

@cache_state = external hidden global %struct.anon, align 8

declare ptr @quantum_system_create(i64 noundef, i32 noundef) local_unnamed_addr #0

declare ptr @quantum_create_cache_circuit(i64 noundef, i32 noundef) local_unnamed_addr #0

declare void @quantum_circuit_destroy(ptr noundef) local_unnamed_addr #0

declare void @quantum_system_destroy(ptr noundef) local_unnamed_addr #0

declare void @quantum_free_cache_entry(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @qg_cache_cleanup() local_unnamed_addr #1 {
  %1 = load i8, ptr getelementptr inbounds (i8, ptr @cache_state, i64 8264), align 8, !tbaa !5, !range !16, !noundef !17
  %2 = trunc nuw i8 %1 to i1
  br i1 %2, label %3, label %13

3:                                                ; preds = %0
  %4 = tail call ptr @quantum_system_create(i64 noundef 1024, i32 noundef 3) #2
  %5 = tail call ptr @quantum_create_cache_circuit(i64 noundef 1024, i32 noundef 1) #2
  br label %7

6:                                                ; preds = %._crit_edge
  tail call void @quantum_cache_destroy(ptr noundef nonnull byval(%struct.quantum_cache_t) align 8 getelementptr inbounds (i8, ptr @cache_state, i64 8192)) #2
  tail call void @quantum_circuit_destroy(ptr noundef %5) #2
  tail call void @quantum_system_destroy(ptr noundef %4) #2
  store i8 0, ptr getelementptr inbounds (i8, ptr @cache_state, i64 8264), align 8, !tbaa !5
  br label %13

7:                                                ; preds = %3, %._crit_edge
  %.013 = phi i64 [ 0, %3 ], [ %12, %._crit_edge ]
  %8 = getelementptr inbounds [1024 x ptr], ptr @cache_state, i64 0, i64 %.013
  %9 = load ptr, ptr %8, align 8, !tbaa !18
  %.not11 = icmp eq ptr %9, null
  br i1 %.not11, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %7, %.lr.ph
  %.01012 = phi ptr [ %11, %.lr.ph ], [ %9, %7 ]
  %10 = getelementptr inbounds i8, ptr %.01012, i64 64
  %11 = load ptr, ptr %10, align 8, !tbaa !19
  tail call void @quantum_free_cache_entry(ptr noundef nonnull %.01012, ptr noundef nonnull getelementptr inbounds (i8, ptr @cache_state, i64 8192), ptr noundef %4) #2
  %.not = icmp eq ptr %11, null
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !21

._crit_edge:                                      ; preds = %.lr.ph, %7
  store ptr null, ptr %8, align 8, !tbaa !18
  %12 = add nuw nsw i64 %.013, 1
  %exitcond.not = icmp eq i64 %12, 1024
  br i1 %exitcond.not, label %6, label %7, !llvm.loop !23

13:                                               ; preds = %0, %6
  ret void
}

declare void @quantum_cache_destroy(ptr noundef byval(%struct.quantum_cache_t) align 8) local_unnamed_addr #0

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 = { 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 = { 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, !15, i64 8264}
!6 = !{!"", !7, i64 0, !9, i64 8192, !12, i64 8248, !12, i64 8256, !15, i64 8264, !10, i64 8268, !11, i64 8272}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"", !10, i64 0, !11, i64 8, !11, i64 16, !12, i64 24, !13, i64 32}
!10 = !{!"int", !7, i64 0}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!"", !11, i64 0, !12, i64 8, !14, i64 16, !15, i64 20}
!14 = !{!"float", !7, i64 0}
!15 = !{!"_Bool", !7, i64 0}
!16 = !{i8 0, i8 2}
!17 = !{}
!18 = !{!11, !11, i64 0}
!19 = !{!20, !11, i64 64}
!20 = !{!"cache_entry", !11, i64 0, !12, i64 8, !12, i64 16, !15, i64 24, !15, i64 25, !12, i64 32, !13, i64 40, !11, i64 64}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}
!23 = distinct !{!23, !22}


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