; LLVM IR from smv1999/CompetitiveProgrammingQuestionBank :: Recursion/All_indices_of_element.ll (Z10allIndexesPiiiS_)

; 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: mustprogress uwtable
define dso_local noundef i32 @_Z10allIndexesPiiiS_(ptr noundef %0, i32 noundef %1, i32 noundef %2, ptr noundef %3) #0 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = 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 ptr %3, ptr %9, align 8, !tbaa !5
  %13 = load i32, ptr %7, align 4, !tbaa !9
  %14 = icmp eq i32 %13, 0
  br i1 %14, label %15, label %16

15:                                               ; preds = %4
  store i32 0, ptr %5, align 4
  br label %80

16:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  %18 = getelementptr inbounds i32, ptr %17, i64 1
  %19 = load i32, ptr %7, align 4, !tbaa !9
  %20 = sub nsw i32 %19, 1
  %21 = load i32, ptr %8, align 4, !tbaa !9
  %22 = load ptr, ptr %9, align 8, !tbaa !5
  %23 = call noundef i32 @_Z10allIndexesPiiiS_(ptr noundef %18, i32 noundef %20, i32 noundef %21, ptr noundef %22)
  store i32 %23, ptr %10, align 4, !tbaa !9
  %24 = load ptr, ptr %6, align 8, !tbaa !5
  %25 = getelementptr inbounds i32, ptr %24, i64 0
  %26 = load i32, ptr %25, align 4, !tbaa !9
  %27 = load i32, ptr %8, align 4, !tbaa !9
  %28 = icmp eq i32 %26, %27
  br i1 %28, label %29, label %56

29:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  %30 = load i32, ptr %10, align 4, !tbaa !9
  %31 = sub nsw i32 %30, 1
  store i32 %31, ptr %11, align 4, !tbaa !9
  br label %32

32:                                               ; preds = %48, %29
  %33 = load i32, ptr %11, align 4, !tbaa !9
  %34 = icmp sge i32 %33, 0
  br i1 %34, label %36, label %35

35:                                               ; preds = %32
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  br label %51

36:                                               ; preds = %32
  %37 = load ptr, ptr %9, align 8, !tbaa !5
  %38 = load i32, ptr %11, align 4, !tbaa !9
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds i32, ptr %37, i64 %39
  %41 = load i32, ptr %40, align 4, !tbaa !9
  %42 = add nsw i32 %41, 1
  %43 = load ptr, ptr %9, align 8, !tbaa !5
  %44 = load i32, ptr %11, align 4, !tbaa !9
  %45 = add nsw i32 %44, 1
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds i32, ptr %43, i64 %46
  store i32 %42, ptr %47, align 4, !tbaa !9
  br label %48

48:                                               ; preds = %36
  %49 = load i32, ptr %11, align 4, !tbaa !9
  %50 = add nsw i32 %49, -1
  store i32 %50, ptr %11, align 4, !tbaa !9
  br label %32, !llvm.loop !11

51:                                               ; preds = %35
  %52 = load ptr, ptr %9, align 8, !tbaa !5
  %53 = getelementptr inbounds i32, ptr %52, i64 0
  store i32 0, ptr %53, align 4, !tbaa !9
  %54 = load i32, ptr %10, align 4, !tbaa !9
  %55 = add nsw i32 %54, 1
  store i32 %55, ptr %10, align 4, !tbaa !9
  br label %78

56:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  %57 = load i32, ptr %10, align 4, !tbaa !9
  %58 = sub nsw i32 %57, 1
  store i32 %58, ptr %12, align 4, !tbaa !9
  br label %59

59:                                               ; preds = %74, %56
  %60 = load i32, ptr %12, align 4, !tbaa !9
  %61 = icmp sge i32 %60, 0
  br i1 %61, label %63, label %62

62:                                               ; preds = %59
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  br label %77

63:                                               ; preds = %59
  %64 = load ptr, ptr %9, align 8, !tbaa !5
  %65 = load i32, ptr %12, align 4, !tbaa !9
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds i32, ptr %64, i64 %66
  %68 = load i32, ptr %67, align 4, !tbaa !9
  %69 = add nsw i32 %68, 1
  %70 = load ptr, ptr %9, align 8, !tbaa !5
  %71 = load i32, ptr %12, align 4, !tbaa !9
  %72 = sext i32 %71 to i64
  %73 = getelementptr inbounds i32, ptr %70, i64 %72
  store i32 %69, ptr %73, align 4, !tbaa !9
  br label %74

74:                                               ; preds = %63
  %75 = load i32, ptr %12, align 4, !tbaa !9
  %76 = add nsw i32 %75, -1
  store i32 %76, ptr %12, align 4, !tbaa !9
  br label %59, !llvm.loop !13

77:                                               ; preds = %62
  br label %78

78:                                               ; preds = %77, %51
  %79 = load i32, ptr %10, align 4, !tbaa !9
  store i32 %79, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  br label %80

80:                                               ; preds = %78, %15
  %81 = load i32, ptr %5, align 4
  ret i32 %81
}

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

; Function Attrs: mustprogress nofree nosync nounwind memory(argmem: readwrite) uwtable
define dso_local noundef i32 @_Z10allIndexesPiiiS_(ptr nocapture noundef readonly %0, i32 noundef %1, i32 noundef %2, ptr nocapture noundef %3) local_unnamed_addr #0 {
  %5 = icmp eq i32 %1, 0
  br i1 %5, label %.loopexit, label %6

6:                                                ; preds = %4
  %7 = getelementptr inbounds i8, ptr %0, i64 4
  %8 = add nsw i32 %1, -1
  %9 = tail call noundef i32 @_Z10allIndexesPiiiS_(ptr noundef nonnull %7, i32 noundef %8, i32 noundef %2, ptr noundef %3)
  %10 = load i32, ptr %0, align 4, !tbaa !5
  %11 = icmp eq i32 %10, %2
  %12 = icmp sgt i32 %9, 0
  br i1 %11, label %.preheader, label %.preheader28

.preheader28:                                     ; preds = %6
  br i1 %12, label %.lr.ph.preheader, label %.loopexit

.lr.ph.preheader:                                 ; preds = %.preheader28
  %13 = zext nneg i32 %9 to i64
  %min.iters.check = icmp ult i32 %9, 8
  br i1 %min.iters.check, label %.lr.ph.preheader60, label %vector.ph

.lr.ph.preheader60:                               ; preds = %middle.block, %.lr.ph.preheader
  %indvars.iv.ph = phi i64 [ %13, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  br label %.lr.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %13, 2147483640
  %ind.end = and i64 %13, 7
  %invariant.gep = getelementptr i32, ptr %3, i64 %13
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %14 = xor i64 %index, -1
  %gep = getelementptr i32, ptr %invariant.gep, i64 %14
  %15 = getelementptr inbounds i8, ptr %gep, i64 -12
  %16 = getelementptr inbounds i8, ptr %gep, i64 -28
  %wide.load = load <4 x i32>, ptr %15, align 4, !tbaa !5
  %wide.load36 = load <4 x i32>, ptr %16, align 4, !tbaa !5
  %17 = add nsw <4 x i32> %wide.load, <i32 1, i32 1, i32 1, i32 1>
  %18 = add nsw <4 x i32> %wide.load36, <i32 1, i32 1, i32 1, i32 1>
  store <4 x i32> %17, ptr %15, align 4, !tbaa !5
  store <4 x i32> %18, ptr %16, align 4, !tbaa !5
  %index.next = add nuw i64 %index, 8
  %19 = icmp eq i64 %index.next, %n.vec
  br i1 %19, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %n.vec, %13
  br i1 %cmp.n, label %.loopexit, label %.lr.ph.preheader60

.preheader:                                       ; preds = %6
  br i1 %12, label %.lr.ph31.preheader, label %._crit_edge

.lr.ph31.preheader:                               ; preds = %.preheader
  %20 = zext nneg i32 %9 to i64
  %min.iters.check42 = icmp ult i32 %9, 8
  br i1 %min.iters.check42, label %.lr.ph31.preheader59, label %vector.ph43

.lr.ph31.preheader59:                             ; preds = %middle.block40, %.lr.ph31.preheader
  %indvars.iv33.ph = phi i64 [ %20, %.lr.ph31.preheader ], [ %ind.end46, %middle.block40 ]
  br label %.lr.ph31

vector.ph43:                                      ; preds = %.lr.ph31.preheader
  %n.vec45 = and i64 %20, 2147483640
  %ind.end46 = and i64 %20, 7
  br label %vector.body48

vector.body48:                                    ; preds = %vector.body48, %vector.ph43
  %index49 = phi i64 [ 0, %vector.ph43 ], [ %index.next57, %vector.body48 ]
  %offset.idx50 = sub i64 %20, %index49
  %21 = getelementptr i32, ptr %3, i64 %offset.idx50
  %22 = getelementptr i8, ptr %21, i64 -16
  %23 = getelementptr i8, ptr %21, i64 -32
  %wide.load51 = load <4 x i32>, ptr %22, align 4, !tbaa !5
  %wide.load53 = load <4 x i32>, ptr %23, align 4, !tbaa !5
  %24 = add nsw <4 x i32> %wide.load51, <i32 1, i32 1, i32 1, i32 1>
  %25 = add nsw <4 x i32> %wide.load53, <i32 1, i32 1, i32 1, i32 1>
  %26 = getelementptr inbounds i32, ptr %3, i64 %offset.idx50
  %27 = getelementptr inbounds i8, ptr %26, i64 -12
  %28 = getelementptr inbounds i8, ptr %26, i64 -28
  store <4 x i32> %24, ptr %27, align 4, !tbaa !5
  store <4 x i32> %25, ptr %28, align 4, !tbaa !5
  %index.next57 = add nuw i64 %index49, 8
  %29 = icmp eq i64 %index.next57, %n.vec45
  br i1 %29, label %middle.block40, label %vector.body48, !llvm.loop !13

middle.block40:                                   ; preds = %vector.body48
  %cmp.n58 = icmp eq i64 %n.vec45, %20
  br i1 %cmp.n58, label %._crit_edge, label %.lr.ph31.preheader59

._crit_edge:                                      ; preds = %.lr.ph31, %middle.block40, %.preheader
  store i32 0, ptr %3, align 4, !tbaa !5
  %30 = add nsw i32 %9, 1
  br label %.loopexit

.lr.ph31:                                         ; preds = %.lr.ph31.preheader59, %.lr.ph31
  %indvars.iv33 = phi i64 [ %indvars.iv.next34, %.lr.ph31 ], [ %indvars.iv33.ph, %.lr.ph31.preheader59 ]
  %indvars.iv.next34 = add nsw i64 %indvars.iv33, -1
  %31 = getelementptr inbounds i32, ptr %3, i64 %indvars.iv.next34
  %32 = load i32, ptr %31, align 4, !tbaa !5
  %33 = add nsw i32 %32, 1
  %34 = getelementptr inbounds i32, ptr %3, i64 %indvars.iv33
  store i32 %33, ptr %34, align 4, !tbaa !5
  %35 = icmp ugt i64 %indvars.iv33, 1
  br i1 %35, label %.lr.ph31, label %._crit_edge, !llvm.loop !14

.lr.ph:                                           ; preds = %.lr.ph.preheader60, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ %indvars.iv.ph, %.lr.ph.preheader60 ]
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %36 = getelementptr inbounds i32, ptr %3, i64 %indvars.iv.next
  %37 = load i32, ptr %36, align 4, !tbaa !5
  %38 = add nsw i32 %37, 1
  store i32 %38, ptr %36, align 4, !tbaa !5
  %39 = icmp ugt i64 %indvars.iv, 1
  br i1 %39, label %.lr.ph, label %.loopexit, !llvm.loop !15

.loopexit:                                        ; preds = %.lr.ph, %middle.block, %.preheader28, %._crit_edge, %4
  %.026 = phi i32 [ 0, %4 ], [ %30, %._crit_edge ], [ %9, %.preheader28 ], [ %9, %middle.block ], [ %9, %.lr.ph ]
  ret i32 %.026
}

attributes #0 = { mustprogress nofree nosync nounwind memory(argmem: readwrite) 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10, !11, !12}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!"llvm.loop.isvectorized", i32 1}
!12 = !{!"llvm.loop.unroll.runtime.disable"}
!13 = distinct !{!13, !10, !11, !12}
!14 = distinct !{!14, !10, !12, !11}
!15 = distinct !{!15, !10, !12, !11}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass