; LLVM IR from HarshCasper/NeoAlgo :: C-Plus-Plus/cp/generate_valid_parenthesis.ll (Z20FindValidParenthesisPciiii)

; 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::basic_ostream" = type { ptr, %"class.std::basic_ios" }
%"class.std::basic_ios" = type { %"class.std::ios_base", ptr, i8, i8, ptr, ptr, ptr, ptr }
%"class.std::ios_base" = type { ptr, i64, i64, i32, i32, i32, ptr, %"struct.std::ios_base::_Words", [8 x %"struct.std::ios_base::_Words"], i32, ptr, %"class.std::locale" }
%"struct.std::ios_base::_Words" = type { ptr, i64 }
%"class.std::locale" = type { ptr }

@_ZSt4cout = external global %"class.std::basic_ostream", align 8

; Function Attrs: mustprogress uwtable
define dso_local void @_Z20FindValidParenthesisPciiii(ptr noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) #0 {
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !9
  store i32 %2, ptr %8, align 4, !tbaa !9
  store i32 %3, ptr %9, align 4, !tbaa !9
  store i32 %4, ptr %10, align 4, !tbaa !9
  %11 = load i32, ptr %8, align 4, !tbaa !9
  %12 = load i32, ptr %7, align 4, !tbaa !9
  %13 = mul nsw i32 2, %12
  %14 = icmp eq i32 %11, %13
  br i1 %14, label %15, label %23

15:                                               ; preds = %5
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  %17 = load i32, ptr %8, align 4, !tbaa !9
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds i8, ptr %16, i64 %18
  store i8 0, ptr %19, align 1, !tbaa !11
  %20 = load ptr, ptr %6, align 8, !tbaa !5
  %21 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, ptr noundef %20)
  %22 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEPFRSoS_E(ptr noundef nonnull align 8 dereferenceable(8) %21, ptr noundef @_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)
  br label %56

23:                                               ; preds = %5
  %24 = load i32, ptr %9, align 4, !tbaa !9
  %25 = load i32, ptr %7, align 4, !tbaa !9
  %26 = icmp slt i32 %24, %25
  br i1 %26, label %27, label %39

27:                                               ; preds = %23
  %28 = load ptr, ptr %6, align 8, !tbaa !5
  %29 = load i32, ptr %8, align 4, !tbaa !9
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds i8, ptr %28, i64 %30
  store i8 40, ptr %31, align 1, !tbaa !11
  %32 = load ptr, ptr %6, align 8, !tbaa !5
  %33 = load i32, ptr %7, align 4, !tbaa !9
  %34 = load i32, ptr %8, align 4, !tbaa !9
  %35 = add nsw i32 %34, 1
  %36 = load i32, ptr %9, align 4, !tbaa !9
  %37 = add nsw i32 %36, 1
  %38 = load i32, ptr %10, align 4, !tbaa !9
  call void @_Z20FindValidParenthesisPciiii(ptr noundef %32, i32 noundef %33, i32 noundef %35, i32 noundef %37, i32 noundef %38)
  br label %39

39:                                               ; preds = %27, %23
  %40 = load i32, ptr %10, align 4, !tbaa !9
  %41 = load i32, ptr %9, align 4, !tbaa !9
  %42 = icmp slt i32 %40, %41
  br i1 %42, label %43, label %55

43:                                               ; preds = %39
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  %45 = load i32, ptr %8, align 4, !tbaa !9
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds i8, ptr %44, i64 %46
  store i8 41, ptr %47, align 1, !tbaa !11
  %48 = load ptr, ptr %6, align 8, !tbaa !5
  %49 = load i32, ptr %7, align 4, !tbaa !9
  %50 = load i32, ptr %8, align 4, !tbaa !9
  %51 = add nsw i32 %50, 1
  %52 = load i32, ptr %9, align 4, !tbaa !9
  %53 = load i32, ptr %10, align 4, !tbaa !9
  %54 = add nsw i32 %53, 1
  call void @_Z20FindValidParenthesisPciiii(ptr noundef %48, i32 noundef %49, i32 noundef %51, i32 noundef %52, i32 noundef %54)
  br label %55

