; LLVM IR from ravireddy07/cpp-book :: src/Google-competitions/KickStart/2020-H-Rugby.ll (Z8ternaryXxxx)

; 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: mustprogress uwtable
declare dso_local noundef i64 @_Z4calcxx(i64 noundef, i64 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 @_Z8ternaryXxxx(i64 noundef %0, i64 noundef %1, i64 noundef %2) #0 {
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  store i64 %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !5
  store i64 %2, ptr %6, align 8, !tbaa !5
  br label %12

12:                                               ; preds = %39, %3
  %13 = load i64, ptr %4, align 8, !tbaa !5
  %14 = load i64, ptr %5, align 8, !tbaa !5
  %15 = sub nsw i64 %14, 5
  %16 = icmp slt i64 %13, %15
  br i1 %16, label %17, label %40

17:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %18 = load i64, ptr %4, align 8, !tbaa !5
  %19 = mul nsw i64 %18, 2
  %20 = load i64, ptr %5, align 8, !tbaa !5
  %21 = add nsw i64 %19, %20
  %22 = sdiv i64 %21, 3
  store i64 %22, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %23 = load i64, ptr %4, align 8, !tbaa !5
  %24 = load i64, ptr %5, align 8, !tbaa !5
  %25 = mul nsw i64 %24, 2
  %26 = add nsw i64 %23, %25
  %27 = sdiv i64 %26, 3
  store i64 %27, ptr %8, align 8, !tbaa !5
  %28 = load i64, ptr %7, align 8, !tbaa !5
  %29 = load i64, ptr %6, align 8, !tbaa !5
  %30 = call noundef i64 @_Z4calcxx(i64 noundef %28, i64 noundef %29)
  %31 = load i64, ptr %8, align 8, !tbaa !5
  %32 = load i64, ptr %6, align 8, !tbaa !5
  %33 = call noundef i64 @_Z4calcxx(i64 noundef %31, i64 noundef %32)
  %34 = icmp slt i64 %30, %33
  br i1 %34, label %35, label %37

35:                                               ; preds = %17
  %36 = load i64, ptr %8, align 8, !tbaa !5
  store i64 %36, ptr %5, align 8, !tbaa !5
  br label %39

37:                                               ; preds = %17
  %38 = load i64, ptr %7, align 8, !tbaa !5
  store i64 %38, ptr %4, align 8, !tbaa !5
  br label %39

39:                                               ; preds = %37, %35
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  br label %12, !llvm.loop !9

40:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %41 = load i64, ptr %4, align 8, !tbaa !5
  %42 = load i64, ptr %6, align 8, !tbaa !5
  %43 = call noundef i64 @_Z4calcxx(i64 noundef %41, i64 noundef %42)
  store i64 %43, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %44 = load i64, ptr %4, align 8, !tbaa !5
  %45 = add nsw i64 %44, 1
  store i64 %45, ptr %10, align 8, !tbaa !5
  br label %46

46:                                               ; preds = %57, %40
  %47 = load i64, ptr %10, align 8, !tbaa !5
  %48 = load i64, ptr %5, align 8, !tbaa !5
  %49 = icmp sle i64 %47, %48
  br i1 %49, label %51, label %50

50:                                               ; preds = %46
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  br label %60

51:                                               ; preds = %46
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  %52 = load i64, ptr %10, align 8, !tbaa !5
  %53 = load i64, ptr %6, align 8, !tbaa !5
  %54 = call noundef i64 @_Z4calcxx(i64 noundef %52, i64 noundef %53)
  store i64 %54, ptr %11, align 8, !tbaa !5
  %55 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %9, ptr noundef nonnull align 8 dereferenceable(8) %11)
  %56 = load i64, ptr %55, align 8, !tbaa !5
  store i64 %56, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  br label %57

57:                                               ; preds = %51
  %58 = load i64, ptr %10, align 8, !tbaa !5
  %59 = add nsw i64 %58, 1
  store i64 %59, ptr %10, align 8, !tbaa !5
  br label %46, !llvm.loop !11

60:                                               ; preds = %50
  %61 = load i64, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  ret i64 %61
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) #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 = !{!"long long", !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"

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_Z4calcxx(i64 noundef, i64 noundef) local_unnamed_addr #0

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

; Function Attrs: mustprogress 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 @_Z8ternaryXxxx(i64 noundef %0, i64 noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = add nsw i64 %1, -5
  %7 = icmp sgt i64 %6, %0
  br i1 %7, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %3, %.lr.ph
  %.02024 = phi i64 [ %.020., %.lr.ph ], [ %0, %3 ]
  %.02123 = phi i64 [ %..021, %.lr.ph ], [ %1, %3 ]
  %8 = shl nsw i64 %.02024, 1
  %9 = add nsw i64 %8, %.02123
  %10 = sdiv i64 %9, 3
  %11 = shl nsw i64 %.02123, 1
  %12 = add nsw i64 %.02024, %11
  %13 = sdiv i64 %12, 3
  %14 = tail call noundef i64 @_Z4calcxx(i64 noundef %10, i64 noundef %2)
  %15 = tail call noundef i64 @_Z4calcxx(i64 noundef %13, i64 noundef %2)
  %16 = icmp slt i64 %14, %15
  %..021 = select i1 %16, i64 %13, i64 %.02123
  %.020. = select i1 %16, i64 %.02024, i64 %10
  %17 = add nsw i64 %..021, -5
  %18 = icmp slt i64 %.020., %17
  br i1 %18, label %.lr.ph, label %._crit_edge, !llvm.loop !5

._crit_edge:                                      ; preds = %.lr.ph, %3
  %.021.lcssa = phi i64 [ %1, %3 ], [ %..021, %.lr.ph ]
  %.020.lcssa = phi i64 [ %0, %3 ], [ %.020., %.lr.ph ]
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %4) #3
  %19 = tail call noundef i64 @_Z4calcxx(i64 noundef %.020.lcssa, i64 noundef %2)
  store i64 %19, ptr %4, align 8, !tbaa !7
  %.not.not26 = icmp slt i64 %.020.lcssa, %.021.lcssa
  br i1 %.not.not26, label %.lr.ph29, label %._crit_edge30

._crit_edge30:                                    ; preds = %.lr.ph29, %._crit_edge
  %20 = phi i64 [ %19, %._crit_edge ], [ %23, %.lr.ph29 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %4) #3
  ret i64 %20

.lr.ph29:                                         ; preds = %._crit_edge, %.lr.ph29
  %.0.in27 = phi i64 [ %.0, %.lr.ph29 ], [ %.020.lcssa, %._crit_edge ]
  %.0 = add nsw i64 %.0.in27, 1
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %5) #3
  %21 = call noundef i64 @_Z4calcxx(i64 noundef %.0, i64 noundef %2)
  store i64 %21, ptr %5, align 8, !tbaa !7
  %22 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %4, ptr noundef nonnull align 8 dereferenceable(8) %5)
  %23 = load i64, ptr %22, align 8, !tbaa !7
  store i64 %23, ptr %4, align 8, !tbaa !7
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %5) #3
  %exitcond.not = icmp eq i64 %.0, %.021.lcssa
  br i1 %exitcond.not, label %._crit_edge30, label %.lr.ph29, !llvm.loop !11
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #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 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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = !{!8, !8, i64 0}
!8 = !{!"long long", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C++ TBAA"}
!11 = distinct !{!11, !6}


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