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

; 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.tensor_network = type { ptr, i64, i64, i64, %struct.tensor_network_metrics, i32, i8 }
%struct.tensor_network_metrics = type { i64, i64, double, double, double }
%struct.tensor_node = type { ptr, ptr, i64, ptr, ptr, i64, i64, i8 }

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

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

15:                                               ; preds = %12
  %16 = load ptr, ptr %7, align 8, !tbaa !5
  %17 = icmp ne ptr %16, null
  br i1 %17, label %20, label %18

18:                                               ; preds = %15, %12, %3
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  call void @set_error(ptr noundef %19, i32 noundef -1)
  store i1 false, ptr %4, align 1
  br label %50

20:                                               ; preds = %15
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.tensor_network, ptr %21, i32 0, i32 1
  %23 = load i64, ptr %22, align 8, !tbaa !9
  %24 = load ptr, ptr %6, align 8, !tbaa !5
  store i64 %23, ptr %24, align 8, !tbaa !16
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  store i64 0, ptr %8, align 8, !tbaa !16
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  store i64 0, ptr %9, align 8, !tbaa !16
  br label %25

25:                                               ; preds = %43, %20
  %26 = load i64, ptr %9, align 8, !tbaa !16
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.tensor_network, ptr %27, i32 0, i32 1
  %29 = load i64, ptr %28, align 8, !tbaa !9
  %30 = icmp ult i64 %26, %29
  br i1 %30, label %32, label %31

31:                                               ; preds = %25
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  br label %46

32:                                               ; preds = %25
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.tensor_network, ptr %33, i32 0, i32 0
  %35 = load ptr, ptr %34, align 8, !tbaa !17
  %36 = load i64, ptr %9, align 8, !tbaa !16
  %37 = getelementptr inbounds ptr, ptr %35, i64 %36
  %38 = load ptr, ptr %37, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.tensor_node, ptr %38, i32 0, i32 5
  %40 = load i64, ptr %39, align 8, !tbaa !18
  %41 = load i64, ptr %8, align 8, !tbaa !16
  %42 = add i64 %41, %40
  store i64 %42, ptr %8, align 8, !tbaa !16
  br label %43

43:                                               ; preds = %32
  %44 = load i64, ptr %9, align 8, !tbaa !16
  %45 = add i64 %44, 1
  store i64 %45, ptr %9, align 8, !tbaa !16
  br label %25, !llvm.loop !20

46:                                               ; preds = %31
  %47 = load i64, ptr %8, align 8, !tbaa !16
  %48 = udiv i64 %47, 2
  %49 = load ptr, ptr %7, align 8, !tbaa !5
  store i64 %48, ptr %49, align 8, !tbaa !16
  store i1 true, ptr %4, align 1
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  br label %50

50:                                               ; preds = %46, %18
  %51 = load i1, ptr %4, align 1
  ret i1 %51
}

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, !11, i64 8}
!10 = !{!"tensor_network", !6, i64 0, !11, i64 8, !11, i64 16, !11, i64 24, !12, i64 32, !14, i64 72, !15, i64 76}
!11 = !{!"long", !7, i64 0}
!12 = !{!"tensor_network_metrics", !11, i64 0, !11, i64 8, !13, i64 16, !13, i64 24, !13, i64 32}
!13 = !{!"double", !7, i64 0}
!14 = !{!"int", !7, i64 0}
!15 = !{!"_Bool", !7, i64 0}
!16 = !{!11, !11, i64 0}
!17 = !{!10, !6, i64 0}
!18 = !{!19, !11, i64 40}
!19 = !{!"tensor_node", !6, i64 0, !6, i64 8, !11, i64 16, !6, i64 24, !6, i64 32, !11, i64 40, !11, i64 48, !15, i64 56}
!20 = distinct !{!20, !21}
!21 = !{!"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"

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

; Function Attrs: nounwind uwtable
define dso_local noundef zeroext i1 @get_network_size(ptr noundef %0, ptr noundef writeonly %1, ptr noundef writeonly %2) local_unnamed_addr #0 {
  %4 = icmp ne ptr %0, null
  %5 = icmp ne ptr %1, null
  %or.cond = and i1 %4, %5
  %6 = icmp ne ptr %2, null
  %or.cond3 = and i1 %or.cond, %6
  br i1 %or.cond3, label %8, label %7

7:                                                ; preds = %3
  tail call void @set_error(ptr noundef %0, i32 noundef -1)
  br label %45

8:                                                ; preds = %3
  %9 = getelementptr inbounds i8, ptr %0, i64 8
  %10 = load i64, ptr %9, align 8, !tbaa !5
  store i64 %10, ptr %1, align 8, !tbaa !15
  %.not = icmp eq i64 %10, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %8
  %11 = load ptr, ptr %0, align 8, !tbaa !16
  %xtraiter = and i64 %10, 3
  %12 = icmp ult i64 %10, 4
  br i1 %12, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.new

.lr.ph.new:                                       ; preds = %.lr.ph
  %unroll_iter = and i64 %10, -4
  br label %20

._crit_edge.loopexit.unr-lcssa:                   ; preds = %20, %.lr.ph
  %.lcssa.ph = phi i64 [ poison, %.lr.ph ], [ %43, %20 ]
  %.023.unr = phi i64 [ 0, %.lr.ph ], [ %44, %20 ]
  %.01722.unr = phi i64 [ 0, %.lr.ph ], [ %43, %20 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge.loopexit, label %.epil.preheader

.epil.preheader:                                  ; preds = %._crit_edge.loopexit.unr-lcssa, %.epil.preheader
  %.023.epil = phi i64 [ %18, %.epil.preheader ], [ %.023.unr, %._crit_edge.loopexit.unr-lcssa ]
  %.01722.epil = phi i64 [ %17, %.epil.preheader ], [ %.01722.unr, %._crit_edge.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.epil.preheader ], [ 0, %._crit_edge.loopexit.unr-lcssa ]
  %13 = getelementptr inbounds ptr, ptr %11, i64 %.023.epil
  %14 = load ptr, ptr %13, align 8, !tbaa !17
  %15 = getelementptr inbounds i8, ptr %14, i64 40
  %16 = load i64, ptr %15, align 8, !tbaa !18
  %17 = add i64 %16, %.01722.epil
  %18 = add nuw i64 %.023.epil, 1
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge.loopexit, label %.epil.preheader, !llvm.loop !20

._crit_edge.loopexit:                             ; preds = %.epil.preheader, %._crit_edge.loopexit.unr-lcssa
  %.lcssa = phi i64 [ %.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %17, %.epil.preheader ]
  %19 = lshr i64 %.lcssa, 1
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %8
  %.017.lcssa = phi i64 [ 0, %8 ], [ %19, %._crit_edge.loopexit ]
  store i64 %.017.lcssa, ptr %2, align 8, !tbaa !15
  br label %45

20:                                               ; preds = %20, %.lr.ph.new
  %.023 = phi i64 [ 0, %.lr.ph.new ], [ %44, %20 ]
  %.01722 = phi i64 [ 0, %.lr.ph.new ], [ %43, %20 ]
  %niter = phi i64 [ 0, %.lr.ph.new ], [ %niter.next.3, %20 ]
  %21 = getelementptr inbounds ptr, ptr %11, i64 %.023
  %22 = load ptr, ptr %21, align 8, !tbaa !17
  %23 = getelementptr inbounds i8, ptr %22, i64 40
  %24 = load i64, ptr %23, align 8, !tbaa !18
  %25 = add i64 %24, %.01722
  %26 = or disjoint i64 %.023, 1
  %27 = getelementptr inbounds ptr, ptr %11, i64 %26
  %28 = load ptr, ptr %27, align 8, !tbaa !17
  %29 = getelementptr inbounds i8, ptr %28, i64 40
  %30 = load i64, ptr %29, align 8, !tbaa !18
  %31 = add i64 %30, %25
  %32 = or disjoint i64 %.023, 2
  %33 = getelementptr inbounds ptr, ptr %11, i64 %32
  %34 = load ptr, ptr %33, align 8, !tbaa !17
  %35 = getelementptr inbounds i8, ptr %34, i64 40
  %36 = load i64, ptr %35, align 8, !tbaa !18
  %37 = add i64 %36, %31
  %38 = or disjoint i64 %.023, 3
  %39 = getelementptr inbounds ptr, ptr %11, i64 %38
  %40 = load ptr, ptr %39, align 8, !tbaa !17
  %41 = getelementptr inbounds i8, ptr %40, i64 40
  %42 = load i64, ptr %41, align 8, !tbaa !18
  %43 = add i64 %42, %37
  %44 = add nuw i64 %.023, 4
  %niter.next.3 = add i64 %niter, 4
  %niter.ncmp.3 = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %._crit_edge.loopexit.unr-lcssa, label %20, !llvm.loop !22

45:                                               ; preds = %._crit_edge, %7
  ret i1 %or.cond3
}

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 = !{!"tensor_network", !7, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !11, i64 32, !13, i64 72, !14, i64 76}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"tensor_network_metrics", !10, i64 0, !10, i64 8, !12, i64 16, !12, i64 24, !12, i64 32}
!12 = !{!"double", !8, i64 0}
!13 = !{!"int", !8, i64 0}
!14 = !{!"_Bool", !8, i64 0}
!15 = !{!10, !10, i64 0}
!16 = !{!6, !7, i64 0}
!17 = !{!7, !7, i64 0}
!18 = !{!19, !10, i64 40}
!19 = !{!"tensor_node", !7, i64 0, !7, i64 8, !10, i64 16, !7, i64 24, !7, i64 32, !10, i64 40, !10, i64 48, !14, i64 56}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.unroll.disable"}
!22 = distinct !{!22, !23}
!23 = !{!"llvm.loop.mustprogress"}


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