; LLVM IR from upupming/algorithm :: kick-start/2021/RoundA/B. L Shaped Plots.ll (Z4calci)

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

@r = external dso_local global i32, align 4
@c = external dso_local global i32, align 4

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

10:                                               ; preds = %62, %1
  %11 = load i32, ptr %4, align 4, !tbaa !5
  %12 = load i32, ptr @r, align 4, !tbaa !5
  %13 = icmp sle i32 %11, %12
  br i1 %13, label %15, label %14

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

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

16:                                               ; preds = %58, %15
  %17 = load i32, ptr %6, align 4, !tbaa !5
  %18 = load i32, ptr @c, align 4, !tbaa !5
  %19 = icmp sle i32 %17, %18
  br i1 %19, label %21, label %20

20:                                               ; preds = %16
  store i32 5, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  br label %61

21:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  store i32 0, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  %22 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) @r, ptr noundef nonnull align 4 dereferenceable(4) @c)
  %23 = load i32, ptr %22, align 4, !tbaa !5
  store i32 %23, ptr %8, align 4, !tbaa !5
  br label %24

24:                                               ; preds = %44, %21
  %25 = load i32, ptr %7, align 4, !tbaa !5
  %26 = load i32, ptr %8, align 4, !tbaa !5
  %27 = icmp slt i32 %25, %26
  br i1 %27, label %28, label %45

28:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  %29 = load i32, ptr %7, align 4, !tbaa !5
  %30 = load i32, ptr %8, align 4, !tbaa !5
  %31 = add nsw i32 %29, %30
  %32 = add nsw i32 %31, 1
  %33 = ashr i32 %32, 1
  store i32 %33, ptr %9, align 4, !tbaa !5
  %34 = load i32, ptr %4, align 4, !tbaa !5
  %35 = load i32, ptr %6, align 4, !tbaa !5
  %36 = load i32, ptr %9, align 4, !tbaa !5
  %37 = load i32, ptr %2, align 4, !tbaa !5
  %38 = call noundef zeroext i1 @_Z5validiiii(i32 noundef %34, i32 noundef %35, i32 noundef %36, i32 noundef %37)
  br i1 %38, label %39, label %41

39:                                               ; preds = %28
  %40 = load i32, ptr %9, align 4, !tbaa !5
  store i32 %40, ptr %7, align 4, !tbaa !5
  br label %44

41:                                               ; preds = %28
  %42 = load i32, ptr %9, align 4, !tbaa !5
  %43 = sub nsw i32 %42, 1
  store i32 %43, ptr %8, align 4, !tbaa !5
  br label %44

44:                                               ; preds = %41, %39
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  br label %24, !llvm.loop !11

45:                                               ; preds = %24
  %46 = load i32, ptr %7, align 4, !tbaa !5
  %47 = icmp slt i32 %46, 2
  br i1 %47, label %48, label %49

48:                                               ; preds = %45
  store i32 7, ptr %5, align 4
  br label %55

49:                                               ; preds = %45
  %50 = load i32, ptr %7, align 4, !tbaa !5
  %51 = sub nsw i32 %50, 1
  %52 = sext i32 %51 to i64
  %53 = load i64, ptr %3, align 8, !tbaa !9
  %54 = add nsw i64 %53, %52
  store i64 %54, ptr %3, align 8, !tbaa !9
  store i32 0, ptr %5, align 4
  br label %55

55:                                               ; preds = %49, %48
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  %56 = load i32, ptr %5, align 4
  switch i32 %56, label %67 [
    i32 0, label %57
    i32 7, label %58
  ]

57:                                               ; preds = %55
  br label %58

58:                                               ; preds = %57, %55
  %59 = load i32, ptr %6, align 4, !tbaa !5
  %60 = add nsw i32 %59, 1
  store i32 %60, ptr %6, align 4, !tbaa !5
  br label %16, !llvm.loop !13

61:                                               ; preds = %20
  br label %62

62:                                               ; preds = %61
  %63 = load i32, ptr %4, align 4, !tbaa !5
  %64 = add nsw i32 %63, 1
  store i32 %64, ptr %4, align 4, !tbaa !5
  br label %10, !llvm.loop !14

65:                                               ; preds = %14
  %66 = load i64, ptr %3, align 8, !tbaa !9
  store i32 1, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  ret i64 %66

67:                                               ; preds = %55
  unreachable
}

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

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { 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 #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 = !{!10, !10, i64 0}
!10 = !{!"long long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}
!14 = distinct !{!14, !12}


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

@r = external dso_local global i32, align 4
@c = external dso_local global i32, align 4

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_Z5validiiii(i32 noundef, i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z4calci(i32 noundef %0) local_unnamed_addr #0 {
  %2 = load i32, ptr @r, align 4, !tbaa !5
  %.not33 = icmp slt i32 %2, 1
  %3 = load i32, ptr @c, align 4
  %4 = icmp slt i32 %3, 1
  %or.cond = select i1 %.not33, i1 true, i1 %4
  br i1 %or.cond, label %._crit_edge36, label %.preheader

.preheader:                                       ; preds = %1, %._crit_edge31
  %5 = phi i32 [ %7, %._crit_edge31 ], [ %2, %1 ]
  %6 = phi i32 [ %8, %._crit_edge31 ], [ %3, %1 ]
  %.035 = phi i64 [ %.1.lcssa, %._crit_edge31 ], [ 0, %1 ]
  %.02334 = phi i32 [ %9, %._crit_edge31 ], [ 1, %1 ]
  %.not2427 = icmp slt i32 %6, 1
  br i1 %.not2427, label %._crit_edge31, label %.lr.ph30

._crit_edge36:                                    ; preds = %._crit_edge31, %1
  %.0.lcssa = phi i64 [ 0, %1 ], [ %.1.lcssa, %._crit_edge31 ]
  ret i64 %.0.lcssa

._crit_edge31.loopexit:                           ; preds = %21
  %.pre = load i32, ptr @r, align 4, !tbaa !5
  br label %._crit_edge31

._crit_edge31:                                    ; preds = %._crit_edge31.loopexit, %.preheader
  %7 = phi i32 [ %5, %.preheader ], [ %.pre, %._crit_edge31.loopexit ]
  %8 = phi i32 [ %6, %.preheader ], [ %25, %._crit_edge31.loopexit ]
  %.1.lcssa = phi i64 [ %.035, %.preheader ], [ %.2, %._crit_edge31.loopexit ]
  %9 = add nuw nsw i32 %.02334, 1
  %.not.not = icmp slt i32 %.02334, %7
  br i1 %.not.not, label %.preheader, label %._crit_edge36, !llvm.loop !9

.lr.ph30:                                         ; preds = %.preheader, %21
  %.129 = phi i64 [ %.2, %21 ], [ %.035, %.preheader ]
  %.02228 = phi i32 [ %24, %21 ], [ 1, %.preheader ]
  %10 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) @r, ptr noundef nonnull align 4 dereferenceable(4) @c)
  %11 = load i32, ptr %10, align 4, !tbaa !5
  %12 = icmp sgt i32 %11, 0
  br i1 %12, label %.lr.ph, label %._crit_edge.thread

.lr.ph:                                           ; preds = %.lr.ph30, %.lr.ph
  %.01826 = phi i32 [ %.119, %.lr.ph ], [ %11, %.lr.ph30 ]
  %.02025 = phi i32 [ %.121, %.lr.ph ], [ 0, %.lr.ph30 ]
  %13 = add nuw nsw i32 %.02025, 1
  %14 = add nuw nsw i32 %13, %.01826
  %15 = lshr i32 %14, 1
  %16 = tail call noundef zeroext i1 @_Z5validiiii(i32 noundef %.02334, i32 noundef %.02228, i32 noundef %15, i32 noundef %0)
  %17 = add nsw i32 %15, -1
  %.121 = select i1 %16, i32 %15, i32 %.02025
  %.119 = select i1 %16, i32 %.01826, i32 %17
  %18 = icmp slt i32 %.121, %.119
  br i1 %18, label %.lr.ph, label %._crit_edge, !llvm.loop !12

._crit_edge:                                      ; preds = %.lr.ph
  %19 = icmp slt i32 %.121, 2
  %20 = add nsw i32 %.121, -1
  br i1 %19, label %._crit_edge.thread, label %21

._crit_edge.thread:                               ; preds = %.lr.ph30, %._crit_edge
  br label %21

21:                                               ; preds = %._crit_edge, %._crit_edge.thread
  %22 = phi i32 [ 0, %._crit_edge.thread ], [ %20, %._crit_edge ]
  %23 = zext i32 %22 to i64
  %.2 = add nsw i64 %.129, %23
  %24 = add nuw nsw i32 %.02228, 1
  %25 = load i32, ptr @c, align 4, !tbaa !5
  %.not24.not = icmp slt i32 %.02228, %25
  br i1 %.not24.not, label %.lr.ph30, label %._crit_edge31.loopexit, !llvm.loop !13
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_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, !11}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!"llvm.loop.unswitch.partial.disable"}
!12 = distinct !{!12, !10}
!13 = distinct !{!13, !10}


; Effective O3 passes: CorrelatedValuePropagationPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimpleLoopUnswitchPass,SimplifyCFGPass,TailCallElimPass