; LLVM IR from DionysiosB/CodeForces :: 0800-0899 (899D-ShovelSale)

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

@.str = external hidden unnamed_addr constant [5 x i8], align 1

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i64, align 8
  %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
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #3
  %8 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %2)
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  store i64 0, ptr %3, align 8, !tbaa !5
  br label %9

9:                                                ; preds = %17, %0
  %10 = load i64, ptr %3, align 8, !tbaa !5
  %11 = mul nsw i64 10, %10
  %12 = add nsw i64 %11, 9
  %13 = load i64, ptr %2, align 8, !tbaa !5
  %14 = mul nsw i64 2, %13
  %15 = sub nsw i64 %14, 1
  %16 = icmp sle i64 %12, %15
  br i1 %16, label %17, label %21

17:                                               ; preds = %9
  %18 = load i64, ptr %3, align 8, !tbaa !5
  %19 = mul nsw i64 10, %18
  %20 = add nsw i64 %19, 9
  store i64 %20, ptr %3, align 8, !tbaa !5
  br label %9, !llvm.loop !9

21:                                               ; preds = %9
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  store i64 0, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %22 = load i64, ptr %3, align 8, !tbaa !5
  store i64 %22, ptr %5, align 8, !tbaa !5
  br label %23

23:                                               ; preds = %52, %21
  %24 = load i64, ptr %5, align 8, !tbaa !5
  %25 = load i64, ptr %2, align 8, !tbaa !5
  %26 = mul nsw i64 2, %25
  %27 = sub nsw i64 %26, 1
  %28 = icmp sle i64 %24, %27
  br i1 %28, label %30, label %29

29:                                               ; preds = %23
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  br label %57

30:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %31 = load i64, ptr %5, align 8, !tbaa !5
  %32 = load i64, ptr %2, align 8, !tbaa !5
  %33 = sub nsw i64 %31, %32
  %34 = icmp sgt i64 %33, 1
  br i1 %34, label %35, label %39

35:                                               ; preds = %30
  %36 = load i64, ptr %5, align 8, !tbaa !5
  %37 = load i64, ptr %2, align 8, !tbaa !5
  %38 = sub nsw i64 %36, %37
  br label %40

39:                                               ; preds = %30
  br label %40

40:                                               ; preds = %39, %35
  %41 = phi i64 [ %38, %35 ], [ 1, %39 ]
  store i64 %41, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %42 = load i64, ptr %5, align 8, !tbaa !5
  %43 = load i64, ptr %6, align 8, !tbaa !5
  %44 = sub nsw i64 %42, %43
  store i64 %44, ptr %7, align 8, !tbaa !5
  %45 = load i64, ptr %7, align 8, !tbaa !5
  %46 = load i64, ptr %6, align 8, !tbaa !5
  %47 = sub nsw i64 %45, %46
  %48 = add nsw i64 %47, 1
  %49 = sdiv i64 %48, 2
  %50 = load i64, ptr %4, align 8, !tbaa !5
  %51 = add nsw i64 %50, %49
  store i64 %51, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  br label %52

52:                                               ; preds = %40
  %53 = load i64, ptr %3, align 8, !tbaa !5
  %54 = add nsw i64 %53, 1
  %55 = load i64, ptr %5, align 8, !tbaa !5
  %56 = add nsw i64 %55, %54
  store i64 %56, ptr %5, align 8, !tbaa !5
  br label %23, !llvm.loop !11

57:                                               ; preds = %29
  %58 = load i64, ptr %4, align 8, !tbaa !5
  %59 = call i32 (ptr, ...) @printf(ptr noundef @.str, i64 noundef %58)
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #3
  ret i32 0
}

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

declare i32 @__isoc99_scanf(ptr noundef, ...) #2

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

declare i32 @printf(ptr noundef, ...) #2

attributes #0 = { mustprogress norecurse 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 = { "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"

@.str = external hidden unnamed_addr constant [5 x i8], align 1

; Function Attrs: mustprogress nofree norecurse nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %1) #4
  %2 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %1)
  %3 = load i64, ptr %1, align 8
  %4 = shl nsw i64 %3, 1
  br label %5

5:                                                ; preds = %5, %0
  %.0 = phi i64 [ 0, %0 ], [ %7, %5 ]
  %6 = mul nsw i64 %.0, 10
  %7 = add nsw i64 %6, 9
  %.not.not = icmp slt i64 %7, %4
  br i1 %.not.not, label %5, label %.preheader, !llvm.loop !5

.preheader:                                       ; preds = %5
  %.not.not1719 = icmp slt i64 %.0, %4
  br i1 %.not.not1719, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %.preheader
  %8 = add nsw i64 %.0, 1
  %9 = shl i64 %.0, 1
  %10 = or disjoint i64 %9, 1
  %smax = call i64 @llvm.smax.i64(i64 %4, i64 %10)
  %11 = add i64 %smax, -1
  %12 = icmp ne i64 %11, %9
  %umin.neg = sext i1 %12 to i64
  %13 = select i1 %12, i64 2, i64 1
  %14 = add i64 %11, %umin.neg
  %15 = sub i64 %14, %9
  %16 = udiv i64 %15, %8
  %17 = add i64 %13, %16
  %min.iters.check = icmp ult i64 %17, 4
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.ph

scalar.ph.preheader:                              ; preds = %middle.block, %.lr.ph
  %.01421.ph = phi i64 [ 0, %.lr.ph ], [ %36, %middle.block ]
  %.01520.ph = phi i64 [ %.0, %.lr.ph ], [ %ind.end, %middle.block ]
  br label %scalar.ph

vector.ph:                                        ; preds = %.lr.ph
  %n.vec = and i64 %17, -4
  %18 = mul i64 %n.vec, %8
  %ind.end = add i64 %.0, %18
  %.splatinsert = insertelement <2 x i64> poison, i64 %.0, i64 0
  %.splat = shufflevector <2 x i64> %.splatinsert, <2 x i64> poison, <2 x i32> zeroinitializer
  %.splatinsert24 = insertelement <2 x i64> poison, i64 %8, i64 0
  %.splat25 = shufflevector <2 x i64> %.splatinsert24, <2 x i64> poison, <2 x i32> zeroinitializer
  %19 = mul nuw <2 x i64> %.splat25, <i64 0, i64 1>
  %induction = add <2 x i64> %.splat, %19
  %20 = shl i64 %8, 1
  %.splatinsert26 = insertelement <2 x i64> poison, i64 %20, i64 0
  %.splat27 = shufflevector <2 x i64> %.splatinsert26, <2 x i64> poison, <2 x i32> zeroinitializer
  %broadcast.splatinsert = insertelement <2 x i64> poison, i64 %3, i64 0
  %broadcast.splat = shufflevector <2 x i64> %broadcast.splatinsert, <2 x i64> poison, <2 x i32> zeroinitializer
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <2 x i64> [ zeroinitializer, %vector.ph ], [ %33, %vector.body ]
  %vec.phi23 = phi <2 x i64> [ zeroinitializer, %vector.ph ], [ %34, %vector.body ]
  %vec.ind = phi <2 x i64> [ %induction, %vector.ph ], [ %vec.ind.next, %vector.body ]
  %step.add = add <2 x i64> %vec.ind, %.splat27
  %21 = sub nsw <2 x i64> %vec.ind, %broadcast.splat
  %22 = sub nsw <2 x i64> %step.add, %broadcast.splat
  %23 = call <2 x i64> @llvm.smax.v2i64(<2 x i64> %21, <2 x i64> <i64 1, i64 1>)
  %24 = call <2 x i64> @llvm.smax.v2i64(<2 x i64> %22, <2 x i64> <i64 1, i64 1>)
  %25 = add nsw <2 x i64> %vec.ind, <i64 1, i64 1>
  %26 = add nsw <2 x i64> %step.add, <i64 1, i64 1>
  %27 = shl nuw <2 x i64> %23, <i64 1, i64 1>
  %28 = shl nuw <2 x i64> %24, <i64 1, i64 1>
  %29 = sub <2 x i64> %25, %27
  %30 = sub <2 x i64> %26, %28
  %31 = sdiv <2 x i64> %29, <i64 2, i64 2>
  %32 = sdiv <2 x i64> %30, <i64 2, i64 2>
  %33 = add <2 x i64> %31, %vec.phi
  %34 = add <2 x i64> %32, %vec.phi23
  %index.next = add nuw i64 %index, 4
  %vec.ind.next = add <2 x i64> %step.add, %.splat27
  %35 = icmp eq i64 %index.next, %n.vec
  br i1 %35, label %middle.block, label %vector.body, !llvm.loop !7

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <2 x i64> %34, %33
  %36 = call i64 @llvm.vector.reduce.add.v2i64(<2 x i64> %bin.rdx)
  %cmp.n = icmp eq i64 %17, %n.vec
  br i1 %cmp.n, label %._crit_edge, label %scalar.ph.preheader

._crit_edge:                                      ; preds = %scalar.ph, %middle.block, %.preheader
  %.014.lcssa = phi i64 [ 0, %.preheader ], [ %36, %middle.block ], [ %44, %scalar.ph ]
  %37 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i64 noundef %.014.lcssa)
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %1) #4
  ret i32 0

scalar.ph:                                        ; preds = %scalar.ph.preheader, %scalar.ph
  %.01421 = phi i64 [ %44, %scalar.ph ], [ %.01421.ph, %scalar.ph.preheader ]
  %.01520 = phi i64 [ %45, %scalar.ph ], [ %.01520.ph, %scalar.ph.preheader ]
  %38 = sub nsw i64 %.01520, %3
  %39 = call i64 @llvm.smax.i64(i64 %38, i64 1)
  %40 = add nsw i64 %.01520, 1
  %41 = shl nuw i64 %39, 1
  %42 = sub i64 %40, %41
  %43 = sdiv i64 %42, 2
  %44 = add nsw i64 %43, %.01421
  %45 = add nsw i64 %8, %.01520
  %.not.not17 = icmp slt i64 %45, %4
  br i1 %.not.not17, label %scalar.ph, label %._crit_edge, !llvm.loop !10
}

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

; Function Attrs: nofree nounwind
declare noundef i32 @__isoc99_scanf(ptr nocapture noundef readonly, ...) local_unnamed_addr #2

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

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.smax.i64(i64, i64) #3

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare <2 x i64> @llvm.smax.v2i64(<2 x i64>, <2 x i64>) #3

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.vector.reduce.add.v2i64(<2 x i64>) #3

attributes #0 = { mustprogress nofree norecurse 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 memory(argmem: readwrite) }
attributes #2 = { nofree nounwind "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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = distinct !{!7, !6, !8, !9}
!8 = !{!"llvm.loop.isvectorized", i32 1}
!9 = !{!"llvm.loop.unroll.runtime.disable"}
!10 = distinct !{!10, !6, !9, !8}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalDCEPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass