; LLVM IR from AlgoGenesis/C :: Dynamic Programming/Domino and Tromino Tiling (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 i32 @numTilings(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  %7 = load i32, ptr %3, align 4, !tbaa !5
  %8 = icmp eq i32 %7, 0
  br i1 %8, label %9, label %10

9:                                                ; preds = %1
  store i32 1, ptr %2, align 4
  br label %65

10:                                               ; preds = %1
  %11 = load i32, ptr %3, align 4, !tbaa !5
  %12 = icmp eq i32 %11, 1
  br i1 %12, label %13, label %14

13:                                               ; preds = %10
  store i32 1, ptr %2, align 4
  br label %65

14:                                               ; preds = %10
  %15 = load i32, ptr %3, align 4, !tbaa !5
  %16 = icmp eq i32 %15, 2
  br i1 %16, label %17, label %18

17:                                               ; preds = %14
  store i32 2, ptr %2, align 4
  br label %65

18:                                               ; preds = %14
  %19 = load i32, ptr %3, align 4, !tbaa !5
  %20 = add nsw i32 %19, 1
  %21 = zext i32 %20 to i64
  %22 = call ptr @llvm.stacksave.p0()
  store ptr %22, ptr %4, align 8
  %23 = alloca i64, i64 %21, align 16
  store i64 %21, ptr %5, align 8
  %24 = getelementptr inbounds i64, ptr %23, i64 0
  store i64 1, ptr %24, align 16, !tbaa !9
  %25 = getelementptr inbounds i64, ptr %23, i64 1
  store i64 1, ptr %25, align 8, !tbaa !9
  %26 = getelementptr inbounds i64, ptr %23, i64 2
  store i64 2, ptr %26, align 16, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 3, ptr %6, align 4, !tbaa !5
  br label %27

27:                                               ; preds = %55, %18
  %28 = load i32, ptr %6, align 4, !tbaa !5
  %29 = load i32, ptr %3, align 4, !tbaa !5
  %30 = icmp sle i32 %28, %29
  br i1 %30, label %32, label %31

31:                                               ; preds = %27
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  br label %58

32:                                               ; preds = %27
  %33 = load i32, ptr %6, align 4, !tbaa !5
  %34 = sub nsw i32 %33, 1
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds i64, ptr %23, i64 %35
  %37 = load i64, ptr %36, align 8, !tbaa !9
  %38 = load i32, ptr %6, align 4, !tbaa !5
  %39 = sub nsw i32 %38, 2
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds i64, ptr %23, i64 %40
  %42 = load i64, ptr %41, align 8, !tbaa !9
  %43 = add nsw i64 %37, %42
  %44 = load i32, ptr %6, align 4, !tbaa !5
  %45 = sub nsw i32 %44, 3
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds i64, ptr %23, i64 %46
  %48 = load i64, ptr %47, align 8, !tbaa !9
  %49 = mul nsw i64 2, %48
  %50 = add nsw i64 %43, %49
  %51 = srem i64 %50, 1000000007
  %52 = load i32, ptr %6, align 4, !tbaa !5
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds i64, ptr %23, i64 %53
  store i64 %51, ptr %54, align 8, !tbaa !9
  br label %55

55:                                               ; preds = %32
  %56 = load i32, ptr %6, align 4, !tbaa !5
  %57 = add nsw i32 %56, 1
  store i32 %57, ptr %6, align 4, !tbaa !5
  br label %27, !llvm.loop !11

58:                                               ; preds = %31
  %59 = load i32, ptr %3, align 4, !tbaa !5
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds i64, ptr %23, i64 %60
  %62 = load i64, ptr %61, align 8, !tbaa !9
  %63 = trunc i64 %62 to i32
  store i32 %63, ptr %2, align 4
  %64 = load ptr, ptr %4, align 8
  call void @llvm.stackrestore.p0(ptr %64)
  br label %65

65:                                               ; preds = %58, %17, %13, %9
  %66 = load i32, ptr %2, align 4
  ret i32 %66
}

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #1

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

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

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #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 }
attributes #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long long", !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"

@switch.table.numTilings = private unnamed_addr constant [3 x i32] [i32 1, i32 1, i32 2], align 4

; Function Attrs: nofree norecurse nosync nounwind uwtable
define dso_local i32 @numTilings(i32 noundef %0) local_unnamed_addr #0 {
  %2 = icmp ult i32 %0, 3
  br i1 %2, label %switch.lookup, label %3

3:                                                ; preds = %1
  %4 = add i32 %0, 1
  %5 = zext i32 %4 to i64
  %6 = tail call ptr @llvm.stacksave.p0()
  %7 = alloca i64, i64 %5, align 16
  store i64 1, ptr %7, align 16, !tbaa !5
  %8 = getelementptr inbounds i8, ptr %7, i64 8
  store i64 1, ptr %8, align 8, !tbaa !5
  %9 = getelementptr inbounds i8, ptr %7, i64 16
  store i64 2, ptr %9, align 16, !tbaa !5
  %.not15 = icmp slt i32 %0, 3
  br i1 %.not15, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %3
  %scevgep = getelementptr i8, ptr %7, i64 16
  %load_initial = load i64, ptr %scevgep, align 16
  %10 = add nsw i64 %5, -3
  %xtraiter = and i64 %10, 1
  %11 = icmp eq i32 %4, 4
  br i1 %11, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

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

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.lr.ph, %.lr.ph.preheader
  %store_forwarded.unr = phi i64 [ %load_initial, %.lr.ph.preheader ], [ %41, %.lr.ph ]
  %indvars.iv.unr = phi i64 [ 3, %.lr.ph.preheader ], [ %indvars.iv.next.1, %.lr.ph ]
  %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
  %12 = getelementptr i64, ptr %7, i64 %indvars.iv.unr
  %13 = getelementptr i8, ptr %12, i64 -16
  %14 = load i64, ptr %13, align 8, !tbaa !5
  %15 = add nsw i64 %14, %store_forwarded.unr
  %16 = getelementptr i8, ptr %12, i64 -24
  %17 = load i64, ptr %16, align 8, !tbaa !5
  %18 = shl nsw i64 %17, 1
  %19 = add nsw i64 %15, %18
  %20 = srem i64 %19, 1000000007
  store i64 %20, ptr %12, align 8, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph.epil, %._crit_edge.loopexit.unr-lcssa, %3
  %21 = sext i32 %0 to i64
  %22 = getelementptr inbounds i64, ptr %7, i64 %21
  %23 = load i64, ptr %22, align 8, !tbaa !5
  %24 = trunc i64 %23 to i32
  tail call void @llvm.stackrestore.p0(ptr %6)
  br label %43

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %store_forwarded = phi i64 [ %load_initial, %.lr.ph.preheader.new ], [ %41, %.lr.ph ]
  %indvars.iv = phi i64 [ 3, %.lr.ph.preheader.new ], [ %indvars.iv.next.1, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %.lr.ph ]
  %25 = getelementptr i64, ptr %7, i64 %indvars.iv
  %26 = getelementptr i8, ptr %25, i64 -16
  %27 = load i64, ptr %26, align 8, !tbaa !5
  %28 = add nsw i64 %27, %store_forwarded
  %29 = getelementptr i8, ptr %25, i64 -24
  %30 = load i64, ptr %29, align 8, !tbaa !5
  %31 = shl nsw i64 %30, 1
  %32 = add nsw i64 %28, %31
  %33 = srem i64 %32, 1000000007
  store i64 %33, ptr %25, align 8, !tbaa !5
  %34 = getelementptr i64, ptr %7, i64 %indvars.iv
  %35 = getelementptr i8, ptr %34, i64 8
  %36 = getelementptr i8, ptr %34, i64 -8
  %37 = load i64, ptr %36, align 8, !tbaa !5
  %38 = add nsw i64 %37, %33
  %39 = shl nsw i64 %27, 1
  %40 = add nsw i64 %38, %39
  %41 = srem i64 %40, 1000000007
  store i64 %41, ptr %35, align 8, !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 %.lr.ph, !llvm.loop !9

switch.lookup:                                    ; preds = %1
  %42 = zext nneg i32 %0 to i64
  %switch.gep = getelementptr inbounds [3 x i32], ptr @switch.table.numTilings, i64 0, i64 %42
  %switch.load = load i32, ptr %switch.gep, align 4
  br label %43

43:                                               ; preds = %switch.lookup, %._crit_edge
  %.013 = phi i32 [ %24, %._crit_edge ], [ %switch.load, %switch.lookup ]
  ret i32 %.013
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #1

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #1

attributes #0 = { nofree norecurse nosync 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 nocallback nofree nosync nounwind willreturn }

!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 long", !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,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass