; LLVM IR from andreimaximov/algorithms :: codeforces/xenia-and-tree/main.ll (Z10preprocessv)

; 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
@DP = external dso_local global [100000 x [20 x i32]], align 16

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z6levelsiii(i32 noundef, i32 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: mustprogress uwtable
define dso_local void @_Z10preprocessv() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  call void @_Z6levelsiii(i32 noundef 0, i32 noundef -1, i32 noundef 0)
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #4
  store i32 1, ptr %1, align 4, !tbaa !5
  br label %5

5:                                                ; preds = %53, %0
  %6 = load i32, ptr %1, align 4, !tbaa !5
  %7 = sitofp i32 %6 to double
  %8 = load i32, ptr @N, align 4, !tbaa !5
  %9 = call noundef double @_ZSt4log2IiEN9__gnu_cxx11__enable_ifIXsr12__is_integerIT_EE7__valueEdE6__typeES2_(i32 noundef %8)
  %10 = call double @llvm.ceil.f64(double %9)
  %11 = fadd double %10, 1.000000e+00
  %12 = fcmp olt double %7, %11
  br i1 %12, label %14, label %13

13:                                               ; preds = %5
  store i32 2, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #4
  br label %56

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

15:                                               ; preds = %49, %14
  %16 = load i32, ptr %3, align 4, !tbaa !5
  %17 = load i32, ptr @N, align 4, !tbaa !5
  %18 = icmp slt i32 %16, %17
  br i1 %18, label %20, label %19

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

20:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  %21 = load i32, ptr %3, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [100000 x [20 x i32]], ptr @DP, i64 0, i64 %22
  %24 = load i32, ptr %1, align 4, !tbaa !5
  %25 = sub nsw i32 %24, 1
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [20 x i32], ptr %23, i64 0, i64 %26
  %28 = load i32, ptr %27, align 4, !tbaa !5
  store i32 %28, ptr %4, align 4, !tbaa !5
  %29 = load i32, ptr %4, align 4, !tbaa !5
  %30 = icmp ne i32 %29, -1
  br i1 %30, label %31, label %40

31:                                               ; preds = %20
  %32 = load i32, ptr %4, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [100000 x [20 x i32]], ptr @DP, i64 0, i64 %33
  %35 = load i32, ptr %1, align 4, !tbaa !5
  %36 = sub nsw i32 %35, 1
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [20 x i32], ptr %34, i64 0, i64 %37
  %39 = load i32, ptr %38, align 4, !tbaa !5
  br label %41

40:                                               ; preds = %20
  br label %41

41:                                               ; preds = %40, %31
  %42 = phi i32 [ %39, %31 ], [ -1, %40 ]
  %43 = load i32, ptr %3, align 4, !tbaa !5
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds [100000 x [20 x i32]], ptr @DP, i64 0, i64 %44
  %46 = load i32, ptr %1, align 4, !tbaa !5
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds [20 x i32], ptr %45, i64 0, i64 %47
  store i32 %42, ptr %48, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  br label %49

49:                                               ; preds = %41
  %50 = load i32, ptr %3, align 4, !tbaa !5
  %51 = add nsw i32 %50, 1
  store i32 %51, ptr %3, align 4, !tbaa !5
  br label %15, !llvm.loop !9

52:                                               ; preds = %19
  br label %53

53:                                               ; preds = %52
  %54 = load i32, ptr %1, align 4, !tbaa !5
  %55 = add nsw i32 %54, 1
  store i32 %55, ptr %1, align 4, !tbaa !5
  br label %5, !llvm.loop !11

56:                                               ; preds = %13
  ret void
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef double @_ZSt4log2IiEN9__gnu_cxx11__enable_ifIXsr12__is_integerIT_EE7__valueEdE6__typeES2_(i32 noundef) #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.ceil.f64(double) #3

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 = { 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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { 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}


; 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
@DP = external dso_local local_unnamed_addr global [100000 x [20 x i32]], align 16

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z6levelsiii(i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local void @_Z10preprocessv() local_unnamed_addr #0 {
  tail call void @_Z6levelsiii(i32 noundef 0, i32 noundef -1, i32 noundef 0)
  %1 = load i32, ptr @N, align 4, !tbaa !5
  %2 = tail call noundef double @_ZSt4log2IiEN9__gnu_cxx11__enable_ifIXsr12__is_integerIT_EE7__valueEdE6__typeES2_(i32 noundef %1)
  %3 = tail call double @llvm.ceil.f64(double %2)
  %4 = fadd double %3, 1.000000e+00
  %5 = fcmp ogt double %4, 1.000000e+00
  br i1 %5, label %.preheader, label %._crit_edge15

.preheader:                                       ; preds = %0, %._crit_edge
  %indvars.iv17 = phi i64 [ %indvars.iv.next18, %._crit_edge ], [ 1, %0 ]
  %6 = load i32, ptr @N, align 4, !tbaa !5
  %7 = icmp sgt i32 %6, 0
  br i1 %7, label %.lr.ph, label %._crit_edge

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

.lr.ph.new:                                       ; preds = %.lr.ph
  %unroll_iter = and i64 %wide.trip.count, 2147483646
  br label %24

._crit_edge15:                                    ; preds = %._crit_edge, %0
  ret void

._crit_edge.loopexit.unr-lcssa:                   ; preds = %40, %.lr.ph
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next.1, %40 ]
  %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
  %10 = getelementptr inbounds [100000 x [20 x i32]], ptr @DP, i64 0, i64 %indvars.iv.unr, i64 %8
  %11 = load i32, ptr %10, align 4, !tbaa !5
  %.not.epil = icmp eq i32 %11, -1
  br i1 %.not.epil, label %._crit_edge.loopexit.epilog-lcssa, label %12

12:                                               ; preds = %.epil.preheader
  %13 = sext i32 %11 to i64
  %14 = getelementptr inbounds [100000 x [20 x i32]], ptr @DP, i64 0, i64 %13, i64 %8
  %15 = load i32, ptr %14, align 4, !tbaa !5
  br label %._crit_edge.loopexit.epilog-lcssa

._crit_edge.loopexit.epilog-lcssa:                ; preds = %12, %.epil.preheader
  %16 = phi i32 [ %15, %12 ], [ -1, %.epil.preheader ]
  %17 = getelementptr inbounds [100000 x [20 x i32]], ptr @DP, i64 0, i64 %indvars.iv.unr, i64 %indvars.iv17
  store i32 %16, ptr %17, align 4, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit.epilog-lcssa, %._crit_edge.loopexit.unr-lcssa, %.preheader
  %indvars.iv.next18 = add nuw nsw i64 %indvars.iv17, 1
  %18 = trunc nsw i64 %indvars.iv.next18 to i32
  %19 = uitofp nneg i32 %18 to double
  %20 = tail call noundef double @_ZSt4log2IiEN9__gnu_cxx11__enable_ifIXsr12__is_integerIT_EE7__valueEdE6__typeES2_(i32 noundef %6)
  %21 = tail call double @llvm.ceil.f64(double %20)
  %22 = fadd double %21, 1.000000e+00
  %23 = fcmp ogt double %22, %19
  br i1 %23, label %.preheader, label %._crit_edge15, !llvm.loop !9

24:                                               ; preds = %40, %.lr.ph.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.new ], [ %indvars.iv.next.1, %40 ]
  %niter = phi i64 [ 0, %.lr.ph.new ], [ %niter.next.1, %40 ]
  %25 = getelementptr inbounds [100000 x [20 x i32]], ptr @DP, i64 0, i64 %indvars.iv, i64 %8
  %26 = load i32, ptr %25, align 4, !tbaa !5
  %.not = icmp eq i32 %26, -1
  br i1 %.not, label %31, label %27

27:                                               ; preds = %24
  %28 = sext i32 %26 to i64
  %29 = getelementptr inbounds [100000 x [20 x i32]], ptr @DP, i64 0, i64 %28, i64 %8
  %30 = load i32, ptr %29, align 4, !tbaa !5
  br label %31

31:                                               ; preds = %24, %27
  %32 = phi i32 [ %30, %27 ], [ -1, %24 ]
  %33 = getelementptr inbounds [100000 x [20 x i32]], ptr @DP, i64 0, i64 %indvars.iv, i64 %indvars.iv17
  store i32 %32, ptr %33, align 4, !tbaa !5
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %34 = getelementptr inbounds [100000 x [20 x i32]], ptr @DP, i64 0, i64 %indvars.iv.next, i64 %8
  %35 = load i32, ptr %34, align 4, !tbaa !5
  %.not.1 = icmp eq i32 %35, -1
  br i1 %.not.1, label %40, label %36

36:                                               ; preds = %31
  %37 = sext i32 %35 to i64
  %38 = getelementptr inbounds [100000 x [20 x i32]], ptr @DP, i64 0, i64 %37, i64 %8
  %39 = load i32, ptr %38, align 4, !tbaa !5
  br label %40

40:                                               ; preds = %36, %31
  %41 = phi i32 [ %39, %36 ], [ -1, %31 ]
  %42 = getelementptr inbounds [100000 x [20 x i32]], ptr @DP, i64 0, i64 %indvars.iv.next, i64 %indvars.iv17
  store i32 %41, ptr %42, align 4, !tbaa !5
  %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 !11
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef double @_ZSt4log2IiEN9__gnu_cxx11__enable_ifIXsr12__is_integerIT_EE7__valueEdE6__typeES2_(i32 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.ceil.f64(double) #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 = { 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 #2 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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}


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