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

@standardtokens = external dso_local global [67 x %struct.tokendefn], align 16
@.str.67 = external hidden unnamed_addr constant [10 x i8], align 1
@.str.68 = external hidden unnamed_addr constant [11 x i8], align 1
@nstandardtokens = external dso_local global i32, align 4
@.str.69 = external hidden unnamed_addr constant [11 x i8], align 1
@.str.70 = external hidden unnamed_addr constant [20 x i8], align 1
@.str.71 = external hidden unnamed_addr constant [37 x i8], align 1
@.str.72 = external hidden unnamed_addr constant [21 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local i32 @_lex_tokndefncmp(ptr noundef, 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 willreturn memory(read)
declare i64 @strlen(ptr noundef) #2

declare void @qsort(ptr noundef, i64 noundef, i64 noundef, ptr noundef) #3

; Function Attrs: nounwind uwtable
define dso_local void @lex_initialize() #0 {
  %1 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #4
  store i32 0, ptr %1, align 4, !tbaa !5
  br label %2

2:                                                ; preds = %19, %0
  %3 = load i32, ptr %1, align 4, !tbaa !5
  %4 = sext i32 %3 to i64
  %5 = getelementptr inbounds [67 x %struct.tokendefn], ptr @standardtokens, i64 0, i64 %4
  %6 = getelementptr inbounds %struct.tokendefn, ptr %5, i32 0, i32 0
  %7 = load ptr, ptr %6, align 8, !tbaa !9
  %8 = icmp eq ptr %7, null
  br i1 %8, label %17, label %9

9:                                                ; preds = %2
  %10 = load i32, ptr %1, align 4, !tbaa !5
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds [67 x %struct.tokendefn], ptr @standardtokens, i64 0, i64 %11
  %13 = getelementptr inbounds %struct.tokendefn, ptr %12, i32 0, i32 0
  %14 = load ptr, ptr %13, align 8, !tbaa !9
  %15 = call i64 @strlen(ptr noundef %14) #5
  %16 = icmp eq i64 %15, 0
  br i1 %16, label %17, label %18

17:                                               ; preds = %9, %2
  br label %22

18:                                               ; preds = %9
  br label %19

19:                                               ; preds = %18
  %20 = load i32, ptr %1, align 4, !tbaa !5
  %21 = add nsw i32 %20, 1
  store i32 %21, ptr %1, align 4, !tbaa !5
  br label %2

22:                                               ; preds = %17
  %23 = load i32, ptr %1, align 4, !tbaa !5
  %24 = sext i32 %23 to i64
  call void @qsort(ptr noundef @standardtokens, i64 noundef %24, i64 noundef 24, ptr noundef @_lex_tokndefncmp)
  %25 = load i32, ptr %1, align 4, !tbaa !5
  store i32 %25, ptr @nstandardtokens, align 4, !tbaa !5
  call void @morpho_defineerror(ptr noundef @.str.69, i32 noundef 5, ptr noundef @.str.70)
  call void @morpho_defineerror(ptr noundef @.str.67, i32 noundef 5, ptr noundef @.str.71)
  call void @morpho_defineerror(ptr noundef @.str.68, i32 noundef 5, ptr noundef @.str.72)
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #4
  ret void
}

declare void @morpho_defineerror(ptr noundef, i32 noundef, ptr noundef) #3

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nounwind willreturn memory(read) "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 = { "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 willreturn memory(read) }

!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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 0}
!10 = !{!"", !11, i64 0, !6, i64 8, !11, i64 16}
!11 = !{!"any pointer", !7, i64 0}


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

@standardtokens = external dso_local global [67 x %struct.tokendefn], align 16
@.str.67 = external hidden unnamed_addr constant [10 x i8], align 1
@.str.68 = external hidden unnamed_addr constant [11 x i8], align 1
@nstandardtokens = external dso_local local_unnamed_addr global i32, align 4
@.str.69 = external hidden unnamed_addr constant [11 x i8], align 1
@.str.70 = external hidden unnamed_addr constant [20 x i8], align 1
@.str.71 = external hidden unnamed_addr constant [37 x i8], align 1
@.str.72 = external hidden unnamed_addr constant [21 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local i32 @_lex_tokndefncmp(ptr noundef, ptr noundef) #0

; Function Attrs: nofree
declare void @qsort(ptr noundef, i64 noundef, i64 noundef, ptr nocapture noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @lex_initialize() local_unnamed_addr #0 {
  %1 = load ptr, ptr @standardtokens, align 16, !tbaa !5
  %2 = icmp eq ptr %1, null
  br i1 %2, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %0, %5
  %indvars.iv = phi i64 [ %indvars.iv.next, %5 ], [ 0, %0 ]
  %3 = phi ptr [ %7, %5 ], [ %1, %0 ]
  %char0 = load i8, ptr %3, align 1
  %4 = icmp eq i8 %char0, 0
  br i1 %4, label %._crit_edge.loopexit, label %5

5:                                                ; preds = %.lr.ph
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %6 = getelementptr inbounds [67 x %struct.tokendefn], ptr @standardtokens, i64 0, i64 %indvars.iv.next
  %7 = load ptr, ptr %6, align 8, !tbaa !5
  %8 = icmp eq ptr %7, null
  br i1 %8, label %._crit_edge.loopexit, label %.lr.ph

._crit_edge.loopexit:                             ; preds = %5, %.lr.ph
  %.lcssa.ph = phi i64 [ %indvars.iv, %.lr.ph ], [ %indvars.iv.next, %5 ]
  %.0.lcssa.ph = trunc i64 %.lcssa.ph to i32
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %0
  %.0.lcssa = phi i32 [ 0, %0 ], [ %.0.lcssa.ph, %._crit_edge.loopexit ]
  %.lcssa = phi i64 [ 0, %0 ], [ %.lcssa.ph, %._crit_edge.loopexit ]
  tail call void @qsort(ptr noundef nonnull @standardtokens, i64 noundef %.lcssa, i64 noundef 24, ptr noundef nonnull @_lex_tokndefncmp) #3
  store i32 %.0.lcssa, ptr @nstandardtokens, align 4, !tbaa !11
  tail call void @morpho_defineerror(ptr noundef nonnull @.str.69, i32 noundef 5, ptr noundef nonnull @.str.70) #3
  tail call void @morpho_defineerror(ptr noundef nonnull @.str.67, i32 noundef 5, ptr noundef nonnull @.str.71) #3
  tail call void @morpho_defineerror(ptr noundef nonnull @.str.68, i32 noundef 5, ptr noundef nonnull @.str.72) #3
  ret void
}

declare void @morpho_defineerror(ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #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 = { nofree "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 = { 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 = !{!"", !7, i64 0, !10, i64 8, !7, i64 16}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!10, !10, i64 0}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass