; LLVM IR from Tunghohin/Competitive_Coding :: Luogu (p1025)

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

@a = external dso_local global [10 x i32], align 16
@k = external dso_local global i32, align 4
@cnt = external dso_local global i32, align 4
@n = external dso_local global i32, align 4
@sum = external dso_local global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local void @dfs(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  store i32 1, ptr @a, align 16, !tbaa !5
  %4 = load i32, ptr %2, align 4, !tbaa !5
  %5 = load i32, ptr @k, align 4, !tbaa !5
  %6 = icmp sgt i32 %4, %5
  br i1 %6, label %7, label %15

7:                                                ; preds = %1
  %8 = load i32, ptr @cnt, align 4, !tbaa !5
  %9 = load i32, ptr @n, align 4, !tbaa !5
  %10 = icmp eq i32 %8, %9
  br i1 %10, label %11, label %14

11:                                               ; preds = %7
  %12 = load i32, ptr @sum, align 4, !tbaa !5
  %13 = add nsw i32 %12, 1
  store i32 %13, ptr @sum, align 4, !tbaa !5
  br label %49

14:                                               ; preds = %7
  br label %49

15:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  %16 = load i32, ptr %2, align 4, !tbaa !5
  %17 = sub nsw i32 %16, 1
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [10 x i32], ptr @a, i64 0, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !5
  store i32 %20, ptr %3, align 4, !tbaa !5
  br label %21

21:                                               ; preds = %46, %15
  %22 = load i32, ptr %3, align 4, !tbaa !5
  %23 = load i32, ptr @n, align 4, !tbaa !5
  %24 = load i32, ptr @cnt, align 4, !tbaa !5
  %25 = sub nsw i32 %23, %24
  %26 = icmp sle i32 %22, %25
  br i1 %26, label %28, label %27

27:                                               ; preds = %21
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  br label %49

28:                                               ; preds = %21
  %29 = load i32, ptr @cnt, align 4, !tbaa !5
  %30 = load i32, ptr @n, align 4, !tbaa !5
  %31 = icmp slt i32 %29, %30
  br i1 %31, label %32, label %45

32:                                               ; preds = %28
  %33 = load i32, ptr %3, align 4, !tbaa !5
  %34 = load i32, ptr @cnt, align 4, !tbaa !5
  %35 = add nsw i32 %34, %33
  store i32 %35, ptr @cnt, align 4, !tbaa !5
  %36 = load i32, ptr %3, align 4, !tbaa !5
  %37 = load i32, ptr %2, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [10 x i32], ptr @a, i64 0, i64 %38
  store i32 %36, ptr %39, align 4, !tbaa !5
  %40 = load i32, ptr %2, align 4, !tbaa !5
  %41 = add nsw i32 %40, 1
  call void @dfs(i32 noundef %41)
  %42 = load i32, ptr %3, align 4, !tbaa !5
  %43 = load i32, ptr @cnt, align 4, !tbaa !5
  %44 = sub nsw i32 %43, %42
  store i32 %44, ptr @cnt, align 4, !tbaa !5
  br label %45

45:                                               ; preds = %32, %28
  br label %46

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

49:                                               ; preds = %27, %14, %11
  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 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"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"

@a = external dso_local local_unnamed_addr global [10 x i32], align 16
@k = external dso_local local_unnamed_addr global i32, align 4
@cnt = external dso_local local_unnamed_addr global i32, align 4
@n = external dso_local local_unnamed_addr global i32, align 4
@sum = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: nofree nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @dfs(i32 noundef %0) local_unnamed_addr #0 {
  store i32 1, ptr @a, align 16, !tbaa !5
  %2 = load i32, ptr @k, align 4, !tbaa !5
  %3 = icmp slt i32 %2, %0
  br i1 %3, label %4, label %11

4:                                                ; preds = %1
  %5 = load i32, ptr @cnt, align 4, !tbaa !5
  %6 = load i32, ptr @n, align 4, !tbaa !5
  %7 = icmp eq i32 %5, %6
  br i1 %7, label %8, label %.loopexit

8:                                                ; preds = %4
  %9 = load i32, ptr @sum, align 4, !tbaa !5
  %10 = add nsw i32 %9, 1
  store i32 %10, ptr @sum, align 4, !tbaa !5
  br label %.loopexit

11:                                               ; preds = %1
  %12 = add nsw i32 %0, -1
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [10 x i32], ptr @a, i64 0, i64 %13
  %15 = load i32, ptr %14, align 4, !tbaa !5
  %16 = load i32, ptr @n, align 4, !tbaa !5
  %17 = load i32, ptr @cnt, align 4, !tbaa !5
  %18 = sub nsw i32 %16, %17
  %.not11 = icmp sgt i32 %15, %18
  br i1 %.not11, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %11
  %19 = sext i32 %0 to i64
  %20 = getelementptr inbounds [10 x i32], ptr @a, i64 0, i64 %19
  %21 = add nsw i32 %0, 1
  br label %22

22:                                               ; preds = %.lr.ph, %30
  %23 = phi i32 [ %17, %.lr.ph ], [ %31, %30 ]
  %24 = phi i32 [ %16, %.lr.ph ], [ %32, %30 ]
  %.012 = phi i32 [ %15, %.lr.ph ], [ %33, %30 ]
  %25 = icmp slt i32 %23, %24
  br i1 %25, label %26, label %30

26:                                               ; preds = %22
  %27 = add nsw i32 %23, %.012
  store i32 %27, ptr @cnt, align 4, !tbaa !5
  store i32 %.012, ptr %20, align 4, !tbaa !5
  tail call void @dfs(i32 noundef %21)
  %28 = load i32, ptr @cnt, align 4, !tbaa !5
  %29 = sub nsw i32 %28, %.012
  store i32 %29, ptr @cnt, align 4, !tbaa !5
  %.pre = load i32, ptr @n, align 4, !tbaa !5
  br label %30

30:                                               ; preds = %22, %26
  %31 = phi i32 [ %23, %22 ], [ %29, %26 ]
  %32 = phi i32 [ %24, %22 ], [ %.pre, %26 ]
  %33 = add nsw i32 %.012, 1
  %34 = sub nsw i32 %32, %31
  %.not.not = icmp slt i32 %.012, %34
  br i1 %.not.not, label %22, label %.loopexit, !llvm.loop !9

.loopexit:                                        ; preds = %30, %11, %4, %8
  ret void
}

attributes #0 = { nofree nosync nounwind memory(readwrite, 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass