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

; 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 uwtable
declare dso_local zeroext i1 @varray_tokendefnadd(ptr noundef, ptr noundef, i32 noundef) #0

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #2

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

6:                                                ; preds = %25, %2
  %7 = load ptr, ptr %4, align 8, !tbaa !5
  %8 = load i32, ptr %5, align 4, !tbaa !9
  %9 = sext i32 %8 to i64
  %10 = getelementptr inbounds %struct.tokendefn, ptr %7, i64 %9
  %11 = getelementptr inbounds %struct.tokendefn, ptr %10, i32 0, i32 0
  %12 = load ptr, ptr %11, align 8, !tbaa !11
  %13 = icmp eq ptr %12, null
  br i1 %13, label %23, label %14

14:                                               ; preds = %6
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = load i32, ptr %5, align 4, !tbaa !9
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds %struct.tokendefn, ptr %15, i64 %17
  %19 = getelementptr inbounds %struct.tokendefn, ptr %18, i32 0, i32 0
  %20 = load ptr, ptr %19, align 8, !tbaa !11
  %21 = call i64 @strlen(ptr noundef %20) #5
  %22 = icmp eq i64 %21, 0
  br i1 %22, label %23, label %24

23:                                               ; preds = %14, %6
  br label %28

24:                                               ; preds = %14
  br label %25

25:                                               ; preds = %24
  %26 = load i32, ptr %5, align 4, !tbaa !9
  %27 = add nsw i32 %26, 1
  store i32 %27, ptr %5, align 4, !tbaa !9
  br label %6

28:                                               ; preds = %23
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.slexer, ptr %29, i32 0, i32 16
  %31 = getelementptr inbounds %struct.varray_tokendefn, ptr %30, i32 0, i32 0
  store i32 0, ptr %31, align 8, !tbaa !13
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.slexer, ptr %32, i32 0, i32 16
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = load i32, ptr %5, align 4, !tbaa !9
  %36 = call zeroext i1 @varray_tokendefnadd(ptr noundef %33, ptr noundef %34, i32 noundef %35)
  %37 = load ptr, ptr %3, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.slexer, ptr %37, i32 0, i32 16
  %39 = getelementptr inbounds %struct.varray_tokendefn, ptr %38, i32 0, i32 2
  %40 = load ptr, ptr %39, align 8, !tbaa !17
  %41 = load ptr, ptr %3, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.slexer, ptr %41, i32 0, i32 14
  store ptr %40, ptr %42, align 8, !tbaa !18
  %43 = load i32, ptr %5, align 4, !tbaa !9
  %44 = load ptr, ptr %3, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.slexer, ptr %44, i32 0, i32 15
  store i32 %43, ptr %45, align 8, !tbaa !19
  %46 = load ptr, ptr %3, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.slexer, ptr %46, i32 0, i32 14
  %48 = load ptr, ptr %47, align 8, !tbaa !18
  %49 = load ptr, ptr %3, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.slexer, ptr %49, i32 0, i32 15
  %51 = load i32, ptr %50, align 8, !tbaa !19
  %52 = sext i32 %51 to i64
  call void @qsort(ptr noundef %48, i64 noundef %52, i64 noundef 24, ptr noundef @_lex_tokndefncmp)
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  ret void
}

declare void @qsort(ptr noundef, i64 noundef, i64 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 = !{!"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, !6, i64 0}
!12 = !{!"", !6, i64 0, !10, i64 8, !6, i64 16}
!13 = !{!14, !10, i64 88}
!14 = !{!"slexer", !6, i64 0, !6, i64 8, !10, i64 16, !10, i64 20, !15, i64 24, !15, i64 25, !6, i64 32, !6, i64 40, !10, i64 48, !10, i64 52, !10, i64 56, !10, i64 60, !10, i64 64, !10, i64 68, !6, i64 72, !10, i64 80, !16, i64 88}
!15 = !{!"_Bool", !7, i64 0}
!16 = !{!"", !10, i64 0, !10, i64 4, !6, i64 8}
!17 = !{!14, !6, i64 96}
!18 = !{!14, !6, i64 72}
!19 = !{!14, !10, i64 80}


; 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
declare dso_local i32 @_lex_tokndefncmp(ptr noundef, ptr noundef) #0

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

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

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

7:                                                ; preds = %.lr.ph
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %8 = getelementptr inbounds %struct.tokendefn, ptr %1, i64 %indvars.iv.next
  %9 = load ptr, ptr %8, align 8, !tbaa !5
  %10 = icmp eq ptr %9, null
  br i1 %10, label %._crit_edge.loopexit, label %.lr.ph

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

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %2
  %.0.lcssa = phi i32 [ 0, %2 ], [ %.0.lcssa.ph, %._crit_edge.loopexit ]
  %.lcssa = phi i64 [ 0, %2 ], [ %.lcssa.ph, %._crit_edge.loopexit ]
  %11 = getelementptr inbounds i8, ptr %0, i64 88
  store i32 0, ptr %11, align 8, !tbaa !11
  %12 = tail call zeroext i1 @varray_tokendefnadd(ptr noundef nonnull %11, ptr noundef nonnull %1, i32 noundef %.0.lcssa)
  %13 = getelementptr inbounds i8, ptr %0, i64 96
  %14 = load ptr, ptr %13, align 8, !tbaa !15
  %15 = getelementptr inbounds i8, ptr %0, i64 72
  store ptr %14, ptr %15, align 8, !tbaa !16
  %16 = getelementptr inbounds i8, ptr %0, i64 80
  store i32 %.0.lcssa, ptr %16, align 8, !tbaa !17
  tail call void @qsort(ptr noundef %14, i64 noundef %.lcssa, i64 noundef 24, ptr noundef nonnull @_lex_tokndefncmp) #2
  ret void
}

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

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 = { 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 = !{!12, !10, i64 88}
!12 = !{!"slexer", !7, i64 0, !7, i64 8, !10, i64 16, !10, i64 20, !13, i64 24, !13, i64 25, !7, i64 32, !7, i64 40, !10, i64 48, !10, i64 52, !10, i64 56, !10, i64 60, !10, i64 64, !10, i64 68, !7, i64 72, !10, i64 80, !14, i64 88}
!13 = !{!"_Bool", !8, i64 0}
!14 = !{!"", !10, i64 0, !10, i64 4, !7, i64 8}
!15 = !{!12, !7, i64 96}
!16 = !{!12, !7, i64 72}
!17 = !{!12, !10, i64 80}


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