; LLVM IR from ravireddy07/cpp-book :: src/Coding-Blocks (SanjeetAndStrings)

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

@dp = external dso_local global [1005 x [1005 x i64]], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z13SanjeetStringNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca i32, align 4
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.memset.p0.i64(ptr align 16 @dp, i8 0, i64 8080200, i1 false)
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #5
  store i64 0, ptr %3, align 8, !tbaa !9
  br label %8

8:                                                ; preds = %18, %1
  %9 = load i64, ptr %3, align 8, !tbaa !9
  %10 = call noundef i64 @_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(32) %0) #5
  %11 = icmp ult i64 %9, %10
  br i1 %11, label %13, label %12

12:                                               ; preds = %8
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #5
  br label %21

13:                                               ; preds = %8
  %14 = load i64, ptr %3, align 8, !tbaa !9
  %15 = getelementptr inbounds [1005 x [1005 x i64]], ptr @dp, i64 0, i64 %14
  %16 = load i64, ptr %3, align 8, !tbaa !9
  %17 = getelementptr inbounds [1005 x i64], ptr %15, i64 0, i64 %16
  store i64 1, ptr %17, align 8, !tbaa !9
  br label %18

18:                                               ; preds = %13
  %19 = load i64, ptr %3, align 8, !tbaa !9
  %20 = add nsw i64 %19, 1
  store i64 %20, ptr %3, align 8, !tbaa !9
  br label %8, !llvm.loop !11

21:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #5
  store i64 2, ptr %4, align 8, !tbaa !9
  br label %22

22:                                               ; preds = %87, %21
  %23 = load i64, ptr %4, align 8, !tbaa !9
  %24 = call noundef i64 @_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(32) %0) #5
  %25 = icmp ule i64 %23, %24
  br i1 %25, label %27, label %26

26:                                               ; preds = %22
  store i32 5, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #5
  br label %90

27:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  store i64 0, ptr %6, align 8, !tbaa !9
  br label %28

28:                                               ; preds = %83, %27
  %29 = load i64, ptr %6, align 8, !tbaa !9
  %30 = call noundef i64 @_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(32) %0) #5
  %31 = icmp ult i64 %29, %30
  br i1 %31, label %33, label %32

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

33:                                               ; preds = %28
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #5
  %34 = load i64, ptr %6, align 8, !tbaa !9
  %35 = load i64, ptr %4, align 8, !tbaa !9
  %36 = add nsw i64 %34, %35
  %37 = sub nsw i64 %36, 1
  store i64 %37, ptr %7, align 8, !tbaa !9
  %38 = load i64, ptr %6, align 8, !tbaa !9
  %39 = call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) %0, i64 noundef %38)
  %40 = load i8, ptr %39, align 1, !tbaa !13
  %41 = sext i8 %40 to i32
  %42 = load i64, ptr %7, align 8, !tbaa !9
  %43 = call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) %0, i64 noundef %42)
  %44 = load i8, ptr %43, align 1, !tbaa !13
  %45 = sext i8 %44 to i32
  %46 = icmp eq i32 %41, %45
  br i1 %46, label %47, label %65

47:                                               ; preds = %33
  %48 = load i64, ptr %6, align 8, !tbaa !9
  %49 = add nsw i64 %48, 1
  %50 = getelementptr inbounds [1005 x [1005 x i64]], ptr @dp, i64 0, i64 %49
  %51 = load i64, ptr %7, align 8, !tbaa !9
  %52 = sub nsw i64 %51, 1
  %53 = getelementptr inbounds [1005 x i64], ptr %50, i64 0, i64 %52
  %54 = load i64, ptr %53, align 8, !tbaa !9
  %55 = add nsw i64 %54, 2
  %56 = load i64, ptr %6, align 8, !tbaa !9
  %57 = load i64, ptr %7, align 8, !tbaa !9
  %58 = icmp eq i64 %56, %57
  %59 = zext i1 %58 to i64
  %60 = sub nsw i64 %55, %59
  %61 = load i64, ptr %6, align 8, !tbaa !9
  %62 = getelementptr inbounds [1005 x [1005 x i64]], ptr @dp, i64 0, i64 %61
  %63 = load i64, ptr %7, align 8, !tbaa !9
  %64 = getelementptr inbounds [1005 x i64], ptr %62, i64 0, i64 %63
  store i64 %60, ptr %64, align 8, !tbaa !9
  br label %82

65:                                               ; preds = %33
  %66 = load i64, ptr %6, align 8, !tbaa !9
  %67 = add nsw i64 %66, 1
  %68 = getelementptr inbounds [1005 x [1005 x i64]], ptr @dp, i64 0, i64 %67
  %69 = load i64, ptr %7, align 8, !tbaa !9
  %70 = getelementptr inbounds [1005 x i64], ptr %68, i64 0, i64 %69
  %71 = load i64, ptr %6, align 8, !tbaa !9
  %72 = getelementptr inbounds [1005 x [1005 x i64]], ptr @dp, i64 0, i64 %71
  %73 = load i64, ptr %7, align 8, !tbaa !9
  %74 = sub nsw i64 %73, 1
  %75 = getelementptr inbounds [1005 x i64], ptr %72, i64 0, i64 %74
  %76 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %70, ptr noundef nonnull align 8 dereferenceable(8) %75)
  %77 = load i64, ptr %76, align 8, !tbaa !9
  %78 = load i64, ptr %6, align 8, !tbaa !9
  %79 = getelementptr inbounds [1005 x [1005 x i64]], ptr @dp, i64 0, i64 %78
  %80 = load i64, ptr %7, align 8, !tbaa !9
  %81 = getelementptr inbounds [1005 x i64], ptr %79, i64 0, i64 %80
  store i64 %77, ptr %81, align 8, !tbaa !9
  br label %82

82:                                               ; preds = %65, %47
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #5
  br label %83

83:                                               ; preds = %82
  %84 = load i64, ptr %6, align 8, !tbaa !9
  %85 = add nsw i64 %84, 1
  store i64 %85, ptr %6, align 8, !tbaa !9
  br label %28, !llvm.loop !14

86:                                               ; preds = %32
  br label %87

87:                                               ; preds = %86
  %88 = load i64, ptr %4, align 8, !tbaa !9
  %89 = add nsw i64 %88, 1
  store i64 %89, ptr %4, align 8, !tbaa !9
  br label %22, !llvm.loop !15

90:                                               ; preds = %26
  %91 = call noundef i64 @_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(32) %0) #5
  %92 = call noundef i64 @_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(32) %0) #5
  %93 = sub i64 %92, 1
  %94 = getelementptr inbounds [1005 x i64], ptr @dp, i64 0, i64 %93
  %95 = load i64, ptr %94, align 8, !tbaa !9
  %96 = sub i64 %91, %95
  ret i64 %96
}

; Function Attrs: 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 willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #2

; Function Attrs: mustprogress nounwind uwtable
declare noundef i64 @_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(32)) #3 align 2

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

; Function Attrs: mustprogress nounwind uwtable
declare noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32), i64 noundef) #3 align 2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) #4

attributes #0 = { 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 #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 #4 = { inlinehint 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !12}
!15 = distinct !{!15, !12}


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

@dp = external dso_local global [1005 x [1005 x i64]], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z13SanjeetStringNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE(ptr noundef nonnull %0) local_unnamed_addr #0 {
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(8080200) @dp, i8 0, i64 8080200, i1 false)
  %2 = tail call noundef i64 @_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(32) %0) #3
  %.not33 = icmp eq i64 %2, 0
  br i1 %.not33, label %.preheader26, label %.lr.ph

.preheader26:                                     ; preds = %.lr.ph, %1
  %3 = tail call noundef i64 @_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(32) %0) #3
  %.not30 = icmp ult i64 %3, 2
  br i1 %.not30, label %._crit_edge32, label %.preheader

.lr.ph:                                           ; preds = %1, %.lr.ph
  %.027 = phi i64 [ %5, %.lr.ph ], [ 0, %1 ]
  %4 = getelementptr inbounds [1005 x [1005 x i64]], ptr @dp, i64 0, i64 %.027, i64 %.027
  store i64 1, ptr %4, align 8, !tbaa !5
  %5 = add nuw nsw i64 %.027, 1
  %6 = tail call noundef i64 @_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(32) %0) #3
  %7 = icmp ult i64 %5, %6
  br i1 %7, label %.lr.ph, label %.preheader26, !llvm.loop !9

.preheader:                                       ; preds = %.preheader26, %._crit_edge
  %.02531 = phi i64 [ %15, %._crit_edge ], [ 2, %.preheader26 ]
  %8 = tail call noundef i64 @_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(32) %0) #3
  %.not34 = icmp eq i64 %8, 0
  br i1 %.not34, label %._crit_edge, label %.lr.ph29

._crit_edge32:                                    ; preds = %._crit_edge, %.preheader26
  %9 = tail call noundef i64 @_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(32) %0) #3
  %10 = tail call noundef i64 @_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(32) %0) #3
  %11 = add i64 %10, -1
  %12 = getelementptr inbounds [1005 x i64], ptr @dp, i64 0, i64 %11
  %13 = load i64, ptr %12, align 8, !tbaa !5
  %14 = sub i64 %9, %13
  ret i64 %14

._crit_edge:                                      ; preds = %36, %.preheader
  %15 = add nuw nsw i64 %.02531, 1
  %16 = tail call noundef i64 @_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(32) %0) #3
  %.not.not = icmp ult i64 %.02531, %16
  br i1 %.not.not, label %.preheader, label %._crit_edge32, !llvm.loop !11

.lr.ph29:                                         ; preds = %.preheader, %36
  %.02428 = phi i64 [ %24, %36 ], [ 0, %.preheader ]
  %17 = add nuw nsw i64 %.02428, %.02531
  %18 = add nsw i64 %17, -1
  %19 = tail call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) %0, i64 noundef %.02428)
  %20 = load i8, ptr %19, align 1, !tbaa !12
  %21 = tail call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) %0, i64 noundef %18)
  %22 = load i8, ptr %21, align 1, !tbaa !12
  %23 = icmp eq i8 %20, %22
  %24 = add nuw nsw i64 %.02428, 1
  br i1 %23, label %25, label %30

25:                                               ; preds = %.lr.ph29
  %26 = add nsw i64 %17, -2
  %27 = getelementptr inbounds [1005 x [1005 x i64]], ptr @dp, i64 0, i64 %24, i64 %26
  %28 = load i64, ptr %27, align 8, !tbaa !5
  %29 = add i64 %28, 2
  br label %36

30:                                               ; preds = %.lr.ph29
  %31 = getelementptr inbounds [1005 x [1005 x i64]], ptr @dp, i64 0, i64 %24, i64 %18
  %32 = add nsw i64 %17, -2
  %33 = getelementptr inbounds [1005 x [1005 x i64]], ptr @dp, i64 0, i64 %.02428, i64 %32
  %34 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %31, ptr noundef nonnull align 8 dereferenceable(8) %33)
  %35 = load i64, ptr %34, align 8, !tbaa !5
  br label %36

36:                                               ; preds = %30, %25
  %.sink = phi i64 [ %35, %30 ], [ %29, %25 ]
  %37 = getelementptr inbounds [1005 x [1005 x i64]], ptr @dp, i64 0, i64 %.02428, i64 %18
  store i64 %.sink, ptr %37, align 8
  %38 = tail call noundef i64 @_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(32) %0) #3
  %39 = icmp ult i64 %24, %38
  br i1 %39, label %.lr.ph29, label %._crit_edge, !llvm.loop !13
}

; 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: mustprogress nounwind uwtable
declare noundef i64 @_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(32)) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress nounwind uwtable
declare noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32), i64 noundef) local_unnamed_addr #0 align 2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #2

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


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,MergedLoadStoreMotionPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass