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

; 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.quantum_circuit_t = type { ptr, i64, i64, i8 }
%struct.circuit_layer_t = type { ptr, i64, i8 }
%struct.quantum_gate_t = type { i64, ptr, ptr, ptr, i64, i8, i32, ptr, i64, i8 }

@.str.11 = external hidden unnamed_addr constant [43 x 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: nounwind uwtable
declare hidden void @set_error(ptr noundef) #1

; 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 zeroext i1 @apply_quantum_gate(ptr noundef, ptr noundef, ptr noundef, i64 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local zeroext i1 @apply_quantum_circuit(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca i1, align 1
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca ptr, 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
  call void @set_error(ptr noundef @.str.11)
  store i1 false, ptr %3, align 1
  br label %75

17:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  store i64 0, ptr %6, align 8, !tbaa !9
  br label %18

18:                                               ; preds = %69, %17
  %19 = load i64, ptr %6, align 8, !tbaa !9
  %20 = load ptr, ptr %5, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.quantum_circuit_t, ptr %20, i32 0, i32 1
  %22 = load i64, ptr %21, align 8, !tbaa !11
  %23 = icmp ult i64 %19, %22
  br i1 %23, label %25, label %24

24:                                               ; preds = %18
  store i32 2, ptr %7, align 4
  br label %72

25:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %26 = load ptr, ptr %5, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.quantum_circuit_t, ptr %26, i32 0, i32 0
  %28 = load ptr, ptr %27, align 8, !tbaa !14
  %29 = load i64, ptr %6, align 8, !tbaa !9
  %30 = getelementptr inbounds ptr, ptr %28, i64 %29
  %31 = load ptr, ptr %30, align 8, !tbaa !5
  store ptr %31, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  store i64 0, ptr %9, align 8, !tbaa !9
  br label %32

32:                                               ; preds = %60, %25
  %33 = load i64, ptr %9, align 8, !tbaa !9
  %34 = load ptr, ptr %8, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.circuit_layer_t, ptr %34, i32 0, i32 1
  %36 = load i64, ptr %35, align 8, !tbaa !15
  %37 = icmp ult i64 %33, %36
  br i1 %37, label %39, label %38

38:                                               ; preds = %32
  store i32 5, ptr %7, align 4
  br label %63

39:                                               ; preds = %32
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  %40 = load ptr, ptr %8, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.circuit_layer_t, ptr %40, i32 0, i32 0
  %42 = load ptr, ptr %41, align 8, !tbaa !17
  %43 = load i64, ptr %9, align 8, !tbaa !9
  %44 = getelementptr inbounds ptr, ptr %42, i64 %43
  %45 = load ptr, ptr %44, align 8, !tbaa !5
  store ptr %45, ptr %10, align 8, !tbaa !5
  %46 = load ptr, ptr %4, align 8, !tbaa !5
  %47 = load ptr, ptr %10, align 8, !tbaa !5
  %48 = load ptr, ptr %10, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.quantum_gate_t, ptr %48, i32 0, i32 2
  %50 = load ptr, ptr %49, align 8, !tbaa !18
  %51 = load ptr, ptr %10, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.quantum_gate_t, ptr %51, i32 0, i32 0
  %53 = load i64, ptr %52, align 8, !tbaa !21
  %54 = call zeroext i1 @apply_quantum_gate(ptr noundef %46, ptr noundef %47, ptr noundef %50, i64 noundef %53)
  br i1 %54, label %56, label %55

55:                                               ; preds = %39
  store i1 false, ptr %3, align 1
  store i32 1, ptr %7, align 4
  br label %57

56:                                               ; preds = %39
  store i32 0, ptr %7, align 4
  br label %57

57:                                               ; preds = %56, %55
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  %58 = load i32, ptr %7, align 4
  switch i32 %58, label %63 [
    i32 0, label %59
  ]

59:                                               ; preds = %57
  br label %60

60:                                               ; preds = %59
  %61 = load i64, ptr %9, align 8, !tbaa !9
  %62 = add i64 %61, 1
  store i64 %62, ptr %9, align 8, !tbaa !9
  br label %32, !llvm.loop !22

63:                                               ; preds = %57, %38
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  %64 = load i32, ptr %7, align 4
  switch i32 %64, label %66 [
    i32 5, label %65
  ]

65:                                               ; preds = %63
  store i32 0, ptr %7, align 4
  br label %66

66:                                               ; preds = %65, %63
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  %67 = load i32, ptr %7, align 4
  switch i32 %67, label %72 [
    i32 0, label %68
  ]

68:                                               ; preds = %66
  br label %69

69:                                               ; preds = %68
  %70 = load i64, ptr %6, align 8, !tbaa !9
  %71 = add i64 %70, 1
  store i64 %71, ptr %6, align 8, !tbaa !9
  br label %18, !llvm.loop !24

72:                                               ; preds = %66, %24
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  %73 = load i32, ptr %7, align 4
  switch i32 %73, label %77 [
    i32 2, label %74
    i32 1, label %75
  ]

74:                                               ; preds = %72
  store i1 true, ptr %3, align 1
  br label %75

75:                                               ; preds = %74, %72, %16
  %76 = load i1, ptr %3, align 1
  ret i1 %76

77:                                               ; preds = %72
  unreachable
}

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 = { 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, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !10, i64 8}
!12 = !{!"quantum_circuit_t", !6, i64 0, !10, i64 8, !10, i64 16, !13, i64 24}
!13 = !{!"_Bool", !7, i64 0}
!14 = !{!12, !6, i64 0}
!15 = !{!16, !10, i64 8}
!16 = !{!"circuit_layer_t", !6, i64 0, !10, i64 8, !13, i64 16}
!17 = !{!16, !6, i64 0}
!18 = !{!19, !6, i64 16}
!19 = !{!"quantum_gate_t", !10, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !10, i64 32, !13, i64 40, !20, i64 44, !6, i64 48, !10, i64 56, !13, i64 64}
!20 = !{!"int", !7, i64 0}
!21 = !{!19, !10, i64 0}
!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"

@.str.11 = external hidden unnamed_addr constant [43 x i8], align 1

; Function Attrs: nounwind uwtable
declare hidden void @set_error(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @apply_quantum_gate(ptr noundef, ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef zeroext i1 @apply_quantum_circuit(ptr noundef %0, ptr noundef readonly %1) local_unnamed_addr #0 {
  %3 = icmp ne ptr %0, null
  %4 = icmp ne ptr %1, null
  %or.cond = and i1 %3, %4
  br i1 %or.cond, label %.preheader, label %8

.preheader:                                       ; preds = %2
  %5 = getelementptr inbounds i8, ptr %1, i64 8
  %6 = load i64, ptr %5, align 8, !tbaa !5
  %7 = icmp eq i64 %6, 0
  br i1 %7, label %.thread, label %.lr.ph36

8:                                                ; preds = %2
  tail call void @set_error(ptr noundef nonnull @.str.11)
  br label %.thread

.lr.ph36:                                         ; preds = %.preheader, %._crit_edge
  %9 = phi i64 [ %25, %._crit_edge ], [ %6, %.preheader ]
  %.02435 = phi i64 [ %26, %._crit_edge ], [ 0, %.preheader ]
  %10 = load ptr, ptr %1, align 8, !tbaa !12
  %11 = getelementptr inbounds ptr, ptr %10, i64 %.02435
  %12 = load ptr, ptr %11, align 8, !tbaa !13
  %13 = getelementptr inbounds i8, ptr %12, i64 8
  %14 = load i64, ptr %13, align 8, !tbaa !14
  %.not33.not = icmp eq i64 %14, 0
  br i1 %.not33.not, label %._crit_edge, label %.lr.ph

15:                                               ; preds = %.lr.ph
  %16 = add nuw i64 %.02334, 1
  %17 = load i64, ptr %13, align 8, !tbaa !14
  %.not = icmp ult i64 %16, %17
  br i1 %.not, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !16

.lr.ph:                                           ; preds = %.lr.ph36, %15
  %.02334 = phi i64 [ %16, %15 ], [ 0, %.lr.ph36 ]
  %18 = load ptr, ptr %12, align 8, !tbaa !18
  %19 = getelementptr inbounds ptr, ptr %18, i64 %.02334
  %20 = load ptr, ptr %19, align 8, !tbaa !13
  %21 = getelementptr inbounds i8, ptr %20, i64 16
  %22 = load ptr, ptr %21, align 8, !tbaa !19
  %23 = load i64, ptr %20, align 8, !tbaa !22
  %24 = tail call zeroext i1 @apply_quantum_gate(ptr noundef nonnull %0, ptr noundef nonnull %20, ptr noundef %22, i64 noundef %23)
  br i1 %24, label %15, label %.thread

._crit_edge.loopexit:                             ; preds = %15
  %.pre = load i64, ptr %5, align 8, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %.lr.ph36
  %25 = phi i64 [ %.pre, %._crit_edge.loopexit ], [ %9, %.lr.ph36 ]
  %26 = add nuw i64 %.02435, 1
  %.not38 = icmp ult i64 %26, %25
  br i1 %.not38, label %.lr.ph36, label %.thread, !llvm.loop !23

.thread:                                          ; preds = %._crit_edge, %.lr.ph, %.preheader, %8
  %.5 = phi i1 [ false, %8 ], [ true, %.preheader ], [ false, %.lr.ph ], [ true, %._crit_edge ]
  ret i1 %.5
}

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

!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, !10, i64 8}
!6 = !{!"quantum_circuit_t", !7, i64 0, !10, 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 0}
!13 = !{!7, !7, i64 0}
!14 = !{!15, !10, i64 8}
!15 = !{!"circuit_layer_t", !7, i64 0, !10, i64 8, !11, i64 16}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!15, !7, i64 0}
!19 = !{!20, !7, i64 16}
!20 = !{!"quantum_gate_t", !10, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !10, i64 32, !11, i64 40, !21, i64 44, !7, i64 48, !10, i64 56, !11, i64 64}
!21 = !{!"int", !8, i64 0}
!22 = !{!20, !10, i64 0}
!23 = distinct !{!23, !17}


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