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

; 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.memory_distribution_t = type { ptr, ptr, i64, i8 }
%struct.quantum_system_t = type { i64, i64, i32, i32, ptr, ptr, ptr, ptr }
%struct.distributed_memory_config_t = type { i64, i64, i64, i8, i64 }

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

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #1

; Function Attrs: nounwind uwtable
declare hidden i32 @validate_system_state(ptr noundef) #2

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

; 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
define dso_local i32 @quantum_protect_distribution(ptr noundef %0, ptr noundef %1) #2 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = icmp ne ptr %11, null
  br i1 %12, label %13, label %16

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

16:                                               ; preds = %13, %2
  store i32 -1, ptr %3, align 4
  br label %127

17:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  %18 = load ptr, ptr %5, align 8, !tbaa !5
  %19 = call i32 @validate_system_state(ptr noundef %18)
  store i32 %19, ptr %6, align 4, !tbaa !9
  %20 = load i32, ptr %6, align 4, !tbaa !9
  %21 = icmp ne i32 %20, 0
  br i1 %21, label %22, label %24

22:                                               ; preds = %17
  %23 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %23, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %126

24:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %25, ptr %8, align 8, !tbaa !5
  %26 = load ptr, ptr %8, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.memory_distribution_t, ptr %26, i32 0, i32 0
  %28 = load ptr, ptr %27, align 8, !tbaa !11
  %29 = icmp ne ptr %28, null
  br i1 %29, label %30, label %40

30:                                               ; preds = %24
  %31 = load ptr, ptr %8, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.memory_distribution_t, ptr %31, i32 0, i32 1
  %33 = load ptr, ptr %32, align 8, !tbaa !15
  %34 = icmp ne ptr %33, null
  br i1 %34, label %35, label %40

35:                                               ; preds = %30
  %36 = load ptr, ptr %8, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.memory_distribution_t, ptr %36, i32 0, i32 2
  %38 = load i64, ptr %37, align 8, !tbaa !16
  %39 = icmp eq i64 %38, 0
  br i1 %39, label %40, label %41

40:                                               ; preds = %35, %30, %24
  store i32 -1, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %125

41:                                               ; preds = %35
  %42 = load ptr, ptr %5, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.quantum_system_t, ptr %42, i32 0, i32 2
  %44 = load i32, ptr %43, align 8, !tbaa !17
  %45 = and i32 %44, 4
  %46 = icmp ne i32 %45, 0
  br i1 %46, label %47, label %124

47:                                               ; preds = %41
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %48 = call noalias ptr @malloc(i64 noundef 40) #5
  store ptr %48, ptr %9, align 8, !tbaa !5
  %49 = load ptr, ptr %9, align 8, !tbaa !5
  %50 = icmp ne ptr %49, null
  br i1 %50, label %52, label %51

51:                                               ; preds = %47
  store i32 -2, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %121

52:                                               ; preds = %47
  %53 = load ptr, ptr %9, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.distributed_memory_config_t, ptr %53, i32 0, i32 0
  store i64 0, ptr %54, align 8, !tbaa !19
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  store i64 0, ptr %10, align 8, !tbaa !21
  br label %55

55:                                               ; preds = %83, %52
  %56 = load i64, ptr %10, align 8, !tbaa !21
  %57 = load ptr, ptr %8, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.memory_distribution_t, ptr %57, i32 0, i32 2
  %59 = load i64, ptr %58, align 8, !tbaa !16
  %60 = icmp ult i64 %56, %59
  br i1 %60, label %62, label %61

61:                                               ; preds = %55
  store i32 2, ptr %7, align 4
  br label %86

62:                                               ; preds = %55
  %63 = load ptr, ptr %8, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.memory_distribution_t, ptr %63, i32 0, i32 0
  %65 = load ptr, ptr %64, align 8, !tbaa !11
  %66 = load i64, ptr %10, align 8, !tbaa !21
  %67 = getelementptr inbounds i64, ptr %65, i64 %66
  %68 = load i64, ptr %67, align 8, !tbaa !21
  %69 = icmp eq i64 %68, 0
  br i1 %69, label %70, label %72

70:                                               ; preds = %62
  %71 = load ptr, ptr %9, align 8, !tbaa !5
  call void @free(ptr noundef %71) #4
  store i32 -1, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %86

72:                                               ; preds = %62
  %73 = load ptr, ptr %8, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.memory_distribution_t, ptr %73, i32 0, i32 0
  %75 = load ptr, ptr %74, align 8, !tbaa !11
  %76 = load i64, ptr %10, align 8, !tbaa !21
  %77 = getelementptr inbounds i64, ptr %75, i64 %76
  %78 = load i64, ptr %77, align 8, !tbaa !21
  %79 = load ptr, ptr %9, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.distributed_memory_config_t, ptr %79, i32 0, i32 0
  %81 = load i64, ptr %80, align 8, !tbaa !19
  %82 = add i64 %81, %78
  store i64 %82, ptr %80, align 8, !tbaa !19
  br label %83

83:                                               ; preds = %72
  %84 = load i64, ptr %10, align 8, !tbaa !21
  %85 = add i64 %84, 1
  store i64 %85, ptr %10, align 8, !tbaa !21
  br label %55, !llvm.loop !22

86:                                               ; preds = %70, %61
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  %87 = load i32, ptr %7, align 4
  switch i32 %87, label %121 [
    i32 2, label %88
  ]

88:                                               ; preds = %86
  %89 = load ptr, ptr %8, align 8, !tbaa !5
  %90 = getelementptr inbounds %struct.memory_distribution_t, ptr %89, i32 0, i32 0
  %91 = load ptr, ptr %90, align 8, !tbaa !11
  %92 = getelementptr inbounds i64, ptr %91, i64 0
  %93 = load i64, ptr %92, align 8, !tbaa !21
  %94 = load ptr, ptr %9, align 8, !tbaa !5
  %95 = getelementptr inbounds %struct.distributed_memory_config_t, ptr %94, i32 0, i32 1
  store i64 %93, ptr %95, align 8, !tbaa !24
  %96 = load ptr, ptr %9, align 8, !tbaa !5
  %97 = getelementptr inbounds %struct.distributed_memory_config_t, ptr %96, i32 0, i32 0
  %98 = load i64, ptr %97, align 8, !tbaa !19
  %99 = load ptr, ptr %9, align 8, !tbaa !5
  %100 = getelementptr inbounds %struct.distributed_memory_config_t, ptr %99, i32 0, i32 1
  %101 = load i64, ptr %100, align 8, !tbaa !24
  %102 = sub i64 %98, %101
  %103 = load ptr, ptr %9, align 8, !tbaa !5
  %104 = getelementptr inbounds %struct.distributed_memory_config_t, ptr %103, i32 0, i32 2
  store i64 %102, ptr %104, align 8, !tbaa !25
  %105 = load ptr, ptr %9, align 8, !tbaa !5
  %106 = getelementptr inbounds %struct.distributed_memory_config_t, ptr %105, i32 0, i32 3
  store i8 1, ptr %106, align 8, !tbaa !26
  %107 = load ptr, ptr %9, align 8, !tbaa !5
  %108 = getelementptr inbounds %struct.distributed_memory_config_t, ptr %107, i32 0, i32 4
  store i64 0, ptr %108, align 8, !tbaa !27
  %109 = load ptr, ptr %5, align 8, !tbaa !5
  %110 = getelementptr inbounds %struct.quantum_system_t, ptr %109, i32 0, i32 4
  %111 = load ptr, ptr %110, align 8, !tbaa !28
  %112 = icmp ne ptr %111, null
  br i1 %112, label %113, label %117

113:                                              ; preds = %88
  %114 = load ptr, ptr %5, align 8, !tbaa !5
  %115 = getelementptr inbounds %struct.quantum_system_t, ptr %114, i32 0, i32 4
  %116 = load ptr, ptr %115, align 8, !tbaa !28
  call void @free(ptr noundef %116) #4
  br label %117

117:                                              ; preds = %113, %88
  %118 = load ptr, ptr %9, align 8, !tbaa !5
  %119 = load ptr, ptr %5, align 8, !tbaa !5
  %120 = getelementptr inbounds %struct.quantum_system_t, ptr %119, i32 0, i32 4
  store ptr %118, ptr %120, align 8, !tbaa !28
  store i32 0, ptr %7, align 4
  br label %121

121:                                              ; preds = %117, %86, %51
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  %122 = load i32, ptr %7, align 4
  switch i32 %122, label %125 [
    i32 0, label %123
  ]

123:                                              ; preds = %121
  br label %124

124:                                              ; preds = %123, %41
  store i32 0, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %125

125:                                              ; preds = %124, %121, %40
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  br label %126

126:                                              ; preds = %125, %22
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  br label %127

127:                                              ; preds = %126, %16
  %128 = load i32, ptr %3, align 4
  ret i32 %128
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nounwind allocsize(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 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 = { nounwind }
attributes #5 = { nounwind allocsize(0) }

!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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"memory_distribution_t", !6, i64 0, !6, i64 8, !13, i64 16, !14, i64 24}
!13 = !{!"long", !7, i64 0}
!14 = !{!"_Bool", !7, i64 0}
!15 = !{!12, !6, i64 8}
!16 = !{!12, !13, i64 16}
!17 = !{!18, !10, i64 16}
!18 = !{!"quantum_system_t", !13, i64 0, !13, i64 8, !10, i64 16, !10, i64 20, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48}
!19 = !{!20, !13, i64 0}
!20 = !{!"distributed_memory_config_t", !13, i64 0, !13, i64 8, !13, i64 16, !14, i64 24, !13, i64 32}
!21 = !{!13, !13, i64 0}
!22 = distinct !{!22, !23}
!23 = !{!"llvm.loop.mustprogress"}
!24 = !{!20, !13, i64 8}
!25 = !{!20, !13, i64 16}
!26 = !{!20, !14, i64 24}
!27 = !{!20, !13, i64 32}
!28 = !{!18, !6, i64 24}


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

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden i32 @validate_system_state(ptr noundef) local_unnamed_addr #1

; 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: nounwind uwtable
define dso_local i32 @quantum_protect_distribution(ptr noundef readonly %0, ptr noundef %1) local_unnamed_addr #1 {
  %3 = icmp ne ptr %0, null
  %4 = icmp ne ptr %1, null
  %or.cond = and i1 %3, %4
  br i1 %or.cond, label %5, label %.thread

5:                                                ; preds = %2
  %6 = tail call i32 @validate_system_state(ptr noundef nonnull %1)
  %.not = icmp eq i32 %6, 0
  br i1 %.not, label %7, label %.thread

7:                                                ; preds = %5
  %8 = load ptr, ptr %0, align 8, !tbaa !5
  %.not45 = icmp eq ptr %8, null
  br i1 %.not45, label %.thread, label %9

9:                                                ; preds = %7
  %10 = getelementptr inbounds i8, ptr %0, i64 8
  %11 = load ptr, ptr %10, align 8, !tbaa !12
  %.not46 = icmp eq ptr %11, null
  br i1 %.not46, label %.thread, label %12

12:                                               ; preds = %9
  %13 = getelementptr inbounds i8, ptr %0, i64 16
  %14 = load i64, ptr %13, align 8, !tbaa !13
  %15 = icmp eq i64 %14, 0
  br i1 %15, label %.thread, label %16

16:                                               ; preds = %12
  %17 = getelementptr inbounds i8, ptr %1, i64 16
  %18 = load i32, ptr %17, align 8, !tbaa !14
  %19 = and i32 %18, 4
  %.not47 = icmp eq i32 %19, 0
  br i1 %.not47, label %.thread, label %20

20:                                               ; preds = %16
  %21 = tail call noalias dereferenceable_or_null(40) ptr @malloc(i64 noundef 40) #3
  %.not48 = icmp eq ptr %21, null
  br i1 %.not48, label %.thread, label %.preheader

.preheader:                                       ; preds = %20, %27
  %22 = phi i64 [ %28, %27 ], [ 0, %20 ]
  %.053 = phi i64 [ %29, %27 ], [ 0, %20 ]
  %23 = getelementptr inbounds i64, ptr %8, i64 %.053
  %24 = load i64, ptr %23, align 8, !tbaa !17
  %25 = icmp eq i64 %24, 0
  br i1 %25, label %26, label %27

26:                                               ; preds = %.preheader
  tail call void @free(ptr noundef nonnull %21) #4
  br label %.thread

27:                                               ; preds = %.preheader
  %28 = add i64 %22, %24
  store i64 %28, ptr %21, align 8, !tbaa !18
  %29 = add nuw i64 %.053, 1
  %exitcond.not = icmp eq i64 %29, %14
  br i1 %exitcond.not, label %.critedge, label %.preheader, !llvm.loop !20

.critedge:                                        ; preds = %27
  %30 = load i64, ptr %8, align 8, !tbaa !17
  %31 = getelementptr inbounds i8, ptr %21, i64 8
  store i64 %30, ptr %31, align 8, !tbaa !22
  %32 = sub i64 %28, %30
  %33 = getelementptr inbounds i8, ptr %21, i64 16
  store i64 %32, ptr %33, align 8, !tbaa !23
  %34 = getelementptr inbounds i8, ptr %21, i64 24
  store i8 1, ptr %34, align 8, !tbaa !24
  %35 = getelementptr inbounds i8, ptr %21, i64 32
  store i64 0, ptr %35, align 8, !tbaa !25
  %36 = getelementptr inbounds i8, ptr %1, i64 24
  %37 = load ptr, ptr %36, align 8, !tbaa !26
  %.not50 = icmp eq ptr %37, null
  br i1 %.not50, label %39, label %38

38:                                               ; preds = %.critedge
  tail call void @free(ptr noundef nonnull %37) #4
  br label %39

39:                                               ; preds = %.critedge, %38
  store ptr %21, ptr %36, align 8, !tbaa !26
  br label %.thread

.thread:                                          ; preds = %20, %26, %16, %39, %5, %7, %9, %12, %2
  %.4 = phi i32 [ -1, %2 ], [ %6, %5 ], [ -1, %12 ], [ -1, %9 ], [ -1, %7 ], [ 0, %39 ], [ 0, %16 ], [ -2, %20 ], [ -1, %26 ]
  ret i32 %.4
}

attributes #0 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(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 #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 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 = { nounwind allocsize(0) }
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, !7, i64 0}
!6 = !{!"memory_distribution_t", !7, i64 0, !7, i64 8, !10, i64 16, !11, i64 24}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"_Bool", !8, i64 0}
!12 = !{!6, !7, i64 8}
!13 = !{!6, !10, i64 16}
!14 = !{!15, !16, i64 16}
!15 = !{!"quantum_system_t", !10, i64 0, !10, i64 8, !16, i64 16, !16, i64 20, !7, i64 24, !7, i64 32, !7, i64 40, !7, i64 48}
!16 = !{!"int", !8, i64 0}
!17 = !{!10, !10, i64 0}
!18 = !{!19, !10, i64 0}
!19 = !{!"distributed_memory_config_t", !10, i64 0, !10, i64 8, !10, i64 16, !11, i64 24, !10, i64 32}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}
!22 = !{!19, !10, i64 8}
!23 = !{!19, !10, i64 16}
!24 = !{!19, !11, i64 24}
!25 = !{!19, !10, i64 32}
!26 = !{!15, !7, i64 24}


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