; LLVM IR from LeetCode-in-C/LeetCode-in-C :: src/main/c/g0301_0400/s0338_counting_bits (Solution)

; 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 @countBits(i32 noundef %0, ptr noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %9 = load i32, ptr %3, align 4, !tbaa !5
  %10 = add nsw i32 %9, 1
  %11 = sext i32 %10 to i64
  %12 = mul i64 %11, 4
  %13 = call noalias ptr @malloc(i64 noundef %12) #4
  store ptr %13, ptr %5, align 8, !tbaa !9
  %14 = load i32, ptr %3, align 4, !tbaa !5
  %15 = add nsw i32 %14, 1
  %16 = load ptr, ptr %4, align 8, !tbaa !9
  store i32 %15, ptr %16, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 1, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 1, ptr %7, align 4, !tbaa !5
  %17 = load ptr, ptr %5, align 8, !tbaa !9
  %18 = getelementptr inbounds i32, ptr %17, i64 0
  store i32 0, ptr %18, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 1, ptr %8, align 4, !tbaa !5
  br label %19

19:                                               ; preds = %47, %2
  %20 = load i32, ptr %8, align 4, !tbaa !5
  %21 = load i32, ptr %3, align 4, !tbaa !5
  %22 = icmp sle i32 %20, %21
  br i1 %22, label %24, label %23

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

24:                                               ; preds = %19
  %25 = load i32, ptr %7, align 4, !tbaa !5
  %26 = load i32, ptr %6, align 4, !tbaa !5
  %27 = icmp eq i32 %25, %26
  br i1 %27, label %28, label %34

28:                                               ; preds = %24
  %29 = load ptr, ptr %5, align 8, !tbaa !9
  %30 = load i32, ptr %8, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds i32, ptr %29, i64 %31
  store i32 1, ptr %32, align 4, !tbaa !5
  store i32 1, ptr %7, align 4, !tbaa !5
  %33 = load i32, ptr %8, align 4, !tbaa !5
  store i32 %33, ptr %6, align 4, !tbaa !5
  br label %46

34:                                               ; preds = %24
  %35 = load ptr, ptr %5, align 8, !tbaa !9
  %36 = load i32, ptr %7, align 4, !tbaa !5
  %37 = add nsw i32 %36, 1
  store i32 %37, ptr %7, align 4, !tbaa !5
  %38 = sext i32 %36 to i64
  %39 = getelementptr inbounds i32, ptr %35, i64 %38
  %40 = load i32, ptr %39, align 4, !tbaa !5
  %41 = add nsw i32 1, %40
  %42 = load ptr, ptr %5, align 8, !tbaa !9
  %43 = load i32, ptr %8, align 4, !tbaa !5
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds i32, ptr %42, i64 %44
  store i32 %41, ptr %45, align 4, !tbaa !5
  br label %46

46:                                               ; preds = %34, %28
  br label %47

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

50:                                               ; preds = %23
  %51 = load ptr, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  ret ptr %51
}

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

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

; 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 allocsize(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 #3 = { nounwind }
attributes #4 = { nounwind allocsize(0) }

!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 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !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: nofree nounwind memory(readwrite, argmem: write) uwtable
define dso_local noalias noundef ptr @countBits(i32 noundef %0, ptr nocapture noundef writeonly %1) local_unnamed_addr #0 {
  %3 = add i32 %0, 1
  %4 = sext i32 %3 to i64
  %5 = shl nsw i64 %4, 2
  %6 = tail call noalias ptr @malloc(i64 noundef %5) #2
  store i32 %3, ptr %1, align 4, !tbaa !5
  store i32 0, ptr %6, align 4, !tbaa !5
  %.not19 = icmp slt i32 %0, 1
  br i1 %.not19, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %2
  %wide.trip.count = zext i32 %3 to i64
  %7 = add nsw i64 %wide.trip.count, -1
  %xtraiter = and i64 %7, 1
  %8 = icmp eq i32 %3, 2
  br i1 %8, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %7, -2
  br label %.lr.ph

._crit_edge.loopexit.unr-lcssa:                   ; preds = %35, %.lr.ph.preheader
  %indvars.iv.unr = phi i64 [ 1, %.lr.ph.preheader ], [ %indvars.iv.next.1, %35 ]
  %.01621.unr = phi i32 [ 1, %.lr.ph.preheader ], [ %.1.1, %35 ]
  %.01720.unr = phi i32 [ 1, %.lr.ph.preheader ], [ %.118.1, %35 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa
  %9 = icmp eq i32 %.01621.unr, %.01720.unr
  br i1 %9, label %._crit_edge.loopexit.epilog-lcssa, label %10

10:                                               ; preds = %.lr.ph.epil
  %11 = sext i32 %.01621.unr to i64
  %12 = getelementptr inbounds i32, ptr %6, i64 %11
  %13 = load i32, ptr %12, align 4, !tbaa !5
  %14 = add nsw i32 %13, 1
  br label %._crit_edge.loopexit.epilog-lcssa

._crit_edge.loopexit.epilog-lcssa:                ; preds = %.lr.ph.epil, %10
  %.sink.epil = phi i32 [ %14, %10 ], [ 1, %.lr.ph.epil ]
  %15 = getelementptr inbounds i32, ptr %6, i64 %indvars.iv.unr
  store i32 %.sink.epil, ptr %15, align 4
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit.epilog-lcssa, %._crit_edge.loopexit.unr-lcssa, %2
  ret ptr %6

.lr.ph:                                           ; preds = %35, %.lr.ph.preheader.new
  %indvars.iv = phi i64 [ 1, %.lr.ph.preheader.new ], [ %indvars.iv.next.1, %35 ]
  %.01621 = phi i32 [ 1, %.lr.ph.preheader.new ], [ %.1.1, %35 ]
  %.01720 = phi i32 [ 1, %.lr.ph.preheader.new ], [ %.118.1, %35 ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %35 ]
  %16 = icmp eq i32 %.01621, %.01720
  br i1 %16, label %17, label %19

17:                                               ; preds = %.lr.ph
  %18 = trunc nuw nsw i64 %indvars.iv to i32
  br label %.lr.ph.1

19:                                               ; preds = %.lr.ph
  %20 = add nsw i32 %.01621, 1
  %21 = sext i32 %.01621 to i64
  %22 = getelementptr inbounds i32, ptr %6, i64 %21
  %23 = load i32, ptr %22, align 4, !tbaa !5
  %24 = add nsw i32 %23, 1
  br label %.lr.ph.1

.lr.ph.1:                                         ; preds = %17, %19
  %.sink = phi i32 [ 1, %17 ], [ %24, %19 ]
  %.118 = phi i32 [ %18, %17 ], [ %.01720, %19 ]
  %.1 = phi i32 [ 1, %17 ], [ %20, %19 ]
  %25 = getelementptr inbounds i32, ptr %6, i64 %indvars.iv
  store i32 %.sink, ptr %25, align 4
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %26 = icmp eq i32 %.1, %.118
  br i1 %26, label %33, label %27

27:                                               ; preds = %.lr.ph.1
  %28 = add nsw i32 %.1, 1
  %29 = sext i32 %.1 to i64
  %30 = getelementptr inbounds i32, ptr %6, i64 %29
  %31 = load i32, ptr %30, align 4, !tbaa !5
  %32 = add nsw i32 %31, 1
  br label %35

33:                                               ; preds = %.lr.ph.1
  %34 = trunc nuw nsw i64 %indvars.iv.next to i32
  br label %35

35:                                               ; preds = %33, %27
  %.sink.1 = phi i32 [ 1, %33 ], [ %32, %27 ]
  %.118.1 = phi i32 [ %34, %33 ], [ %.118, %27 ]
  %.1.1 = phi i32 [ 1, %33 ], [ %28, %27 ]
  %36 = getelementptr inbounds i32, ptr %6, i64 %indvars.iv.next
  store i32 %.sink.1, ptr %36, align 4
  %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 %._crit_edge.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !9
}

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #1

attributes #0 = { nofree nounwind memory(readwrite, argmem: write) 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 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 allocsize(0) }

!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: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,MergedLoadStoreMotionPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass