; LLVM IR from bqi343/cp-notebook :: Contests/USACO Solutions/2008-09/toys.ll (Z7ternaryv)

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

@D = external dso_local global i32, align 4

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_Z4testi(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 i32 @_Z7ternaryv() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  store i32 1, ptr %1, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  %11 = load i32, ptr @D, align 4, !tbaa !5
  %12 = mul nsw i32 50, %11
  store i32 %12, ptr %2, align 4, !tbaa !5
  br label %13

13:                                               ; preds = %51, %49, %0
  %14 = load i32, ptr %1, align 4, !tbaa !5
  %15 = add nsw i32 %14, 2
  %16 = load i32, ptr %2, align 4, !tbaa !5
  %17 = icmp slt i32 %15, %16
  br i1 %17, label %18, label %52

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

35:                                               ; preds = %18
  %36 = load i32, ptr %3, align 4, !tbaa !5
  %37 = add nsw i32 %36, 1
  store i32 %37, ptr %1, align 4, !tbaa !5
  store i32 2, ptr %7, align 4
  br label %49, !llvm.loop !11

38:                                               ; preds = %18
  %39 = load i64, ptr %5, align 8, !tbaa !9
  %40 = load i64, ptr %6, align 8, !tbaa !9
  %41 = icmp slt i64 %39, %40
  br i1 %41, label %42, label %45

42:                                               ; preds = %38
  %43 = load i32, ptr %4, align 4, !tbaa !5
  %44 = sub nsw i32 %43, 1
  store i32 %44, ptr %2, align 4, !tbaa !5
  br label %48

45:                                               ; preds = %38
  %46 = load i32, ptr %3, align 4, !tbaa !5
  %47 = add nsw i32 %46, 1
  store i32 %47, ptr %1, align 4, !tbaa !5
  br label %48

48:                                               ; preds = %45, %42
  store i32 0, ptr %7, align 4
  br label %49

49:                                               ; preds = %48, %35
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  %50 = load i32, ptr %7, align 4
  switch i32 %50, label %65 [
    i32 0, label %51
    i32 2, label %13
  ]

51:                                               ; preds = %49
  br label %13, !llvm.loop !11

52:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %53 = load i32, ptr %1, align 4, !tbaa !5
  %54 = call noundef i64 @_Z4testi(i32 noundef %53)
  store i64 %54, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %55 = load i32, ptr %1, align 4, !tbaa !5
  %56 = add nsw i32 %55, 1
  %57 = call noundef i64 @_Z4testi(i32 noundef %56)
  store i64 %57, ptr %9, align 8, !tbaa !9
  %58 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %8, ptr noundef nonnull align 8 dereferenceable(8) %9)
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %59 = load i32, ptr %1, align 4, !tbaa !5
  %60 = add nsw i32 %59, 2
  %61 = call noundef i64 @_Z4testi(i32 noundef %60)
  store i64 %61, ptr %10, align 8, !tbaa !9
  %62 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %58, ptr noundef nonnull align 8 dereferenceable(8) %10)
  %63 = load i64, ptr %62, align 8, !tbaa !9
  %64 = trunc i64 %63 to i32
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  store i32 1, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  ret i32 %64

65:                                               ; preds = %49
  unreachable
}

; 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 = !{!"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"}


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

@D = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_Z4testi(i32 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 i32 @_Z7ternaryv() local_unnamed_addr #0 {
  %1 = alloca i64, align 8
  %2 = alloca i64, align 8
  %3 = alloca i64, align 8
  %4 = load i32, ptr @D, align 4, !tbaa !5
  %5 = icmp sgt i32 %4, 0
  br i1 %5, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %0
  %6 = mul nuw nsw i32 %4, 50
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %22
  %.023 = phi i32 [ %.2, %22 ], [ 1, %.lr.ph.preheader ]
  %.01722 = phi i32 [ %.219, %22 ], [ %6, %.lr.ph.preheader ]
  %7 = shl nuw nsw i32 %.023, 1
  %8 = add nuw nsw i32 %7, %.01722
  %9 = udiv i32 %8, 3
  %10 = shl nuw nsw i32 %.01722, 1
  %11 = add nuw nsw i32 %.023, %10
  %12 = udiv i32 %11, 3
  %13 = tail call noundef i64 @_Z4testi(i32 noundef %9)
  %14 = tail call noundef i64 @_Z4testi(i32 noundef %12)
  %15 = icmp eq i64 %13, 2000000014
  br i1 %15, label %16, label %18

16:                                               ; preds = %.lr.ph
  %17 = add nuw nsw i32 %9, 1
  br label %22, !llvm.loop !9

18:                                               ; preds = %.lr.ph
  %19 = icmp slt i64 %13, %14
  %20 = add nsw i32 %12, -1
  %21 = add nuw nsw i32 %9, 1
  %.118 = select i1 %19, i32 %20, i32 %.01722
  %.1 = select i1 %19, i32 %.023, i32 %21
  br label %22

22:                                               ; preds = %18, %16
  %.219 = phi i32 [ %.01722, %16 ], [ %.118, %18 ]
  %.2 = phi i32 [ %17, %16 ], [ %.1, %18 ]
  %23 = add nuw nsw i32 %.2, 2
  %24 = icmp slt i32 %23, %.219
  br i1 %24, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %22, %0
  %.0.lcssa = phi i32 [ 1, %0 ], [ %.2, %22 ]
  %.lcssa = phi i32 [ 3, %0 ], [ %23, %22 ]
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %1) #3
  %25 = tail call noundef i64 @_Z4testi(i32 noundef %.0.lcssa)
  store i64 %25, ptr %1, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #3
  %26 = add nuw nsw i32 %.0.lcssa, 1
  %27 = tail call noundef i64 @_Z4testi(i32 noundef %26)
  store i64 %27, ptr %2, align 8, !tbaa !11
  %28 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %1, ptr noundef nonnull align 8 dereferenceable(8) %2)
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #3
  %29 = call noundef i64 @_Z4testi(i32 noundef %.lcssa)
  store i64 %29, ptr %3, align 8, !tbaa !11
  %30 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %28, ptr noundef nonnull align 8 dereferenceable(8) %3)
  %31 = load i64, ptr %30, align 8, !tbaa !11
  %32 = trunc i64 %31 to i32
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %1) #3
  ret i32 %32
}

; 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 = !{!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"}
!11 = !{!12, !12, i64 0}
!12 = !{!"long long", !7, i64 0}


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