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

@.str.67 = external hidden unnamed_addr constant [10 x i8], align 1

; 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
declare dso_local void @lex_recordtoken(ptr noundef, i32 noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local signext i8 @lex_advance(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local signext i8 @lex_peek(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local signext i8 @lex_peekahead(ptr noundef, i32 noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @lex_newline(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local zeroext i1 @lex_skipmultilinecomment(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 i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i8, align 1
  %12 = alloca i32, align 4
  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
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 0, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.slexer, ptr %13, i32 0, i32 2
  %15 = load i32, ptr %14, align 8, !tbaa !11
  store i32 %15, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.slexer, ptr %16, i32 0, i32 3
  %18 = load i32, ptr %17, align 4, !tbaa !15
  store i32 %18, ptr %10, align 4, !tbaa !9
  br label %19

19:                                               ; preds = %61, %3
  call void @llvm.lifetime.start.p0(i64 1, ptr %11) #3
  %20 = load ptr, ptr %5, align 8, !tbaa !5
  %21 = call signext i8 @lex_peek(ptr noundef %20)
  store i8 %21, ptr %11, align 1, !tbaa !16
  %22 = load i8, ptr %11, align 1, !tbaa !16
  %23 = sext i8 %22 to i32
  switch i32 %23, label %54 [
    i32 0, label %24
    i32 10, label %30
    i32 47, label %32
    i32 42, label %43
  ]

24:                                               ; preds = %19
  %25 = load ptr, ptr %7, align 8, !tbaa !5
  %26 = load i32, ptr %9, align 4, !tbaa !9
  %27 = load i32, ptr %10, align 4, !tbaa !9
  call void (ptr, ptr, ptr, i32, i32, ...) @morpho_writeerrorwithid(ptr noundef %25, ptr noundef @.str.67, ptr noundef null, i32 noundef %26, i32 noundef %27)
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = load ptr, ptr %6, align 8, !tbaa !5
  call void @lex_recordtoken(ptr noundef %28, i32 noundef 71, ptr noundef %29)
  store i1 false, ptr %4, align 1
  store i32 1, ptr %12, align 4
  br label %58

30:                                               ; preds = %19
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  call void @lex_newline(ptr noundef %31)
  br label %55

32:                                               ; preds = %19
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  %34 = call signext i8 @lex_peekahead(ptr noundef %33, i32 noundef 1)
  %35 = sext i8 %34 to i32
  %36 = icmp eq i32 %35, 42
  br i1 %36, label %37, label %42

37:                                               ; preds = %32
  %38 = load i32, ptr %8, align 4, !tbaa !9
  %39 = add i32 %38, 1
  store i32 %39, ptr %8, align 4, !tbaa !9
  %40 = load ptr, ptr %5, align 8, !tbaa !5
  %41 = call signext i8 @lex_advance(ptr noundef %40)
  br label %42

42:                                               ; preds = %37, %32
  br label %55

43:                                               ; preds = %19
  %44 = load ptr, ptr %5, align 8, !tbaa !5
  %45 = call signext i8 @lex_peekahead(ptr noundef %44, i32 noundef 1)
  %46 = sext i8 %45 to i32
  %47 = icmp eq i32 %46, 47
  br i1 %47, label %48, label %53

48:                                               ; preds = %43
  %49 = load i32, ptr %8, align 4, !tbaa !9
  %50 = add i32 %49, -1
  store i32 %50, ptr %8, align 4, !tbaa !9
  %51 = load ptr, ptr %5, align 8, !tbaa !5
  %52 = call signext i8 @lex_advance(ptr noundef %51)
  br label %53

53:                                               ; preds = %48, %43
  br label %55

54:                                               ; preds = %19
  br label %55

55:                                               ; preds = %54, %53, %42, %30
  %56 = load ptr, ptr %5, align 8, !tbaa !5
  %57 = call signext i8 @lex_advance(ptr noundef %56)
  store i32 0, ptr %12, align 4
  br label %58

58:                                               ; preds = %55, %24
  call void @llvm.lifetime.end.p0(i64 1, ptr %11) #3
  %59 = load i32, ptr %12, align 4
  switch i32 %59, label %65 [
    i32 0, label %60
  ]

60:                                               ; preds = %58
  br label %61

61:                                               ; preds = %60
  %62 = load i32, ptr %8, align 4, !tbaa !9
  %63 = icmp ugt i32 %62, 0
  br i1 %63, label %19, label %64, !llvm.loop !17

64:                                               ; preds = %61
  store i1 true, ptr %4, align 1
  store i32 1, ptr %12, align 4
  br label %65

65:                                               ; preds = %64, %58
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  %66 = load i1, ptr %4, align 1
  ret i1 %66
}

declare void @morpho_writeerrorwithid(ptr noundef, ptr noundef, ptr noundef, i32 noundef, i32 noundef, ...) #2

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 = { 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 16}
!12 = !{!"slexer", !6, i64 0, !6, i64 8, !10, i64 16, !10, i64 20, !13, i64 24, !13, 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, !14, i64 88}
!13 = !{!"_Bool", !7, i64 0}
!14 = !{!"", !10, i64 0, !10, i64 4, !6, i64 8}
!15 = !{!12, !10, i64 20}
!16 = !{!7, !7, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"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.67 = external hidden unnamed_addr constant [10 x i8], align 1

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

; Function Attrs: nounwind uwtable
declare dso_local signext i8 @lex_advance(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local signext i8 @lex_peek(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local signext i8 @lex_peekahead(ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @lex_newline(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef zeroext i1 @lex_skipmultilinecomment(ptr noundef %0, ptr noundef %1, ptr noundef %2) local_unnamed_addr #0 {
  %4 = getelementptr inbounds i8, ptr %0, i64 16
  %5 = load i32, ptr %4, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %0, i64 20
  %7 = load i32, ptr %6, align 4, !tbaa !13
  br label %8

8:                                                ; preds = %23, %3
  %.020 = phi i32 [ 0, %3 ], [ %.121, %23 ]
  %9 = tail call signext i8 @lex_peek(ptr noundef %0)
  switch i8 %9, label %23 [
    i8 0, label %.thread
    i8 10, label %10
    i8 47, label %11
    i8 42, label %17
  ]

.thread:                                          ; preds = %8
  tail call void (ptr, ptr, ptr, i32, i32, ...) @morpho_writeerrorwithid(ptr noundef %2, ptr noundef nonnull @.str.67, ptr noundef null, i32 noundef %5, i32 noundef %7) #2
  tail call void @lex_recordtoken(ptr noundef %0, i32 noundef 71, ptr noundef %1)
  br label %.loopexit

10:                                               ; preds = %8
  tail call void @lex_newline(ptr noundef %0)
  br label %23

11:                                               ; preds = %8
  %12 = tail call signext i8 @lex_peekahead(ptr noundef %0, i32 noundef 1)
  %13 = icmp eq i8 %12, 42
  br i1 %13, label %14, label %23

14:                                               ; preds = %11
  %15 = add i32 %.020, 1
  %16 = tail call signext i8 @lex_advance(ptr noundef %0)
  br label %23

17:                                               ; preds = %8
  %18 = tail call signext i8 @lex_peekahead(ptr noundef %0, i32 noundef 1)
  %19 = icmp eq i8 %18, 47
  br i1 %19, label %20, label %23

20:                                               ; preds = %17
  %21 = add i32 %.020, -1
  %22 = tail call signext i8 @lex_advance(ptr noundef %0)
  br label %23

23:                                               ; preds = %10, %14, %11, %20, %17, %8
  %.121 = phi i32 [ %.020, %8 ], [ %21, %20 ], [ %.020, %17 ], [ %15, %14 ], [ %.020, %11 ], [ %.020, %10 ]
  %24 = tail call signext i8 @lex_advance(ptr noundef %0)
  %.not = icmp eq i32 %.121, 0
  br i1 %.not, label %.loopexit, label %8, !llvm.loop !14

.loopexit:                                        ; preds = %23, %.thread
  %cond28 = phi i1 [ false, %.thread ], [ true, %23 ]
  ret i1 %cond28
}

declare void @morpho_writeerrorwithid(ptr noundef, ptr noundef, ptr noundef, i32 noundef, i32 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 = { "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, !10, i64 16}
!6 = !{!"slexer", !7, i64 0, !7, i64 8, !10, i64 16, !10, i64 20, !11, i64 24, !11, 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, !12, i64 88}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"_Bool", !8, i64 0}
!12 = !{!"", !10, i64 0, !10, i64 4, !7, i64 8}
!13 = !{!6, !10, i64 20}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass