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

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

@mxX = external dso_local global i64, align 8
@mnX = external dso_local global i64, align 8

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

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

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_Z8ternaryXxxx(i64 noundef, i64 noundef, i64 noundef) #1

; 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

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z8ternaryYxx(i64 noundef %0, i64 noundef %1) #1 {
  %3 = alloca i64, align 8
  %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
  store i64 %0, ptr %3, align 8, !tbaa !5
  store i64 %1, ptr %4, align 8, !tbaa !5
  br label %10

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

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

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

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

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

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

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

51:                                               ; preds = %47
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  br label %62

52:                                               ; preds = %47
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %53 = load i64, ptr @mnX, align 8, !tbaa !5
  %54 = load i64, ptr @mxX, align 8, !tbaa !5
  %55 = load i64, ptr %8, align 8, !tbaa !5
  %56 = call noundef i64 @_Z8ternaryXxxx(i64 noundef %53, i64 noundef %54, i64 noundef %55)
  store i64 %56, ptr %9, align 8, !tbaa !5
  %57 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %7, ptr noundef nonnull align 8 dereferenceable(8) %9)
  %58 = load i64, ptr %57, align 8, !tbaa !5
  store i64 %58, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  br label %59

59:                                               ; preds = %52
  %60 = load i64, ptr %8, align 8, !tbaa !5
  %61 = add nsw i64 %60, 1
  store i64 %61, ptr %8, align 8, !tbaa !5
  br label %47, !llvm.loop !11

62:                                               ; preds = %51
  %63 = load i64, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  ret i64 %63
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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"

@mxX = external dso_local local_unnamed_addr global i64, align 8
@mnX = external dso_local local_unnamed_addr global i64, align 8

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

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

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

; 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

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z8ternaryYxx(i64 noundef %0, i64 noundef %1) local_unnamed_addr #1 {
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = add nsw i64 %1, -5
  %6 = icmp sgt i64 %5, %0
  br i1 %6, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %2, %.lr.ph
  %.01620 = phi i64 [ %.016., %.lr.ph ], [ %0, %2 ]
  %.01719 = phi i64 [ %..017, %.lr.ph ], [ %1, %2 ]
  %7 = shl nsw i64 %.01620, 1
  %8 = add nsw i64 %7, %.01719
  %9 = sdiv i64 %8, 3
  %10 = shl nsw i64 %.01719, 1
  %11 = add nsw i64 %.01620, %10
  %12 = sdiv i64 %11, 3
  %13 = load i64, ptr @mnX, align 8, !tbaa !5
  %14 = load i64, ptr @mxX, align 8, !tbaa !5
  %15 = tail call noundef i64 @_Z8ternaryXxxx(i64 noundef %13, i64 noundef %14, i64 noundef %9)
  %16 = load i64, ptr @mnX, align 8, !tbaa !5
  %17 = load i64, ptr @mxX, align 8, !tbaa !5
  %18 = tail call noundef i64 @_Z8ternaryXxxx(i64 noundef %16, i64 noundef %17, i64 noundef %12)
  %19 = icmp slt i64 %15, %18
  %..017 = select i1 %19, i64 %12, i64 %.01719
  %.016. = select i1 %19, i64 %.01620, i64 %9
  %20 = add nsw i64 %..017, -5
  %21 = icmp slt i64 %.016., %20
  br i1 %21, label %.lr.ph, label %._crit_edge, !llvm.loop !9

._crit_edge:                                      ; preds = %.lr.ph, %2
  %.017.lcssa = phi i64 [ %1, %2 ], [ %..017, %.lr.ph ]
  %.016.lcssa = phi i64 [ %0, %2 ], [ %.016., %.lr.ph ]
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #3
  %22 = load i64, ptr @mnX, align 8, !tbaa !5
  %23 = load i64, ptr @mxX, align 8, !tbaa !5
  %24 = tail call noundef i64 @_Z8ternaryXxxx(i64 noundef %22, i64 noundef %23, i64 noundef %.016.lcssa)
  store i64 %24, ptr %3, align 8, !tbaa !5
  %.not.not22 = icmp slt i64 %.016.lcssa, %.017.lcssa
  br i1 %.not.not22, label %.lr.ph25, label %._crit_edge26

._crit_edge26:                                    ; preds = %.lr.ph25, %._crit_edge
  %25 = phi i64 [ %24, %._crit_edge ], [ %30, %.lr.ph25 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #3
  ret i64 %25

.lr.ph25:                                         ; preds = %._crit_edge, %.lr.ph25
  %.0.in23 = phi i64 [ %.0, %.lr.ph25 ], [ %.016.lcssa, %._crit_edge ]
  %.0 = add nsw i64 %.0.in23, 1
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %4) #3
  %26 = load i64, ptr @mnX, align 8, !tbaa !5
  %27 = load i64, ptr @mxX, align 8, !tbaa !5
  %28 = call noundef i64 @_Z8ternaryXxxx(i64 noundef %26, i64 noundef %27, i64 noundef %.0)
  store i64 %28, ptr %4, align 8, !tbaa !5
  %29 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %3, ptr noundef nonnull align 8 dereferenceable(8) %4)
  %30 = load i64, ptr %29, align 8, !tbaa !5
  store i64 %30, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %4) #3
  %exitcond.not = icmp eq i64 %.0, %.017.lcssa
  br i1 %exitcond.not, label %._crit_edge26, label %.lr.ph25, !llvm.loop !11
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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}


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