; LLVM IR from DimitrisJim/leetcode_solutions :: C/Algorithms (637)

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

@SIZE = external hidden global i64, align 8
@counts = external hidden global ptr, align 8
@values = external hidden global ptr, align 8

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

; 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
define dso_local ptr @averageOfLevels(ptr noundef %0, ptr noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  %8 = load i64, ptr @SIZE, align 8, !tbaa !9
  %9 = call noalias ptr @calloc(i64 noundef %8, i64 noundef 8) #5
  store ptr %9, ptr @counts, align 8, !tbaa !5
  %10 = load i64, ptr @SIZE, align 8, !tbaa !9
  %11 = call noalias ptr @calloc(i64 noundef %10, i64 noundef 8) #5
  store ptr %11, ptr @values, align 8, !tbaa !5
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  call void @breadth(ptr noundef %12, i32 noundef 0)
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #6
  %13 = load i64, ptr @SIZE, align 8, !tbaa !9
  %14 = mul i64 %13, 8
  %15 = call noalias ptr @malloc(i64 noundef %14) #7
  store ptr %15, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #6
  store i32 0, ptr %6, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #6
  store i32 0, ptr %7, align 4, !tbaa !11
  br label %16

16:                                               ; preds = %49, %2
  %17 = load i32, ptr %7, align 4, !tbaa !11
  %18 = sext i32 %17 to i64
  %19 = load i64, ptr @SIZE, align 8, !tbaa !9
  %20 = icmp slt i64 %18, %19
  br i1 %20, label %22, label %21

21:                                               ; preds = %16
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #6
  br label %52

22:                                               ; preds = %16
  %23 = load ptr, ptr @counts, align 8, !tbaa !5
  %24 = load i32, ptr %7, align 4, !tbaa !11
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds i64, ptr %23, i64 %25
  %27 = load i64, ptr %26, align 8, !tbaa !9
  %28 = icmp ne i64 %27, 0
  br i1 %28, label %29, label %48

29:                                               ; preds = %22
  %30 = load ptr, ptr @values, align 8, !tbaa !5
  %31 = load i32, ptr %7, align 4, !tbaa !11
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds i64, ptr %30, i64 %32
  %34 = load i64, ptr %33, align 8, !tbaa !9
  %35 = sitofp i64 %34 to double
  %36 = load ptr, ptr @counts, align 8, !tbaa !5
  %37 = load i32, ptr %7, align 4, !tbaa !11
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds i64, ptr %36, i64 %38
  %40 = load i64, ptr %39, align 8, !tbaa !9
  %41 = sitofp i64 %40 to double
  %42 = fdiv double %35, %41
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  %44 = load i32, ptr %6, align 4, !tbaa !11
  %45 = add nsw i32 %44, 1
  store i32 %45, ptr %6, align 4, !tbaa !11
  %46 = sext i32 %44 to i64
  %47 = getelementptr inbounds double, ptr %43, i64 %46
  store double %42, ptr %47, align 8, !tbaa !13
  br label %48

48:                                               ; preds = %29, %22
  br label %49

49:                                               ; preds = %48
  %50 = load i32, ptr %7, align 4, !tbaa !11
  %51 = add nsw i32 %50, 1
  store i32 %51, ptr %7, align 4, !tbaa !11
  br label %16, !llvm.loop !15

52:                                               ; preds = %21
  %53 = load ptr, ptr @counts, align 8, !tbaa !5
  call void @free(ptr noundef %53) #6
  %54 = load ptr, ptr @values, align 8, !tbaa !5
  call void @free(ptr noundef %54) #6
  %55 = load i32, ptr %6, align 4, !tbaa !11
  %56 = load ptr, ptr %4, align 8, !tbaa !5
  store i32 %55, ptr %56, align 4, !tbaa !11
  %57 = load ptr, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #6
  ret ptr %57
}

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

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

; Function Attrs: nounwind
declare void @free(ptr noundef) #4

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,1) "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 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 #4 = { nounwind "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 #5 = { nounwind allocsize(0,1) }
attributes #6 = { nounwind }
attributes #7 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"double", !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"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"

@SIZE = external hidden local_unnamed_addr global i64, align 8
@counts = external hidden local_unnamed_addr global ptr, align 8
@values = external hidden local_unnamed_addr global ptr, align 8

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

; Function Attrs: nounwind uwtable
define dso_local noalias noundef ptr @averageOfLevels(ptr noundef %0, ptr nocapture noundef writeonly %1) local_unnamed_addr #0 {
  %3 = load i64, ptr @SIZE, align 8, !tbaa !5
  %4 = tail call noalias ptr @calloc(i64 noundef %3, i64 noundef 8) #4
  store ptr %4, ptr @counts, align 8, !tbaa !9
  %5 = tail call noalias ptr @calloc(i64 noundef %3, i64 noundef 8) #4
  store ptr %5, ptr @values, align 8, !tbaa !9
  tail call void @breadth(ptr noundef %0, i32 noundef 0)
  %6 = load i64, ptr @SIZE, align 8, !tbaa !5
  %7 = shl i64 %6, 3
  %8 = tail call noalias ptr @malloc(i64 noundef %7) #5
  %9 = icmp sgt i64 %6, 0
  %.pre = load ptr, ptr @counts, align 8, !tbaa !9
  br i1 %9, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %2
  %10 = load ptr, ptr @values, align 8
  %xtraiter = and i64 %6, 1
  %11 = icmp eq i64 %6, 1
  br i1 %11, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.new

.lr.ph.new:                                       ; preds = %.lr.ph
  %unroll_iter = and i64 %6, 9223372036854775806
  br label %24

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

.epil.preheader:                                  ; preds = %._crit_edge.loopexit.unr-lcssa
  %12 = getelementptr inbounds i64, ptr %.pre, i64 %indvars.iv.unr
  %13 = load i64, ptr %12, align 8, !tbaa !5
  %.not.epil = icmp eq i64 %13, 0
  br i1 %.not.epil, label %._crit_edge, label %14

14:                                               ; preds = %.epil.preheader
  %15 = getelementptr inbounds i64, ptr %10, i64 %indvars.iv.unr
  %16 = load i64, ptr %15, align 8, !tbaa !5
  %17 = sitofp i64 %16 to double
  %18 = sitofp i64 %13 to double
  %19 = fdiv double %17, %18
  %20 = add nsw i32 %.01013.unr, 1
  %21 = sext i32 %.01013.unr to i64
  %22 = getelementptr inbounds double, ptr %8, i64 %21
  store double %19, ptr %22, align 8, !tbaa !11
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %14, %.epil.preheader, %2
  %.010.lcssa = phi i32 [ 0, %2 ], [ %.1.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %20, %14 ], [ %.01013.unr, %.epil.preheader ]
  tail call void @free(ptr noundef %.pre) #6
  %23 = load ptr, ptr @values, align 8, !tbaa !9
  tail call void @free(ptr noundef %23) #6
  store i32 %.010.lcssa, ptr %1, align 4, !tbaa !13
  ret ptr %8

24:                                               ; preds = %48, %.lr.ph.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.new ], [ %indvars.iv.next.1, %48 ]
  %.01013 = phi i32 [ 0, %.lr.ph.new ], [ %.1.1, %48 ]
  %niter = phi i64 [ 0, %.lr.ph.new ], [ %niter.next.1, %48 ]
  %25 = getelementptr inbounds i64, ptr %.pre, i64 %indvars.iv
  %26 = load i64, ptr %25, align 8, !tbaa !5
  %.not = icmp eq i64 %26, 0
  br i1 %.not, label %36, label %27

27:                                               ; preds = %24
  %28 = getelementptr inbounds i64, ptr %10, i64 %indvars.iv
  %29 = load i64, ptr %28, align 8, !tbaa !5
  %30 = sitofp i64 %29 to double
  %31 = sitofp i64 %26 to double
  %32 = fdiv double %30, %31
  %33 = add nsw i32 %.01013, 1
  %34 = sext i32 %.01013 to i64
  %35 = getelementptr inbounds double, ptr %8, i64 %34
  store double %32, ptr %35, align 8, !tbaa !11
  br label %36

36:                                               ; preds = %24, %27
  %.1 = phi i32 [ %33, %27 ], [ %.01013, %24 ]
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %37 = getelementptr inbounds i64, ptr %.pre, i64 %indvars.iv.next
  %38 = load i64, ptr %37, align 8, !tbaa !5
  %.not.1 = icmp eq i64 %38, 0
  br i1 %.not.1, label %48, label %39

39:                                               ; preds = %36
  %40 = getelementptr inbounds i64, ptr %10, i64 %indvars.iv.next
  %41 = load i64, ptr %40, align 8, !tbaa !5
  %42 = sitofp i64 %41 to double
  %43 = sitofp i64 %38 to double
  %44 = fdiv double %42, %43
  %45 = add nsw i32 %.1, 1
  %46 = sext i32 %.1 to i64
  %47 = getelementptr inbounds double, ptr %8, i64 %46
  store double %44, ptr %47, align 8, !tbaa !11
  br label %48

48:                                               ; preds = %39, %36
  %.1.1 = phi i32 [ %45, %39 ], [ %.1, %36 ]
  %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 %24, !llvm.loop !15
}

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

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

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #3

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 = { mustprogress nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) 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 = { 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 #3 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, 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 #4 = { nounwind allocsize(0,1) }
attributes #5 = { nounwind allocsize(0) }
attributes #6 = { 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 = !{!"long", !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 = !{!12, !12, i64 0}
!12 = !{!"double", !7, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"int", !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


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