; LLVM IR from Tunghohin/Competitive_Coding :: Codeforces/cf1517_C.ll (Z6insertiiii)

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

@res = external dso_local global [510 x [510 x i32]], align 16
@dx = external dso_local global [2 x i32], align 4
@dy = external dso_local global [2 x i32], align 4
@cnt = external dso_local global i32, align 4

; Function Attrs: mustprogress uwtable
define dso_local void @_Z6insertiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) #0 {
  %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
  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
  store i32 %3, ptr %8, align 4, !tbaa !5
  %12 = load i32, ptr @cnt, align 4, !tbaa !5
  %13 = load i32, ptr %7, align 4, !tbaa !5
  %14 = icmp sge i32 %12, %13
  br i1 %14, label %15, label %16

15:                                               ; preds = %4
  br label %67

16:                                               ; preds = %4
  %17 = load i32, ptr %7, align 4, !tbaa !5
  %18 = load i32, ptr %5, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [510 x [510 x i32]], ptr @res, i64 0, i64 %19
  %21 = load i32, ptr %6, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [510 x i32], ptr %20, i64 0, i64 %22
  store i32 %17, ptr %23, align 4, !tbaa !5
  %24 = load i32, ptr @cnt, align 4, !tbaa !5
  %25 = add nsw i32 %24, 1
  store i32 %25, ptr @cnt, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  store i32 0, ptr %9, align 4, !tbaa !5
  br label %26

26:                                               ; preds = %64, %16
  %27 = load i32, ptr %9, align 4, !tbaa !5
  %28 = icmp slt i32 %27, 2
  br i1 %28, label %30, label %29

29:                                               ; preds = %26
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  br label %67

30:                                               ; preds = %26
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  %31 = load i32, ptr %5, align 4, !tbaa !5
  %32 = load i32, ptr %9, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [2 x i32], ptr @dx, i64 0, i64 %33
  %35 = load i32, ptr %34, align 4, !tbaa !5
  %36 = add nsw i32 %31, %35
  store i32 %36, ptr %10, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  %37 = load i32, ptr %6, align 4, !tbaa !5
  %38 = load i32, ptr %9, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [2 x i32], ptr @dy, i64 0, i64 %39
  %41 = load i32, ptr %40, align 4, !tbaa !5
  %42 = add nsw i32 %37, %41
  store i32 %42, ptr %11, align 4, !tbaa !5
  %43 = load i32, ptr %10, align 4, !tbaa !5
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds [510 x [510 x i32]], ptr @res, i64 0, i64 %44
  %46 = load i32, ptr %11, align 4, !tbaa !5
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds [510 x i32], ptr %45, i64 0, i64 %47
  %49 = load i32, ptr %48, align 4, !tbaa !5
  %50 = icmp ne i32 %49, 0
  br i1 %50, label %63, label %51

51:                                               ; preds = %30
  %52 = load i32, ptr %10, align 4, !tbaa !5
  %53 = load i32, ptr %8, align 4, !tbaa !5
  %54 = icmp sle i32 %52, %53
  br i1 %54, label %55, label %63

55:                                               ; preds = %51
  %56 = load i32, ptr %11, align 4, !tbaa !5
  %57 = icmp sge i32 %56, 1
  br i1 %57, label %58, label %63

58:                                               ; preds = %55
  %59 = load i32, ptr %10, align 4, !tbaa !5
  %60 = load i32, ptr %11, align 4, !tbaa !5
  %61 = load i32, ptr %7, align 4, !tbaa !5
  %62 = load i32, ptr %8, align 4, !tbaa !5
  call void @_Z6insertiiii(i32 noundef %59, i32 noundef %60, i32 noundef %61, i32 noundef %62)
  br label %63

63:                                               ; preds = %58, %55, %51, %30
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  br label %64

64:                                               ; preds = %63
  %65 = load i32, ptr %9, align 4, !tbaa !5
  %66 = add nsw i32 %65, 1
  store i32 %66, ptr %9, align 4, !tbaa !5
  br label %26, !llvm.loop !9

67:                                               ; preds = %29, %15
  ret void
}

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

@res = external dso_local local_unnamed_addr global [510 x [510 x i32]], align 16
@dx = external dso_local local_unnamed_addr global [2 x i32], align 4
@dy = external dso_local local_unnamed_addr global [2 x i32], align 4
@cnt = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress nofree nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z6insertiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  br label %tailrecurse

tailrecurse:                                      ; preds = %32, %4
  %.tr = phi i32 [ %0, %4 ], [ %25, %32 ]
  %.tr27 = phi i32 [ %1, %4 ], [ %27, %32 ]
  %5 = load i32, ptr @cnt, align 4, !tbaa !5
  %.not = icmp slt i32 %5, %2
  br i1 %.not, label %6, label %.loopexit

6:                                                ; preds = %tailrecurse
  %7 = sext i32 %.tr to i64
  %8 = sext i32 %.tr27 to i64
  %9 = getelementptr inbounds [510 x [510 x i32]], ptr @res, i64 0, i64 %7, i64 %8
  store i32 %2, ptr %9, align 4, !tbaa !5
  %10 = add nsw i32 %5, 1
  store i32 %10, ptr @cnt, align 4, !tbaa !5
  %11 = load i32, ptr @dx, align 4, !tbaa !5
  %12 = add nsw i32 %11, %.tr
  %13 = load i32, ptr @dy, align 4, !tbaa !5
  %14 = add nsw i32 %13, %.tr27
  %15 = sext i32 %12 to i64
  %16 = sext i32 %14 to i64
  %17 = getelementptr inbounds [510 x [510 x i32]], ptr @res, i64 0, i64 %15, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %.not24 = icmp eq i32 %18, 0
  br i1 %.not24, label %19, label %23

19:                                               ; preds = %6
  %20 = icmp sle i32 %12, %3
  %21 = icmp sgt i32 %14, 0
  %or.cond = and i1 %20, %21
  br i1 %or.cond, label %22, label %23

22:                                               ; preds = %19
  tail call void @_Z6insertiiii(i32 noundef %12, i32 noundef %14, i32 noundef %2, i32 noundef %3)
  br label %23

23:                                               ; preds = %22, %19, %6
  %24 = load i32, ptr getelementptr inbounds (i8, ptr @dx, i64 4), align 4, !tbaa !5
  %25 = add nsw i32 %24, %.tr
  %26 = load i32, ptr getelementptr inbounds (i8, ptr @dy, i64 4), align 4, !tbaa !5
  %27 = add nsw i32 %26, %.tr27
  %28 = sext i32 %25 to i64
  %29 = sext i32 %27 to i64
  %30 = getelementptr inbounds [510 x [510 x i32]], ptr @res, i64 0, i64 %28, i64 %29
  %31 = load i32, ptr %30, align 4, !tbaa !5
  %.not24.1 = icmp eq i32 %31, 0
  br i1 %.not24.1, label %32, label %.loopexit

32:                                               ; preds = %23
  %33 = icmp sle i32 %25, %3
  %34 = icmp sgt i32 %27, 0
  %or.cond.1 = and i1 %33, %34
  br i1 %or.cond.1, label %tailrecurse, label %.loopexit

.loopexit:                                        ; preds = %23, %32, %tailrecurse
  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,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass