; LLVM IR from VastoLorde95/Competitive-Programming :: Codeforces/222 Division 2/Problem A/A.ll (Z3bfsii)

; 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
@m = external dso_local global i32, align 4
@tot = external dso_local global i32, align 4
@k = external dso_local global i32, align 4
@cnt = external dso_local global i32, align 4
@grid = external dso_local global [502 x [502 x i32]], align 16

; Function Attrs: mustprogress uwtable
define dso_local void @_Z3bfsii(i32 noundef %0, i32 noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !5
  %5 = load i32, ptr %3, align 4, !tbaa !5
  %6 = icmp slt i32 %5, 0
  br i1 %6, label %18, label %7

7:                                                ; preds = %2
  %8 = load i32, ptr %3, align 4, !tbaa !5
  %9 = load i32, ptr @n, align 4, !tbaa !5
  %10 = icmp sge i32 %8, %9
  br i1 %10, label %18, label %11

11:                                               ; preds = %7
  %12 = load i32, ptr %4, align 4, !tbaa !5
  %13 = icmp slt i32 %12, 0
  br i1 %13, label %18, label %14

14:                                               ; preds = %11
  %15 = load i32, ptr %4, align 4, !tbaa !5
  %16 = load i32, ptr @m, align 4, !tbaa !5
  %17 = icmp sge i32 %15, %16
  br i1 %17, label %18, label %19

18:                                               ; preds = %14, %11, %7, %2
  br label %66

19:                                               ; preds = %14
  %20 = load i32, ptr @cnt, align 4, !tbaa !5
  %21 = load i32, ptr @tot, align 4, !tbaa !5
  %22 = load i32, ptr @k, align 4, !tbaa !5
  %23 = sub nsw i32 %21, %22
  %24 = icmp eq i32 %20, %23
  br i1 %24, label %25, label %26

25:                                               ; preds = %19
  br label %66

26:                                               ; preds = %19
  %27 = load i32, ptr %3, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [502 x [502 x i32]], ptr @grid, i64 0, i64 %28
  %30 = load i32, ptr %4, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [502 x i32], ptr %29, i64 0, i64 %31
  %33 = load i32, ptr %32, align 4, !tbaa !5
  %34 = icmp eq i32 %33, 3
  br i1 %34, label %44, label %35

35:                                               ; preds = %26
  %36 = load i32, ptr %3, align 4, !tbaa !5
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [502 x [502 x i32]], ptr @grid, i64 0, i64 %37
  %39 = load i32, ptr %4, align 4, !tbaa !5
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [502 x i32], ptr %38, i64 0, i64 %40
  %42 = load i32, ptr %41, align 4, !tbaa !5
  %43 = icmp eq i32 %42, 2
  br i1 %43, label %44, label %45

44:                                               ; preds = %35, %26
  br label %66

45:                                               ; preds = %35
  %46 = load i32, ptr %3, align 4, !tbaa !5
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds [502 x [502 x i32]], ptr @grid, i64 0, i64 %47
  %49 = load i32, ptr %4, align 4, !tbaa !5
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds [502 x i32], ptr %48, i64 0, i64 %50
  store i32 3, ptr %51, align 4, !tbaa !5
  %52 = load i32, ptr @cnt, align 4, !tbaa !5
  %53 = add nsw i32 %52, 1
  store i32 %53, ptr @cnt, align 4, !tbaa !5
  %54 = load i32, ptr %3, align 4, !tbaa !5
  %55 = add nsw i32 %54, 1
  %56 = load i32, ptr %4, align 4, !tbaa !5
  call void @_Z3bfsii(i32 noundef %55, i32 noundef %56)
  %57 = load i32, ptr %3, align 4, !tbaa !5
  %58 = sub nsw i32 %57, 1
  %59 = load i32, ptr %4, align 4, !tbaa !5
  call void @_Z3bfsii(i32 noundef %58, i32 noundef %59)
  %60 = load i32, ptr %3, align 4, !tbaa !5
  %61 = load i32, ptr %4, align 4, !tbaa !5
  %62 = add nsw i32 %61, 1
  call void @_Z3bfsii(i32 noundef %60, i32 noundef %62)
  %63 = load i32, ptr %3, align 4, !tbaa !5
  %64 = load i32, ptr %4, align 4, !tbaa !5
  %65 = sub nsw i32 %64, 1
  call void @_Z3bfsii(i32 noundef %63, i32 noundef %65)
  br label %66

66:                                               ; preds = %45, %44, %25, %18
  ret void
}

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

!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"}


; 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
@m = external dso_local local_unnamed_addr global i32, align 4
@tot = external dso_local local_unnamed_addr global i32, align 4
@k = external dso_local local_unnamed_addr global i32, align 4
@cnt = external dso_local local_unnamed_addr global i32, align 4
@grid = external dso_local local_unnamed_addr global [502 x [502 x i32]], align 16

; Function Attrs: mustprogress nofree nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z3bfsii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = zext nneg i32 %0 to i64
  %4 = icmp slt i32 %0, 0
  br i1 %4, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %2
  %5 = add nuw nsw i32 %0, 1
  %6 = add nsw i32 %0, -1
  %7 = load i32, ptr @n, align 4, !tbaa !5
  %8 = icmp sgt i32 %7, %0
  %9 = icmp sgt i32 %1, -1
  %or.cond.not2429 = and i1 %9, %8
  %10 = load i32, ptr @m, align 4
  %.not30 = icmp sgt i32 %10, %1
  %or.cond2131 = select i1 %or.cond.not2429, i1 %.not30, i1 false
  br i1 %or.cond2131, label %.lr.ph33, label %._crit_edge

.lr.ph33:                                         ; preds = %.lr.ph, %tailrecurse
  %.tr252632 = phi i32 [ %23, %tailrecurse ], [ %1, %.lr.ph ]
  %11 = load i32, ptr @cnt, align 4, !tbaa !5
  %12 = load i32, ptr @tot, align 4, !tbaa !5
  %13 = load i32, ptr @k, align 4, !tbaa !5
  %14 = sub nsw i32 %12, %13
  %15 = icmp eq i32 %11, %14
  br i1 %15, label %._crit_edge, label %16

16:                                               ; preds = %.lr.ph33
  %17 = zext nneg i32 %.tr252632 to i64
  %18 = getelementptr inbounds [502 x [502 x i32]], ptr @grid, i64 0, i64 %3, i64 %17
  %19 = load i32, ptr %18, align 4, !tbaa !5
  %20 = and i32 %19, -2
  %switch = icmp eq i32 %20, 2
  br i1 %switch, label %._crit_edge, label %tailrecurse

tailrecurse:                                      ; preds = %16
  store i32 3, ptr %18, align 4, !tbaa !5
  %21 = add nsw i32 %11, 1
  store i32 %21, ptr @cnt, align 4, !tbaa !5
  tail call void @_Z3bfsii(i32 noundef %5, i32 noundef %.tr252632)
  tail call void @_Z3bfsii(i32 noundef %6, i32 noundef %.tr252632)
  %22 = add nuw nsw i32 %.tr252632, 1
  tail call void @_Z3bfsii(i32 noundef %0, i32 noundef %22)
  %23 = add nsw i32 %.tr252632, -1
  %24 = load i32, ptr @n, align 4, !tbaa !5
  %25 = icmp sgt i32 %24, %0
  %26 = icmp sgt i32 %.tr252632, 0
  %or.cond.not24 = and i1 %26, %25
  %27 = load i32, ptr @m, align 4
  %.not = icmp sge i32 %27, %.tr252632
  %or.cond21 = select i1 %or.cond.not24, i1 %.not, i1 false
  br i1 %or.cond21, label %.lr.ph33, label %._crit_edge

._crit_edge:                                      ; preds = %tailrecurse, %.lr.ph33, %16, %.lr.ph, %2
  ret void
}

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


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