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

; 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
@lp = external dso_local global i32, align 4

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z7getNextv() #0 {
  %1 = alloca i32, align 4
  %2 = 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 -1, ptr %2, align 4, !tbaa !5
  br label %3

3:                                                ; preds = %36, %0
  %4 = load i32, ptr %1, align 4, !tbaa !5
  %5 = load i32, ptr @lp, align 4, !tbaa !5
  %6 = icmp sle i32 %4, %5
  br i1 %6, label %8, label %7

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

8:                                                ; preds = %3
  %9 = load i32, ptr %2, align 4, !tbaa !5
  %10 = load i32, ptr %1, align 4, !tbaa !5
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds [10005 x i32], ptr @Next, i64 0, i64 %11
  store i32 %9, ptr %12, align 4, !tbaa !5
  br label %13

13:                                               ; preds = %30, %8
  %14 = load i32, ptr %2, align 4, !tbaa !5
  %15 = icmp ne i32 %14, -1
  br i1 %15, label %16, label %28

16:                                               ; preds = %13
  %17 = load i32, ptr %1, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [10005 x i8], ptr @pat, i64 0, i64 %18
  %20 = load i8, ptr %19, align 1, !tbaa !9
  %21 = sext i8 %20 to i32
  %22 = load i32, ptr %2, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [10005 x i8], ptr @pat, i64 0, i64 %23
  %25 = load i8, ptr %24, align 1, !tbaa !9
  %26 = sext i8 %25 to i32
  %27 = icmp ne i32 %21, %26
  br label %28

28:                                               ; preds = %16, %13
  %29 = phi i1 [ false, %13 ], [ %27, %16 ]
  br i1 %29, label %30, label %35

30:                                               ; preds = %28
  %31 = load i32, ptr %2, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [10005 x i32], ptr @Next, i64 0, i64 %32
  %34 = load i32, ptr %33, align 4, !tbaa !5
  store i32 %34, ptr %2, align 4, !tbaa !5
  br label %13, !llvm.loop !10

35:                                               ; preds = %28
  br label %36

36:                                               ; preds = %35
  %37 = load i32, ptr %1, align 4, !tbaa !5
  %38 = add nsw i32 %37, 1
  store i32 %38, ptr %1, align 4, !tbaa !5
  %39 = load i32, ptr %2, align 4, !tbaa !5
  %40 = add nsw i32 %39, 1
  store i32 %40, ptr %2, align 4, !tbaa !5
  br label %3, !llvm.loop !12

41:                                               ; preds = %7
  ret void
}

; 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

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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
@lp = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z7getNextv() local_unnamed_addr #0 {
  %1 = load i32, ptr @lp, align 4, !tbaa !5
  %.not16 = icmp slt i32 %1, 0
  br i1 %.not16, label %._crit_edge, label %.lr.ph19.preheader

.lr.ph19.preheader:                               ; preds = %0
  %2 = add nuw i32 %1, 1
  %wide.trip.count = zext i32 %2 to i64
  br label %.lr.ph19

._crit_edge:                                      ; preds = %.critedge, %0
  ret void

.lr.ph19:                                         ; preds = %.lr.ph19.preheader, %.critedge
  %indvars.iv = phi i64 [ 0, %.lr.ph19.preheader ], [ %indvars.iv.next, %.critedge ]
  %.018 = phi i32 [ -1, %.lr.ph19.preheader ], [ %13, %.critedge ]
  %3 = getelementptr inbounds [10005 x i32], ptr @Next, i64 0, i64 %indvars.iv
  store i32 %.018, ptr %3, align 4, !tbaa !5
  %.not1012 = icmp eq i32 %.018, -1
  br i1 %.not1012, label %.critedge, label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph19
  %4 = getelementptr inbounds [10005 x i8], ptr @pat, i64 0, i64 %indvars.iv
  %5 = load i8, ptr %4, align 1, !tbaa !9
  br label %6

6:                                                ; preds = %.lr.ph, %10
  %.113 = phi i32 [ %.018, %.lr.ph ], [ %12, %10 ]
  %7 = sext i32 %.113 to i64
  %8 = getelementptr inbounds [10005 x i8], ptr @pat, i64 0, i64 %7
  %9 = load i8, ptr %8, align 1, !tbaa !9
  %.not11 = icmp eq i8 %5, %9
  br i1 %.not11, label %.critedge, label %10

10:                                               ; preds = %6
  %11 = getelementptr inbounds [10005 x i32], ptr @Next, i64 0, i64 %7
  %12 = load i32, ptr %11, align 4, !tbaa !5
  %.not10 = icmp eq i32 %12, -1
  br i1 %.not10, label %.critedge, label %6, !llvm.loop !10

.critedge:                                        ; preds = %10, %6, %.lr.ph19
  %.1.lcssa = phi i32 [ -1, %.lr.ph19 ], [ %.113, %6 ], [ -1, %10 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %13 = 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 %.lr.ph19, !llvm.loop !12
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, 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,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass