; LLVM IR from kaynewbie/algorithm :: algorithm/Interview-questions (find-element-count-one-in-n)

; 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 ptr @findElementsCountMoreThanOneInN(ptr noundef %0, i32 noundef %1, i32 noundef %2, ptr noundef %3) #0 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !9
  store i32 %2, ptr %8, align 4, !tbaa !9
  store ptr %3, ptr %9, align 8, !tbaa !5
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  %17 = icmp eq ptr %16, null
  br i1 %17, label %24, label %18

18:                                               ; preds = %4
  %19 = load i32, ptr %7, align 4, !tbaa !9
  %20 = icmp sle i32 %19, 0
  br i1 %20, label %24, label %21

21:                                               ; preds = %18
  %22 = load i32, ptr %8, align 4, !tbaa !9
  %23 = icmp slt i32 %22, 2
  br i1 %23, label %24, label %25

24:                                               ; preds = %21, %18, %4
  store ptr null, ptr %5, align 8
  br label %89

25:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #2
  %26 = load i32, ptr %8, align 4, !tbaa !9
  %27 = call ptr @createMap(i32 noundef %26)
  store ptr %27, ptr %10, align 8, !tbaa !5
  store i32 0, ptr %12, align 4, !tbaa !9
  br label %28

28:                                               ; preds = %50, %25
  %29 = load i32, ptr %12, align 4, !tbaa !9
  %30 = load i32, ptr %7, align 4, !tbaa !9
  %31 = load i32, ptr %8, align 4, !tbaa !9
  %32 = sub nsw i32 %30, %31
  %33 = icmp sle i32 %29, %32
  br i1 %33, label %34, label %52

34:                                               ; preds = %28
  store i32 0, ptr %13, align 4, !tbaa !9
  br label %35

35:                                               ; preds = %39, %34
  %36 = load i32, ptr %13, align 4, !tbaa !9
  %37 = load i32, ptr %8, align 4, !tbaa !9
  %38 = icmp slt i32 %36, %37
  br i1 %38, label %39, label %50

39:                                               ; preds = %35
  %40 = load ptr, ptr %10, align 8, !tbaa !5
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = load i32, ptr %12, align 4, !tbaa !9
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds i32, ptr %41, i64 %43
  %45 = load i32, ptr %44, align 4, !tbaa !9
  call void @saveValue(ptr noundef %40, i32 noundef %45)
  %46 = load i32, ptr %13, align 4, !tbaa !9
  %47 = add nsw i32 %46, 1
  store i32 %47, ptr %13, align 4, !tbaa !9
  %48 = load i32, ptr %12, align 4, !tbaa !9
  %49 = add nsw i32 %48, 1
  store i32 %49, ptr %12, align 4, !tbaa !9
  br label %35, !llvm.loop !11

50:                                               ; preds = %35
  %51 = load ptr, ptr %10, align 8, !tbaa !5
  call void @doMinusForAll(ptr noundef %51)
  br label %28, !llvm.loop !13

52:                                               ; preds = %28
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #2
  %53 = load i32, ptr %12, align 4, !tbaa !9
  store i32 %53, ptr %15, align 4, !tbaa !9
  br label %54

54:                                               ; preds = %81, %52
  %55 = load i32, ptr %15, align 4, !tbaa !9
  %56 = load i32, ptr %7, align 4, !tbaa !9
  %57 = icmp slt i32 %55, %56
  br i1 %57, label %59, label %58

58:                                               ; preds = %54
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #2
  br label %84

59:                                               ; preds = %54
  %60 = load ptr, ptr %6, align 8, !tbaa !5
  %61 = load i32, ptr %7, align 4, !tbaa !9
  %62 = load ptr, ptr %6, align 8, !tbaa !5
  %63 = load i32, ptr %15, align 4, !tbaa !9
  %64 = sext i32 %63 to i64
  %65 = getelementptr inbounds i32, ptr %62, i64 %64
  %66 = load i32, ptr %65, align 4, !tbaa !9
  %67 = call i32 @elementCountInArray(ptr noundef %60, i32 noundef %61, i32 noundef %66)
  store i32 %67, ptr %14, align 4, !tbaa !9
  %68 = load i32, ptr %14, align 4, !tbaa !9
  %69 = load i32, ptr %7, align 4, !tbaa !9
  %70 = load i32, ptr %8, align 4, !tbaa !9
  %71 = sdiv i32 %69, %70
  %72 = icmp sgt i32 %68, %71
  br i1 %72, label %73, label %80

73:                                               ; preds = %59
  %74 = load ptr, ptr %10, align 8, !tbaa !5
  %75 = load ptr, ptr %6, align 8, !tbaa !5
  %76 = load i32, ptr %15, align 4, !tbaa !9
  %77 = sext i32 %76 to i64
  %78 = getelementptr inbounds i32, ptr %75, i64 %77
  %79 = load i32, ptr %78, align 4, !tbaa !9
  call void @saveValue(ptr noundef %74, i32 noundef %79)
  br label %80

80:                                               ; preds = %73, %59
  br label %81

81:                                               ; preds = %80
  %82 = load i32, ptr %15, align 4, !tbaa !9
  %83 = add nsw i32 %82, 1
  store i32 %83, ptr %15, align 4, !tbaa !9
  br label %54, !llvm.loop !14

84:                                               ; preds = %58
  %85 = load ptr, ptr %10, align 8, !tbaa !5
  %86 = load ptr, ptr %9, align 8, !tbaa !5
  %87 = call ptr @fetchValues(ptr noundef %85, ptr noundef %86)
  store ptr %87, ptr %11, align 8, !tbaa !5
  %88 = load ptr, ptr %11, align 8, !tbaa !5
  store ptr %88, ptr %5, align 8
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  br label %89

89:                                               ; preds = %84, %24
  %90 = load ptr, ptr %5, align 8
  ret ptr %90
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nounwind uwtable
declare dso_local ptr @createMap(i32 noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local void @saveValue(ptr noundef, i32 noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local void @doMinusForAll(ptr noundef) #0

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

; 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 ptr @fetchValues(ptr noundef, ptr 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"}
!13 = distinct !{!13, !12}
!14 = distinct !{!14, !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: nounwind uwtable
define dso_local ptr @findElementsCountMoreThanOneInN(ptr noundef %0, i32 noundef %1, i32 noundef %2, ptr noundef %3) local_unnamed_addr #0 {
  %5 = icmp eq ptr %0, null
  %6 = icmp slt i32 %1, 1
  %or.cond = or i1 %5, %6
  %7 = icmp slt i32 %2, 2
  %or.cond3 = or i1 %or.cond, %7
  br i1 %or.cond3, label %28, label %8

8:                                                ; preds = %4
  %9 = tail call ptr @createMap(i32 noundef %2)
  %10 = sub nsw i32 %1, %2
  %.not44 = icmp slt i32 %10, 0
  br i1 %.not44, label %.preheader, label %.preheader41.us

.preheader41.us:                                  ; preds = %8, %._crit_edge.us
  %.03745.us = phi i32 [ %12, %._crit_edge.us ], [ 0, %8 ]
  %11 = sext i32 %.03745.us to i64
  %12 = add i32 %.03745.us, %2
  br label %13

13:                                               ; preds = %.preheader41.us, %13
  %indvars.iv = phi i64 [ %11, %.preheader41.us ], [ %indvars.iv.next, %13 ]
  %14 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  %15 = load i32, ptr %14, align 4, !tbaa !5
  tail call void @saveValue(ptr noundef %9, i32 noundef %15)
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %lftr.wideiv = trunc i64 %indvars.iv.next to i32
  %exitcond.not = icmp eq i32 %12, %lftr.wideiv
  br i1 %exitcond.not, label %._crit_edge.us, label %13, !llvm.loop !9

._crit_edge.us:                                   ; preds = %13
  %16 = trunc nsw i64 %indvars.iv to i32
  tail call void @doMinusForAll(ptr noundef %9)
  %.not.us.not = icmp sgt i32 %10, %16
  br i1 %.not.us.not, label %.preheader41.us, label %.preheader, !llvm.loop !11

.preheader:                                       ; preds = %._crit_edge.us, %8
  %.037.lcssa = phi i32 [ 0, %8 ], [ %12, %._crit_edge.us ]
  %17 = icmp slt i32 %.037.lcssa, %1
  br i1 %17, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %.preheader
  %18 = sext i32 %.037.lcssa to i64
  br label %.lr.ph

._crit_edge:                                      ; preds = %27, %.preheader
  %19 = tail call ptr @fetchValues(ptr noundef %9, ptr noundef %3)
  br label %28

.lr.ph:                                           ; preds = %.lr.ph.preheader, %27
  %indvars.iv52 = phi i64 [ %18, %.lr.ph.preheader ], [ %indvars.iv.next53, %27 ]
  %20 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv52
  %21 = load i32, ptr %20, align 4, !tbaa !5
  %22 = tail call i32 @elementCountInArray(ptr noundef nonnull %0, i32 noundef %1, i32 noundef %21)
  %23 = udiv i32 %1, %2
  %24 = icmp sgt i32 %22, %23
  br i1 %24, label %25, label %27

25:                                               ; preds = %.lr.ph
  %26 = load i32, ptr %20, align 4, !tbaa !5
  tail call void @saveValue(ptr noundef %9, i32 noundef %26)
  br label %27

27:                                               ; preds = %.lr.ph, %25
  %indvars.iv.next53 = add nsw i64 %indvars.iv52, 1
  %lftr.wideiv55 = trunc i64 %indvars.iv.next53 to i32
  %exitcond56.not = icmp eq i32 %lftr.wideiv55, %1
  br i1 %exitcond56.not, label %._crit_edge, label %.lr.ph, !llvm.loop !12

28:                                               ; preds = %4, %._crit_edge
  %.035 = phi ptr [ %19, %._crit_edge ], [ null, %4 ]
  ret ptr %.035
}

; Function Attrs: nounwind uwtable
declare dso_local ptr @createMap(i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @saveValue(ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @doMinusForAll(ptr noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
declare dso_local ptr @fetchValues(ptr noundef, ptr 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"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}


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