; 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.quantum_cache_config_t = type { double, double, i8, i32, i32, i32 }
%struct.cache_entry = type { ptr, i64, i64, i8, i8, i64, %struct.quantum_protection_t, ptr }

@cache_state = external hidden global %struct.anon, align 8
@__const.qg_cache_free.qconfig = external hidden unnamed_addr constant %struct.quantum_cache_config_t, 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: 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) #1

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

12:                                               ; preds = %1
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = icmp ne ptr %13, null
  br i1 %14, label %16, label %15

15:                                               ; preds = %12, %1
  store i32 -4, ptr %2, align 4
  br label %49

16:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  %17 = call ptr @quantum_system_create(i64 noundef 1024, i32 noundef 3)
  store ptr %17, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 32, ptr %5) #4
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %5, ptr align 8 @__const.qg_cache_free.qconfig, i64 32, i1 false)
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %18 = call ptr @quantum_create_cache_circuit(i64 noundef 1024, i32 noundef 1)
  store ptr %18, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %19 = load ptr, ptr %3, align 8, !tbaa !5
  %20 = ptrtoint ptr %19 to i64
  %21 = urem i64 %20, 1024
  store i64 %21, ptr %7, align 8, !tbaa !19
  %22 = load i64, ptr %7, align 8, !tbaa !19
  %23 = icmp uge i64 %22, 1024
  br i1 %23, label %24, label %27

24:                                               ; preds = %16
  %25 = load ptr, ptr %6, align 8, !tbaa !5
  call void @quantum_circuit_destroy(ptr noundef %25)
  %26 = load ptr, ptr %4, align 8, !tbaa !5
  call void @quantum_system_destroy(ptr noundef %26)
  store i32 -4, ptr %2, align 4
  store i32 1, ptr %8, align 4
  br label %48

27:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %28 = load i64, ptr %7, align 8, !tbaa !19
  %29 = getelementptr inbounds [1024 x ptr], ptr @cache_state, i64 0, i64 %28
  %30 = load ptr, ptr %29, align 8, !tbaa !5
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = load ptr, ptr %6, align 8, !tbaa !5
  %34 = call ptr @quantum_remove_cache_entry(ptr noundef %30, ptr noundef %31, ptr noundef getelementptr inbounds (%struct.anon, ptr @cache_state, i32 0, i32 1), ptr noundef %32, ptr noundef %33, ptr noundef null)
  store ptr %34, ptr %9, align 8, !tbaa !5
  %35 = load ptr, ptr %9, align 8, !tbaa !5
  %36 = icmp ne ptr %35, null
  br i1 %36, label %37, label %45

37:                                               ; preds = %27
  %38 = load ptr, ptr %9, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.cache_entry, ptr %38, i32 0, i32 1
  %40 = load i64, ptr %39, align 8, !tbaa !20
  %41 = load i64, ptr getelementptr inbounds (%struct.anon, ptr @cache_state, i32 0, i32 3), align 8, !tbaa !22
  %42 = sub i64 %41, %40
  store i64 %42, ptr getelementptr inbounds (%struct.anon, ptr @cache_state, i32 0, i32 3), align 8, !tbaa !22
  %43 = load ptr, ptr %9, align 8, !tbaa !5
  %44 = load ptr, ptr %4, align 8, !tbaa !5
  call void @quantum_free_cache_entry(ptr noundef %43, ptr noundef getelementptr inbounds (%struct.anon, ptr @cache_state, i32 0, i32 1), ptr noundef %44)
  br label %45

45:                                               ; preds = %37, %27
  %46 = load ptr, ptr %6, align 8, !tbaa !5
  call void @quantum_circuit_destroy(ptr noundef %46)
  %47 = load ptr, ptr %4, align 8, !tbaa !5
  call void @quantum_system_destroy(ptr noundef %47)
  store i32 0, ptr %2, align 4
  store i32 1, ptr %8, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  br label %48

48:                                               ; preds = %45, %24
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 32, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  br label %49

49:                                               ; preds = %48, %15
  %50 = load i32, ptr %2, align 4
  ret i32 %50
}

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

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

declare void @quantum_circuit_destroy(ptr noundef) #3

declare void @quantum_system_destroy(ptr noundef) #3

declare ptr @quantum_remove_cache_entry(ptr noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef) #3

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

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


; 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

; Function Attrs: nounwind uwtable
define dso_local range(i32 -4, 1) i32 @qg_cache_free(ptr noundef %0) local_unnamed_addr #0 {
  %2 = load i8, ptr getelementptr inbounds (i8, ptr @cache_state, i64 8264), align 8, !tbaa !5, !range !16, !noundef !17
  %3 = trunc nuw i8 %2 to i1
  %4 = icmp ne ptr %0, null
  %or.cond = and i1 %4, %3
  br i1 %or.cond, label %5, label %19

5:                                                ; preds = %1
  %6 = tail call ptr @quantum_system_create(i64 noundef 1024, i32 noundef 3) #2
  %7 = tail call ptr @quantum_create_cache_circuit(i64 noundef 1024, i32 noundef 1) #2
  %8 = ptrtoint ptr %0 to i64
  %9 = and i64 %8, 1023
  %10 = getelementptr inbounds [1024 x ptr], ptr @cache_state, i64 0, i64 %9
  %11 = load ptr, ptr %10, align 8, !tbaa !18
  %12 = tail call ptr @quantum_remove_cache_entry(ptr noundef %11, ptr noundef nonnull %0, ptr noundef nonnull getelementptr inbounds (i8, ptr @cache_state, i64 8192), ptr noundef %6, ptr noundef %7, ptr noundef null) #2
  %.not = icmp eq ptr %12, null
  br i1 %.not, label %18, label %13

13:                                               ; preds = %5
  %14 = getelementptr inbounds i8, ptr %12, i64 8
  %15 = load i64, ptr %14, align 8, !tbaa !19
  %16 = load i64, ptr getelementptr inbounds (i8, ptr @cache_state, i64 8256), align 8, !tbaa !21
  %17 = sub i64 %16, %15
  store i64 %17, ptr getelementptr inbounds (i8, ptr @cache_state, i64 8256), align 8, !tbaa !21
  tail call void @quantum_free_cache_entry(ptr noundef nonnull %12, ptr noundef nonnull getelementptr inbounds (i8, ptr @cache_state, i64 8192), ptr noundef %6) #2
  br label %18

18:                                               ; preds = %13, %5
  tail call void @quantum_circuit_destroy(ptr noundef %7) #2
  tail call void @quantum_system_destroy(ptr noundef %6) #2
  br label %19

19:                                               ; preds = %1, %18
  %.1 = phi i32 [ 0, %18 ], [ -4, %1 ]
  ret i32 %.1
}

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

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

declare void @quantum_circuit_destroy(ptr noundef) local_unnamed_addr #1

declare void @quantum_system_destroy(ptr noundef) local_unnamed_addr #1

declare ptr @quantum_remove_cache_entry(ptr noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

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

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 = { "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, !12, i64 8}
!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 = !{!6, !12, i64 8256}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,SROAPass,SimplifyCFGPass,TailCallElimPass