; LLVM IR from AdarshGaur/Competitive-Programming :: Google/Kickstart 2022/Kickstart Round A 2022/Interesting Integers.ll (Z3prev)

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

@dp = external dso_local global [100007 x i64], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z3prev() #0 {
  %1 = alloca i64, align 8
  %2 = alloca i64, align 8
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  store i64 0, ptr @dp, align 16, !tbaa !5
  store i64 1, ptr getelementptr inbounds ([100007 x i64], ptr @dp, i64 0, i64 1), align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #2
  store i64 2, ptr %1, align 8, !tbaa !5
  br label %6

6:                                                ; preds = %43, %0
  %7 = load i64, ptr %1, align 8, !tbaa !5
  %8 = icmp slt i64 %7, 100007
  br i1 %8, label %10, label %9

9:                                                ; preds = %6
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #2
  br label %46

10:                                               ; preds = %6
  %11 = load i64, ptr %1, align 8, !tbaa !5
  %12 = sub nsw i64 %11, 1
  %13 = getelementptr inbounds [100007 x i64], ptr @dp, i64 0, i64 %12
  %14 = load i64, ptr %13, align 8, !tbaa !5
  %15 = load i64, ptr %1, align 8, !tbaa !5
  %16 = getelementptr inbounds [100007 x i64], ptr @dp, i64 0, i64 %15
  store i64 %14, ptr %16, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #2
  %17 = load i64, ptr %1, align 8, !tbaa !5
  store i64 %17, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  store i64 0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  store i64 1, ptr %4, align 8, !tbaa !5
  br label %18

18:                                               ; preds = %21, %10
  %19 = load i64, ptr %2, align 8, !tbaa !5
  %20 = icmp ne i64 %19, 0
  br i1 %20, label %21, label %32

21:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %22 = load i64, ptr %2, align 8, !tbaa !5
  %23 = srem i64 %22, 10
  store i64 %23, ptr %5, align 8, !tbaa !5
  %24 = load i64, ptr %5, align 8, !tbaa !5
  %25 = load i64, ptr %3, align 8, !tbaa !5
  %26 = add nsw i64 %25, %24
  store i64 %26, ptr %3, align 8, !tbaa !5
  %27 = load i64, ptr %5, align 8, !tbaa !5
  %28 = load i64, ptr %4, align 8, !tbaa !5
  %29 = mul nsw i64 %28, %27
  store i64 %29, ptr %4, align 8, !tbaa !5
  %30 = load i64, ptr %2, align 8, !tbaa !5
  %31 = sdiv i64 %30, 10
  store i64 %31, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  br label %18, !llvm.loop !9

32:                                               ; preds = %18
  %33 = load i64, ptr %4, align 8, !tbaa !5
  %34 = load i64, ptr %3, align 8, !tbaa !5
  %35 = srem i64 %33, %34
  %36 = icmp eq i64 %35, 0
  br i1 %36, label %37, label %42

37:                                               ; preds = %32
  %38 = load i64, ptr %1, align 8, !tbaa !5
  %39 = getelementptr inbounds [100007 x i64], ptr @dp, i64 0, i64 %38
  %40 = load i64, ptr %39, align 8, !tbaa !5
  %41 = add nsw i64 %40, 1
  store i64 %41, ptr %39, align 8, !tbaa !5
  br label %42

42:                                               ; preds = %37, %32
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #2
  br label %43

43:                                               ; preds = %42
  %44 = load i64, ptr %1, align 8, !tbaa !5
  %45 = add nsw i64 %44, 1
  store i64 %45, ptr %1, align 8, !tbaa !5
  br label %6, !llvm.loop !11

46:                                               ; preds = %9
  ret void
}

; 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

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

@dp = external dso_local local_unnamed_addr global [100007 x i64], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(write, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z3prev() local_unnamed_addr #0 {
  store i64 0, ptr @dp, align 16, !tbaa !5
  store i64 1, ptr getelementptr inbounds (i8, ptr @dp, i64 8), align 8, !tbaa !5
  br label %2

1:                                                ; preds = %15
  ret void

2:                                                ; preds = %0, %15
  %3 = phi i64 [ 1, %0 ], [ %16, %15 ]
  %.021 = phi i64 [ 2, %0 ], [ %17, %15 ]
  %4 = getelementptr inbounds [100007 x i64], ptr @dp, i64 0, i64 %.021
  store i64 %3, ptr %4, align 8, !tbaa !5
  br label %5

5:                                                ; preds = %2, %5
  %.01520 = phi i64 [ 1, %2 ], [ %8, %5 ]
  %.01619 = phi i64 [ 0, %2 ], [ %7, %5 ]
  %.01718 = phi i64 [ %.021, %2 ], [ %9, %5 ]
  %6 = srem i64 %.01718, 10
  %7 = add nsw i64 %.01619, %6
  %8 = mul nsw i64 %.01520, %6
  %9 = sdiv i64 %.01718, 10
  %.01718.off = add nsw i64 %.01718, 9
  %.not = icmp ult i64 %.01718.off, 19
  br i1 %.not, label %10, label %5, !llvm.loop !9

10:                                               ; preds = %5
  %11 = srem i64 %8, %7
  %12 = icmp eq i64 %11, 0
  br i1 %12, label %13, label %15

13:                                               ; preds = %10
  %14 = add nsw i64 %3, 1
  store i64 %14, ptr %4, align 8, !tbaa !5
  br label %15

15:                                               ; preds = %13, %10
  %16 = phi i64 [ %14, %13 ], [ %3, %10 ]
  %17 = add nuw nsw i64 %.021, 1
  %exitcond.not = icmp eq i64 %17, 100007
  br i1 %exitcond.not, label %1, label %2, !llvm.loop !11
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(write, argmem: none, inaccessiblemem: none) 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 = !{!"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: BDCEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopRotatePass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass