; LLVM IR from ncduy0303/Competitive-Programming :: Data Structures/Sparse Table.ll (Z18build_sparse_tablev)

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

@n = external dso_local global i32, align 4
@arr = external dso_local global [200001 x i32], align 16
@dp = external dso_local global [200001 x [18 x i32]], align 16

; Function Attrs: mustprogress uwtable
define dso_local void @_Z18build_sparse_tablev() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  store i32 1, ptr %1, align 4, !tbaa !5
  br label %5

5:                                                ; preds = %19, %0
  %6 = load i32, ptr %1, align 4, !tbaa !5
  %7 = load i32, ptr @n, align 4, !tbaa !5
  %8 = icmp sle i32 %6, %7
  br i1 %8, label %10, label %9

9:                                                ; preds = %5
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  br label %22

10:                                               ; preds = %5
  %11 = load i32, ptr %1, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [200001 x i32], ptr @arr, i64 0, i64 %12
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %15 = load i32, ptr %1, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [200001 x [18 x i32]], ptr @dp, i64 0, i64 %16
  %18 = getelementptr inbounds [18 x i32], ptr %17, i64 0, i64 0
  store i32 %14, ptr %18, align 8, !tbaa !5
  br label %19

19:                                               ; preds = %10
  %20 = load i32, ptr %1, align 4, !tbaa !5
  %21 = add nsw i32 %20, 1
  store i32 %21, ptr %1, align 4, !tbaa !5
  br label %5, !llvm.loop !9

22:                                               ; preds = %9
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  store i32 1, ptr %2, align 4, !tbaa !5
  br label %23

23:                                               ; preds = %68, %22
  %24 = load i32, ptr %2, align 4, !tbaa !5
  %25 = icmp slt i32 %24, 18
  br i1 %25, label %27, label %26

26:                                               ; preds = %23
  store i32 5, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  br label %71

27:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  store i32 1, ptr %4, align 4, !tbaa !5
  br label %28

28:                                               ; preds = %64, %27
  %29 = load i32, ptr %4, align 4, !tbaa !5
  %30 = load i32, ptr %2, align 4, !tbaa !5
  %31 = shl i32 1, %30
  %32 = add nsw i32 %29, %31
  %33 = load i32, ptr @n, align 4, !tbaa !5
  %34 = add nsw i32 %33, 1
  %35 = icmp sle i32 %32, %34
  br i1 %35, label %37, label %36

36:                                               ; preds = %28
  store i32 8, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  br label %67

37:                                               ; preds = %28
  %38 = load i32, ptr %4, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [200001 x [18 x i32]], ptr @dp, i64 0, i64 %39
  %41 = load i32, ptr %2, align 4, !tbaa !5
  %42 = sub nsw i32 %41, 1
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds [18 x i32], ptr %40, i64 0, i64 %43
  %45 = load i32, ptr %4, align 4, !tbaa !5
  %46 = load i32, ptr %2, align 4, !tbaa !5
  %47 = sub nsw i32 %46, 1
  %48 = shl i32 1, %47
  %49 = add nsw i32 %45, %48
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds [200001 x [18 x i32]], ptr @dp, i64 0, i64 %50
  %52 = load i32, ptr %2, align 4, !tbaa !5
  %53 = sub nsw i32 %52, 1
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds [18 x i32], ptr %51, i64 0, i64 %54
  %56 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %44, ptr noundef nonnull align 4 dereferenceable(4) %55)
  %57 = load i32, ptr %56, align 4, !tbaa !5
  %58 = load i32, ptr %4, align 4, !tbaa !5
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds [200001 x [18 x i32]], ptr @dp, i64 0, i64 %59
  %61 = load i32, ptr %2, align 4, !tbaa !5
  %62 = sext i32 %61 to i64
  %63 = getelementptr inbounds [18 x i32], ptr %60, i64 0, i64 %62
  store i32 %57, ptr %63, align 4, !tbaa !5
  br label %64

64:                                               ; preds = %37
  %65 = load i32, ptr %4, align 4, !tbaa !5
  %66 = add nsw i32 %65, 1
  store i32 %66, ptr %4, align 4, !tbaa !5
  br label %28, !llvm.loop !11

67:                                               ; preds = %36
  br label %68

68:                                               ; preds = %67
  %69 = load i32, ptr %2, align 4, !tbaa !5
  %70 = add nsw i32 %69, 1
  store i32 %70, ptr %2, align 4, !tbaa !5
  br label %23, !llvm.loop !12

71:                                               ; preds = %26
  ret void
}

; 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: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) #2

attributes #0 = { mustprogress 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 = { inlinehint mustprogress 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 #3 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}


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

@n = external dso_local local_unnamed_addr global i32, align 4
@arr = external dso_local local_unnamed_addr global [200001 x i32], align 16
@dp = external dso_local global [200001 x [18 x i32]], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z18build_sparse_tablev() local_unnamed_addr #0 {
  %1 = load i32, ptr @n, align 4, !tbaa !5
  %.not20 = icmp slt i32 %1, 1
  br i1 %.not20, label %.preheader.preheader, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %0
  %2 = zext nneg i32 %1 to i64
  %xtraiter = and i64 %2, 3
  %3 = icmp ult i32 %1, 4
  br i1 %3, label %.preheader.preheader.loopexit.unr-lcssa, label %.lr.ph.preheader.new

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

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %indvars.iv = phi i64 [ 1, %.lr.ph.preheader.new ], [ %indvars.iv.next.3, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.3, %.lr.ph ]
  %4 = getelementptr inbounds [200001 x i32], ptr @arr, i64 0, i64 %indvars.iv
  %5 = load i32, ptr %4, align 4, !tbaa !5
  %6 = getelementptr inbounds [200001 x [18 x i32]], ptr @dp, i64 0, i64 %indvars.iv
  store i32 %5, ptr %6, align 8, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %7 = getelementptr inbounds [200001 x i32], ptr @arr, i64 0, i64 %indvars.iv.next
  %8 = load i32, ptr %7, align 4, !tbaa !5
  %9 = getelementptr inbounds [200001 x [18 x i32]], ptr @dp, i64 0, i64 %indvars.iv.next
  store i32 %8, ptr %9, align 8, !tbaa !5
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %10 = getelementptr inbounds [200001 x i32], ptr @arr, i64 0, i64 %indvars.iv.next.1
  %11 = load i32, ptr %10, align 4, !tbaa !5
  %12 = getelementptr inbounds [200001 x [18 x i32]], ptr @dp, i64 0, i64 %indvars.iv.next.1
  store i32 %11, ptr %12, align 8, !tbaa !5
  %indvars.iv.next.2 = add nuw nsw i64 %indvars.iv, 3
  %13 = getelementptr inbounds [200001 x i32], ptr @arr, i64 0, i64 %indvars.iv.next.2
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %15 = getelementptr inbounds [200001 x [18 x i32]], ptr @dp, i64 0, i64 %indvars.iv.next.2
  store i32 %14, ptr %15, align 8, !tbaa !5
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %niter.next.3 = add i64 %niter, 4
  %niter.ncmp.3 = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %.preheader.preheader.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !9

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

.lr.ph.epil:                                      ; preds = %.preheader.preheader.loopexit.unr-lcssa, %.lr.ph.epil
  %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %.lr.ph.epil ], [ %indvars.iv.unr, %.preheader.preheader.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph.epil ], [ 0, %.preheader.preheader.loopexit.unr-lcssa ]
  %16 = getelementptr inbounds [200001 x i32], ptr @arr, i64 0, i64 %indvars.iv.epil
  %17 = load i32, ptr %16, align 4, !tbaa !5
  %18 = getelementptr inbounds [200001 x [18 x i32]], ptr @dp, i64 0, i64 %indvars.iv.epil
  store i32 %17, ptr %18, align 8, !tbaa !5
  %indvars.iv.next.epil = add nuw nsw i64 %indvars.iv.epil, 1
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %.preheader.preheader, label %.lr.ph.epil, !llvm.loop !11

.preheader.preheader:                             ; preds = %.preheader.preheader.loopexit.unr-lcssa, %.lr.ph.epil, %0
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %._crit_edge
  %19 = phi i32 [ %27, %._crit_edge ], [ %1, %.preheader.preheader ]
  %indvars.iv30 = phi i64 [ %indvars.iv.next31, %._crit_edge ], [ 1, %.preheader.preheader ]
  %20 = trunc nuw nsw i64 %indvars.iv30 to i32
  %21 = shl nuw nsw i32 1, %20
  %.not1822 = icmp sgt i32 %21, %19
  br i1 %.not1822, label %._crit_edge, label %.lr.ph24

.lr.ph24:                                         ; preds = %.preheader
  %22 = add nsw i64 %indvars.iv30, -1
  %23 = trunc nuw nsw i64 %22 to i32
  %24 = shl nuw nsw i32 1, %23
  %25 = zext nneg i32 %24 to i64
  br label %28

26:                                               ; preds = %._crit_edge
  ret void

._crit_edge:                                      ; preds = %28, %.preheader
  %27 = phi i32 [ %19, %.preheader ], [ %35, %28 ]
  %indvars.iv.next31 = add nuw nsw i64 %indvars.iv30, 1
  %exitcond33.not = icmp eq i64 %indvars.iv.next31, 18
  br i1 %exitcond33.not, label %26, label %.preheader, !llvm.loop !13

28:                                               ; preds = %.lr.ph24, %28
  %indvars.iv27 = phi i64 [ 1, %.lr.ph24 ], [ %indvars.iv.next28, %28 ]
  %29 = getelementptr inbounds [200001 x [18 x i32]], ptr @dp, i64 0, i64 %indvars.iv27, i64 %22
  %30 = add nuw nsw i64 %indvars.iv27, %25
  %31 = getelementptr inbounds [200001 x [18 x i32]], ptr @dp, i64 0, i64 %30, i64 %22
  %32 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %29, ptr noundef nonnull align 4 dereferenceable(4) %31)
  %33 = load i32, ptr %32, align 4, !tbaa !5
  %34 = getelementptr inbounds [200001 x [18 x i32]], ptr @dp, i64 0, i64 %indvars.iv27, i64 %indvars.iv30
  store i32 %33, ptr %34, align 4, !tbaa !5
  %indvars.iv.next28 = add nuw nsw i64 %indvars.iv27, 1
  %35 = load i32, ptr @n, align 4, !tbaa !5
  %36 = add nsw i32 %35, 1
  %37 = trunc i64 %indvars.iv.next28 to i32
  %38 = add i32 %21, %37
  %.not18 = icmp sgt i32 %38, %36
  br i1 %.not18, label %._crit_edge, label %28, !llvm.loop !14
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #1

attributes #0 = { mustprogress 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 = { inlinehint mustprogress 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++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.unroll.disable"}
!13 = distinct !{!13, !10}
!14 = distinct !{!14, !10}


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