; LLVM IR from eyangch/competitive-programming :: Codeforces/1526/d.ll/Z4testNSt7 (cxx1112basic_stringIcSt11char_traitsIcESaIcEEE)

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

%"class.std::__cxx11::basic_string" = type { %"struct.std::__cxx11::basic_string<char>::_Alloc_hider", i64, %union.anon }
%"struct.std::__cxx11::basic_string<char>::_Alloc_hider" = type { ptr }
%union.anon = type { i64, [8 x i8] }

@N = external dso_local global i64, align 8
@_Z1sB5cxx11 = external dso_local global %"class.std::__cxx11::basic_string", align 8
@bit = external dso_local global ptr, align 8

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_Z3cvtc(i8 noundef signext) #1

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z4testNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE(ptr noundef %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca i64, align 8
  %4 = alloca [4 x i64], align 16
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  %9 = load ptr, ptr @bit, align 8, !tbaa !5
  %10 = load i64, ptr @N, align 8, !tbaa !9
  call void @_ZN3BIT4initEx(ptr noundef nonnull align 8 dereferenceable(2400008) %9, i64 noundef %10)
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  store i64 0, ptr %3, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 32, ptr %4) #4
  call void @llvm.memset.p0.i64(ptr align 16 %4, i8 0, i64 32, i1 false)
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  store i64 0, ptr %5, align 8, !tbaa !9
  br label %11

11:                                               ; preds = %68, %1
  %12 = load i64, ptr %5, align 8, !tbaa !9
  %13 = load i64, ptr @N, align 8, !tbaa !9
  %14 = icmp slt i64 %12, %13
  br i1 %14, label %16, label %15

15:                                               ; preds = %11
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  br label %71

16:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %17 = load i64, ptr %5, align 8, !tbaa !9
  %18 = call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) @_Z1sB5cxx11, i64 noundef %17)
  %19 = load i8, ptr %18, align 1, !tbaa !11
  %20 = call noundef i64 @_Z3cvtc(i8 noundef signext %19)
  store i64 %20, ptr %6, align 8, !tbaa !9
  br label %21

21:                                               ; preds = %41, %16
  %22 = load i64, ptr %6, align 8, !tbaa !9
  %23 = getelementptr inbounds [4 x i64], ptr %4, i64 0, i64 %22
  %24 = load i64, ptr %23, align 8, !tbaa !9
  %25 = load i64, ptr @N, align 8, !tbaa !9
  %26 = icmp slt i64 %24, %25
  br i1 %26, label %27, label %39

27:                                               ; preds = %21
  %28 = load i64, ptr %6, align 8, !tbaa !9
  %29 = getelementptr inbounds [4 x i64], ptr %4, i64 0, i64 %28
  %30 = load i64, ptr %29, align 8, !tbaa !9
  %31 = call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) %0, i64 noundef %30)
  %32 = load i8, ptr %31, align 1, !tbaa !11
  %33 = sext i8 %32 to i32
  %34 = load i64, ptr %5, align 8, !tbaa !9
  %35 = call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) @_Z1sB5cxx11, i64 noundef %34)
  %36 = load i8, ptr %35, align 1, !tbaa !11
  %37 = sext i8 %36 to i32
  %38 = icmp ne i32 %33, %37
  br label %39

39:                                               ; preds = %27, %21
  %40 = phi i1 [ false, %21 ], [ %38, %27 ]
  br i1 %40, label %41, label %46

41:                                               ; preds = %39
  %42 = load i64, ptr %6, align 8, !tbaa !9
  %43 = getelementptr inbounds [4 x i64], ptr %4, i64 0, i64 %42
  %44 = load i64, ptr %43, align 8, !tbaa !9
  %45 = add nsw i64 %44, 1
  store i64 %45, ptr %43, align 8, !tbaa !9
  br label %21, !llvm.loop !12

46:                                               ; preds = %39
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %47 = load i64, ptr %6, align 8, !tbaa !9
  %48 = getelementptr inbounds [4 x i64], ptr %4, i64 0, i64 %47
  %49 = load i64, ptr %48, align 8, !tbaa !9
  %50 = add nsw i64 %49, 1
  store i64 %50, ptr %48, align 8, !tbaa !9
  store i64 %49, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %51 = load i64, ptr %7, align 8, !tbaa !9
  %52 = load ptr, ptr @bit, align 8, !tbaa !5
  %53 = load i64, ptr @N, align 8, !tbaa !9
  %54 = sub nsw i64 %53, 1
  %55 = call noundef i64 @_ZN3BIT3qryEx(ptr noundef nonnull align 8 dereferenceable(2400008) %52, i64 noundef %54)
  %56 = add nsw i64 %51, %55
  %57 = load ptr, ptr @bit, align 8, !tbaa !5
  %58 = load i64, ptr %7, align 8, !tbaa !9
  %59 = call noundef i64 @_ZN3BIT3qryEx(ptr noundef nonnull align 8 dereferenceable(2400008) %57, i64 noundef %58)
  %60 = sub nsw i64 %56, %59
  store i64 %60, ptr %8, align 8, !tbaa !9
  %61 = load i64, ptr %8, align 8, !tbaa !9
  %62 = load i64, ptr %5, align 8, !tbaa !9
  %63 = sub nsw i64 %61, %62
  %64 = load i64, ptr %3, align 8, !tbaa !9
  %65 = add nsw i64 %64, %63
  store i64 %65, ptr %3, align 8, !tbaa !9
  %66 = load ptr, ptr @bit, align 8, !tbaa !5
  %67 = load i64, ptr %7, align 8, !tbaa !9
  call void @_ZN3BIT3updExx(ptr noundef nonnull align 8 dereferenceable(2400008) %66, i64 noundef %67, i64 noundef 1)
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  br label %68

68:                                               ; preds = %46
  %69 = load i64, ptr %5, align 8, !tbaa !9
  %70 = add nsw i64 %69, 1
  store i64 %70, ptr %5, align 8, !tbaa !9
  br label %11, !llvm.loop !14

71:                                               ; preds = %15
  %72 = load i64, ptr %3, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 32, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  ret i64 %72
}

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN3BIT4initEx(ptr noundef nonnull align 8 dereferenceable(2400008), i64 noundef) #2 align 2

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

; 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) #1 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZN3BIT3qryEx(ptr noundef nonnull align 8 dereferenceable(2400008), i64 noundef) #1 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN3BIT3updExx(ptr noundef nonnull align 8 dereferenceable(2400008), i64 noundef, i64 noundef) #1 align 2

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

attributes #0 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = { 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 #3 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"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 = !{!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"

%"class.std::__cxx11::basic_string" = type { %"struct.std::__cxx11::basic_string<char>::_Alloc_hider", i64, %union.anon }
%"struct.std::__cxx11::basic_string<char>::_Alloc_hider" = type { ptr }
%union.anon = type { i64, [8 x i8] }

@N = external dso_local local_unnamed_addr global i64, align 8
@_Z1sB5cxx11 = external dso_local global %"class.std::__cxx11::basic_string", align 8
@bit = external dso_local local_unnamed_addr global ptr, align 8

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_Z3cvtc(i8 noundef signext) local_unnamed_addr #1

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z4testNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE(ptr noundef %0) local_unnamed_addr #2 {
  %2 = alloca [4 x i64], align 16
  %3 = load ptr, ptr @bit, align 8, !tbaa !5
  %4 = load i64, ptr @N, align 8, !tbaa !9
  tail call void @_ZN3BIT4initEx(ptr noundef nonnull align 8 dereferenceable(2400008) %3, i64 noundef %4)
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %2) #4
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(32) %2, i8 0, i64 32, i1 false)
  %5 = load i64, ptr @N, align 8, !tbaa !9
  %6 = icmp sgt i64 %5, 0
  br i1 %6, label %.lr.ph28, label %._crit_edge

._crit_edge:                                      ; preds = %.critedge, %1
  %.0.lcssa = phi i64 [ 0, %1 ], [ %30, %.critedge ]
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %2) #4
  ret i64 %.0.lcssa

.lr.ph28:                                         ; preds = %1, %.critedge
  %.027 = phi i64 [ %30, %.critedge ], [ 0, %1 ]
  %.01526 = phi i64 [ %32, %.critedge ], [ 0, %1 ]
  %7 = tail call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) @_Z1sB5cxx11, i64 noundef %.01526)
  %8 = load i8, ptr %7, align 1, !tbaa !11
  %9 = tail call noundef i64 @_Z3cvtc(i8 noundef signext %8)
  %10 = getelementptr inbounds [4 x i64], ptr %2, i64 0, i64 %9
  %.promoted = load i64, ptr %10, align 8, !tbaa !9
  %11 = load i64, ptr @N, align 8, !tbaa !9
  %12 = icmp slt i64 %.promoted, %11
  br i1 %12, label %.lr.ph, label %.critedge

.lr.ph:                                           ; preds = %.lr.ph28, %18
  %13 = phi i64 [ %19, %18 ], [ %.promoted, %.lr.ph28 ]
  %14 = tail call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) %0, i64 noundef %13)
  %15 = load i8, ptr %14, align 1, !tbaa !11
  %16 = tail call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) @_Z1sB5cxx11, i64 noundef %.01526)
  %17 = load i8, ptr %16, align 1, !tbaa !11
  %.not = icmp eq i8 %15, %17
  %.pre.pre = load i64, ptr @N, align 8, !tbaa !9
  br i1 %.not, label %.critedge, label %18

18:                                               ; preds = %.lr.ph
  %19 = add nsw i64 %13, 1
  %20 = icmp slt i64 %19, %.pre.pre
  br i1 %20, label %.lr.ph, label %.critedge, !llvm.loop !12

.critedge:                                        ; preds = %.lr.ph, %18, %.lr.ph28
  %21 = phi i64 [ %11, %.lr.ph28 ], [ %.pre.pre, %18 ], [ %.pre.pre, %.lr.ph ]
  %.lcssa19 = phi i64 [ %.promoted, %.lr.ph28 ], [ %13, %.lr.ph ], [ %19, %18 ]
  %22 = add nsw i64 %.lcssa19, 1
  store i64 %22, ptr %10, align 8, !tbaa !9
  %23 = load ptr, ptr @bit, align 8, !tbaa !5
  %24 = add nsw i64 %21, -1
  %25 = tail call noundef i64 @_ZN3BIT3qryEx(ptr noundef nonnull align 8 dereferenceable(2400008) %23, i64 noundef %24)
  %26 = load ptr, ptr @bit, align 8, !tbaa !5
  %27 = tail call noundef i64 @_ZN3BIT3qryEx(ptr noundef nonnull align 8 dereferenceable(2400008) %26, i64 noundef %.lcssa19)
  %.neg17 = sub i64 %.027, %.01526
  %28 = add i64 %.neg17, %.lcssa19
  %29 = add i64 %28, %25
  %30 = sub i64 %29, %27
  %31 = load ptr, ptr @bit, align 8, !tbaa !5
  tail call void @_ZN3BIT3updExx(ptr noundef nonnull align 8 dereferenceable(2400008) %31, i64 noundef %.lcssa19, i64 noundef 1)
  %32 = add nuw nsw i64 %.01526, 1
  %33 = load i64, ptr @N, align 8, !tbaa !9
  %34 = icmp slt i64 %32, %33
  br i1 %34, label %.lr.ph28, label %._crit_edge, !llvm.loop !14
}

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN3BIT4initEx(ptr noundef nonnull align 8 dereferenceable(2400008), i64 noundef) local_unnamed_addr #2 align 2

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

; 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 #1 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZN3BIT3qryEx(ptr noundef nonnull align 8 dereferenceable(2400008), i64 noundef) local_unnamed_addr #1 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN3BIT3updExx(ptr noundef nonnull align 8 dereferenceable(2400008), i64 noundef, i64 noundef) local_unnamed_addr #1 align 2

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

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
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 = { 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 #3 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"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 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}


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