; LLVM IR from devparthgarg/Data-Structures-and-Algorithms :: 05. Arrays/21 Majority Element/21a Majority Element.ll (Z15majorityElementPii)

; 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 i32 @_Z15majorityElementPii(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %10

10:                                               ; preds = %57, %2
  %11 = load i32, ptr %6, align 4, !tbaa !9
  %12 = load i32, ptr %5, align 4, !tbaa !9
  %13 = icmp slt i32 %11, %12
  br i1 %13, label %15, label %14

14:                                               ; preds = %10
  store i32 2, ptr %7, align 4
  br label %60

15:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  store i32 1, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  %16 = load i32, ptr %6, align 4, !tbaa !9
  %17 = add nsw i32 %16, 1
  store i32 %17, ptr %9, align 4, !tbaa !9
  br label %18

18:                                               ; preds = %39, %15
  %19 = load i32, ptr %9, align 4, !tbaa !9
  %20 = load i32, ptr %5, align 4, !tbaa !9
  %21 = icmp slt i32 %19, %20
  br i1 %21, label %23, label %22

22:                                               ; preds = %18
  store i32 5, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  br label %42

23:                                               ; preds = %18
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = load i32, ptr %6, align 4, !tbaa !9
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds i32, ptr %24, i64 %26
  %28 = load i32, ptr %27, align 4, !tbaa !9
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = load i32, ptr %9, align 4, !tbaa !9
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds i32, ptr %29, i64 %31
  %33 = load i32, ptr %32, align 4, !tbaa !9
  %34 = icmp eq i32 %28, %33
  br i1 %34, label %35, label %38

35:                                               ; preds = %23
  %36 = load i32, ptr %8, align 4, !tbaa !9
  %37 = add nsw i32 %36, 1
  store i32 %37, ptr %8, align 4, !tbaa !9
  br label %38

38:                                               ; preds = %35, %23
  br label %39

39:                                               ; preds = %38
  %40 = load i32, ptr %9, align 4, !tbaa !9
  %41 = add nsw i32 %40, 1
  store i32 %41, ptr %9, align 4, !tbaa !9
  br label %18, !llvm.loop !11

42:                                               ; preds = %22
  %43 = load i32, ptr %8, align 4, !tbaa !9
  %44 = load i32, ptr %5, align 4, !tbaa !9
  %45 = sdiv i32 %44, 2
  %46 = icmp sgt i32 %43, %45
  br i1 %46, label %47, label %53

47:                                               ; preds = %42
  %48 = load ptr, ptr %4, align 8, !tbaa !5
  %49 = load i32, ptr %6, align 4, !tbaa !9
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds i32, ptr %48, i64 %50
  %52 = load i32, ptr %51, align 4, !tbaa !9
  store i32 %52, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %54

53:                                               ; preds = %42
  store i32 0, ptr %7, align 4
  br label %54

54:                                               ; preds = %53, %47
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  %55 = load i32, ptr %7, align 4
  switch i32 %55, label %60 [
    i32 0, label %56
  ]

56:                                               ; preds = %54
  br label %57

57:                                               ; preds = %56
  %58 = load i32, ptr %6, align 4, !tbaa !9
  %59 = add nsw i32 %58, 1
  store i32 %59, ptr %6, align 4, !tbaa !9
  br label %10, !llvm.loop !13

60:                                               ; preds = %54, %14
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  %61 = load i32, ptr %7, align 4
  switch i32 %61, label %65 [
    i32 2, label %62
    i32 1, label %63
  ]

62:                                               ; preds = %60
  store i32 -1, ptr %3, align 4
  br label %63

63:                                               ; preds = %62, %60
  %64 = load i32, ptr %3, align 4
  ret i32 %64

65:                                               ; preds = %60
  unreachable
}

; 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 = !{!"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 norecurse nosync nounwind willreturn memory(argmem: read) uwtable
define dso_local noundef i32 @_Z15majorityElementPii(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = sdiv i32 %1, 2
  %smax = tail call i32 @llvm.smax.i32(i32 %1, i32 0)
  %wide.trip.count38 = zext nneg i32 %smax to i64
  %wide.trip.count = zext i32 %1 to i64
  br label %4

4:                                                ; preds = %._crit_edge, %2
  %indvars.iv35 = phi i64 [ %indvars.iv.next36, %._crit_edge ], [ 0, %2 ]
  %indvars.iv = phi i64 [ %indvars.iv.next, %._crit_edge ], [ 1, %2 ]
  %5 = xor i64 %indvars.iv35, -1
  %6 = add nsw i64 %5, %wide.trip.count
  %exitcond39.not = icmp eq i64 %indvars.iv35, %wide.trip.count38
  br i1 %exitcond39.not, label %.loopexit, label %7

7:                                                ; preds = %4
  %indvars.iv.next36 = add nuw nsw i64 %indvars.iv35, 1
  %8 = trunc nuw i64 %indvars.iv.next36 to i32
  %9 = icmp slt i32 %8, %1
  br i1 %9, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %7
  %10 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv35
  %11 = load i32, ptr %10, align 4, !tbaa !5
  %min.iters.check = icmp ult i64 %6, 8
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.ph

scalar.ph.preheader:                              ; preds = %middle.block, %.lr.ph
  %indvars.iv32.ph = phi i64 [ %indvars.iv, %.lr.ph ], [ %ind.end, %middle.block ]
  %.01829.ph = phi i32 [ 1, %.lr.ph ], [ %22, %middle.block ]
  br label %scalar.ph

vector.ph:                                        ; preds = %.lr.ph
  %n.vec = and i64 %6, -8
  %ind.end = add i64 %indvars.iv, %n.vec
  %broadcast.splatinsert = insertelement <4 x i32> poison, i32 %11, i64 0
  %broadcast.splat = shufflevector <4 x i32> %broadcast.splatinsert, <4 x i32> poison, <4 x i32> zeroinitializer
  %12 = getelementptr i32, ptr %0, i64 %indvars.iv
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <4 x i32> [ <i32 1, i32 0, i32 0, i32 0>, %vector.ph ], [ %19, %vector.body ]
  %vec.phi42 = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %20, %vector.body ]
  %13 = getelementptr i32, ptr %12, i64 %index
  %14 = getelementptr inbounds i8, ptr %13, i64 16
  %wide.load = load <4 x i32>, ptr %13, align 4, !tbaa !5
  %wide.load43 = load <4 x i32>, ptr %14, align 4, !tbaa !5
  %15 = icmp eq <4 x i32> %broadcast.splat, %wide.load
  %16 = icmp eq <4 x i32> %broadcast.splat, %wide.load43
  %17 = zext <4 x i1> %15 to <4 x i32>
  %18 = zext <4 x i1> %16 to <4 x i32>
  %19 = add <4 x i32> %vec.phi, %17
  %20 = add <4 x i32> %vec.phi42, %18
  %index.next = add nuw i64 %index, 8
  %21 = icmp eq i64 %index.next, %n.vec
  br i1 %21, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <4 x i32> %20, %19
  %22 = tail call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx)
  %cmp.n = icmp eq i64 %6, %n.vec
  br i1 %cmp.n, label %._crit_edge, label %scalar.ph.preheader

._crit_edge:                                      ; preds = %scalar.ph, %middle.block, %7
  %.018.lcssa = phi i32 [ 1, %7 ], [ %22, %middle.block ], [ %spec.select, %scalar.ph ]
  %.not = icmp sgt i32 %.018.lcssa, %3
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  br i1 %.not, label %.thread, label %4

scalar.ph:                                        ; preds = %scalar.ph.preheader, %scalar.ph
  %indvars.iv32 = phi i64 [ %indvars.iv.next33, %scalar.ph ], [ %indvars.iv32.ph, %scalar.ph.preheader ]
  %.01829 = phi i32 [ %spec.select, %scalar.ph ], [ %.01829.ph, %scalar.ph.preheader ]
  %23 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv32
  %24 = load i32, ptr %23, align 4, !tbaa !5
  %25 = icmp eq i32 %11, %24
  %26 = zext i1 %25 to i32
  %spec.select = add nuw nsw i32 %.01829, %26
  %indvars.iv.next33 = add nuw nsw i64 %indvars.iv32, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next33, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %scalar.ph, !llvm.loop !13

.thread:                                          ; preds = %._crit_edge
  %27 = and i64 %indvars.iv35, 4294967295
  %28 = getelementptr inbounds i32, ptr %0, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !5
  br label %.loopexit

.loopexit:                                        ; preds = %4, %.thread
  %spec.select25 = phi i32 [ %29, %.thread ], [ -1, %4 ]
  ret i32 %spec.select25
}

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.add.v4i32(<4 x 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" }
attributes #1 = { 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 = !{!"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, !12, !11}


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