; LLVM IR from ahr9n/psolving-paradigms :: 4.dynamic-programming/13.tiling-(counting)/CodeForces 431C - k-Tree.ll (Z2dpib)

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

@k = external dso_local global i32, align 4
@least = external dso_local global i32, align 4
@memo = external dso_local global [105 x [2 x i32]], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z2dpib(i32 noundef %0, i1 noundef zeroext %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i8, align 1
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  %9 = zext i1 %1 to i8
  store i8 %9, ptr %5, align 1, !tbaa !9
  %10 = load i32, ptr %4, align 4, !tbaa !5
  %11 = icmp eq i32 %10, 0
  br i1 %11, label %12, label %16

12:                                               ; preds = %2
  %13 = load i8, ptr %5, align 1, !tbaa !9, !range !11, !noundef !12
  %14 = trunc i8 %13 to i1
  %15 = zext i1 %14 to i32
  store i32 %15, ptr %3, align 4
  br label %76

16:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %17 = load i32, ptr %4, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [105 x [2 x i32]], ptr @memo, i64 0, i64 %18
  %20 = load i8, ptr %5, align 1, !tbaa !9, !range !11, !noundef !12
  %21 = trunc i8 %20 to i1
  %22 = zext i1 %21 to i64
  %23 = getelementptr inbounds [2 x i32], ptr %19, i64 0, i64 %22
  store ptr %23, ptr %6, align 8, !tbaa !13
  %24 = load ptr, ptr %6, align 8, !tbaa !13
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = xor i32 %25, -1
  %27 = icmp ne i32 %26, 0
  br i1 %27, label %28, label %31

28:                                               ; preds = %16
  %29 = load ptr, ptr %6, align 8, !tbaa !13
  %30 = load i32, ptr %29, align 4, !tbaa !5
  store i32 %30, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %75

31:                                               ; preds = %16
  %32 = load ptr, ptr %6, align 8, !tbaa !13
  store i32 0, ptr %32, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  store i32 1, ptr %8, align 4, !tbaa !5
  br label %33

33:                                               ; preds = %68, %31
  %34 = load i32, ptr %8, align 4, !tbaa !5
  %35 = load i32, ptr @k, align 4, !tbaa !5
  %36 = icmp sle i32 %34, %35
  br i1 %36, label %38, label %37

37:                                               ; preds = %33
  store i32 2, ptr %7, align 4
  br label %71

38:                                               ; preds = %33
  %39 = load i32, ptr %8, align 4, !tbaa !5
  %40 = load i32, ptr %4, align 4, !tbaa !5
  %41 = icmp sgt i32 %39, %40
  br i1 %41, label %42, label %43

42:                                               ; preds = %38
  store i32 2, ptr %7, align 4
  br label %71

43:                                               ; preds = %38
  %44 = load i32, ptr %4, align 4, !tbaa !5
  %45 = load i32, ptr %8, align 4, !tbaa !5
  %46 = sub nsw i32 %44, %45
  %47 = load i8, ptr %5, align 1, !tbaa !9, !range !11, !noundef !12
  %48 = trunc i8 %47 to i1
  %49 = zext i1 %48 to i32
  %50 = load i32, ptr %8, align 4, !tbaa !5
  %51 = load i32, ptr @least, align 4, !tbaa !5
  %52 = icmp sge i32 %50, %51
  %53 = zext i1 %52 to i32
  %54 = or i32 %49, %53
  %55 = icmp ne i32 %54, 0
  %56 = call noundef i32 @_Z2dpib(i32 noundef %46, i1 noundef zeroext %55)
  %57 = load ptr, ptr %6, align 8, !tbaa !13
  %58 = load i32, ptr %57, align 4, !tbaa !5
  %59 = add nsw i32 %58, %56
  store i32 %59, ptr %57, align 4, !tbaa !5
  %60 = load ptr, ptr %6, align 8, !tbaa !13
  %61 = load i32, ptr %60, align 4, !tbaa !5
  %62 = icmp sge i32 %61, 1000000007
  br i1 %62, label %63, label %67

63:                                               ; preds = %43
  %64 = load ptr, ptr %6, align 8, !tbaa !13
  %65 = load i32, ptr %64, align 4, !tbaa !5
  %66 = sub nsw i32 %65, 1000000007
  store i32 %66, ptr %64, align 4, !tbaa !5
  br label %67

67:                                               ; preds = %63, %43
  br label %68

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

71:                                               ; preds = %42, %37
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  br label %72

72:                                               ; preds = %71
  %73 = load ptr, ptr %6, align 8, !tbaa !13
  %74 = load i32, ptr %73, align 4, !tbaa !5
  store i32 %74, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %75

75:                                               ; preds = %72, %28
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  br label %76

76:                                               ; preds = %75, %12
  %77 = load i32, ptr %3, align 4
  ret i32 %77
}

; 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

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 = { 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 = !{!10, !10, i64 0}
!10 = !{!"bool", !7, i64 0}
!11 = !{i8 0, i8 2}
!12 = !{}
!13 = !{!14, !14, i64 0}
!14 = !{!"any pointer", !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"

@k = external dso_local local_unnamed_addr global i32, align 4
@least = external dso_local local_unnamed_addr global i32, align 4
@memo = external dso_local local_unnamed_addr global [105 x [2 x i32]], align 16

; Function Attrs: mustprogress nofree nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_Z2dpib(i32 noundef %0, i1 noundef zeroext %1) local_unnamed_addr #0 {
  %3 = icmp eq i32 %0, 0
  br i1 %3, label %4, label %6

4:                                                ; preds = %2
  %5 = zext i1 %1 to i32
  br label %._crit_edge

6:                                                ; preds = %2
  %7 = sext i32 %0 to i64
  %8 = zext i1 %1 to i64
  %9 = getelementptr inbounds [105 x [2 x i32]], ptr @memo, i64 0, i64 %7, i64 %8
  %10 = load i32, ptr %9, align 4, !tbaa !5
  %.not = icmp eq i32 %10, -1
  br i1 %.not, label %11, label %._crit_edge

11:                                               ; preds = %6
  store i32 0, ptr %9, align 4, !tbaa !5
  %12 = load i32, ptr @k, align 4, !tbaa !5
  %.not2223 = icmp slt i32 %12, 1
  %13 = icmp slt i32 %0, 1
  %or.cond24 = or i1 %13, %.not2223
  br i1 %or.cond24, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %11, %.lr.ph
  %.025 = phi i32 [ %23, %.lr.ph ], [ 1, %11 ]
  %14 = sub nsw i32 %0, %.025
  %15 = load i32, ptr @least, align 4, !tbaa !5
  %16 = icmp sge i32 %.025, %15
  %17 = or i1 %16, %1
  %18 = tail call noundef i32 @_Z2dpib(i32 noundef %14, i1 noundef zeroext %17)
  %19 = load i32, ptr %9, align 4, !tbaa !5
  %20 = add nsw i32 %19, %18
  %21 = icmp sgt i32 %20, 1000000006
  %22 = add nsw i32 %20, -1000000007
  %spec.select = select i1 %21, i32 %22, i32 %20
  store i32 %spec.select, ptr %9, align 4, !tbaa !5
  %23 = add nuw nsw i32 %.025, 1
  %24 = load i32, ptr @k, align 4, !tbaa !5
  %.not22 = icmp sge i32 %.025, %24
  %25 = icmp sge i32 %.025, %0
  %or.cond = or i1 %25, %.not22
  br i1 %or.cond, label %._crit_edge, label %.lr.ph, !llvm.loop !9

._crit_edge:                                      ; preds = %.lr.ph, %11, %6, %4
  %.1 = phi i32 [ %5, %4 ], [ %10, %6 ], [ 0, %11 ], [ %spec.select, %.lr.ph ]
  ret i32 %.1
}

attributes #0 = { mustprogress nofree nosync nounwind memory(readwrite, argmem: none, 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, !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"}


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