55:                                               ; preds = %43, %39
  br label %56

56:                                               ; preds = %55, %15
  ret void
}

; Function Attrs: inlinehint mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef) #1

; Function Attrs: mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEPFRSoS_E(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef) #0 align 2

; Function Attrs: inlinehint mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_(ptr noundef nonnull align 8 dereferenceable(8)) #1

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 = { inlinehint 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" }

!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 = !{!"int", !7, i64 0}
!11 = !{!7, !7, i64 0}


; 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::basic_ostream" = type { ptr, %"class.std::basic_ios" }
%"class.std::basic_ios" = type { %"class.std::ios_base", ptr, i8, i8, ptr, ptr, ptr, ptr }
%"class.std::ios_base" = type { ptr, i64, i64, i32, i32, i32, ptr, %"struct.std::ios_base::_Words", [8 x %"struct.std::ios_base::_Words"], i32, ptr, %"class.std::locale" }
%"struct.std::ios_base::_Words" = type { ptr, i64 }
%"class.std::locale" = type { ptr }

@_ZSt4cout = external global %"class.std::basic_ostream", align 8

; Function Attrs: mustprogress uwtable
define dso_local void @_Z20FindValidParenthesisPciiii(ptr noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) local_unnamed_addr #0 {
  %6 = shl nsw i32 %1, 1
  %7 = icmp eq i32 %6, %2
  br i1 %7, label %tailrecurse._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %5
  %8 = icmp slt i32 %3, %1
  %9 = add nsw i32 %3, 1
  %10 = sext i32 %2 to i64
  %smax40 = tail call i32 @llvm.smax.i32(i32 %4, i32 %3)
  br i1 %8, label %.lr.ph.split.us, label %.lr.ph.split.preheader

.lr.ph.split.preheader:                           ; preds = %.lr.ph
  %11 = xor i32 %2, -1
  %12 = add i32 %6, %11
  %13 = sub i32 %smax40, %4
  %14 = tail call i32 @llvm.umin.i32(i32 %12, i32 %13)
  %min.iters.check = icmp ult i32 %14, 32
  br i1 %min.iters.check, label %.lr.ph.split.preheader48, label %vector.ph

.lr.ph.split.preheader48:                         ; preds = %vector.body, %.lr.ph.split.preheader
  %indvars.iv.ph = phi i64 [ %10, %.lr.ph.split.preheader ], [ %ind.end, %vector.body ]
  %.tr2932.ph = phi i32 [ %4, %.lr.ph.split.preheader ], [ %ind.end46, %vector.body ]
  br label %.lr.ph.split

vector.ph:                                        ; preds = %.lr.ph.split.preheader
  %umin = zext i32 %14 to i64
  %15 = add nuw nsw i64 %umin, 1
  %n.mod.vf = and i64 %15, 31
  %16 = icmp eq i64 %n.mod.vf, 0
  %17 = select i1 %16, i64 32, i64 %n.mod.vf
  %n.vec = sub nsw i64 %15, %17
  %ind.end = add nsw i64 %n.vec, %10
  %.cast = trunc i64 %n.vec to i32
  %ind.end46 = add i32 %.cast, %4
  %invariant.gep = getelementptr i8, ptr %0, i64 %10
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %gep = getelementptr i8, ptr %invariant.gep, i64 %index
  %18 = getelementptr inbounds i8, ptr %gep, i64 16
  store <16 x i8> <i8 41, i8 41, i8 41, i8 41, i8 41, i8 41, i8 41, i8 41, i8 41, i8 41, i8 41, i8 41, i8 41, i8 41, i8 41, i8 41>, ptr %gep, align 1, !tbaa !5
  store <16 x i8> <i8 41, i8 41, i8 41, i8 41, i8 41, i8 41, i8 41, i8 41, i8 41, i8 41, i8 41, i8 41, i8 41, i8 41, i8 41, i8 41>, ptr %18, align 1, !tbaa !5
  %index.next = add nuw i64 %index, 32
  %19 = icmp eq i64 %index.next, %n.vec
  br i1 %19, label %.lr.ph.split.preheader48, label %vector.body, !llvm.loop !8

.lr.ph.split.us:                                  ; preds = %.lr.ph, %tailrecurse.us
  %indvars.iv37 = phi i64 [ %indvars.iv.next38, %tailrecurse.us ], [ %10, %.lr.ph ]
  %.tr2932.us = phi i32 [ %22, %tailrecurse.us ], [ %4, %.lr.ph ]
  %20 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv37
  store i8 40, ptr %20, align 1, !tbaa !5
  %indvars.iv.next38 = add nsw i64 %indvars.iv37, 1
  %21 = trunc nsw i64 %indvars.iv.next38 to i32
  tail call void @_Z20FindValidParenthesisPciiii(ptr noundef %0, i32 noundef %1, i32 noundef %21, i32 noundef %9, i32 noundef %.tr2932.us)
  %exitcond41.not = icmp eq i32 %.tr2932.us, %smax40
  br i1 %exitcond41.not, label %.loopexit, label %tailrecurse.us

tailrecurse.us:                                   ; preds = %.lr.ph.split.us
  store i8 41, ptr %20, align 1, !tbaa !5
  %22 = add i32 %.tr2932.us, 1
  %23 = icmp eq i32 %6, %21
  br i1 %23, label %tailrecurse._crit_edge, label %.lr.ph.split.us

tailrecurse._crit_edge:                           ; preds = %tailrecurse, %tailrecurse.us, %5
  %.tr27.lcssa = phi i32 [ %2, %5 ], [ %6, %tailrecurse.us ], [ %6, %tailrecurse ]
  %24 = sext i32 %.tr27.lcssa to i64
  %25 = getelementptr inbounds i8, ptr %0, i64 %24
  store i8 0, ptr %25, align 1, !tbaa !5
  %26 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, ptr noundef %0)
  %27 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEPFRSoS_E(ptr noundef nonnull align 8 dereferenceable(8) %26, ptr noundef nonnull @_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)
  br label %.loopexit

.lr.ph.split:                                     ; preds = %.lr.ph.split.preheader48, %tailrecurse
  %indvars.iv = phi i64 [ %indvars.iv.next, %tailrecurse ], [ %indvars.iv.ph, %.lr.ph.split.preheader48 ]
  %.tr2932 = phi i32 [ %29, %tailrecurse ], [ %.tr2932.ph, %.lr.ph.split.preheader48 ]
  %exitcond.not = icmp eq i32 %.tr2932, %smax40
  br i1 %exitcond.not, label %.loopexit, label %tailrecurse

tailrecurse:                                      ; preds = %.lr.ph.split
  %28 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv
  store i8 41, ptr %28, align 1, !tbaa !5
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %29 = add i32 %.tr2932, 1
  %30 = trunc nsw i64 %indvars.iv.next to i32
  %31 = icmp eq i32 %6, %30
  br i1 %31, label %tailrecurse._crit_edge, label %.lr.ph.split, !llvm.loop !11

.loopexit:                                        ; preds = %.lr.ph.split, %.lr.ph.split.us, %tailrecurse._crit_edge
  ret void
}

; Function Attrs: inlinehint mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef) local_unnamed_addr #1

; Function Attrs: mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEPFRSoS_E(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef) local_unnamed_addr #0 align 2

; Function Attrs: inlinehint mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_(ptr noundef nonnull align 8 dereferenceable(8)) #1

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

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

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 = { inlinehint 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 #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++ TBAA"}
!8 = distinct !{!8, !9, !10}
!9 = !{!"llvm.loop.isvectorized", i32 1}
!10 = !{!"llvm.loop.unroll.runtime.disable"}
!11 = distinct !{!11, !10, !9}


; Effective O3 passes: GVNPass,GlobalDCEPass,GlobalOptPass,IndVarSimplifyPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,SROAPass,SimplifyCFGPass,TailCallElimPass