; LLVM IR from kaynewbie/algorithm :: algorithm/CodingInterviews (question-39)

; 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 @moreThanHalfNum(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
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  %11 = icmp eq ptr %10, null
  br i1 %11, label %15, label %12

12:                                               ; preds = %2
  %13 = load i32, ptr %5, align 4, !tbaa !9
  %14 = icmp sle i32 %13, 0
  br i1 %14, label %15, label %16

15:                                               ; preds = %12, %2
  store i32 0, ptr %3, align 4
  br label %59

16:                                               ; preds = %12
  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 %6, align 4, !tbaa !9
  store i32 0, ptr %7, 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 %17

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

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

22:                                               ; preds = %17
  %23 = load i32, ptr %6, align 4, !tbaa !9
  %24 = icmp eq i32 %23, 0
  br i1 %24, label %25, label %31

25:                                               ; preds = %22
  %26 = load ptr, ptr %4, align 8, !tbaa !5
  %27 = load i32, ptr %8, align 4, !tbaa !9
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds i32, ptr %26, i64 %28
  %30 = load i32, ptr %29, align 4, !tbaa !9
  store i32 %30, ptr %7, align 4, !tbaa !9
  store i32 1, ptr %6, align 4, !tbaa !9
  br label %46

31:                                               ; preds = %22
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = load i32, ptr %8, align 4, !tbaa !9
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds i32, ptr %32, i64 %34
  %36 = load i32, ptr %35, align 4, !tbaa !9
  %37 = load i32, ptr %7, align 4, !tbaa !9
  %38 = icmp eq i32 %36, %37
  br i1 %38, label %39, label %42

39:                                               ; preds = %31
  %40 = load i32, ptr %6, align 4, !tbaa !9
  %41 = add nsw i32 %40, 1
  store i32 %41, ptr %6, align 4, !tbaa !9
  br label %45

42:                                               ; preds = %31
  %43 = load i32, ptr %6, align 4, !tbaa !9
  %44 = add nsw i32 %43, -1
  store i32 %44, ptr %6, align 4, !tbaa !9
  br label %45

45:                                               ; preds = %42, %39
  br label %46

46:                                               ; preds = %45, %25
  br label %47

47:                                               ; preds = %46
  %48 = load i32, ptr %8, align 4, !tbaa !9
  %49 = add nsw i32 %48, 1
  store i32 %49, ptr %8, align 4, !tbaa !9
  br label %17, !llvm.loop !11

50:                                               ; preds = %21
  %51 = load ptr, ptr %4, align 8, !tbaa !5
  %52 = load i32, ptr %5, align 4, !tbaa !9
  %53 = load i32, ptr %7, align 4, !tbaa !9
  %54 = call zeroext i1 @checkMoreThanHalf(ptr noundef %51, i32 noundef %52, i32 noundef %53)
  br i1 %54, label %56, label %55

55:                                               ; preds = %50
  store i32 0, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %58

56:                                               ; preds = %50
  %57 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %57, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %58

58:                                               ; preds = %56, %55
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %59

59:                                               ; preds = %58, %15
  %60 = load i32, ptr %3, align 4
  ret i32 %60
}

; 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

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @checkMoreThanHalf(ptr noundef, i32 noundef, i32 noundef) #0

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"}


; 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: nounwind uwtable
define dso_local i32 @moreThanHalfNum(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp eq ptr %0, null
  %4 = icmp slt i32 %1, 1
  %or.cond = or i1 %3, %4
  br i1 %or.cond, label %30, label %.preheader.preheader

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

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

.unr-lcssa.loopexit:                              ; preds = %29
  %6 = icmp eq i32 %.120.1, 0
  br label %.unr-lcssa

.unr-lcssa:                                       ; preds = %.unr-lcssa.loopexit, %.preheader.preheader
  %.1.lcssa.ph = phi i32 [ poison, %.preheader.preheader ], [ %.1.1, %.unr-lcssa.loopexit ]
  %indvars.iv.unr = phi i64 [ 0, %.preheader.preheader ], [ %indvars.iv.next.1, %.unr-lcssa.loopexit ]
  %.01826.unr = phi i32 [ 0, %.preheader.preheader ], [ %.1.1, %.unr-lcssa.loopexit ]
  %.01925.unr = phi i1 [ true, %.preheader.preheader ], [ %6, %.unr-lcssa.loopexit ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %9, label %.preheader.epil

.preheader.epil:                                  ; preds = %.unr-lcssa
  %7 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.unr
  %8 = load i32, ptr %7, align 4, !tbaa !5
  %.1.epil = select i1 %.01925.unr, i32 %8, i32 %.01826.unr
  br label %9

9:                                                ; preds = %.unr-lcssa, %.preheader.epil
  %.1.lcssa = phi i32 [ %.1.lcssa.ph, %.unr-lcssa ], [ %.1.epil, %.preheader.epil ]
  %10 = tail call zeroext i1 @checkMoreThanHalf(ptr noundef nonnull %0, i32 noundef %1, i32 noundef %.1.lcssa)
  %.018. = select i1 %10, i32 %.1.lcssa, i32 0
  br label %30

.preheader:                                       ; preds = %29, %.preheader.preheader.new
  %indvars.iv = phi i64 [ 0, %.preheader.preheader.new ], [ %indvars.iv.next.1, %29 ]
  %.01826 = phi i32 [ 0, %.preheader.preheader.new ], [ %.1.1, %29 ]
  %.01925 = phi i32 [ 0, %.preheader.preheader.new ], [ %.120.1, %29 ]
  %niter = phi i64 [ 0, %.preheader.preheader.new ], [ %niter.next.1, %29 ]
  %11 = icmp eq i32 %.01925, 0
  %12 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  %13 = load i32, ptr %12, align 4, !tbaa !5
  br i1 %11, label %.preheader.1, label %14

14:                                               ; preds = %.preheader
  %15 = icmp eq i32 %13, %.01826
  br i1 %15, label %16, label %18

16:                                               ; preds = %14
  %17 = add nsw i32 %.01925, 1
  br label %.preheader.1

18:                                               ; preds = %14
  %19 = add nsw i32 %.01925, -1
  br label %.preheader.1

.preheader.1:                                     ; preds = %.preheader, %18, %16
  %.120 = phi i32 [ %17, %16 ], [ %19, %18 ], [ 1, %.preheader ]
  %.1 = phi i32 [ %.01826, %16 ], [ %.01826, %18 ], [ %13, %.preheader ]
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %20 = icmp eq i32 %.120, 0
  %21 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.next
  %22 = load i32, ptr %21, align 4, !tbaa !5
  br i1 %20, label %29, label %23

23:                                               ; preds = %.preheader.1
  %24 = icmp eq i32 %22, %.1
  br i1 %24, label %27, label %25

25:                                               ; preds = %23
  %26 = add nsw i32 %.120, -1
  br label %29

27:                                               ; preds = %23
  %28 = add nsw i32 %.120, 1
  br label %29

29:                                               ; preds = %27, %25, %.preheader.1
  %.120.1 = phi i32 [ %28, %27 ], [ %26, %25 ], [ 1, %.preheader.1 ]
  %.1.1 = phi i32 [ %.1, %27 ], [ %.1, %25 ], [ %22, %.preheader.1 ]
  %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 %.unr-lcssa.loopexit, label %.preheader, !llvm.loop !9

30:                                               ; preds = %2, %9
  %.122 = phi i32 [ %.018., %9 ], [ 0, %2 ]
  ret i32 %.122
}

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @checkMoreThanHalf(ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

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" }

!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}
!10 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass