; LLVM IR from ritikbanger/Hacktoberfest2022-DSA :: 06. Sorting/collecting_numbers.ll (Z7nCrModpxx)

; 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: 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 nounwind uwtable
declare dso_local noundef i64 @_Z3minxx(i64 noundef, i64 noundef) #1

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z7nCrModpxx(i64 noundef %0, i64 noundef %1) #1 {
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  %9 = alloca i64, align 8
  store i64 %0, ptr %3, align 8, !tbaa !5
  store i64 %1, ptr %4, align 8, !tbaa !5
  %10 = load i64, ptr %4, align 8, !tbaa !5
  %11 = load i64, ptr %3, align 8, !tbaa !5
  %12 = load i64, ptr %4, align 8, !tbaa !5
  %13 = sub nsw i64 %11, %12
  %14 = icmp sgt i64 %10, %13
  br i1 %14, label %15, label %19

15:                                               ; preds = %2
  %16 = load i64, ptr %3, align 8, !tbaa !5
  %17 = load i64, ptr %4, align 8, !tbaa !5
  %18 = sub nsw i64 %16, %17
  store i64 %18, ptr %4, align 8, !tbaa !5
  br label %19

19:                                               ; preds = %15, %2
  %20 = load i64, ptr %4, align 8, !tbaa !5
  %21 = add nsw i64 %20, 1
  %22 = call ptr @llvm.stacksave.p0()
  store ptr %22, ptr %5, align 8
  %23 = alloca i64, i64 %21, align 16
  store i64 %21, ptr %6, align 8
  %24 = mul nuw i64 8, %21
  call void @llvm.memset.p0.i64(ptr align 16 %23, i8 0, i64 %24, i1 false)
  %25 = getelementptr inbounds i64, ptr %23, i64 0
  store i64 1, ptr %25, align 16, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  store i64 1, ptr %7, align 8, !tbaa !5
  br label %26

26:                                               ; preds = %55, %19
  %27 = load i64, ptr %7, align 8, !tbaa !5
  %28 = load i64, ptr %3, align 8, !tbaa !5
  %29 = icmp sle i64 %27, %28
  br i1 %29, label %31, label %30

30:                                               ; preds = %26
  store i32 2, ptr %8, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  br label %58

31:                                               ; preds = %26
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %32 = load i64, ptr %7, align 8, !tbaa !5
  %33 = load i64, ptr %4, align 8, !tbaa !5
  %34 = call noundef i64 @_Z3minxx(i64 noundef %32, i64 noundef %33)
  store i64 %34, ptr %9, align 8, !tbaa !5
  br label %35

35:                                               ; preds = %51, %31
  %36 = load i64, ptr %9, align 8, !tbaa !5
  %37 = icmp sgt i64 %36, 0
  br i1 %37, label %39, label %38

38:                                               ; preds = %35
  store i32 5, ptr %8, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  br label %54

39:                                               ; preds = %35
  %40 = load i64, ptr %9, align 8, !tbaa !5
  %41 = getelementptr inbounds i64, ptr %23, i64 %40
  %42 = load i64, ptr %41, align 8, !tbaa !5
  %43 = load i64, ptr %9, align 8, !tbaa !5
  %44 = sub nsw i64 %43, 1
  %45 = getelementptr inbounds i64, ptr %23, i64 %44
  %46 = load i64, ptr %45, align 8, !tbaa !5
  %47 = add nsw i64 %42, %46
  %48 = srem i64 %47, 1000000007
  %49 = load i64, ptr %9, align 8, !tbaa !5
  %50 = getelementptr inbounds i64, ptr %23, i64 %49
  store i64 %48, ptr %50, align 8, !tbaa !5
  br label %51

51:                                               ; preds = %39
  %52 = load i64, ptr %9, align 8, !tbaa !5
  %53 = add nsw i64 %52, -1
  store i64 %53, ptr %9, align 8, !tbaa !5
  br label %35, !llvm.loop !9

54:                                               ; preds = %38
  br label %55

55:                                               ; preds = %54
  %56 = load i64, ptr %7, align 8, !tbaa !5
  %57 = add nsw i64 %56, 1
  store i64 %57, ptr %7, align 8, !tbaa !5
  br label %26, !llvm.loop !11

58:                                               ; preds = %30
  %59 = load i64, ptr %4, align 8, !tbaa !5
  %60 = getelementptr inbounds i64, ptr %23, i64 %59
  %61 = load i64, ptr %60, align 8, !tbaa !5
  store i32 1, ptr %8, align 4
  %62 = load ptr, ptr %5, align 8
  call void @llvm.stackrestore.p0(ptr %62)
  ret i64 %61
}

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { nocallback nofree nosync nounwind willreturn }
attributes #3 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = !{!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 nounwind uwtable
declare dso_local noundef i64 @_Z3minxx(i64 noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z7nCrModpxx(i64 noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = sub nsw i64 %0, %1
  %spec.select = tail call i64 @llvm.smin.i64(i64 %3, i64 %1)
  %4 = add nsw i64 %spec.select, 1
  %5 = alloca i64, i64 %4, align 16
  %6 = shl nuw i64 %4, 3
  call void @llvm.memset.p0.i64(ptr nonnull align 16 %5, i8 0, i64 %6, i1 false)
  store i64 1, ptr %5, align 16, !tbaa !5
  %.not22 = icmp slt i64 %0, 1
  br i1 %.not22, label %._crit_edge26, label %.lr.ph25

._crit_edge26:                                    ; preds = %._crit_edge, %2
  %7 = getelementptr inbounds i64, ptr %5, i64 %spec.select
  %8 = load i64, ptr %7, align 8, !tbaa !5
  ret i64 %8

.lr.ph25:                                         ; preds = %2, %._crit_edge
  %.01723 = phi i64 [ %19, %._crit_edge ], [ 1, %2 ]
  %9 = tail call noundef i64 @_Z3minxx(i64 noundef %.01723, i64 noundef %spec.select)
  %10 = icmp sgt i64 %9, 0
  br i1 %10, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %.lr.ph25
  %xtraiter = and i64 %9, 1
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol

.lr.ph.prol:                                      ; preds = %.lr.ph.preheader
  %11 = getelementptr inbounds i64, ptr %5, i64 %9
  %12 = load i64, ptr %11, align 8, !tbaa !5
  %13 = getelementptr i8, ptr %11, i64 -8
  %14 = load i64, ptr %13, align 8, !tbaa !5
  %15 = add nsw i64 %14, %12
  %16 = srem i64 %15, 1000000007
  store i64 %16, ptr %11, align 8, !tbaa !5
  %17 = add nsw i64 %9, -1
  br label %.lr.ph.prol.loopexit

.lr.ph.prol.loopexit:                             ; preds = %.lr.ph.prol, %.lr.ph.preheader
  %.021.unr = phi i64 [ %9, %.lr.ph.preheader ], [ %17, %.lr.ph.prol ]
  %18 = icmp eq i64 %9, 1
  br i1 %18, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph.prol.loopexit, %.lr.ph, %.lr.ph25
  %19 = add nuw i64 %.01723, 1
  %exitcond.not = icmp eq i64 %.01723, %0
  br i1 %exitcond.not, label %._crit_edge26, label %.lr.ph25, !llvm.loop !9

.lr.ph:                                           ; preds = %.lr.ph.prol.loopexit, %.lr.ph
  %.021 = phi i64 [ %32, %.lr.ph ], [ %.021.unr, %.lr.ph.prol.loopexit ]
  %20 = getelementptr inbounds i64, ptr %5, i64 %.021
  %21 = load i64, ptr %20, align 8, !tbaa !5
  %22 = getelementptr i8, ptr %20, i64 -8
  %23 = load i64, ptr %22, align 8, !tbaa !5
  %24 = add nsw i64 %23, %21
  %25 = srem i64 %24, 1000000007
  store i64 %25, ptr %20, align 8, !tbaa !5
  %26 = add nsw i64 %.021, -1
  %27 = getelementptr inbounds i64, ptr %5, i64 %26
  %28 = getelementptr i8, ptr %27, i64 -8
  %29 = load i64, ptr %28, align 8, !tbaa !5
  %30 = add nsw i64 %29, %23
  %31 = srem i64 %30, 1000000007
  store i64 %31, ptr %27, align 8, !tbaa !5
  %32 = add nsw i64 %.021, -2
  %33 = icmp ugt i64 %26, 1
  br i1 %33, label %.lr.ph, label %._crit_edge, !llvm.loop !11
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

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

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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass