; LLVM IR from Morpho-lang/morpho :: src/support (lex)

; 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.varray_tokendefn = type { i32, i32, ptr }
%struct.tokendefn = type { ptr, i32, ptr }

; 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: nounwind uwtable
define dso_local zeroext i1 @varray_tokendefnadd(ptr noundef %0, ptr noundef %1, i32 noundef %2) #1 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i32 %2, ptr %6, align 4, !tbaa !9
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.varray_tokendefn, ptr %9, i32 0, i32 1
  %11 = load i32, ptr %10, align 4, !tbaa !11
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.varray_tokendefn, ptr %12, i32 0, i32 0
  %14 = load i32, ptr %13, align 8, !tbaa !13
  %15 = load i32, ptr %6, align 4, !tbaa !9
  %16 = add i32 %14, %15
  %17 = icmp ult i32 %11, %16
  br i1 %17, label %18, label %42

18:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  %19 = load ptr, ptr %4, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.varray_tokendefn, ptr %19, i32 0, i32 0
  %21 = load i32, ptr %20, align 8, !tbaa !13
  %22 = load i32, ptr %6, align 4, !tbaa !9
  %23 = add i32 %21, %22
  %24 = call i32 @varray_powerof2ceiling(i32 noundef %23)
  store i32 %24, ptr %7, align 4, !tbaa !9
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.varray_tokendefn, ptr %25, i32 0, i32 2
  %27 = load ptr, ptr %26, align 8, !tbaa !14
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.varray_tokendefn, ptr %28, i32 0, i32 1
  %30 = load i32, ptr %29, align 4, !tbaa !11
  %31 = zext i32 %30 to i64
  %32 = mul i64 %31, 24
  %33 = load i32, ptr %7, align 4, !tbaa !9
  %34 = zext i32 %33 to i64
  %35 = mul i64 %34, 24
  %36 = call ptr @morpho_allocate(ptr noundef %27, i64 noundef %32, i64 noundef %35)
  %37 = load ptr, ptr %4, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.varray_tokendefn, ptr %37, i32 0, i32 2
  store ptr %36, ptr %38, align 8, !tbaa !14
  %39 = load i32, ptr %7, align 4, !tbaa !9
  %40 = load ptr, ptr %4, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.varray_tokendefn, ptr %40, i32 0, i32 1
  store i32 %39, ptr %41, align 4, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  br label %42

42:                                               ; preds = %18, %3
  %43 = load ptr, ptr %4, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.varray_tokendefn, ptr %43, i32 0, i32 2
  %45 = load ptr, ptr %44, align 8, !tbaa !14
  %46 = icmp ne ptr %45, null
  br i1 %46, label %47, label %74

47:                                               ; preds = %42
  %48 = load ptr, ptr %5, align 8, !tbaa !5
  %49 = icmp ne ptr %48, null
  br i1 %49, label %50, label %74

50:                                               ; preds = %47
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  store i32 0, ptr %8, align 4, !tbaa !9
  br label %51

51:                                               ; preds = %70, %50
  %52 = load i32, ptr %8, align 4, !tbaa !9
  %53 = load i32, ptr %6, align 4, !tbaa !9
  %54 = icmp ult i32 %52, %53
  br i1 %54, label %56, label %55

55:                                               ; preds = %51
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  br label %73

56:                                               ; preds = %51
  %57 = load ptr, ptr %4, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.varray_tokendefn, ptr %57, i32 0, i32 2
  %59 = load ptr, ptr %58, align 8, !tbaa !14
  %60 = load ptr, ptr %4, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.varray_tokendefn, ptr %60, i32 0, i32 0
  %62 = load i32, ptr %61, align 8, !tbaa !13
  %63 = add i32 %62, 1
  store i32 %63, ptr %61, align 8, !tbaa !13
  %64 = zext i32 %62 to i64
  %65 = getelementptr inbounds %struct.tokendefn, ptr %59, i64 %64
  %66 = load ptr, ptr %5, align 8, !tbaa !5
  %67 = load i32, ptr %8, align 4, !tbaa !9
  %68 = zext i32 %67 to i64
  %69 = getelementptr inbounds %struct.tokendefn, ptr %66, i64 %68
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %65, ptr align 8 %69, i64 24, i1 false), !tbaa.struct !15
  br label %70

70:                                               ; preds = %56
  %71 = load i32, ptr %8, align 4, !tbaa !9
  %72 = add i32 %71, 1
  store i32 %72, ptr %8, align 4, !tbaa !9
  br label %51, !llvm.loop !16

73:                                               ; preds = %55
  br label %74

74:                                               ; preds = %73, %47, %42
  %75 = load ptr, ptr %4, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.varray_tokendefn, ptr %75, i32 0, i32 2
  %77 = load ptr, ptr %76, align 8, !tbaa !14
  %78 = icmp ne ptr %77, null
  ret i1 %78
}

declare i32 @varray_powerof2ceiling(i32 noundef) #2

declare ptr @morpho_allocate(ptr noundef, i64 noundef, i64 noundef) #2

; 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 = { 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 = { "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 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 4}
!12 = !{!"", !10, i64 0, !10, i64 4, !6, i64 8}
!13 = !{!12, !10, i64 0}
!14 = !{!12, !6, i64 8}
!15 = !{i64 0, i64 8, !5, i64 8, i64 4, !9, i64 16, i64 8, !5}
!16 = distinct !{!16, !17}
!17 = !{!"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"

%struct.tokendefn = type { ptr, i32, ptr }

; Function Attrs: nounwind uwtable
define dso_local zeroext i1 @varray_tokendefnadd(ptr nocapture noundef %0, ptr noundef readonly %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = getelementptr inbounds i8, ptr %0, i64 4
  %5 = load i32, ptr %4, align 4, !tbaa !5
  %6 = load i32, ptr %0, align 8, !tbaa !11
  %7 = add i32 %6, %2
  %8 = icmp ult i32 %5, %7
  br i1 %8, label %9, label %._crit_edge

._crit_edge:                                      ; preds = %3
  %.phi.trans.insert = getelementptr inbounds i8, ptr %0, i64 8
  %.pre = load ptr, ptr %.phi.trans.insert, align 8, !tbaa !12
  br label %19

9:                                                ; preds = %3
  %10 = tail call i32 @varray_powerof2ceiling(i32 noundef %7) #3
  %11 = getelementptr inbounds i8, ptr %0, i64 8
  %12 = load ptr, ptr %11, align 8, !tbaa !12
  %13 = load i32, ptr %4, align 4, !tbaa !5
  %14 = zext i32 %13 to i64
  %15 = mul nuw nsw i64 %14, 24
  %16 = zext i32 %10 to i64
  %17 = mul nuw nsw i64 %16, 24
  %18 = tail call ptr @morpho_allocate(ptr noundef %12, i64 noundef %15, i64 noundef %17) #3
  store ptr %18, ptr %11, align 8, !tbaa !12
  store i32 %10, ptr %4, align 4, !tbaa !5
  br label %19

19:                                               ; preds = %._crit_edge, %9
  %20 = phi ptr [ %.pre, %._crit_edge ], [ %18, %9 ]
  %21 = getelementptr inbounds i8, ptr %0, i64 8
  %22 = icmp ne ptr %20, null
  %23 = icmp ne ptr %1, null
  %or.cond = and i1 %23, %22
  %24 = icmp ne i32 %2, 0
  %or.cond24 = and i1 %or.cond, %24
  br i1 %or.cond24, label %.lr.ph.preheader, label %.loopexit

.lr.ph.preheader:                                 ; preds = %19
  %wide.trip.count = zext i32 %2 to i64
  %xtraiter = and i64 %wide.trip.count, 1
  %25 = icmp eq i32 %2, 1
  br i1 %25, label %.loopexit.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %wide.trip.count, 4294967294
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader.new ], [ %indvars.iv.next.1, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %.lr.ph ]
  %26 = load ptr, ptr %21, align 8, !tbaa !12
  %27 = load i32, ptr %0, align 8, !tbaa !11
  %28 = add i32 %27, 1
  store i32 %28, ptr %0, align 8, !tbaa !11
  %29 = zext i32 %27 to i64
  %30 = getelementptr inbounds %struct.tokendefn, ptr %26, i64 %29
  %31 = getelementptr inbounds %struct.tokendefn, ptr %1, i64 %indvars.iv
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(24) %30, ptr noundef nonnull align 8 dereferenceable(24) %31, i64 24, i1 false), !tbaa.struct !13
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %32 = load ptr, ptr %21, align 8, !tbaa !12
  %33 = load i32, ptr %0, align 8, !tbaa !11
  %34 = add i32 %33, 1
  store i32 %34, ptr %0, align 8, !tbaa !11
  %35 = zext i32 %33 to i64
  %36 = getelementptr inbounds %struct.tokendefn, ptr %32, i64 %35
  %37 = getelementptr inbounds %struct.tokendefn, ptr %1, i64 %indvars.iv.next
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(24) %36, ptr noundef nonnull align 8 dereferenceable(24) %37, i64 24, i1 false), !tbaa.struct !13
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %.loopexit.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !16

.loopexit.loopexit.unr-lcssa:                     ; preds = %.lr.ph, %.lr.ph.preheader
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next.1, %.lr.ph ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.loopexit.loopexit, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %.loopexit.loopexit.unr-lcssa
  %38 = load ptr, ptr %21, align 8, !tbaa !12
  %39 = load i32, ptr %0, align 8, !tbaa !11
  %40 = add i32 %39, 1
  store i32 %40, ptr %0, align 8, !tbaa !11
  %41 = zext i32 %39 to i64
  %42 = getelementptr inbounds %struct.tokendefn, ptr %38, i64 %41
  %43 = getelementptr inbounds %struct.tokendefn, ptr %1, i64 %indvars.iv.unr
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(24) %42, ptr noundef nonnull align 8 dereferenceable(24) %43, i64 24, i1 false), !tbaa.struct !13
  br label %.loopexit.loopexit

.loopexit.loopexit:                               ; preds = %.loopexit.loopexit.unr-lcssa, %.lr.ph.epil
  %.pre26 = load ptr, ptr %21, align 8, !tbaa !12
  br label %.loopexit

.loopexit:                                        ; preds = %.loopexit.loopexit, %19
  %44 = phi ptr [ %.pre26, %.loopexit.loopexit ], [ %20, %19 ]
  %45 = icmp ne ptr %44, null
  ret i1 %45
}

declare i32 @varray_powerof2ceiling(i32 noundef) local_unnamed_addr #1

declare ptr @morpho_allocate(ptr noundef, i64 noundef, i64 noundef) local_unnamed_addr #1

; 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 = { 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 = { 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, !7, i64 4}
!6 = !{!"", !7, i64 0, !7, i64 4, !10, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !7, i64 0}
!12 = !{!6, !10, i64 8}
!13 = !{i64 0, i64 8, !14, i64 8, i64 4, !15, i64 16, i64 8, !14}
!14 = !{!10, !10, i64 0}
!15 = !{!7, !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


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