; LLVM IR from bqi343/cp-notebook :: Contests/USACO Training/Ch 5/5.2 snail.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"

@ar = external dso_local global [120 x [120 x i32]], align 16
@xdir = external dso_local global [4 x i32], align 16
@ydir = external dso_local global [4 x i32], align 16

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_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
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  %14 = load i32, ptr %5, align 4, !tbaa !5
  store i32 %14, ptr %8, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  %15 = load i32, ptr %6, align 4, !tbaa !5
  store i32 %15, ptr %9, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  store i32 0, ptr %10, align 4, !tbaa !5
  br label %16

16:                                               ; preds = %31, %3
  %17 = load i32, ptr %5, align 4, !tbaa !5
  %18 = load i32, ptr %7, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [4 x i32], ptr @xdir, i64 0, i64 %19
  %21 = load i32, ptr %20, align 4, !tbaa !5
  %22 = add nsw i32 %17, %21
  %23 = load i32, ptr %6, align 4, !tbaa !5
  %24 = load i32, ptr %7, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [4 x i32], ptr @ydir, i64 0, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %28 = add nsw i32 %23, %27
  %29 = call noundef i32 @_Z5validii(i32 noundef %22, i32 noundef %28)
  %30 = icmp eq i32 %29, 2
  br i1 %30, label %31, label %52

31:                                               ; preds = %16
  %32 = load i32, ptr %7, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [4 x i32], ptr @xdir, i64 0, i64 %33
  %35 = load i32, ptr %34, align 4, !tbaa !5
  %36 = load i32, ptr %5, align 4, !tbaa !5
  %37 = add nsw i32 %36, %35
  store i32 %37, ptr %5, align 4, !tbaa !5
  %38 = load i32, ptr %7, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [4 x i32], ptr @ydir, i64 0, i64 %39
  %41 = load i32, ptr %40, align 4, !tbaa !5
  %42 = load i32, ptr %6, align 4, !tbaa !5
  %43 = add nsw i32 %42, %41
  store i32 %43, ptr %6, align 4, !tbaa !5
  %44 = load i32, ptr %5, align 4, !tbaa !5
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [120 x [120 x i32]], ptr @ar, i64 0, i64 %45
  %47 = load i32, ptr %6, align 4, !tbaa !5
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [120 x i32], ptr %46, i64 0, i64 %48
  store i32 1, ptr %49, align 4, !tbaa !5
  %50 = load i32, ptr %10, align 4, !tbaa !5
  %51 = add nsw i32 %50, 1
  store i32 %51, ptr %10, align 4, !tbaa !5
  br label %16, !llvm.loop !9

52:                                               ; preds = %16
  %53 = load i32, ptr %10, align 4, !tbaa !5
  %54 = icmp eq i32 %53, 0
  br i1 %54, label %55, label %56

55:                                               ; preds = %52
  store i32 0, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %120

56:                                               ; preds = %52
  %57 = load i32, ptr %5, align 4, !tbaa !5
  %58 = load i32, ptr %7, align 4, !tbaa !5
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds [4 x i32], ptr @xdir, i64 0, i64 %59
  %61 = load i32, ptr %60, align 4, !tbaa !5
  %62 = add nsw i32 %57, %61
  %63 = load i32, ptr %6, align 4, !tbaa !5
  %64 = load i32, ptr %7, align 4, !tbaa !5
  %65 = sext i32 %64 to i64
  %66 = getelementptr inbounds [4 x i32], ptr @ydir, i64 0, i64 %65
  %67 = load i32, ptr %66, align 4, !tbaa !5
  %68 = add nsw i32 %63, %67
  %69 = call noundef i32 @_Z5validii(i32 noundef %62, i32 noundef %68)
  %70 = icmp eq i32 %69, 1
  br i1 %70, label %71, label %88

71:                                               ; preds = %56
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #4
  %72 = load i32, ptr %5, align 4, !tbaa !5
  %73 = load i32, ptr %6, align 4, !tbaa !5
  %74 = load i32, ptr %7, align 4, !tbaa !5
  %75 = add nsw i32 %74, 1
  %76 = srem i32 %75, 4
  %77 = call noundef i32 @_Z3dfsiii(i32 noundef %72, i32 noundef %73, i32 noundef %76)
  store i32 %77, ptr %12, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #4
  %78 = load i32, ptr %5, align 4, !tbaa !5
  %79 = load i32, ptr %6, align 4, !tbaa !5
  %80 = load i32, ptr %7, align 4, !tbaa !5
  %81 = add nsw i32 %80, 3
  %82 = srem i32 %81, 4
  %83 = call noundef i32 @_Z3dfsiii(i32 noundef %78, i32 noundef %79, i32 noundef %82)
  store i32 %83, ptr %13, align 4, !tbaa !5
  %84 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %12, ptr noundef nonnull align 4 dereferenceable(4) %13)
  %85 = load i32, ptr %84, align 4, !tbaa !5
  %86 = load i32, ptr %10, align 4, !tbaa !5
  %87 = add nsw i32 %86, %85
  store i32 %87, ptr %10, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #4
  br label %88

88:                                               ; preds = %71, %56
  br label %89

89:                                               ; preds = %99, %88
  %90 = load i32, ptr %5, align 4, !tbaa !5
  %91 = load i32, ptr %8, align 4, !tbaa !5
  %92 = icmp ne i32 %90, %91
  br i1 %92, label %97, label %93

93:                                               ; preds = %89
  %94 = load i32, ptr %6, align 4, !tbaa !5
  %95 = load i32, ptr %9, align 4, !tbaa !5
  %96 = icmp ne i32 %94, %95
  br label %97

97:                                               ; preds = %93, %89
  %98 = phi i1 [ true, %89 ], [ %96, %93 ]
  br i1 %98, label %99, label %118

99:                                               ; preds = %97
  %100 = load i32, ptr %5, align 4, !tbaa !5
  %101 = sext i32 %100 to i64
  %102 = getelementptr inbounds [120 x [120 x i32]], ptr @ar, i64 0, i64 %101
  %103 = load i32, ptr %6, align 4, !tbaa !5
  %104 = sext i32 %103 to i64
  %105 = getelementptr inbounds [120 x i32], ptr %102, i64 0, i64 %104
  store i32 0, ptr %105, align 4, !tbaa !5
  %106 = load i32, ptr %7, align 4, !tbaa !5
  %107 = sext i32 %106 to i64
  %108 = getelementptr inbounds [4 x i32], ptr @xdir, i64 0, i64 %107
  %109 = load i32, ptr %108, align 4, !tbaa !5
  %110 = load i32, ptr %5, align 4, !tbaa !5
  %111 = sub nsw i32 %110, %109
  store i32 %111, ptr %5, align 4, !tbaa !5
  %112 = load i32, ptr %7, align 4, !tbaa !5
  %113 = sext i32 %112 to i64
  %114 = getelementptr inbounds [4 x i32], ptr @ydir, i64 0, i64 %113
  %115 = load i32, ptr %114, align 4, !tbaa !5
  %116 = load i32, ptr %6, align 4, !tbaa !5
  %117 = sub nsw i32 %116, %115
  store i32 %117, ptr %6, align 4, !tbaa !5
  br label %89, !llvm.loop !11

118:                                              ; preds = %97
  %119 = load i32, ptr %10, align 4, !tbaa !5
  store i32 %119, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %120

120:                                              ; preds = %118, %55
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  %121 = load i32, ptr %4, align 4
  ret i32 %121
}

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) #3

; 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 = { inlinehint 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 #4 = { 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"}
!11 = distinct !{!11, !10}


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

@ar = external dso_local local_unnamed_addr global [120 x [120 x i32]], align 16
@xdir = external dso_local local_unnamed_addr global [4 x i32], align 16
@ydir = external dso_local local_unnamed_addr global [4 x i32], align 16

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z3dfsiii(i32 noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = sext i32 %2 to i64
  %7 = getelementptr inbounds [4 x i32], ptr @xdir, i64 0, i64 %6
  %8 = getelementptr inbounds [4 x i32], ptr @ydir, i64 0, i64 %6
  %9 = load i32, ptr %7, align 4, !tbaa !5
  %10 = add nsw i32 %9, %0
  %11 = load i32, ptr %8, align 4, !tbaa !5
  %12 = add nsw i32 %11, %1
  %13 = tail call noundef i32 @_Z5validii(i32 noundef %10, i32 noundef %12)
  %14 = icmp eq i32 %13, 2
  br i1 %14, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %3, %.lr.ph
  %.045 = phi i32 [ %22, %.lr.ph ], [ 0, %3 ]
  %.03844 = phi i32 [ %16, %.lr.ph ], [ %0, %3 ]
  %.04043 = phi i32 [ %18, %.lr.ph ], [ %1, %3 ]
  %15 = load i32, ptr %7, align 4, !tbaa !5
  %16 = add nsw i32 %15, %.03844
  %17 = load i32, ptr %8, align 4, !tbaa !5
  %18 = add nsw i32 %17, %.04043
  %19 = sext i32 %16 to i64
  %20 = sext i32 %18 to i64
  %21 = getelementptr inbounds [120 x [120 x i32]], ptr @ar, i64 0, i64 %19, i64 %20
  store i32 1, ptr %21, align 4, !tbaa !5
  %22 = add nuw nsw i32 %.045, 1
  %23 = add nsw i32 %15, %16
  %24 = add nsw i32 %17, %18
  %25 = tail call noundef i32 @_Z5validii(i32 noundef %23, i32 noundef %24)
  %26 = icmp eq i32 %25, 2
  br i1 %26, label %.lr.ph, label %._crit_edge, !llvm.loop !9

._crit_edge:                                      ; preds = %.lr.ph
  %27 = load i32, ptr %7, align 4, !tbaa !5
  %28 = add nsw i32 %27, %16
  %29 = load i32, ptr %8, align 4, !tbaa !5
  %30 = add nsw i32 %29, %18
  %31 = tail call noundef i32 @_Z5validii(i32 noundef %28, i32 noundef %30)
  %32 = icmp eq i32 %31, 1
  br i1 %32, label %33, label %43

33:                                               ; preds = %._crit_edge
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %4) #3
  %34 = add nsw i32 %2, 1
  %35 = srem i32 %34, 4
  %36 = tail call noundef i32 @_Z3dfsiii(i32 noundef %16, i32 noundef %18, i32 noundef %35)
  store i32 %36, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %5) #3
  %37 = add nsw i32 %2, 3
  %38 = srem i32 %37, 4
  %39 = tail call noundef i32 @_Z3dfsiii(i32 noundef %16, i32 noundef %18, i32 noundef %38)
  store i32 %39, ptr %5, align 4, !tbaa !5
  %40 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %4, ptr noundef nonnull align 4 dereferenceable(4) %5)
  %41 = load i32, ptr %40, align 4, !tbaa !5
  %42 = add nsw i32 %41, %22
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %4) #3
  br label %43

43:                                               ; preds = %33, %._crit_edge
  %.1 = phi i32 [ %42, %33 ], [ %22, %._crit_edge ]
  %44 = icmp ne i32 %16, %0
  %45 = icmp ne i32 %18, %1
  %46 = or i1 %44, %45
  br i1 %46, label %.lr.ph53.preheader, label %.loopexit

.lr.ph53.preheader:                               ; preds = %43
  %.pre = load i32, ptr %7, align 4, !tbaa !5
  %.pre56 = load i32, ptr %8, align 4, !tbaa !5
  br label %.lr.ph53

.lr.ph53:                                         ; preds = %.lr.ph53.preheader, %.lr.ph53
  %.13951 = phi i32 [ %50, %.lr.ph53 ], [ %16, %.lr.ph53.preheader ]
  %.14150 = phi i32 [ %51, %.lr.ph53 ], [ %18, %.lr.ph53.preheader ]
  %47 = sext i32 %.13951 to i64
  %48 = sext i32 %.14150 to i64
  %49 = getelementptr inbounds [120 x [120 x i32]], ptr @ar, i64 0, i64 %47, i64 %48
  store i32 0, ptr %49, align 4, !tbaa !5
  %50 = sub nsw i32 %.13951, %.pre
  %51 = sub nsw i32 %.14150, %.pre56
  %52 = icmp ne i32 %50, %0
  %53 = icmp ne i32 %51, %1
  %54 = select i1 %52, i1 true, i1 %53
  br i1 %54, label %.lr.ph53, label %.loopexit, !llvm.loop !11

.loopexit:                                        ; preds = %.lr.ph53, %3, %43
  %.037 = phi i32 [ %.1, %43 ], [ 0, %3 ], [ %.1, %.lr.ph53 ]
  ret i32 %.037
}

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #2

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

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 nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint 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 #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"}
!11 = distinct !{!11, !10}


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