; LLVM IR from Lodour/ACM-CheatSheet :: src/3_字符串 (3_AC自动机)

; 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.AC_automaton = type { [500100 x %"struct.AC_automaton::node"], ptr, ptr, [500100 x ptr] }
%"struct.AC_automaton::node" = type { [29 x ptr], ptr, ptr, i32, i32 }

@T = external dso_local global %struct.AC_automaton, align 8
@txt = external dso_local global [1001000 x i8], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca [55 x i8], align 16
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #5
  %9 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %2)
  br label %10

10:                                               ; preds = %52, %0
  %11 = load i32, ptr %2, align 4, !tbaa !5
  %12 = add nsw i32 %11, -1
  store i32 %12, ptr %2, align 4, !tbaa !5
  %13 = icmp ne i32 %11, 0
  br i1 %13, label %14, label %56

14:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #5
  %15 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %3)
  call void @_ZN12AC_automaton4initEv(ptr noundef nonnull align 8 dereferenceable(132026416) @T)
  call void @llvm.lifetime.start.p0(i64 55, ptr %4) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #5
  store i32 0, ptr %5, align 4, !tbaa !5
  br label %16

16:                                               ; preds = %49, %14
  %17 = load i32, ptr %5, align 4, !tbaa !5
  %18 = load i32, ptr %3, align 4, !tbaa !5
  %19 = icmp slt i32 %17, %18
  br i1 %19, label %21, label %20

20:                                               ; preds = %16
  store i32 4, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #5
  br label %52

21:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #5
  %22 = load ptr, ptr getelementptr inbounds (%struct.AC_automaton, ptr @T, i32 0, i32 2), align 8, !tbaa !9
  store ptr %22, ptr %7, align 8, !tbaa !12
  %23 = getelementptr inbounds [55 x i8], ptr %4, i64 0, i64 0
  %24 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef %23)
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #5
  store i32 0, ptr %8, align 4, !tbaa !5
  br label %25

25:                                               ; preds = %41, %21
  %26 = load i32, ptr %8, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [55 x i8], ptr %4, i64 0, i64 %27
  %29 = load i8, ptr %28, align 1, !tbaa !13
  %30 = icmp ne i8 %29, 0
  br i1 %30, label %32, label %31

31:                                               ; preds = %25
  store i32 7, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  br label %44

32:                                               ; preds = %25
  %33 = load ptr, ptr %7, align 8, !tbaa !12
  %34 = load i32, ptr %8, align 4, !tbaa !5
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [55 x i8], ptr %4, i64 0, i64 %35
  %37 = load i8, ptr %36, align 1, !tbaa !13
  %38 = sext i8 %37 to i32
  %39 = sub nsw i32 %38, 97
  %40 = call noundef ptr @_ZN12AC_automaton6appendEPNS_4nodeEi(ptr noundef nonnull align 8 dereferenceable(132026416) @T, ptr noundef %33, i32 noundef %39)
  store ptr %40, ptr %7, align 8, !tbaa !12
  br label %41

41:                                               ; preds = %32
  %42 = load i32, ptr %8, align 4, !tbaa !5
  %43 = add nsw i32 %42, 1
  store i32 %43, ptr %8, align 4, !tbaa !5
  br label %25, !llvm.loop !14

44:                                               ; preds = %31
  %45 = load ptr, ptr %7, align 8, !tbaa !12
  %46 = getelementptr inbounds %"struct.AC_automaton::node", ptr %45, i32 0, i32 3
  %47 = load i32, ptr %46, align 8, !tbaa !16
  %48 = add nsw i32 %47, 1
  store i32 %48, ptr %46, align 8, !tbaa !16
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #5
  br label %49

49:                                               ; preds = %44
  %50 = load i32, ptr %5, align 4, !tbaa !5
  %51 = add nsw i32 %50, 1
  store i32 %51, ptr %5, align 4, !tbaa !5
  br label %16, !llvm.loop !18

52:                                               ; preds = %20
  call void @_ZN12AC_automaton5buildEv(ptr noundef nonnull align 8 dereferenceable(132026416) @T)
  %53 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef @txt)
  %54 = call noundef i32 @_ZN12AC_automaton5queryEPc(ptr noundef nonnull align 8 dereferenceable(132026416) @T, ptr noundef @txt)
  %55 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i32 noundef %54)
  call void @llvm.lifetime.end.p0(i64 55, ptr %4) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #5
  br label %10, !llvm.loop !19

56:                                               ; preds = %10
  store i32 0, ptr %1, align 4
  store i32 1, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #5
  %57 = load i32, ptr %1, align 4
  ret i32 %57
}

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

declare i32 @__isoc99_scanf(ptr noundef, ...) #2

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN12AC_automaton4initEv(ptr noundef nonnull align 8 dereferenceable(132026416)) #3 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef ptr @_ZN12AC_automaton6appendEPNS_4nodeEi(ptr noundef nonnull align 8 dereferenceable(132026416), ptr noundef, i32 noundef) #4 align 2

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN12AC_automaton5buildEv(ptr noundef nonnull align 8 dereferenceable(132026416)) #4 align 2

declare i32 @printf(ptr noundef, ...) #2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZN12AC_automaton5queryEPc(ptr noundef nonnull align 8 dereferenceable(132026416), ptr noundef) #4 align 2

attributes #0 = { mustprogress norecurse 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 = { "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 = { mustprogress 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 #4 = { mustprogress 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 #5 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !11, i64 128025608}
!10 = !{!"_ZTS12AC_automaton", !7, i64 0, !11, i64 128025600, !11, i64 128025608, !7, i64 128025616}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!11, !11, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!17, !6, i64 248}
!17 = !{!"_ZTSN12AC_automaton4nodeE", !7, i64 0, !11, i64 232, !11, i64 240, !6, i64 248, !6, i64 252}
!18 = distinct !{!18, !15}
!19 = distinct !{!19, !15}


; 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.AC_automaton = type { [500100 x %"struct.AC_automaton::node"], ptr, ptr, [500100 x ptr] }
%"struct.AC_automaton::node" = type { [29 x ptr], ptr, ptr, i32, i32 }

@T = external dso_local global %struct.AC_automaton, align 8
@txt = external dso_local global [1001000 x i8], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca [55 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1) #5
  %4 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %1)
  %5 = load i32, ptr %1, align 4, !tbaa !5
  %6 = add nsw i32 %5, -1
  store i32 %6, ptr %1, align 4, !tbaa !5
  %.not18 = icmp eq i32 %5, 0
  br i1 %.not18, label %._crit_edge21, label %.lr.ph20

.lr.ph20:                                         ; preds = %0, %._crit_edge17
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %2) #5
  %7 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %2)
  call void @_ZN12AC_automaton4initEv(ptr noundef nonnull align 8 dereferenceable(132026416) @T)
  call void @llvm.lifetime.start.p0(i64 55, ptr nonnull %3) #5
  %8 = load i32, ptr %2, align 4, !tbaa !5
  %9 = icmp sgt i32 %8, 0
  br i1 %9, label %.lr.ph16, label %._crit_edge17

._crit_edge17:                                    ; preds = %._crit_edge, %.lr.ph20
  call void @_ZN12AC_automaton5buildEv(ptr noundef nonnull align 8 dereferenceable(132026416) @T)
  %10 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull @txt)
  %11 = call noundef i32 @_ZN12AC_automaton5queryEPc(ptr noundef nonnull align 8 dereferenceable(132026416) @T, ptr noundef nonnull @txt)
  %12 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %11)
  call void @llvm.lifetime.end.p0(i64 55, ptr nonnull %3) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %2) #5
  %13 = load i32, ptr %1, align 4, !tbaa !5
  %14 = add nsw i32 %13, -1
  store i32 %14, ptr %1, align 4, !tbaa !5
  %.not = icmp eq i32 %13, 0
  br i1 %.not, label %._crit_edge21, label %.lr.ph20, !llvm.loop !9

.lr.ph16:                                         ; preds = %.lr.ph20, %._crit_edge
  %.0814 = phi i32 [ %21, %._crit_edge ], [ 0, %.lr.ph20 ]
  %15 = load ptr, ptr getelementptr inbounds (i8, ptr @T, i64 128025608), align 8, !tbaa !11
  %16 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull %3)
  %17 = load i8, ptr %3, align 16, !tbaa !14
  %.not1011 = icmp eq i8 %17, 0
  br i1 %.not1011, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %.lr.ph16
  %.07.lcssa = phi ptr [ %15, %.lr.ph16 ], [ %27, %.lr.ph ]
  %18 = getelementptr inbounds i8, ptr %.07.lcssa, i64 248
  %19 = load i32, ptr %18, align 8, !tbaa !15
  %20 = add nsw i32 %19, 1
  store i32 %20, ptr %18, align 8, !tbaa !15
  %21 = add nuw nsw i32 %.0814, 1
  %22 = load i32, ptr %2, align 4, !tbaa !5
  %23 = icmp slt i32 %21, %22
  br i1 %23, label %.lr.ph16, label %._crit_edge17, !llvm.loop !17

.lr.ph:                                           ; preds = %.lr.ph16, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %.lr.ph16 ]
  %24 = phi i8 [ %29, %.lr.ph ], [ %17, %.lr.ph16 ]
  %.0712 = phi ptr [ %27, %.lr.ph ], [ %15, %.lr.ph16 ]
  %25 = sext i8 %24 to i32
  %26 = add nsw i32 %25, -97
  %27 = call noundef ptr @_ZN12AC_automaton6appendEPNS_4nodeEi(ptr noundef nonnull align 8 dereferenceable(132026416) @T, ptr noundef %.0712, i32 noundef %26)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %28 = getelementptr inbounds [55 x i8], ptr %3, i64 0, i64 %indvars.iv.next
  %29 = load i8, ptr %28, align 1, !tbaa !14
  %.not10 = icmp eq i8 %29, 0
  br i1 %.not10, label %._crit_edge, label %.lr.ph, !llvm.loop !18

._crit_edge21:                                    ; preds = %._crit_edge17, %0
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1) #5
  ret i32 0
}

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

; Function Attrs: nofree nounwind
declare noundef i32 @__isoc99_scanf(ptr nocapture noundef readonly, ...) local_unnamed_addr #2

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN12AC_automaton4initEv(ptr noundef nonnull align 8 dereferenceable(132026416)) local_unnamed_addr #3 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef ptr @_ZN12AC_automaton6appendEPNS_4nodeEi(ptr noundef nonnull align 8 dereferenceable(132026416), ptr noundef, i32 noundef) local_unnamed_addr #4 align 2

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN12AC_automaton5buildEv(ptr noundef nonnull align 8 dereferenceable(132026416)) local_unnamed_addr #4 align 2

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZN12AC_automaton5queryEPc(ptr noundef nonnull align 8 dereferenceable(132026416), ptr noundef) local_unnamed_addr #4 align 2

attributes #0 = { mustprogress norecurse 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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nofree nounwind "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 = { mustprogress 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 #4 = { mustprogress 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 #5 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!12, !13, i64 128025608}
!12 = !{!"_ZTS12AC_automaton", !7, i64 0, !13, i64 128025600, !13, i64 128025608, !7, i64 128025616}
!13 = !{!"any pointer", !7, i64 0}
!14 = !{!7, !7, i64 0}
!15 = !{!16, !6, i64 248}
!16 = !{!"_ZTSN12AC_automaton4nodeE", !7, i64 0, !13, i64 232, !13, i64 240, !6, i64 248, !6, i64 252}
!17 = distinct !{!17, !10}
!18 = distinct !{!18, !10}


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