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

%struct.Map = type { ptr, i32, i32 }
%struct.Element = type { i32, i32 }

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

; Function Attrs: nounwind uwtable
define dso_local void @saveValue(ptr noundef %0, i32 noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i8, align 1
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  %11 = icmp eq ptr %10, null
  br i1 %11, label %12, label %13

12:                                               ; preds = %2
  br label %104

13:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.start.p0(i64 1, ptr %6) #2
  store i8 0, ptr %6, align 1, !tbaa !11
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.Map, ptr %14, i32 0, i32 2
  %16 = load i32, ptr %15, align 4, !tbaa !13
  store i32 %16, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %17

17:                                               ; preds = %44, %13
  %18 = load i32, ptr %7, 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 %7) #2
  br label %47

22:                                               ; preds = %17
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.Map, ptr %23, i32 0, i32 0
  %25 = load ptr, ptr %24, align 8, !tbaa !15
  %26 = load i32, ptr %7, align 4, !tbaa !9
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds %struct.Element, ptr %25, i64 %27
  %29 = getelementptr inbounds %struct.Element, ptr %28, i32 0, i32 0
  %30 = load i32, ptr %29, align 4, !tbaa !16
  %31 = load i32, ptr %4, align 4, !tbaa !9
  %32 = icmp eq i32 %30, %31
  br i1 %32, label %33, label %43

33:                                               ; preds = %22
  store i8 1, ptr %6, align 1, !tbaa !11
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.Map, ptr %34, i32 0, i32 0
  %36 = load ptr, ptr %35, align 8, !tbaa !15
  %37 = load i32, ptr %7, align 4, !tbaa !9
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds %struct.Element, ptr %36, i64 %38
  %40 = getelementptr inbounds %struct.Element, ptr %39, i32 0, i32 1
  %41 = load i32, ptr %40, align 4, !tbaa !18
  %42 = add nsw i32 %41, 1
  store i32 %42, ptr %40, align 4, !tbaa !18
  br label %43

43:                                               ; preds = %33, %22
  br label %44

44:                                               ; preds = %43
  %45 = load i32, ptr %7, align 4, !tbaa !9
  %46 = add nsw i32 %45, 1
  store i32 %46, ptr %7, align 4, !tbaa !9
  br label %17, !llvm.loop !19

47:                                               ; preds = %21
  %48 = load i8, ptr %6, align 1, !tbaa !11, !range !21, !noundef !22
  %49 = trunc i8 %48 to i1
  br i1 %49, label %103, label %50

50:                                               ; preds = %47
  %51 = load ptr, ptr %3, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.Map, ptr %51, i32 0, i32 1
  %53 = load i32, ptr %52, align 8, !tbaa !23
  %54 = load ptr, ptr %3, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.Map, ptr %54, i32 0, i32 2
  %56 = load i32, ptr %55, align 4, !tbaa !13
  %57 = icmp slt i32 %53, %56
  br i1 %57, label %58, label %102

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

59:                                               ; preds = %97, %58
  %60 = load i32, ptr %8, align 4, !tbaa !9
  %61 = load ptr, ptr %3, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.Map, ptr %61, i32 0, i32 2
  %63 = load i32, ptr %62, align 4, !tbaa !13
  %64 = icmp slt i32 %60, %63
  br i1 %64, label %66, label %65

65:                                               ; preds = %59
  store i32 5, ptr %9, align 4
  br label %100

66:                                               ; preds = %59
  %67 = load ptr, ptr %3, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.Map, ptr %67, i32 0, i32 0
  %69 = load ptr, ptr %68, align 8, !tbaa !15
  %70 = load i32, ptr %8, align 4, !tbaa !9
  %71 = sext i32 %70 to i64
  %72 = getelementptr inbounds %struct.Element, ptr %69, i64 %71
  %73 = getelementptr inbounds %struct.Element, ptr %72, i32 0, i32 0
  %74 = load i32, ptr %73, align 4, !tbaa !16
  %75 = icmp eq i32 %74, 0
  br i1 %75, label %76, label %96

76:                                               ; preds = %66
  %77 = load i32, ptr %4, align 4, !tbaa !9
  %78 = load ptr, ptr %3, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.Map, ptr %78, i32 0, i32 0
  %80 = load ptr, ptr %79, align 8, !tbaa !15
  %81 = load i32, ptr %8, align 4, !tbaa !9
  %82 = sext i32 %81 to i64
  %83 = getelementptr inbounds %struct.Element, ptr %80, i64 %82
  %84 = getelementptr inbounds %struct.Element, ptr %83, i32 0, i32 0
  store i32 %77, ptr %84, align 4, !tbaa !16
  %85 = load ptr, ptr %3, align 8, !tbaa !5
  %86 = getelementptr inbounds %struct.Map, ptr %85, i32 0, i32 0
  %87 = load ptr, ptr %86, align 8, !tbaa !15
  %88 = load i32, ptr %8, align 4, !tbaa !9
  %89 = sext i32 %88 to i64
  %90 = getelementptr inbounds %struct.Element, ptr %87, i64 %89
  %91 = getelementptr inbounds %struct.Element, ptr %90, i32 0, i32 1
  store i32 1, ptr %91, align 4, !tbaa !18
  %92 = load ptr, ptr %3, align 8, !tbaa !5
  %93 = getelementptr inbounds %struct.Map, ptr %92, i32 0, i32 1
  %94 = load i32, ptr %93, align 8, !tbaa !23
  %95 = add nsw i32 %94, 1
  store i32 %95, ptr %93, align 8, !tbaa !23
  store i32 5, ptr %9, align 4
  br label %100

96:                                               ; preds = %66
  br label %97

97:                                               ; preds = %96
  %98 = load i32, ptr %8, align 4, !tbaa !9
  %99 = add nsw i32 %98, 1
  store i32 %99, ptr %8, align 4, !tbaa !9
  br label %59, !llvm.loop !24

100:                                              ; preds = %76, %65
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  br label %101

101:                                              ; preds = %100
  br label %102

102:                                              ; preds = %101, %50
  br label %103

103:                                              ; preds = %102, %47
  call void @llvm.lifetime.end.p0(i64 1, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  br label %104

104:                                              ; preds = %103, %12
  ret void
}

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = !{!12, !12, i64 0}
!12 = !{!"_Bool", !7, i64 0}
!13 = !{!14, !10, i64 12}
!14 = !{!"Map", !6, i64 0, !10, i64 8, !10, i64 12}
!15 = !{!14, !6, i64 0}
!16 = !{!17, !10, i64 0}
!17 = !{!"Element", !10, i64 0, !10, i64 4}
!18 = !{!17, !10, i64 4}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = !{i8 0, i8 2}
!22 = !{}
!23 = !{!14, !10, i64 8}
!24 = distinct !{!24, !20}


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

%struct.Element = type { i32, i32 }

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @saveValue(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp eq ptr %0, null
  br i1 %3, label %.loopexit, label %4

4:                                                ; preds = %2
  %5 = getelementptr inbounds i8, ptr %0, i64 12
  %6 = load i32, ptr %5, align 4, !tbaa !5
  %7 = icmp sgt i32 %6, 0
  br i1 %7, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %4
  %8 = load ptr, ptr %0, align 8, !tbaa !11
  %wide.trip.count = zext nneg i32 %6 to i64
  %invariant.gep = getelementptr i8, ptr %8, i64 4
  br label %.outer

.outer:                                           ; preds = %.thread, %.lr.ph
  %indvars.iv.ph = phi i64 [ %indvars.iv.next41, %.thread ], [ 0, %.lr.ph ]
  %.02428.ph = phi i1 [ true, %.thread ], [ false, %.lr.ph ]
  br label %9

._crit_edge:                                      ; preds = %13
  br i1 %.02428.ph, label %.loopexit, label %.critedge

9:                                                ; preds = %.outer, %13
  %indvars.iv = phi i64 [ %indvars.iv.next, %13 ], [ %indvars.iv.ph, %.outer ]
  %10 = getelementptr inbounds %struct.Element, ptr %8, i64 %indvars.iv
  %11 = load i32, ptr %10, align 4, !tbaa !12
  %12 = icmp eq i32 %11, %1
  br i1 %12, label %.thread, label %13

13:                                               ; preds = %9
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %9, !llvm.loop !14

.thread:                                          ; preds = %9
  %gep = getelementptr %struct.Element, ptr %invariant.gep, i64 %indvars.iv
  %14 = load i32, ptr %gep, align 4, !tbaa !16
  %15 = add nsw i32 %14, 1
  store i32 %15, ptr %gep, align 4, !tbaa !16
  %indvars.iv.next41 = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not42 = icmp eq i64 %indvars.iv.next41, %wide.trip.count
  br i1 %exitcond.not42, label %.loopexit, label %.outer, !llvm.loop !14

.critedge:                                        ; preds = %._crit_edge
  %16 = getelementptr inbounds i8, ptr %0, i64 8
  %17 = load i32, ptr %16, align 8, !tbaa !17
  %18 = icmp slt i32 %17, %6
  %or.cond = and i1 %18, %7
  br i1 %or.cond, label %.lr.ph33, label %.loopexit

.lr.ph33:                                         ; preds = %.critedge
  %19 = load ptr, ptr %0, align 8, !tbaa !11
  %wide.trip.count38 = zext nneg i32 %6 to i64
  br label %21

20:                                               ; preds = %21
  %indvars.iv.next36 = add nuw nsw i64 %indvars.iv35, 1
  %exitcond39.not = icmp eq i64 %indvars.iv.next36, %wide.trip.count38
  br i1 %exitcond39.not, label %.loopexit, label %21, !llvm.loop !18

21:                                               ; preds = %.lr.ph33, %20
  %indvars.iv35 = phi i64 [ 0, %.lr.ph33 ], [ %indvars.iv.next36, %20 ]
  %22 = getelementptr inbounds %struct.Element, ptr %19, i64 %indvars.iv35
  %23 = load i32, ptr %22, align 4, !tbaa !12
  %24 = icmp eq i32 %23, 0
  br i1 %24, label %25, label %20

25:                                               ; preds = %21
  %26 = getelementptr inbounds %struct.Element, ptr %19, i64 %indvars.iv35
  store i32 %1, ptr %26, align 4, !tbaa !12
  %27 = getelementptr inbounds %struct.Element, ptr %19, i64 %indvars.iv35, i32 1
  store i32 1, ptr %27, align 4, !tbaa !16
  %28 = add nsw i32 %17, 1
  store i32 %28, ptr %16, align 8, !tbaa !17
  br label %.loopexit

.loopexit:                                        ; preds = %.thread, %20, %4, %._crit_edge, %.critedge, %25, %2
  ret void
}

attributes #0 = { nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) 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, !10, i64 12}
!6 = !{!"Map", !7, i64 0, !10, i64 8, !10, i64 12}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!6, !7, i64 0}
!12 = !{!13, !10, i64 0}
!13 = !{!"Element", !10, i64 0, !10, i64 4}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!13, !10, i64 4}
!17 = !{!6, !10, i64 8}
!18 = distinct !{!18, !15}


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