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

; 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 @_Z4lastPiiiii(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 %72

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 = load i32, ptr %11, align 4, !tbaa !9
  %26 = sub nsw i32 %25, 1
  %27 = icmp eq i32 %24, %26
  br i1 %27, label %37, label %28

28:                                               ; preds = %17
  %29 = load i32, ptr %10, align 4, !tbaa !9
  %30 = load ptr, ptr %7, align 8, !tbaa !5
  %31 = load i32, ptr %12, align 4, !tbaa !9
  %32 = add nsw i32 %31, 1
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds i32, ptr %30, i64 %33
  %35 = load i32, ptr %34, align 4, !tbaa !9
  %36 = icmp slt i32 %29, %35
  br i1 %36, label %37, label %47

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

45:                                               ; preds = %37
  %46 = load i32, ptr %12, align 4, !tbaa !9
  store i32 %46, ptr %6, align 4
  store i32 1, ptr %13, align 4
  br label %71

47:                                               ; preds = %37, %28
  %48 = load i32, ptr %10, align 4, !tbaa !9
  %49 = load ptr, ptr %7, align 8, !tbaa !5
  %50 = load i32, ptr %12, align 4, !tbaa !9
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds i32, ptr %49, i64 %51
  %53 = load i32, ptr %52, align 4, !tbaa !9
  %54 = icmp slt i32 %48, %53
  br i1 %54, label %55, label %63

55:                                               ; preds = %47
  %56 = load ptr, ptr %7, align 8, !tbaa !5
  %57 = load i32, ptr %8, align 4, !tbaa !9
  %58 = load i32, ptr %12, align 4, !tbaa !9
  %59 = sub nsw i32 %58, 1
  %60 = load i32, ptr %10, align 4, !tbaa !9
  %61 = load i32, ptr %11, align 4, !tbaa !9
  %62 = call noundef i32 @_Z4lastPiiiii(ptr noundef %56, i32 noundef %57, i32 noundef %59, i32 noundef %60, i32 noundef %61)
  store i32 %62, ptr %6, align 4
  store i32 1, ptr %13, align 4
  br label %71

63:                                               ; preds = %47
  %64 = load ptr, ptr %7, align 8, !tbaa !5
  %65 = load i32, ptr %12, align 4, !tbaa !9
  %66 = add nsw i32 %65, 1
  %67 = load i32, ptr %9, align 4, !tbaa !9
  %68 = load i32, ptr %10, align 4, !tbaa !9
  %69 = load i32, ptr %11, align 4, !tbaa !9
  %70 = call noundef i32 @_Z4lastPiiiii(ptr noundef %64, i32 noundef %66, i32 noundef %67, i32 noundef %68, i32 noundef %69)
  store i32 %70, ptr %6, align 4
  store i32 1, ptr %13, align 4
  br label %71

71:                                               ; preds = %63, %55, %45
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  br label %73

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

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

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 @_Z4lastPiiiii(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
  %.not3741 = icmp slt i32 %2, %1
  br i1 %.not3741, label %tailrecurse.outer._crit_edge, label %.lr.ph.lr.ph

.lr.ph.lr.ph:                                     ; preds = %5
  %6 = add nsw i32 %4, -1
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.lr.ph, %tailrecurse.outer
  %.tr33.ph43 = phi i32 [ %2, %.lr.ph.lr.ph ], [ %.tr3338, %tailrecurse.outer ]
  %.tr32.ph42 = phi i32 [ %1, %.lr.ph.lr.ph ], [ %22, %tailrecurse.outer ]
  br label %7

7:                                                ; preds = %.lr.ph, %tailrecurse
  %.tr3338 = phi i32 [ %.tr33.ph43, %.lr.ph ], [ %21, %tailrecurse ]
  %8 = sub nsw i32 %.tr3338, %.tr32.ph42
  %9 = sdiv i32 %8, 2
  %10 = add nsw i32 %9, %.tr32.ph42
  %11 = icmp eq i32 %10, %6
  %.pre49 = sext i32 %10 to i64
  br i1 %11, label %._crit_edge48, label %12

12:                                               ; preds = %7
  %gep = getelementptr i32, ptr %invariant.gep, i64 %.pre49
  %13 = load i32, ptr %gep, align 4, !tbaa !5
  %14 = icmp sgt i32 %13, %3
  br i1 %14, label %._crit_edge48, label %._crit_edge

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

._crit_edge48:                                    ; preds = %7, %12
  %15 = getelementptr inbounds i32, ptr %0, i64 %.pre49
  %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_edge48
  %19 = phi i32 [ %16, %._crit_edge48 ], [ %.pre, %._crit_edge ]
  %20 = icmp sgt i32 %19, %3
  br i1 %20, label %tailrecurse, label %tailrecurse.outer

tailrecurse:                                      ; preds = %18
  %21 = add nsw i32 %10, -1
  %.not.not = icmp sgt i32 %8, 1
  br i1 %.not.not, label %7, label %tailrecurse.outer._crit_edge

tailrecurse.outer:                                ; preds = %18
  %22 = add nsw i32 %10, 1
  %.not37.not = icmp sgt i32 %.tr3338, %10
  br i1 %.not37.not, label %.lr.ph, label %tailrecurse.outer._crit_edge

tailrecurse.outer._crit_edge:                     ; preds = %tailrecurse.outer, %._crit_edge48, %tailrecurse, %5
  %.1 = phi i32 [ -1, %5 ], [ -1, %tailrecurse ], [ %10, %._crit_edge48 ], [ -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: BDCEPass,CorrelatedValuePropagationPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass