; LLVM IR from ritikbanger/Hacktoberfest2022-DSA :: 05. Searching/Aggressive cows.ll (Z5checkiPxix)

; 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 nounwind uwtable
define dso_local noundef zeroext i1 @_Z5checkiPxix(i32 noundef %0, ptr noundef %1, i32 noundef %2, i64 noundef %3) #0 {
  %5 = alloca i1, align 1
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  %11 = alloca i64, align 8
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  store i32 %0, ptr %6, align 4, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !9
  store i32 %2, ptr %8, align 4, !tbaa !5
  store i64 %3, ptr %9, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  store i32 1, ptr %10, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #2
  %14 = load ptr, ptr %7, align 8, !tbaa !9
  %15 = getelementptr inbounds i64, ptr %14, i64 0
  %16 = load i64, ptr %15, align 8, !tbaa !11
  store i64 %16, ptr %11, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  store i32 1, ptr %12, align 4, !tbaa !5
  br label %17

17:                                               ; preds = %46, %4
  %18 = load i32, ptr %12, align 4, !tbaa !5
  %19 = load i32, ptr %8, align 4, !tbaa !5
  %20 = icmp slt i32 %18, %19
  br i1 %20, label %22, label %21

21:                                               ; preds = %17
  store i32 2, ptr %13, align 4
  br label %49

22:                                               ; preds = %17
  %23 = load ptr, ptr %7, align 8, !tbaa !9
  %24 = load i32, ptr %12, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds i64, ptr %23, i64 %25
  %27 = load i64, ptr %26, align 8, !tbaa !11
  %28 = load i64, ptr %11, align 8, !tbaa !11
  %29 = sub nsw i64 %27, %28
  %30 = load i64, ptr %9, align 8, !tbaa !11
  %31 = icmp sge i64 %29, %30
  br i1 %31, label %32, label %40

32:                                               ; preds = %22
  %33 = load ptr, ptr %7, align 8, !tbaa !9
  %34 = load i32, ptr %12, align 4, !tbaa !5
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds i64, ptr %33, i64 %35
  %37 = load i64, ptr %36, align 8, !tbaa !11
  store i64 %37, ptr %11, align 8, !tbaa !11
  %38 = load i32, ptr %10, align 4, !tbaa !5
  %39 = add nsw i32 %38, 1
  store i32 %39, ptr %10, align 4, !tbaa !5
  br label %40

40:                                               ; preds = %32, %22
  %41 = load i32, ptr %10, align 4, !tbaa !5
  %42 = load i32, ptr %6, align 4, !tbaa !5
  %43 = icmp eq i32 %41, %42
  br i1 %43, label %44, label %45

44:                                               ; preds = %40
  store i1 true, ptr %5, align 1
  store i32 1, ptr %13, align 4
  br label %49

45:                                               ; preds = %40
  br label %46

46:                                               ; preds = %45
  %47 = load i32, ptr %12, align 4, !tbaa !5
  %48 = add nsw i32 %47, 1
  store i32 %48, ptr %12, align 4, !tbaa !5
  br label %17, !llvm.loop !13

49:                                               ; preds = %44, %21
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  %50 = load i32, ptr %13, align 4
  switch i32 %50, label %52 [
    i32 2, label %51
  ]

51:                                               ; preds = %49
  store i1 false, ptr %5, align 1
  store i32 1, ptr %13, align 4
  br label %52

52:                                               ; preds = %51, %49
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  %53 = load i1, ptr %5, align 1
  ret i1 %53
}

; 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 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 #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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long long", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


; 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 zeroext i1 @_Z5checkiPxix(i32 noundef %0, ptr nocapture noundef readonly %1, i32 noundef %2, i64 noundef %3) local_unnamed_addr #0 {
  %.not2022 = icmp sgt i32 %2, 1
  br i1 %.not2022, label %.lr.ph.preheader, label %.critedge

.lr.ph.preheader:                                 ; preds = %4
  %5 = load i64, ptr %1, align 8, !tbaa !5
  %6 = zext nneg i32 %2 to i64
  %wide.trip.count = zext nneg i32 %2 to i64
  %7 = getelementptr inbounds i8, ptr %1, i64 8
  %8 = load i64, ptr %7, align 8, !tbaa !5
  %9 = sub nsw i64 %8, %5
  %.not29 = icmp sge i64 %9, %3
  %spec.select30 = select i1 %.not29, i32 2, i32 1
  %10 = icmp eq i32 %spec.select30, %0
  br i1 %10, label %.critedge, label %.lr.ph35

.lr.ph:                                           ; preds = %.lr.ph35
  %spec.select21 = select i1 %.not33, i64 %16, i64 %.0152432
  %11 = getelementptr inbounds i64, ptr %1, i64 %indvars.iv.next
  %12 = load i64, ptr %11, align 8, !tbaa !5
  %13 = sub nsw i64 %12, %spec.select21
  %.not = icmp sge i64 %13, %3
  %14 = zext i1 %.not to i32
  %spec.select = add nuw nsw i32 %spec.select34, %14
  %15 = icmp eq i32 %spec.select, %0
  br i1 %15, label %.critedge.loopexit, label %.lr.ph35, !llvm.loop !9

.lr.ph35:                                         ; preds = %.lr.ph.preheader, %.lr.ph
  %spec.select34 = phi i32 [ %spec.select, %.lr.ph ], [ %spec.select30, %.lr.ph.preheader ]
  %.not33 = phi i1 [ %.not, %.lr.ph ], [ %.not29, %.lr.ph.preheader ]
  %16 = phi i64 [ %12, %.lr.ph ], [ %8, %.lr.ph.preheader ]
  %.0152432 = phi i64 [ %spec.select21, %.lr.ph ], [ %5, %.lr.ph.preheader ]
  %indvars.iv31 = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 1, %.lr.ph.preheader ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv31, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %.critedge.loopexit, label %.lr.ph, !llvm.loop !9

.critedge.loopexit:                               ; preds = %.lr.ph, %.lr.ph35
  %.not20.le = icmp ult i64 %indvars.iv.next, %6
  br label %.critedge

.critedge:                                        ; preds = %.critedge.loopexit, %.lr.ph.preheader, %4
  %.not20.lcssa = phi i1 [ false, %4 ], [ true, %.lr.ph.preheader ], [ %.not20.le, %.critedge.loopexit ]
  ret i1 %.not20.lcssa
}

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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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