; LLVM IR from Izaron/ACM-ICPC :: Timus Online Judge/Volume 1/1017. Staircases.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"

@m = external dso_local global [505 x [505 x i64]], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z3dfsii(i32 noundef %0, i32 noundef %1) #0 {
  %3 = alloca i64, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i64, align 8
  %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 %4, align 4, !tbaa !5
  %9 = icmp slt i32 %8, 0
  br i1 %9, label %10, label %11

10:                                               ; preds = %2
  store i64 0, ptr %3, align 8
  br label %66

11:                                               ; preds = %2
  %12 = load i32, ptr %4, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [505 x [505 x i64]], ptr @m, i64 0, i64 %13
  %15 = load i32, ptr %5, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [505 x i64], ptr %14, i64 0, i64 %16
  %18 = load i64, ptr %17, align 8, !tbaa !9
  %19 = icmp ne i64 %18, -1
  br i1 %19, label %20, label %28

20:                                               ; preds = %11
  %21 = load i32, ptr %4, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [505 x [505 x i64]], ptr @m, i64 0, i64 %22
  %24 = load i32, ptr %5, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [505 x i64], ptr %23, i64 0, i64 %25
  %27 = load i64, ptr %26, align 8, !tbaa !9
  store i64 %27, ptr %3, align 8
  br label %66

28:                                               ; preds = %11
  %29 = load i32, ptr %4, align 4, !tbaa !5
  %30 = icmp eq i32 %29, 0
  br i1 %30, label %31, label %38

31:                                               ; preds = %28
  %32 = load i32, ptr %4, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [505 x [505 x i64]], ptr @m, i64 0, i64 %33
  %35 = load i32, ptr %5, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [505 x i64], ptr %34, i64 0, i64 %36
  store i64 1, ptr %37, align 8, !tbaa !9
  store i64 1, ptr %3, align 8
  br label %66

38:                                               ; preds = %28
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  store i64 0, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %39 = load i32, ptr %5, align 4, !tbaa !5
  %40 = add nsw i32 %39, 1
  store i32 %40, ptr %7, align 4, !tbaa !5
  br label %41

41:                                               ; preds = %54, %38
  %42 = load i32, ptr %7, align 4, !tbaa !5
  %43 = load i32, ptr %4, align 4, !tbaa !5
  %44 = icmp sle i32 %42, %43
  br i1 %44, label %46, label %45

45:                                               ; preds = %41
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %57

46:                                               ; preds = %41
  %47 = load i32, ptr %4, align 4, !tbaa !5
  %48 = load i32, ptr %7, align 4, !tbaa !5
  %49 = sub nsw i32 %47, %48
  %50 = load i32, ptr %7, align 4, !tbaa !5
  %51 = call noundef i64 @_Z3dfsii(i32 noundef %49, i32 noundef %50)
  %52 = load i64, ptr %6, align 8, !tbaa !9
  %53 = add nsw i64 %52, %51
  store i64 %53, ptr %6, align 8, !tbaa !9
  br label %54

54:                                               ; preds = %46
  %55 = load i32, ptr %7, align 4, !tbaa !5
  %56 = add nsw i32 %55, 1
  store i32 %56, ptr %7, align 4, !tbaa !5
  br label %41, !llvm.loop !11

57:                                               ; preds = %45
  %58 = load i64, ptr %6, align 8, !tbaa !9
  %59 = load i32, ptr %4, align 4, !tbaa !5
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [505 x [505 x i64]], ptr @m, i64 0, i64 %60
  %62 = load i32, ptr %5, align 4, !tbaa !5
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds [505 x i64], ptr %61, i64 0, i64 %63
  store i64 %58, ptr %64, align 8, !tbaa !9
  %65 = load i64, ptr %6, align 8, !tbaa !9
  store i64 %65, ptr %3, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  br label %66

66:                                               ; preds = %57, %31, %20, %10
  %67 = load i64, ptr %3, align 8
  ret i64 %67
}

; 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 = !{!"long long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"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"

@m = external dso_local local_unnamed_addr global [505 x [505 x i64]], align 16

; Function Attrs: mustprogress nofree nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i64 @_Z3dfsii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp slt i32 %0, 0
  br i1 %3, label %14, label %4

4:                                                ; preds = %2
  %5 = zext nneg i32 %0 to i64
  %6 = sext i32 %1 to i64
  %7 = getelementptr inbounds [505 x [505 x i64]], ptr @m, i64 0, i64 %5, i64 %6
  %8 = load i64, ptr %7, align 8, !tbaa !5
  %.not = icmp eq i64 %8, -1
  br i1 %.not, label %9, label %14

9:                                                ; preds = %4
  %10 = icmp eq i32 %0, 0
  br i1 %10, label %.sink.split, label %.preheader

.preheader:                                       ; preds = %9
  %.not25.not26 = icmp slt i32 %1, %0
  br i1 %.not25.not26, label %.lr.ph, label %.sink.split

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %.0.in28 = phi i32 [ %.0, %.lr.ph ], [ %1, %.preheader ]
  %.02127 = phi i64 [ %13, %.lr.ph ], [ 0, %.preheader ]
  %.0 = add nsw i32 %.0.in28, 1
  %11 = sub nsw i32 %0, %.0
  %12 = tail call noundef i64 @_Z3dfsii(i32 noundef %11, i32 noundef %.0)
  %13 = add nsw i64 %12, %.02127
  %exitcond.not = icmp eq i32 %.0, %0
  br i1 %exitcond.not, label %.sink.split, label %.lr.ph, !llvm.loop !9

.sink.split:                                      ; preds = %.lr.ph, %.preheader, %9
  %.021.lcssa.sink = phi i64 [ 1, %9 ], [ 0, %.preheader ], [ %13, %.lr.ph ]
  store i64 %.021.lcssa.sink, ptr %7, align 8, !tbaa !5
  br label %14

14:                                               ; preds = %.sink.split, %4, %2
  %.022 = phi i64 [ 0, %2 ], [ %8, %4 ], [ %.021.lcssa.sink, %.sink.split ]
  ret i64 %.022
}

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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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