; LLVM IR from tsotchke/quantum_geometric_tensor :: src/quantum_geometric/core (tensor_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"

@.str.4 = external hidden unnamed_addr constant [40 x i8], align 1

declare void @geometric_log_error(ptr noundef, ...) #0

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

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

; Function Attrs: nounwind uwtable
define dso_local zeroext i1 @qg_tensor_reshape(ptr noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3) #2 {
  %5 = alloca i1, align 1
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store ptr %2, ptr %8, align 8, !tbaa !5
  store i64 %3, ptr %9, align 8, !tbaa !9
  %12 = load ptr, ptr %6, align 8, !tbaa !5
  %13 = icmp ne ptr %12, null
  br i1 %13, label %14, label %23

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

17:                                               ; preds = %14
  %18 = load ptr, ptr %8, align 8, !tbaa !5
  %19 = icmp ne ptr %18, null
  br i1 %19, label %20, label %23

20:                                               ; preds = %17
  %21 = load i64, ptr %9, align 8, !tbaa !9
  %22 = icmp eq i64 %21, 0
  br i1 %22, label %23, label %24

23:                                               ; preds = %20, %17, %14, %4
  call void (ptr, ...) @geometric_log_error(ptr noundef @.str.4)
  store i1 false, ptr %5, align 1
  br label %45

24:                                               ; preds = %20
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  store i64 1, ptr %10, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  store i64 0, ptr %11, align 8, !tbaa !9
  br label %25

25:                                               ; preds = %37, %24
  %26 = load i64, ptr %11, align 8, !tbaa !9
  %27 = load i64, ptr %9, align 8, !tbaa !9
  %28 = icmp ult i64 %26, %27
  br i1 %28, label %30, label %29

29:                                               ; preds = %25
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  br label %40

30:                                               ; preds = %25
  %31 = load ptr, ptr %8, align 8, !tbaa !5
  %32 = load i64, ptr %11, align 8, !tbaa !9
  %33 = getelementptr inbounds i64, ptr %31, i64 %32
  %34 = load i64, ptr %33, align 8, !tbaa !9
  %35 = load i64, ptr %10, align 8, !tbaa !9
  %36 = mul i64 %35, %34
  store i64 %36, ptr %10, align 8, !tbaa !9
  br label %37

37:                                               ; preds = %30
  %38 = load i64, ptr %11, align 8, !tbaa !9
  %39 = add i64 %38, 1
  store i64 %39, ptr %11, align 8, !tbaa !9
  br label %25, !llvm.loop !11

40:                                               ; preds = %29
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = load ptr, ptr %7, align 8, !tbaa !5
  %43 = load i64, ptr %10, align 8, !tbaa !9
  %44 = mul i64 %43, 8
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %41, ptr align 4 %42, i64 %44, i1 false)
  store i1 true, ptr %5, align 1
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  br label %45

45:                                               ; preds = %40, %23
  %46 = load i1, ptr %5, align 1
  ret i1 %46
}

; 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) #3

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 = { nocallback nofree nosync 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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"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"

@.str.4 = external hidden unnamed_addr constant [40 x i8], align 1

declare void @geometric_log_error(ptr noundef, ...) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef zeroext i1 @qg_tensor_reshape(ptr noundef writeonly %0, ptr noundef readonly %1, ptr noundef readonly %2, i64 noundef %3) local_unnamed_addr #1 {
  %5 = icmp ne ptr %0, null
  %6 = icmp ne ptr %1, null
  %or.cond.not24.not29 = and i1 %5, %6
  %7 = icmp ne ptr %2, null
  %or.cond3.not21.not26 = and i1 %or.cond.not24.not29, %7
  %8 = icmp ne i64 %3, 0
  %or.cond5.not = and i1 %or.cond3.not21.not26, %8
  br i1 %or.cond5.not, label %.preheader.preheader, label %10

.preheader.preheader:                             ; preds = %4
  %xtraiter = and i64 %3, 7
  %9 = icmp ult i64 %3, 8
  br i1 %9, label %.unr-lcssa, label %.preheader.preheader.new

.preheader.preheader.new:                         ; preds = %.preheader.preheader
  %unroll_iter = and i64 %3, -8
  br label %.preheader

10:                                               ; preds = %4
  tail call void (ptr, ...) @geometric_log_error(ptr noundef nonnull @.str.4) #3
  br label %48

.unr-lcssa:                                       ; preds = %.preheader, %.preheader.preheader
  %.lcssa.ph = phi i64 [ poison, %.preheader.preheader ], [ %46, %.preheader ]
  %.031.unr = phi i64 [ 0, %.preheader.preheader ], [ %47, %.preheader ]
  %.01830.unr = phi i64 [ 1, %.preheader.preheader ], [ %46, %.preheader ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.epilog-lcssa, label %.preheader.epil

.preheader.epil:                                  ; preds = %.unr-lcssa, %.preheader.epil
  %.031.epil = phi i64 [ %14, %.preheader.epil ], [ %.031.unr, %.unr-lcssa ]
  %.01830.epil = phi i64 [ %13, %.preheader.epil ], [ %.01830.unr, %.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.preheader.epil ], [ 0, %.unr-lcssa ]
  %11 = getelementptr inbounds i64, ptr %2, i64 %.031.epil
  %12 = load i64, ptr %11, align 8, !tbaa !5
  %13 = mul i64 %12, %.01830.epil
  %14 = add nuw i64 %.031.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 %.epilog-lcssa, label %.preheader.epil, !llvm.loop !9

.epilog-lcssa:                                    ; preds = %.preheader.epil, %.unr-lcssa
  %.lcssa = phi i64 [ %.lcssa.ph, %.unr-lcssa ], [ %13, %.preheader.epil ]
  %15 = shl i64 %.lcssa, 3
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 4 %0, ptr nonnull align 4 %1, i64 %15, i1 false)
  br label %48

.preheader:                                       ; preds = %.preheader, %.preheader.preheader.new
  %.031 = phi i64 [ 0, %.preheader.preheader.new ], [ %47, %.preheader ]
  %.01830 = phi i64 [ 1, %.preheader.preheader.new ], [ %46, %.preheader ]
  %niter = phi i64 [ 0, %.preheader.preheader.new ], [ %niter.next.7, %.preheader ]
  %16 = getelementptr inbounds i64, ptr %2, i64 %.031
  %17 = load i64, ptr %16, align 8, !tbaa !5
  %18 = mul i64 %17, %.01830
  %19 = or disjoint i64 %.031, 1
  %20 = getelementptr inbounds i64, ptr %2, i64 %19
  %21 = load i64, ptr %20, align 8, !tbaa !5
  %22 = mul i64 %21, %18
  %23 = or disjoint i64 %.031, 2
  %24 = getelementptr inbounds i64, ptr %2, i64 %23
  %25 = load i64, ptr %24, align 8, !tbaa !5
  %26 = mul i64 %25, %22
  %27 = or disjoint i64 %.031, 3
  %28 = getelementptr inbounds i64, ptr %2, i64 %27
  %29 = load i64, ptr %28, align 8, !tbaa !5
  %30 = mul i64 %29, %26
  %31 = or disjoint i64 %.031, 4
  %32 = getelementptr inbounds i64, ptr %2, i64 %31
  %33 = load i64, ptr %32, align 8, !tbaa !5
  %34 = mul i64 %33, %30
  %35 = or disjoint i64 %.031, 5
  %36 = getelementptr inbounds i64, ptr %2, i64 %35
  %37 = load i64, ptr %36, align 8, !tbaa !5
  %38 = mul i64 %37, %34
  %39 = or disjoint i64 %.031, 6
  %40 = getelementptr inbounds i64, ptr %2, i64 %39
  %41 = load i64, ptr %40, align 8, !tbaa !5
  %42 = mul i64 %41, %38
  %43 = or disjoint i64 %.031, 7
  %44 = getelementptr inbounds i64, ptr %2, i64 %43
  %45 = load i64, ptr %44, align 8, !tbaa !5
  %46 = mul i64 %45, %42
  %47 = add nuw i64 %.031, 8
  %niter.next.7 = add i64 %niter, 8
  %niter.ncmp.7 = icmp eq i64 %niter.next.7, %unroll_iter
  br i1 %niter.ncmp.7, label %.unr-lcssa, label %.preheader, !llvm.loop !11

48:                                               ; preds = %.epilog-lcssa, %10
  ret i1 %or.cond5.not
}

; Function Attrs: mustprogress 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) #2

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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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, !6, i64 0}
!6 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.unroll.disable"}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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