; LLVM IR from Hunterdii/GeeksforGeeks-POTD :: April 2025 GFG SOLUTION (April-25)

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

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

13:                                               ; preds = %9
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %38

14:                                               ; preds = %9
  %15 = load i32, ptr %5, align 4, !tbaa !9
  %16 = icmp ne i32 %15, 0
  br i1 %16, label %23, label %17

17:                                               ; preds = %14
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = load i32, ptr %7, align 4, !tbaa !9
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds i32, ptr %18, i64 %20
  %22 = load i32, ptr %21, align 4, !tbaa !9
  store i32 %22, ptr %6, align 4, !tbaa !9
  br label %23

23:                                               ; preds = %17, %14
  %24 = load ptr, ptr %3, align 8, !tbaa !5
  %25 = load i32, ptr %7, 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 i32, ptr %6, align 4, !tbaa !9
  %30 = icmp eq i32 %28, %29
  %31 = zext i1 %30 to i64
  %32 = select i1 %30, i32 1, i32 -1
  %33 = load i32, ptr %5, align 4, !tbaa !9
  %34 = add nsw i32 %33, %32
  store i32 %34, ptr %5, align 4, !tbaa !9
  br label %35

35:                                               ; preds = %23
  %36 = load i32, ptr %7, align 4, !tbaa !9
  %37 = add nsw i32 %36, 1
  store i32 %37, ptr %7, align 4, !tbaa !9
  br label %9, !llvm.loop !11

38:                                               ; preds = %13
  store i32 0, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  store i32 0, ptr %8, align 4, !tbaa !9
  br label %39

39:                                               ; preds = %56, %38
  %40 = load i32, ptr %8, align 4, !tbaa !9
  %41 = load i32, ptr %4, align 4, !tbaa !9
  %42 = icmp slt i32 %40, %41
  br i1 %42, label %44, label %43

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

44:                                               ; preds = %39
  %45 = load ptr, ptr %3, align 8, !tbaa !5
  %46 = load i32, ptr %8, align 4, !tbaa !9
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds i32, ptr %45, i64 %47
  %49 = load i32, ptr %48, align 4, !tbaa !9
  %50 = load i32, ptr %6, align 4, !tbaa !9
  %51 = icmp eq i32 %49, %50
  br i1 %51, label %52, label %55

52:                                               ; preds = %44
  %53 = load i32, ptr %5, align 4, !tbaa !9
  %54 = add nsw i32 %53, 1
  store i32 %54, ptr %5, align 4, !tbaa !9
  br label %55

55:                                               ; preds = %52, %44
  br label %56

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

59:                                               ; preds = %43
  %60 = load i32, ptr %5, align 4, !tbaa !9
  %61 = load i32, ptr %4, align 4, !tbaa !9
  %62 = sdiv i32 %61, 2
  %63 = icmp sgt i32 %60, %62
  br i1 %63, label %64, label %66

64:                                               ; preds = %59
  %65 = load i32, ptr %6, align 4, !tbaa !9
  br label %67

66:                                               ; preds = %59
  br label %67

67:                                               ; preds = %66, %64
  %68 = phi i32 [ %65, %64 ], [ -1, %66 ]
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  ret i32 %68
}

; 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 = { 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/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: nofree norecurse nosync nounwind memory(argmem: read) uwtable
define dso_local i32 @majorityElement(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp sgt i32 %1, 0
  br i1 %3, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %2
  %wide.trip.count = zext nneg i32 %1 to i64
  %xtraiter = and i64 %wide.trip.count, 1
  %4 = icmp eq i32 %1, 1
  br i1 %4, label %.preheader.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %wide.trip.count, 2147483646
  br label %.lr.ph

.preheader.unr-lcssa.loopexit:                    ; preds = %.lr.ph
  %5 = icmp eq i32 %27, 0
  br label %.preheader.unr-lcssa

.preheader.unr-lcssa:                             ; preds = %.preheader.unr-lcssa.loopexit, %.lr.ph.preheader
  %..02025.lcssa.ph = phi i32 [ poison, %.lr.ph.preheader ], [ %..02025.1, %.preheader.unr-lcssa.loopexit ]
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next.1, %.preheader.unr-lcssa.loopexit ]
  %.02025.unr = phi i32 [ undef, %.lr.ph.preheader ], [ %..02025.1, %.preheader.unr-lcssa.loopexit ]
  %.02124.unr = phi i1 [ true, %.lr.ph.preheader ], [ %5, %.preheader.unr-lcssa.loopexit ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.preheader, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %.preheader.unr-lcssa
  %6 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.unr
  %7 = load i32, ptr %6, align 4, !tbaa !5
  %..02025.epil = select i1 %.02124.unr, i32 %7, i32 %.02025.unr
  br label %.preheader

.preheader:                                       ; preds = %.preheader.unr-lcssa, %.lr.ph.epil
  %..02025.lcssa = phi i32 [ %..02025.lcssa.ph, %.preheader.unr-lcssa ], [ %..02025.epil, %.lr.ph.epil ]
  br i1 %3, label %.lr.ph29.preheader, label %._crit_edge

.lr.ph29.preheader:                               ; preds = %.preheader
  %wide.trip.count35 = zext nneg i32 %1 to i64
  %min.iters.check = icmp ult i32 %1, 8
  br i1 %min.iters.check, label %.lr.ph29.preheader41, label %vector.ph

.lr.ph29.preheader41:                             ; preds = %middle.block, %.lr.ph29.preheader
  %indvars.iv32.ph = phi i64 [ 0, %.lr.ph29.preheader ], [ %n.vec, %middle.block ]
  %.12227.ph = phi i32 [ 0, %.lr.ph29.preheader ], [ %17, %middle.block ]
  br label %.lr.ph29

vector.ph:                                        ; preds = %.lr.ph29.preheader
  %n.vec = and i64 %wide.trip.count, 2147483640
  %broadcast.splatinsert = insertelement <4 x i32> poison, i32 %..02025.lcssa, i64 0
  %broadcast.splat = shufflevector <4 x i32> %broadcast.splatinsert, <4 x i32> poison, <4 x i32> zeroinitializer
  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> [ zeroinitializer, %vector.ph ], [ %14, %vector.body ]
  %vec.phi39 = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %15, %vector.body ]
  %8 = getelementptr inbounds i32, ptr %0, i64 %index
  %9 = getelementptr inbounds i8, ptr %8, i64 16
  %wide.load = load <4 x i32>, ptr %8, align 4, !tbaa !5
  %wide.load40 = load <4 x i32>, ptr %9, align 4, !tbaa !5
  %10 = icmp eq <4 x i32> %wide.load, %broadcast.splat
  %11 = icmp eq <4 x i32> %wide.load40, %broadcast.splat
  %12 = zext <4 x i1> %10 to <4 x i32>
  %13 = zext <4 x i1> %11 to <4 x i32>
  %14 = add <4 x i32> %vec.phi, %12
  %15 = add <4 x i32> %vec.phi39, %13
  %index.next = add nuw i64 %index, 8
  %16 = icmp eq i64 %index.next, %n.vec
  br i1 %16, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <4 x i32> %15, %14
  %17 = tail call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx)
  %cmp.n = icmp eq i64 %n.vec, %wide.trip.count
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph29.preheader41

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader.new ], [ %indvars.iv.next.1, %.lr.ph ]
  %.02025 = phi i32 [ undef, %.lr.ph.preheader.new ], [ %..02025.1, %.lr.ph ]
  %.02124 = phi i32 [ 0, %.lr.ph.preheader.new ], [ %27, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %.lr.ph ]
  %.not = icmp eq i32 %.02124, 0
  %18 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  %19 = load i32, ptr %18, align 4, !tbaa !5
  %..02025 = select i1 %.not, i32 %19, i32 %.02025
  %20 = icmp eq i32 %19, %..02025
  %21 = select i1 %20, i32 1, i32 -1
  %22 = add nsw i32 %21, %.02124
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %.not.1 = icmp eq i32 %22, 0
  %23 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.next
  %24 = load i32, ptr %23, align 4, !tbaa !5
  %..02025.1 = select i1 %.not.1, i32 %24, i32 %..02025
  %25 = icmp eq i32 %24, %..02025.1
  %26 = select i1 %25, i32 1, i32 -1
  %27 = add nsw i32 %26, %22
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %.preheader.unr-lcssa.loopexit, label %.lr.ph, !llvm.loop !13

._crit_edge:                                      ; preds = %.lr.ph29, %middle.block, %2, %.preheader
  %.020.lcssa38 = phi i32 [ %..02025.lcssa, %.preheader ], [ undef, %2 ], [ %..02025.lcssa, %middle.block ], [ %..02025.lcssa, %.lr.ph29 ]
  %.122.lcssa = phi i32 [ 0, %.preheader ], [ 0, %2 ], [ %17, %middle.block ], [ %spec.select, %.lr.ph29 ]
  %28 = sdiv i32 %1, 2
  %29 = icmp sgt i32 %.122.lcssa, %28
  %30 = select i1 %29, i32 %.020.lcssa38, i32 -1
  ret i32 %30

.lr.ph29:                                         ; preds = %.lr.ph29.preheader41, %.lr.ph29
  %indvars.iv32 = phi i64 [ %indvars.iv.next33, %.lr.ph29 ], [ %indvars.iv32.ph, %.lr.ph29.preheader41 ]
  %.12227 = phi i32 [ %spec.select, %.lr.ph29 ], [ %.12227.ph, %.lr.ph29.preheader41 ]
  %31 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv32
  %32 = load i32, ptr %31, align 4, !tbaa !5
  %33 = icmp eq i32 %32, %..02025.lcssa
  %34 = zext i1 %33 to i32
  %spec.select = add nuw nsw i32 %.12227, %34
  %indvars.iv.next33 = add nuw nsw i64 %indvars.iv32, 1
  %exitcond36.not = icmp eq i64 %indvars.iv.next33, %wide.trip.count35
  br i1 %exitcond36.not, label %._crit_edge, label %.lr.ph29, !llvm.loop !14
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.add.v4i32(<4 x i32>) #1

attributes #0 = { nofree norecurse nosync nounwind 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/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}
!14 = distinct !{!14, !10, !12, !11}


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