; LLVM IR from wuzhouhui/leetcode :: 3_longest_norep_2 (lengthOfLongestSubstring)

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

@lengthOfLongestSubstring.chcnt = external hidden global [256 x i8], align 16

; Function Attrs: nounwind uwtable
define dso_local i32 @lengthOfLongestSubstring(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  store i32 0, ptr %3, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  store i32 0, 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
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  call void @llvm.memset.p0.i64(ptr align 16 @lengthOfLongestSubstring.chcnt, i8 0, i64 256, i1 false)
  br label %7

7:                                                ; preds = %74, %34, %1
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  %9 = load i32, ptr %5, align 4, !tbaa !9
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds i8, ptr %8, i64 %10
  %12 = load i8, ptr %11, align 1, !tbaa !11
  %13 = icmp ne i8 %12, 0
  br i1 %13, label %14, label %77

14:                                               ; preds = %7
  %15 = load ptr, ptr %2, align 8, !tbaa !5
  %16 = load i32, ptr %5, align 4, !tbaa !9
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds i8, ptr %15, i64 %17
  %19 = load i8, ptr %18, align 1, !tbaa !11
  %20 = sext i8 %19 to i64
  %21 = getelementptr inbounds [256 x i8], ptr @lengthOfLongestSubstring.chcnt, i64 0, i64 %20
  %22 = load i8, ptr %21, align 1, !tbaa !11
  %23 = add i8 %22, 1
  store i8 %23, ptr %21, align 1, !tbaa !11
  %24 = load ptr, ptr %2, align 8, !tbaa !5
  %25 = load i32, ptr %5, align 4, !tbaa !9
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds i8, ptr %24, i64 %26
  %28 = load i8, ptr %27, align 1, !tbaa !11
  %29 = sext i8 %28 to i64
  %30 = getelementptr inbounds [256 x i8], ptr @lengthOfLongestSubstring.chcnt, i64 0, i64 %29
  %31 = load i8, ptr %30, align 1, !tbaa !11
  %32 = sext i8 %31 to i32
  %33 = icmp slt i32 %32, 2
  br i1 %33, label %34, label %37

34:                                               ; preds = %14
  %35 = load i32, ptr %5, align 4, !tbaa !9
  %36 = add nsw i32 %35, 1
  store i32 %36, ptr %5, align 4, !tbaa !9
  br label %7, !llvm.loop !12

37:                                               ; preds = %14
  %38 = load i32, ptr %5, align 4, !tbaa !9
  %39 = load i32, ptr %4, align 4, !tbaa !9
  %40 = sub nsw i32 %38, %39
  store i32 %40, ptr %6, align 4, !tbaa !9
  %41 = load i32, ptr %6, align 4, !tbaa !9
  %42 = load i32, ptr %3, align 4, !tbaa !9
  %43 = icmp sgt i32 %41, %42
  br i1 %43, label %44, label %46

44:                                               ; preds = %37
  %45 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %45, ptr %3, align 4, !tbaa !9
  br label %46

46:                                               ; preds = %44, %37
  br label %47

47:                                               ; preds = %68, %46
  br label %48

48:                                               ; preds = %47
  %49 = load ptr, ptr %2, align 8, !tbaa !5
  %50 = load i32, ptr %4, align 4, !tbaa !9
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds i8, ptr %49, i64 %51
  %53 = load i8, ptr %52, align 1, !tbaa !11
  %54 = sext i8 %53 to i64
  %55 = getelementptr inbounds [256 x i8], ptr @lengthOfLongestSubstring.chcnt, i64 0, i64 %54
  %56 = load i8, ptr %55, align 1, !tbaa !11
  %57 = add i8 %56, -1
  store i8 %57, ptr %55, align 1, !tbaa !11
  %58 = load ptr, ptr %2, align 8, !tbaa !5
  %59 = load i32, ptr %4, align 4, !tbaa !9
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds i8, ptr %58, i64 %60
  %62 = load i8, ptr %61, align 1, !tbaa !11
  %63 = sext i8 %62 to i64
  %64 = getelementptr inbounds [256 x i8], ptr @lengthOfLongestSubstring.chcnt, i64 0, i64 %63
  %65 = load i8, ptr %64, align 1, !tbaa !11
  %66 = sext i8 %65 to i32
  %67 = icmp eq i32 %66, 0
  br i1 %67, label %68, label %71

68:                                               ; preds = %48
  %69 = load i32, ptr %4, align 4, !tbaa !9
  %70 = add nsw i32 %69, 1
  store i32 %70, ptr %4, align 4, !tbaa !9
  br label %47

71:                                               ; preds = %48
  %72 = load i32, ptr %4, align 4, !tbaa !9
  %73 = add nsw i32 %72, 1
  store i32 %73, ptr %4, align 4, !tbaa !9
  br label %74

74:                                               ; preds = %71
  %75 = load i32, ptr %5, align 4, !tbaa !9
  %76 = add nsw i32 %75, 1
  store i32 %76, ptr %5, align 4, !tbaa !9
  br label %7, !llvm.loop !12

77:                                               ; preds = %7
  %78 = load i32, ptr %5, align 4, !tbaa !9
  %79 = load i32, ptr %4, align 4, !tbaa !9
  %80 = sub nsw i32 %78, %79
  store i32 %80, ptr %6, align 4, !tbaa !9
  %81 = load i32, ptr %6, align 4, !tbaa !9
  %82 = load i32, ptr %3, align 4, !tbaa !9
  %83 = icmp sgt i32 %81, %82
  br i1 %83, label %84, label %86

84:                                               ; preds = %77
  %85 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %85, ptr %3, align 4, !tbaa !9
  br label %86

86:                                               ; preds = %84, %77
  %87 = load i32, ptr %3, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #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 4, ptr %3) #3
  ret i32 %87
}

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


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

@lengthOfLongestSubstring.chcnt = external hidden local_unnamed_addr global [256 x i8], align 16

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, argmem: read, inaccessiblemem: none) uwtable
define dso_local range(i32 0, -2147483648) i32 @lengthOfLongestSubstring(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(256) @lengthOfLongestSubstring.chcnt, i8 0, i64 256, i1 false)
  %2 = load i8, ptr %0, align 1, !tbaa !5
  %.not3239 = icmp eq i8 %2, 0
  br i1 %.not3239, label %.outer._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %1, %.outer
  %3 = phi i8 [ %42, %.outer ], [ %2, %1 ]
  %4 = phi ptr [ %41, %.outer ], [ %0, %1 ]
  %.0.ph42 = phi i32 [ %39, %.outer ], [ 0, %1 ]
  %.024.ph41 = phi i32 [ %38, %.outer ], [ 0, %1 ]
  %.025.ph40 = phi i32 [ %spec.select, %.outer ], [ 0, %1 ]
  %5 = sext i32 %.0.ph42 to i64
  br label %6

6:                                                ; preds = %.lr.ph, %18
  %indvars.iv = phi i64 [ %5, %.lr.ph ], [ %indvars.iv.next, %18 ]
  %7 = phi i8 [ %3, %.lr.ph ], [ %20, %18 ]
  %8 = phi ptr [ %4, %.lr.ph ], [ %19, %18 ]
  %9 = sext i8 %7 to i64
  %10 = getelementptr inbounds [256 x i8], ptr @lengthOfLongestSubstring.chcnt, i64 0, i64 %9
  %11 = load i8, ptr %10, align 1, !tbaa !5
  %12 = add i8 %11, 1
  store i8 %12, ptr %10, align 1, !tbaa !5
  %13 = load i8, ptr %8, align 1, !tbaa !5
  %14 = sext i8 %13 to i64
  %15 = getelementptr inbounds [256 x i8], ptr @lengthOfLongestSubstring.chcnt, i64 0, i64 %14
  %16 = load i8, ptr %15, align 1, !tbaa !5
  %17 = icmp slt i8 %16, 2
  br i1 %17, label %18, label %21

18:                                               ; preds = %6
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %19 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv.next
  %20 = load i8, ptr %19, align 1, !tbaa !5
  %.not = icmp eq i8 %20, 0
  br i1 %.not, label %.outer._crit_edge.loopexit, label %6, !llvm.loop !8

21:                                               ; preds = %6
  %22 = trunc nsw i64 %indvars.iv to i32
  %23 = sub nsw i32 %22, %.024.ph41
  %24 = sext i32 %.024.ph41 to i64
  br label %25

25:                                               ; preds = %25, %21
  %indvars.iv52 = phi i64 [ %indvars.iv.next53, %25 ], [ %24, %21 ]
  %26 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv52
  %27 = load i8, ptr %26, align 1, !tbaa !5
  %28 = sext i8 %27 to i64
  %29 = getelementptr inbounds [256 x i8], ptr @lengthOfLongestSubstring.chcnt, i64 0, i64 %28
  %30 = load i8, ptr %29, align 1, !tbaa !5
  %31 = add i8 %30, -1
  store i8 %31, ptr %29, align 1, !tbaa !5
  %32 = load i8, ptr %26, align 1, !tbaa !5
  %33 = sext i8 %32 to i64
  %34 = getelementptr inbounds [256 x i8], ptr @lengthOfLongestSubstring.chcnt, i64 0, i64 %33
  %35 = load i8, ptr %34, align 1, !tbaa !5
  %36 = icmp eq i8 %35, 0
  %indvars.iv.next53 = add nsw i64 %indvars.iv52, 1
  br i1 %36, label %25, label %.outer

.outer:                                           ; preds = %25
  %spec.select = tail call i32 @llvm.smax.i32(i32 %23, i32 %.025.ph40)
  %37 = trunc nsw i64 %indvars.iv52 to i32
  %38 = add nsw i32 %37, 1
  %39 = add nsw i32 %22, 1
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds i8, ptr %0, i64 %40
  %42 = load i8, ptr %41, align 1, !tbaa !5
  %.not32 = icmp eq i8 %42, 0
  br i1 %.not32, label %.outer._crit_edge, label %.lr.ph, !llvm.loop !8

.outer._crit_edge.loopexit:                       ; preds = %18
  %43 = trunc nsw i64 %indvars.iv.next to i32
  br label %.outer._crit_edge

.outer._crit_edge:                                ; preds = %.outer, %.outer._crit_edge.loopexit, %1
  %.025.ph.lcssa = phi i32 [ 0, %1 ], [ %.025.ph40, %.outer._crit_edge.loopexit ], [ %spec.select, %.outer ]
  %.024.ph.lcssa = phi i32 [ 0, %1 ], [ %.024.ph41, %.outer._crit_edge.loopexit ], [ %38, %.outer ]
  %.0.lcssa = phi i32 [ 0, %1 ], [ %43, %.outer._crit_edge.loopexit ], [ %39, %.outer ]
  %44 = sub nsw i32 %.0.lcssa, %.024.ph.lcssa
  %spec.select30 = tail call i32 @llvm.smax.i32(i32 %44, i32 %.025.ph.lcssa)
  ret i32 %spec.select30
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #2

attributes #0 = { nofree norecurse nosync nounwind memory(readwrite, argmem: 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 nounwind willreturn memory(argmem: write) }
attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}


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