; LLVM IR from kenjin/Awesome-LC-C-Cracker :: leetcode-c-sol/0467_unique-substrings-in-wraparound-string (findSubstringInWraproundString)

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

; Function Attrs: nounwind uwtable
define dso_local i32 @findSubstringInWraproundString(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca [26 x i32], align 16
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 104, ptr %3) #3
  call void @llvm.memset.p0.i64(ptr align 16 %3, i8 0, i64 104, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  store i32 -1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  store i32 0, ptr %5, align 4, !tbaa !9
  br label %9

9:                                                ; preds = %44, %1
  %10 = load ptr, ptr %2, align 8, !tbaa !5
  %11 = load i8, ptr %10, align 1, !tbaa !11
  %12 = icmp ne i8 %11, 0
  br i1 %12, label %13, label %52

13:                                               ; preds = %9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %14 = load ptr, ptr %2, align 8, !tbaa !5
  %15 = load i8, ptr %14, align 1, !tbaa !11
  %16 = sext i8 %15 to i32
  %17 = sub nsw i32 %16, 97
  store i32 %17, ptr %6, align 4, !tbaa !9
  %18 = load i32, ptr %4, align 4, !tbaa !9
  %19 = icmp sge i32 %18, 0
  br i1 %19, label %20, label %29

20:                                               ; preds = %13
  %21 = load i32, ptr %4, align 4, !tbaa !9
  %22 = add nsw i32 %21, 1
  %23 = srem i32 %22, 26
  %24 = load i32, ptr %6, align 4, !tbaa !9
  %25 = icmp eq i32 %23, %24
  br i1 %25, label %26, label %29

26:                                               ; preds = %20
  %27 = load i32, ptr %5, align 4, !tbaa !9
  %28 = add nsw i32 %27, 1
  store i32 %28, ptr %5, align 4, !tbaa !9
  br label %30

29:                                               ; preds = %20, %13
  store i32 1, ptr %5, align 4, !tbaa !9
  br label %30

30:                                               ; preds = %29, %26
  %31 = load i32, ptr %6, align 4, !tbaa !9
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [26 x i32], ptr %3, i64 0, i64 %32
  %34 = load i32, ptr %33, align 4, !tbaa !9
  %35 = load i32, ptr %5, align 4, !tbaa !9
  %36 = icmp sgt i32 %34, %35
  br i1 %36, label %37, label %42

37:                                               ; preds = %30
  %38 = load i32, ptr %6, align 4, !tbaa !9
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [26 x i32], ptr %3, i64 0, i64 %39
  %41 = load i32, ptr %40, align 4, !tbaa !9
  br label %44

42:                                               ; preds = %30
  %43 = load i32, ptr %5, align 4, !tbaa !9
  br label %44

44:                                               ; preds = %42, %37
  %45 = phi i32 [ %41, %37 ], [ %43, %42 ]
  %46 = load i32, ptr %6, align 4, !tbaa !9
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds [26 x i32], ptr %3, i64 0, i64 %47
  store i32 %45, ptr %48, align 4, !tbaa !9
  %49 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %49, ptr %4, align 4, !tbaa !9
  %50 = load ptr, ptr %2, align 8, !tbaa !5
  %51 = getelementptr inbounds i8, ptr %50, i32 1
  store ptr %51, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  br label %9, !llvm.loop !12

52:                                               ; preds = %9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 0, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 0, ptr %8, align 4, !tbaa !9
  br label %53

53:                                               ; preds = %64, %52
  %54 = load i32, ptr %8, align 4, !tbaa !9
  %55 = icmp slt i32 %54, 26
  br i1 %55, label %57, label %56

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

57:                                               ; preds = %53
  %58 = load i32, ptr %8, align 4, !tbaa !9
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds [26 x i32], ptr %3, i64 0, i64 %59
  %61 = load i32, ptr %60, align 4, !tbaa !9
  %62 = load i32, ptr %7, align 4, !tbaa !9
  %63 = add nsw i32 %62, %61
  store i32 %63, ptr %7, align 4, !tbaa !9
  br label %64

64:                                               ; preds = %57
  %65 = load i32, ptr %8, align 4, !tbaa !9
  %66 = add nsw i32 %65, 1
  store i32 %66, ptr %8, align 4, !tbaa !9
  br label %53, !llvm.loop !14

67:                                               ; preds = %56
  %68 = load i32, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 104, ptr %3) #3
  ret i32 %68
}

; 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 nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}


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

; Function Attrs: nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) uwtable
define dso_local i32 @findSubstringInWraproundString(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
  %2 = alloca [26 x i32], align 16
  call void @llvm.lifetime.start.p0(i64 104, ptr nonnull %2) #4
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(104) %2, i8 0, i64 104, i1 false)
  %3 = load i8, ptr %0, align 1, !tbaa !5
  %.not23 = icmp eq i8 %3, 0
  br i1 %.not23, label %.preheader, label %.lr.ph

.preheader.loopexit:                              ; preds = %17
  %4 = load <16 x i32>, ptr %2, align 16, !tbaa !8
  %.phi.trans.insert60 = getelementptr inbounds i8, ptr %2, i64 64
  %5 = load <8 x i32>, ptr %.phi.trans.insert60, align 16, !tbaa !8
  %.phi.trans.insert76 = getelementptr inbounds i8, ptr %2, i64 96
  %.pre77 = load i32, ptr %.phi.trans.insert76, align 16, !tbaa !8
  %.phi.trans.insert78 = getelementptr inbounds i8, ptr %2, i64 100
  %.pre79 = load i32, ptr %.phi.trans.insert78, align 4, !tbaa !8
  %6 = tail call i32 @llvm.vector.reduce.add.v16i32(<16 x i32> %4)
  %7 = tail call i32 @llvm.vector.reduce.add.v8i32(<8 x i32> %5)
  %op.rdx = add i32 %6, %7
  %op.rdx80 = add i32 %op.rdx, %.pre77
  %op.rdx81 = add i32 %op.rdx80, %.pre79
  br label %.preheader

.preheader:                                       ; preds = %.preheader.loopexit, %1
  %8 = phi i32 [ %op.rdx81, %.preheader.loopexit ], [ 0, %1 ]
  call void @llvm.lifetime.end.p0(i64 104, ptr nonnull %2) #4
  ret i32 %8

.lr.ph:                                           ; preds = %1, %17
  %9 = phi i8 [ %22, %17 ], [ %3, %1 ]
  %.020.fr26 = phi i32 [ %11, %17 ], [ -1, %1 ]
  %.01825 = phi ptr [ %21, %17 ], [ %0, %1 ]
  %.01924 = phi i32 [ %.1, %17 ], [ 0, %1 ]
  %.fr29 = freeze i8 %9
  %10 = sext i8 %.fr29 to i32
  %11 = add nsw i32 %10, -97
  %12 = icmp sgt i32 %.020.fr26, -1
  br i1 %12, label %13, label %17

13:                                               ; preds = %.lr.ph
  %.cmp = icmp ult i32 %.020.fr26, 25
  %.v = select i1 %.cmp, i32 1, i32 -25
  %14 = add nsw i32 %.020.fr26, %.v
  %15 = icmp eq i32 %14, %11
  %16 = add nsw i32 %.01924, 1
  %spec.select = select i1 %15, i32 %16, i32 1
  br label %17

17:                                               ; preds = %13, %.lr.ph
  %.1 = phi i32 [ 1, %.lr.ph ], [ %spec.select, %13 ]
  %18 = sext i32 %11 to i64
  %19 = getelementptr inbounds [26 x i32], ptr %2, i64 0, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !8
  %..1 = tail call i32 @llvm.smax.i32(i32 %20, i32 %.1)
  store i32 %..1, ptr %19, align 4, !tbaa !8
  %21 = getelementptr inbounds i8, ptr %.01825, i64 1
  %22 = load i8, ptr %21, align 1, !tbaa !5
  %.not = icmp eq i8 %22, 0
  br i1 %.not, label %.preheader.loopexit, label %.lr.ph, !llvm.loop !10
}

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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #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: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #3

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.add.v16i32(<16 x i32>) #3

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.add.v8i32(<8 x i32>) #3

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


; Effective O3 passes: BDCEPass,CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SCCPPass,SLPVectorizerPass,SROAPass,SimplifyCFGPass,TailCallElimPass