; LLVM IR from CZWin32768/ACMICPC-Templates-in-Latex :: atl/codes/String/KMP.ll (Z5queryv)

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

@pat = external dso_local global [10005 x i8], align 16
@Next = external dso_local global [10005 x i32], align 16
@text = external dso_local global [1000005 x i8], align 16
@lt = external dso_local global i32, align 4
@lp = external dso_local global i32, align 4

; 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: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z5queryv() #1 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #2
  store i32 0, ptr %1, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #2
  store i32 0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %4

4:                                                ; preds = %41, %0
  %5 = load i32, ptr %2, align 4, !tbaa !5
  %6 = load i32, ptr @lt, align 4, !tbaa !5
  %7 = icmp sle i32 %5, %6
  br i1 %7, label %9, label %8

8:                                                ; preds = %4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #2
  br label %46

9:                                                ; preds = %4
  %10 = load i32, ptr %3, align 4, !tbaa !5
  %11 = load i32, ptr @lp, align 4, !tbaa !5
  %12 = icmp eq i32 %10, %11
  br i1 %12, label %13, label %16

13:                                               ; preds = %9
  %14 = load i32, ptr %1, align 4, !tbaa !5
  %15 = add nsw i32 %14, 1
  store i32 %15, ptr %1, align 4, !tbaa !5
  br label %16

16:                                               ; preds = %13, %9
  br label %17

17:                                               ; preds = %35, %16
  %18 = load i32, ptr %3, align 4, !tbaa !5
  %19 = xor i32 %18, -1
  %20 = icmp ne i32 %19, 0
  br i1 %20, label %21, label %33

21:                                               ; preds = %17
  %22 = load i32, ptr %2, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [1000005 x i8], ptr @text, i64 0, i64 %23
  %25 = load i8, ptr %24, align 1, !tbaa !9
  %26 = sext i8 %25 to i32
  %27 = load i32, ptr %3, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [10005 x i8], ptr @pat, i64 0, i64 %28
  %30 = load i8, ptr %29, align 1, !tbaa !9
  %31 = sext i8 %30 to i32
  %32 = icmp ne i32 %26, %31
  br label %33

33:                                               ; preds = %21, %17
  %34 = phi i1 [ false, %17 ], [ %32, %21 ]
  br i1 %34, label %35, label %40

35:                                               ; preds = %33
  %36 = load i32, ptr %3, align 4, !tbaa !5
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [10005 x i32], ptr @Next, i64 0, i64 %37
  %39 = load i32, ptr %38, align 4, !tbaa !5
  store i32 %39, ptr %3, align 4, !tbaa !5
  br label %17, !llvm.loop !10

40:                                               ; preds = %33
  br label %41

41:                                               ; preds = %40
  %42 = load i32, ptr %2, align 4, !tbaa !5
  %43 = add nsw i32 %42, 1
  store i32 %43, ptr %2, align 4, !tbaa !5
  %44 = load i32, ptr %3, align 4, !tbaa !5
  %45 = add nsw i32 %44, 1
  store i32 %45, ptr %3, align 4, !tbaa !5
  br label %4, !llvm.loop !12

46:                                               ; preds = %8
  %47 = load i32, ptr %1, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #2
  ret i32 %47
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = distinct !{!12, !11}


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

@pat = external dso_local local_unnamed_addr global [10005 x i8], align 16
@Next = external dso_local local_unnamed_addr global [10005 x i32], align 16
@text = external dso_local local_unnamed_addr global [1000005 x i8], align 16
@lt = external dso_local local_unnamed_addr global i32, align 4
@lp = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_Z5queryv() local_unnamed_addr #0 {
  %1 = load i32, ptr @lt, align 4, !tbaa !5
  %.not19 = icmp slt i32 %1, 0
  br i1 %.not19, label %._crit_edge, label %.lr.ph23

.lr.ph23:                                         ; preds = %0
  %2 = load i32, ptr @lp, align 4, !tbaa !5
  %3 = add nuw i32 %1, 1
  %wide.trip.count = zext i32 %3 to i64
  br label %4

._crit_edge:                                      ; preds = %.critedge, %0
  %.011.lcssa = phi i32 [ 0, %0 ], [ %spec.select, %.critedge ]
  ret i32 %.011.lcssa

4:                                                ; preds = %.lr.ph23, %.critedge
  %indvars.iv = phi i64 [ 0, %.lr.ph23 ], [ %indvars.iv.next, %.critedge ]
  %.022 = phi i32 [ 0, %.lr.ph23 ], [ %16, %.critedge ]
  %.01120 = phi i32 [ 0, %.lr.ph23 ], [ %spec.select, %.critedge ]
  %5 = icmp eq i32 %.022, %2
  %6 = zext i1 %5 to i32
  %spec.select = add nuw nsw i32 %.01120, %6
  %.not1315 = icmp eq i32 %.022, -1
  br i1 %.not1315, label %.critedge, label %.lr.ph

.lr.ph:                                           ; preds = %4
  %7 = getelementptr inbounds [1000005 x i8], ptr @text, i64 0, i64 %indvars.iv
  %8 = load i8, ptr %7, align 1, !tbaa !9
  br label %9

9:                                                ; preds = %.lr.ph, %13
  %.116 = phi i32 [ %.022, %.lr.ph ], [ %15, %13 ]
  %10 = sext i32 %.116 to i64
  %11 = getelementptr inbounds [10005 x i8], ptr @pat, i64 0, i64 %10
  %12 = load i8, ptr %11, align 1, !tbaa !9
  %.not14 = icmp eq i8 %8, %12
  br i1 %.not14, label %.critedge, label %13

13:                                               ; preds = %9
  %14 = getelementptr inbounds [10005 x i32], ptr @Next, i64 0, i64 %10
  %15 = load i32, ptr %14, align 4, !tbaa !5
  %.not13 = icmp eq i32 %15, -1
  br i1 %.not13, label %.critedge, label %9, !llvm.loop !10

.critedge:                                        ; preds = %13, %9, %4
  %.1.lcssa = phi i32 [ -1, %4 ], [ %.116, %9 ], [ -1, %13 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %16 = add nsw i32 %.1.lcssa, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %4, !llvm.loop !12
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) 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" }

!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 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = distinct !{!12, !11}


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