; LLVM IR from upupming/algorithm :: acwing/算法竞赛进阶指南/0x18-总结与练习/153. 双栈排序.ll (Z3dfsii)

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

@n = external dso_local global i32, align 4
@color = external dso_local global [1010 x i32], align 16
@g = external dso_local global [1010 x [1010 x i8]], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i1 @_Z3dfsii(i32 noundef %0, i32 noundef %1) #0 {
  %3 = alloca i1, align 1
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !5
  %8 = load i32, ptr %5, align 4, !tbaa !5
  %9 = load i32, ptr %4, align 4, !tbaa !5
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds [1010 x i32], ptr @color, i64 0, i64 %10
  store i32 %8, ptr %11, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 1, ptr %6, align 4, !tbaa !5
  br label %12

12:                                               ; preds = %48, %2
  %13 = load i32, ptr %6, align 4, !tbaa !5
  %14 = load i32, ptr @n, align 4, !tbaa !5
  %15 = icmp sle i32 %13, %14
  br i1 %15, label %17, label %16

16:                                               ; preds = %12
  store i32 2, ptr %7, align 4
  br label %51

17:                                               ; preds = %12
  %18 = load i32, ptr %4, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [1010 x [1010 x i8]], ptr @g, i64 0, i64 %19
  %21 = load i32, ptr %6, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [1010 x i8], ptr %20, i64 0, i64 %22
  %24 = load i8, ptr %23, align 1, !tbaa !9, !range !11, !noundef !12
  %25 = trunc i8 %24 to i1
  br i1 %25, label %26, label %47

26:                                               ; preds = %17
  %27 = load i32, ptr %6, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [1010 x i32], ptr @color, i64 0, i64 %28
  %30 = load i32, ptr %29, align 4, !tbaa !5
  %31 = load i32, ptr %5, align 4, !tbaa !5
  %32 = icmp eq i32 %30, %31
  br i1 %32, label %33, label %34

33:                                               ; preds = %26
  store i1 false, ptr %3, align 1
  store i32 1, ptr %7, align 4
  br label %51

34:                                               ; preds = %26
  %35 = load i32, ptr %6, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [1010 x i32], ptr @color, i64 0, i64 %36
  %38 = load i32, ptr %37, align 4, !tbaa !5
  %39 = icmp eq i32 %38, 0
  br i1 %39, label %40, label %46

40:                                               ; preds = %34
  %41 = load i32, ptr %6, align 4, !tbaa !5
  %42 = load i32, ptr %5, align 4, !tbaa !5
  %43 = sub nsw i32 3, %42
  %44 = call noundef zeroext i1 @_Z3dfsii(i32 noundef %41, i32 noundef %43)
  br i1 %44, label %46, label %45

45:                                               ; preds = %40
  store i1 false, ptr %3, align 1
  store i32 1, ptr %7, align 4
  br label %51

46:                                               ; preds = %40, %34
  br label %47

47:                                               ; preds = %46, %17
  br label %48

48:                                               ; preds = %47
  %49 = load i32, ptr %6, align 4, !tbaa !5
  %50 = add nsw i32 %49, 1
  store i32 %50, ptr %6, align 4, !tbaa !5
  br label %12, !llvm.loop !13

51:                                               ; preds = %45, %33, %16
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  %52 = load i32, ptr %7, align 4
  switch i32 %52, label %56 [
    i32 2, label %53
    i32 1, label %54
  ]

53:                                               ; preds = %51
  store i1 true, ptr %3, align 1
  br label %54

54:                                               ; preds = %53, %51
  %55 = load i1, ptr %3, align 1
  ret i1 %55

56:                                               ; preds = %51
  unreachable
}

; 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 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"bool", !7, i64 0}
!11 = !{i8 0, i8 2}
!12 = !{}
!13 = distinct !{!13, !14}
!14 = !{!"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"

@n = external dso_local local_unnamed_addr global i32, align 4
@color = external dso_local local_unnamed_addr global [1010 x i32], align 16
@g = external dso_local local_unnamed_addr global [1010 x [1010 x i8]], align 16

; Function Attrs: mustprogress nofree nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef zeroext i1 @_Z3dfsii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = sext i32 %0 to i64
  %4 = getelementptr inbounds [1010 x i32], ptr @color, i64 0, i64 %3
  store i32 %1, ptr %4, align 4, !tbaa !5
  %5 = load i32, ptr @n, align 4, !tbaa !5
  %.not16 = icmp slt i32 %5, 1
  br i1 %.not16, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %2
  %6 = sub nsw i32 3, %1
  br label %7

7:                                                ; preds = %.lr.ph, %21
  %8 = phi i32 [ %5, %.lr.ph ], [ %22, %21 ]
  %indvars.iv = phi i64 [ 1, %.lr.ph ], [ %indvars.iv.next, %21 ]
  %9 = getelementptr inbounds [1010 x [1010 x i8]], ptr @g, i64 0, i64 %3, i64 %indvars.iv
  %10 = load i8, ptr %9, align 1, !tbaa !9, !range !11, !noundef !12
  %11 = trunc nuw i8 %10 to i1
  br i1 %11, label %12, label %21

12:                                               ; preds = %7
  %13 = getelementptr inbounds [1010 x i32], ptr @color, i64 0, i64 %indvars.iv
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %15 = icmp eq i32 %14, %1
  br i1 %15, label %._crit_edge, label %16

16:                                               ; preds = %12
  %17 = icmp eq i32 %14, 0
  br i1 %17, label %18, label %21

18:                                               ; preds = %16
  %19 = trunc nuw nsw i64 %indvars.iv to i32
  %20 = tail call noundef zeroext i1 @_Z3dfsii(i32 noundef %19, i32 noundef %6)
  br i1 %20, label %._crit_edge23, label %._crit_edge

._crit_edge23:                                    ; preds = %18
  %.pre = load i32, ptr @n, align 4, !tbaa !5
  br label %21

21:                                               ; preds = %._crit_edge23, %7, %16
  %22 = phi i32 [ %.pre, %._crit_edge23 ], [ %8, %7 ], [ %8, %16 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %23 = sext i32 %22 to i64
  %.not.not = icmp slt i64 %indvars.iv, %23
  br i1 %.not.not, label %7, label %._crit_edge, !llvm.loop !13

._crit_edge:                                      ; preds = %21, %12, %18, %2
  %.not.lcssa = phi i1 [ true, %2 ], [ false, %18 ], [ false, %12 ], [ true, %21 ]
  ret i1 %.not.lcssa
}

attributes #0 = { mustprogress 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"bool", !7, i64 0}
!11 = !{i8 0, i8 2}
!12 = !{}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


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