; LLVM IR from ritikbanger/Hacktoberfest2022-DSA :: 05. Searching/3_First and last occurrences of x.ll (Z5firstPiiiii)

; 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

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

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

17:                                               ; preds = %5
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  %18 = load i32, ptr %8, align 4, !tbaa !9
  %19 = load i32, ptr %9, align 4, !tbaa !9
  %20 = load i32, ptr %8, align 4, !tbaa !9
  %21 = sub nsw i32 %19, %20
  %22 = sdiv i32 %21, 2
  %23 = add nsw i32 %18, %22
  store i32 %23, ptr %12, align 4, !tbaa !9
  %24 = load i32, ptr %12, align 4, !tbaa !9
  %25 = icmp eq i32 %24, 0
  br i1 %25, label %35, label %26

26:                                               ; preds = %17
  %27 = load i32, ptr %10, align 4, !tbaa !9
  %28 = load ptr, ptr %7, align 8, !tbaa !5
  %29 = load i32, ptr %12, align 4, !tbaa !9
  %30 = sub nsw i32 %29, 1
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds i32, ptr %28, i64 %31
  %33 = load i32, ptr %32, align 4, !tbaa !9
  %34 = icmp sgt i32 %27, %33
  br i1 %34, label %35, label %45

35:                                               ; preds = %26, %17
  %36 = load ptr, ptr %7, align 8, !tbaa !5
  %37 = load i32, ptr %12, align 4, !tbaa !9
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds i32, ptr %36, i64 %38
  %40 = load i32, ptr %39, align 4, !tbaa !9
  %41 = load i32, ptr %10, align 4, !tbaa !9
  %42 = icmp eq i32 %40, %41
  br i1 %42, label %43, label %45

43:                                               ; preds = %35
  %44 = load i32, ptr %12, align 4, !tbaa !9
  store i32 %44, ptr %6, align 4
  store i32 1, ptr %13, align 4
  br label %69

45:                                               ; preds = %35, %26
  %46 = load i32, ptr %10, align 4, !tbaa !9
  %47 = load ptr, ptr %7, align 8, !tbaa !5
  %48 = load i32, ptr %12, align 4, !tbaa !9
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds i32, ptr %47, i64 %49
  %51 = load i32, ptr %50, align 4, !tbaa !9
  %52 = icmp sgt i32 %46, %51
  br i1 %52, label %53, label %61

53:                                               ; preds = %45
  %54 = load ptr, ptr %7, align 8, !tbaa !5
  %55 = load i32, ptr %12, align 4, !tbaa !9
  %56 = add nsw i32 %55, 1
  %57 = load i32, ptr %9, align 4, !tbaa !9
  %58 = load i32, ptr %10, align 4, !tbaa !9
  %59 = load i32, ptr %11, align 4, !tbaa !9
  %60 = call noundef i32 @_Z5firstPiiiii(ptr noundef %54, i32 noundef %56, i32 noundef %57, i32 noundef %58, i32 noundef %59)
  store i32 %60, ptr %6, align 4
  store i32 1, ptr %13, align 4
  br label %69

61:                                               ; preds = %45
  %62 = load ptr, ptr %7, align 8, !tbaa !5
  %63 = load i32, ptr %8, align 4, !tbaa !9
  %64 = load i32, ptr %12, align 4, !tbaa !9
  %65 = sub nsw i32 %64, 1
  %66 = load i32, ptr %10, align 4, !tbaa !9
  %67 = load i32, ptr %11, align 4, !tbaa !9
  %68 = call noundef i32 @_Z5firstPiiiii(ptr noundef %62, i32 noundef %63, i32 noundef %65, i32 noundef %66, i32 noundef %67)
  store i32 %68, ptr %6, align 4
  store i32 1, ptr %13, align 4
  br label %69

69:                                               ; preds = %61, %53, %43
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  br label %71

70:                                               ; preds = %5
  store i32 -1, ptr %6, align 4
  br label %71

71:                                               ; preds = %70, %69
  %72 = load i32, ptr %6, align 4
  ret i32 %72
}

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


; 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 norecurse nosync nounwind willreturn memory(argmem: read) uwtable
define dso_local noundef i32 @_Z5firstPiiiii(ptr nocapture noundef readonly %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) local_unnamed_addr #0 {
  %invariant.gep = getelementptr i8, ptr %0, i64 -4
  %.not3640 = icmp slt i32 %2, %1
  br i1 %.not3640, label %tailrecurse.outer._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %5, %tailrecurse.outer
  %.tr32.ph42 = phi i32 [ %.tr3237, %tailrecurse.outer ], [ %2, %5 ]
  %.tr31.ph41 = phi i32 [ %21, %tailrecurse.outer ], [ %1, %5 ]
  br label %6

6:                                                ; preds = %.lr.ph, %tailrecurse
  %.tr3237 = phi i32 [ %.tr32.ph42, %.lr.ph ], [ %22, %tailrecurse ]
  %7 = sub nsw i32 %.tr3237, %.tr31.ph41
  %8 = sdiv i32 %7, 2
  %9 = add nsw i32 %8, %.tr31.ph41
  %10 = icmp eq i32 %9, 0
  br i1 %10, label %._crit_edge47, label %11

11:                                               ; preds = %6
  %12 = sext i32 %9 to i64
  %gep = getelementptr i32, ptr %invariant.gep, i64 %12
  %13 = load i32, ptr %gep, align 4, !tbaa !5
  %14 = icmp slt i32 %13, %3
  br i1 %14, label %._crit_edge47, label %._crit_edge

._crit_edge:                                      ; preds = %11
  %.phi.trans.insert = getelementptr inbounds i32, ptr %0, i64 %12
  %.pre = load i32, ptr %.phi.trans.insert, align 4, !tbaa !5
  br label %18

._crit_edge47:                                    ; preds = %6, %11
  %.pre-phi49 = phi i64 [ %12, %11 ], [ 0, %6 ]
  %15 = getelementptr inbounds i32, ptr %0, i64 %.pre-phi49
  %16 = load i32, ptr %15, align 4, !tbaa !5
  %17 = icmp eq i32 %16, %3
  br i1 %17, label %tailrecurse.outer._crit_edge, label %18

18:                                               ; preds = %._crit_edge, %._crit_edge47
  %19 = phi i32 [ %16, %._crit_edge47 ], [ %.pre, %._crit_edge ]
  %20 = icmp slt i32 %19, %3
  br i1 %20, label %tailrecurse.outer, label %tailrecurse

tailrecurse.outer:                                ; preds = %18
  %21 = add nsw i32 %9, 1
  %.not36.not = icmp sgt i32 %.tr3237, %9
  br i1 %.not36.not, label %.lr.ph, label %tailrecurse.outer._crit_edge

tailrecurse:                                      ; preds = %18
  %22 = add nsw i32 %9, -1
  %.not.not = icmp sgt i32 %7, 1
  br i1 %.not.not, label %6, label %tailrecurse.outer._crit_edge

tailrecurse.outer._crit_edge:                     ; preds = %tailrecurse.outer, %._crit_edge47, %tailrecurse, %5
  %.1 = phi i32 [ -1, %5 ], [ -1, %tailrecurse ], [ %9, %._crit_edge47 ], [ -1, %tailrecurse.outer ]
  ret i32 %.1
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: read) 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"}


; Effective O3 passes: ADCEPass,BDCEPass,CorrelatedValuePropagationPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass