; LLVM IR from brpapa/algorithms :: solutions/spoj/LABYR1.ll (Z3dfsiii)

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

@state = external dso_local global [1001 x [1001 x i32]], align 16
@dr = external dso_local global [4 x i32], align 16
@dc = external dso_local global [4 x i32], align 16
@lr = external dso_local global i32, align 4
@lc = external dso_local global i32, align 4
@maxLvl = external dso_local global i32, align 4

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_Z7isValidii(i32 noundef, i32 noundef) #0

; Function Attrs: mustprogress uwtable
define dso_local void @_Z3dfsiii(i32 noundef %0, i32 noundef %1, i32 noundef %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !5
  store i32 %2, ptr %6, align 4, !tbaa !5
  %10 = load i32, ptr %6, align 4, !tbaa !5
  %11 = load i32, ptr @maxLvl, align 4, !tbaa !5
  %12 = icmp sgt i32 %10, %11
  br i1 %12, label %13, label %17

13:                                               ; preds = %3
  %14 = load i32, ptr %6, align 4, !tbaa !5
  store i32 %14, ptr @maxLvl, align 4, !tbaa !5
  %15 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %15, ptr @lr, align 4, !tbaa !5
  %16 = load i32, ptr %5, align 4, !tbaa !5
  store i32 %16, ptr @lc, align 4, !tbaa !5
  br label %17

17:                                               ; preds = %13, %3
  %18 = load i32, ptr %4, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [1001 x [1001 x i32]], ptr @state, i64 0, i64 %19
  %21 = load i32, ptr %5, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [1001 x i32], ptr %20, i64 0, i64 %22
  store i32 1, ptr %23, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 0, ptr %7, align 4, !tbaa !5
  br label %24

24:                                               ; preds = %59, %17
  %25 = load i32, ptr %7, align 4, !tbaa !5
  %26 = icmp slt i32 %25, 4
  br i1 %26, label %28, label %27

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

28:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %29 = load i32, ptr %4, align 4, !tbaa !5
  %30 = load i32, ptr %7, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [4 x i32], ptr @dr, i64 0, i64 %31
  %33 = load i32, ptr %32, align 4, !tbaa !5
  %34 = add nsw i32 %29, %33
  store i32 %34, ptr %8, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %35 = load i32, ptr %5, align 4, !tbaa !5
  %36 = load i32, ptr %7, align 4, !tbaa !5
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [4 x i32], ptr @dc, i64 0, i64 %37
  %39 = load i32, ptr %38, align 4, !tbaa !5
  %40 = add nsw i32 %35, %39
  store i32 %40, ptr %9, align 4, !tbaa !5
  %41 = load i32, ptr %8, align 4, !tbaa !5
  %42 = load i32, ptr %9, align 4, !tbaa !5
  %43 = call noundef zeroext i1 @_Z7isValidii(i32 noundef %41, i32 noundef %42)
  br i1 %43, label %44, label %58

44:                                               ; preds = %28
  %45 = load i32, ptr %8, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [1001 x [1001 x i32]], ptr @state, i64 0, i64 %46
  %48 = load i32, ptr %9, align 4, !tbaa !5
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds [1001 x i32], ptr %47, i64 0, i64 %49
  %51 = load i32, ptr %50, align 4, !tbaa !5
  %52 = icmp eq i32 %51, 0
  br i1 %52, label %53, label %58

53:                                               ; preds = %44
  %54 = load i32, ptr %8, align 4, !tbaa !5
  %55 = load i32, ptr %9, align 4, !tbaa !5
  %56 = load i32, ptr %6, align 4, !tbaa !5
  %57 = add nsw i32 %56, 1
  call void @_Z3dfsiii(i32 noundef %54, i32 noundef %55, i32 noundef %57)
  br label %58

58:                                               ; preds = %53, %44, %28
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  br label %59

59:                                               ; preds = %58
  %60 = load i32, ptr %7, align 4, !tbaa !5
  %61 = add nsw i32 %60, 1
  store i32 %61, ptr %7, align 4, !tbaa !5
  br label %24, !llvm.loop !9

62:                                               ; preds = %27
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 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 memory(argmem: readwrite) }
attributes #3 = { 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 = 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"

@state = external dso_local local_unnamed_addr global [1001 x [1001 x i32]], align 16
@dr = external dso_local local_unnamed_addr global [4 x i32], align 16
@dc = external dso_local local_unnamed_addr global [4 x i32], align 16
@lr = external dso_local local_unnamed_addr global i32, align 4
@lc = external dso_local local_unnamed_addr global i32, align 4
@maxLvl = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_Z7isValidii(i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z3dfsiii(i32 noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  br label %tailrecurse

tailrecurse:                                      ; preds = %56, %3
  %.tr = phi i32 [ %0, %3 ], [ %52, %56 ]
  %.tr22 = phi i32 [ %1, %3 ], [ %54, %56 ]
  %.tr23 = phi i32 [ %2, %3 ], [ %11, %56 ]
  %4 = load i32, ptr @maxLvl, align 4, !tbaa !5
  %5 = icmp slt i32 %4, %.tr23
  br i1 %5, label %6, label %7

6:                                                ; preds = %tailrecurse
  store i32 %.tr23, ptr @maxLvl, align 4, !tbaa !5
  store i32 %.tr, ptr @lr, align 4, !tbaa !5
  store i32 %.tr22, ptr @lc, align 4, !tbaa !5
  br label %7

7:                                                ; preds = %6, %tailrecurse
  %8 = sext i32 %.tr to i64
  %9 = sext i32 %.tr22 to i64
  %10 = getelementptr inbounds [1001 x [1001 x i32]], ptr @state, i64 0, i64 %8, i64 %9
  store i32 1, ptr %10, align 4, !tbaa !5
  %11 = add nsw i32 %.tr23, 1
  %12 = load i32, ptr @dr, align 16, !tbaa !5
  %13 = add nsw i32 %12, %.tr
  %14 = load i32, ptr @dc, align 16, !tbaa !5
  %15 = add nsw i32 %14, %.tr22
  %16 = tail call noundef zeroext i1 @_Z7isValidii(i32 noundef %13, i32 noundef %15)
  br i1 %16, label %17, label %24

17:                                               ; preds = %7
  %18 = sext i32 %13 to i64
  %19 = sext i32 %15 to i64
  %20 = getelementptr inbounds [1001 x [1001 x i32]], ptr @state, i64 0, i64 %18, i64 %19
  %21 = load i32, ptr %20, align 4, !tbaa !5
  %22 = icmp eq i32 %21, 0
  br i1 %22, label %23, label %24

23:                                               ; preds = %17
  tail call void @_Z3dfsiii(i32 noundef %13, i32 noundef %15, i32 noundef %11)
  br label %24

24:                                               ; preds = %23, %17, %7
  %25 = load i32, ptr getelementptr inbounds (i8, ptr @dr, i64 4), align 4, !tbaa !5
  %26 = add nsw i32 %25, %.tr
  %27 = load i32, ptr getelementptr inbounds (i8, ptr @dc, i64 4), align 4, !tbaa !5
  %28 = add nsw i32 %27, %.tr22
  %29 = tail call noundef zeroext i1 @_Z7isValidii(i32 noundef %26, i32 noundef %28)
  br i1 %29, label %30, label %37

30:                                               ; preds = %24
  %31 = sext i32 %26 to i64
  %32 = sext i32 %28 to i64
  %33 = getelementptr inbounds [1001 x [1001 x i32]], ptr @state, i64 0, i64 %31, i64 %32
  %34 = load i32, ptr %33, align 4, !tbaa !5
  %35 = icmp eq i32 %34, 0
  br i1 %35, label %36, label %37

36:                                               ; preds = %30
  tail call void @_Z3dfsiii(i32 noundef %26, i32 noundef %28, i32 noundef %11)
  br label %37

37:                                               ; preds = %36, %30, %24
  %38 = load i32, ptr getelementptr inbounds (i8, ptr @dr, i64 8), align 8, !tbaa !5
  %39 = add nsw i32 %38, %.tr
  %40 = load i32, ptr getelementptr inbounds (i8, ptr @dc, i64 8), align 8, !tbaa !5
  %41 = add nsw i32 %40, %.tr22
  %42 = tail call noundef zeroext i1 @_Z7isValidii(i32 noundef %39, i32 noundef %41)
  br i1 %42, label %43, label %50

43:                                               ; preds = %37
  %44 = sext i32 %39 to i64
  %45 = sext i32 %41 to i64
  %46 = getelementptr inbounds [1001 x [1001 x i32]], ptr @state, i64 0, i64 %44, i64 %45
  %47 = load i32, ptr %46, align 4, !tbaa !5
  %48 = icmp eq i32 %47, 0
  br i1 %48, label %49, label %50

49:                                               ; preds = %43
  tail call void @_Z3dfsiii(i32 noundef %39, i32 noundef %41, i32 noundef %11)
  br label %50

50:                                               ; preds = %49, %43, %37
  %51 = load i32, ptr getelementptr inbounds (i8, ptr @dr, i64 12), align 4, !tbaa !5
  %52 = add nsw i32 %51, %.tr
  %53 = load i32, ptr getelementptr inbounds (i8, ptr @dc, i64 12), align 4, !tbaa !5
  %54 = add nsw i32 %53, %.tr22
  %55 = tail call noundef zeroext i1 @_Z7isValidii(i32 noundef %52, i32 noundef %54)
  br i1 %55, label %56, label %62

56:                                               ; preds = %50
  %57 = sext i32 %52 to i64
  %58 = sext i32 %54 to i64
  %59 = getelementptr inbounds [1001 x [1001 x i32]], ptr @state, i64 0, i64 %57, i64 %58
  %60 = load i32, ptr %59, align 4, !tbaa !5
  %61 = icmp eq i32 %60, 0
  br i1 %61, label %tailrecurse, label %62

62:                                               ; preds = %56, %50
  ret void
}

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

!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,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,LoopRotatePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass