; LLVM IR from Tunghohin/Competitive_Coding :: Codeforces/cf510_D.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"

%struct.Node = type { i32, i32, i64 }

@idx = external dso_local global i32, align 4
@tr = external dso_local global [3600180 x %struct.Node], align 16

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

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z6insertiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) #1 {
  %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
  store i32 %0, ptr %6, align 4, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !5
  store i32 %2, ptr %8, align 4, !tbaa !5
  store i32 %3, ptr %9, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  %13 = load i32, ptr @idx, align 4, !tbaa !5
  %14 = add nsw i32 %13, 1
  store i32 %14, ptr @idx, align 4, !tbaa !5
  store i32 %14, ptr %10, align 4, !tbaa !5
  %15 = load i32, ptr %6, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [3600180 x %struct.Node], ptr @tr, i64 0, i64 %16
  %18 = load i32, ptr %10, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [3600180 x %struct.Node], ptr @tr, i64 0, i64 %19
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %20, ptr align 16 %17, i64 16, i1 false), !tbaa.struct !9
  %21 = load i32, ptr %7, align 4, !tbaa !5
  %22 = load i32, ptr %8, align 4, !tbaa !5
  %23 = icmp eq i32 %21, %22
  br i1 %23, label %24, label %32

24:                                               ; preds = %4
  %25 = load i32, ptr %10, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [3600180 x %struct.Node], ptr @tr, i64 0, i64 %26
  %28 = getelementptr inbounds %struct.Node, ptr %27, i32 0, i32 2
  %29 = load i64, ptr %28, align 8, !tbaa !12
  %30 = add nsw i64 %29, 1
  store i64 %30, ptr %28, align 8, !tbaa !12
  %31 = load i32, ptr %10, align 4, !tbaa !5
  store i32 %31, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %94

32:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  %33 = load i32, ptr %7, align 4, !tbaa !5
  %34 = load i32, ptr %8, align 4, !tbaa !5
  %35 = add nsw i32 %33, %34
  %36 = ashr i32 %35, 1
  store i32 %36, ptr %12, align 4, !tbaa !5
  %37 = load i32, ptr %9, align 4, !tbaa !5
  %38 = load i32, ptr %12, align 4, !tbaa !5
  %39 = icmp sle i32 %37, %38
  br i1 %39, label %40, label %54

40:                                               ; preds = %32
  %41 = load i32, ptr %6, align 4, !tbaa !5
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds [3600180 x %struct.Node], ptr @tr, i64 0, i64 %42
  %44 = getelementptr inbounds %struct.Node, ptr %43, i32 0, i32 0
  %45 = load i32, ptr %44, align 16, !tbaa !14
  %46 = load i32, ptr %7, align 4, !tbaa !5
  %47 = load i32, ptr %12, align 4, !tbaa !5
  %48 = load i32, ptr %9, align 4, !tbaa !5
  %49 = call noundef i32 @_Z6insertiiii(i32 noundef %45, i32 noundef %46, i32 noundef %47, i32 noundef %48)
  %50 = load i32, ptr %10, align 4, !tbaa !5
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds [3600180 x %struct.Node], ptr @tr, i64 0, i64 %51
  %53 = getelementptr inbounds %struct.Node, ptr %52, i32 0, i32 0
  store i32 %49, ptr %53, align 16, !tbaa !14
  br label %69

54:                                               ; preds = %32
  %55 = load i32, ptr %6, align 4, !tbaa !5
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [3600180 x %struct.Node], ptr @tr, i64 0, i64 %56
  %58 = getelementptr inbounds %struct.Node, ptr %57, i32 0, i32 1
  %59 = load i32, ptr %58, align 4, !tbaa !15
  %60 = load i32, ptr %12, align 4, !tbaa !5
  %61 = add nsw i32 %60, 1
  %62 = load i32, ptr %8, align 4, !tbaa !5
  %63 = load i32, ptr %9, align 4, !tbaa !5
  %64 = call noundef i32 @_Z6insertiiii(i32 noundef %59, i32 noundef %61, i32 noundef %62, i32 noundef %63)
  %65 = load i32, ptr %10, align 4, !tbaa !5
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds [3600180 x %struct.Node], ptr @tr, i64 0, i64 %66
  %68 = getelementptr inbounds %struct.Node, ptr %67, i32 0, i32 1
  store i32 %64, ptr %68, align 4, !tbaa !15
  br label %69

69:                                               ; preds = %54, %40
  %70 = load i32, ptr %10, align 4, !tbaa !5
  %71 = sext i32 %70 to i64
  %72 = getelementptr inbounds [3600180 x %struct.Node], ptr @tr, i64 0, i64 %71
  %73 = getelementptr inbounds %struct.Node, ptr %72, i32 0, i32 0
  %74 = load i32, ptr %73, align 16, !tbaa !14
  %75 = sext i32 %74 to i64
  %76 = getelementptr inbounds [3600180 x %struct.Node], ptr @tr, i64 0, i64 %75
  %77 = getelementptr inbounds %struct.Node, ptr %76, i32 0, i32 2
  %78 = load i64, ptr %77, align 8, !tbaa !12
  %79 = load i32, ptr %10, align 4, !tbaa !5
  %80 = sext i32 %79 to i64
  %81 = getelementptr inbounds [3600180 x %struct.Node], ptr @tr, i64 0, i64 %80
  %82 = getelementptr inbounds %struct.Node, ptr %81, i32 0, i32 1
  %83 = load i32, ptr %82, align 4, !tbaa !15
  %84 = sext i32 %83 to i64
  %85 = getelementptr inbounds [3600180 x %struct.Node], ptr @tr, i64 0, i64 %84
  %86 = getelementptr inbounds %struct.Node, ptr %85, i32 0, i32 2
  %87 = load i64, ptr %86, align 8, !tbaa !12
  %88 = add nsw i64 %78, %87
  %89 = load i32, ptr %10, align 4, !tbaa !5
  %90 = sext i32 %89 to i64
  %91 = getelementptr inbounds [3600180 x %struct.Node], ptr @tr, i64 0, i64 %90
  %92 = getelementptr inbounds %struct.Node, ptr %91, i32 0, i32 2
  store i64 %88, ptr %92, align 8, !tbaa !12
  %93 = load i32, ptr %10, align 4, !tbaa !5
  store i32 %93, ptr %5, align 4
  store i32 1, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  br label %94

94:                                               ; preds = %69, %24
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  %95 = load i32, ptr %5, align 4
  ret i32 %95
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 = !{i64 0, i64 4, !5, i64 4, i64 4, !5, i64 8, i64 8, !10}
!10 = !{!11, !11, i64 0}
!11 = !{!"long long", !7, i64 0}
!12 = !{!13, !11, i64 8}
!13 = !{!"_ZTS4Node", !6, i64 0, !6, i64 4, !11, i64 8}
!14 = !{!13, !6, i64 0}
!15 = !{!13, !6, i64 4}


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

%struct.Node = type { i32, i32, i64 }

@idx = external dso_local local_unnamed_addr global i32, align 4
@tr = external dso_local local_unnamed_addr global [3600180 x %struct.Node], align 16

; Function Attrs: mustprogress nofree nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef range(i32 -2147483647, -2147483648) i32 @_Z6insertiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = load i32, ptr @idx, align 4, !tbaa !5
  %6 = add nsw i32 %5, 1
  store i32 %6, ptr @idx, align 4, !tbaa !5
  %7 = sext i32 %0 to i64
  %8 = getelementptr inbounds [3600180 x %struct.Node], ptr @tr, i64 0, i64 %7
  %9 = sext i32 %6 to i64
  %10 = getelementptr inbounds [3600180 x %struct.Node], ptr @tr, i64 0, i64 %9
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(16) %10, ptr noundef nonnull align 16 dereferenceable(16) %8, i64 16, i1 false), !tbaa.struct !9
  %11 = icmp eq i32 %1, %2
  br i1 %11, label %12, label %16

12:                                               ; preds = %4
  %13 = getelementptr inbounds i8, ptr %10, i64 8
  %14 = load i64, ptr %13, align 8, !tbaa !12
  %15 = add nsw i64 %14, 1
  store i64 %15, ptr %13, align 8, !tbaa !12
  br label %39

16:                                               ; preds = %4
  %17 = add nsw i32 %2, %1
  %18 = ashr i32 %17, 1
  %.not = icmp slt i32 %18, %3
  br i1 %.not, label %22, label %19

19:                                               ; preds = %16
  %20 = load i32, ptr %8, align 16, !tbaa !14
  %21 = tail call noundef i32 @_Z6insertiiii(i32 noundef %20, i32 noundef %1, i32 noundef %18, i32 noundef %3)
  store i32 %21, ptr %10, align 16, !tbaa !14
  %.phi.trans.insert = getelementptr inbounds i8, ptr %10, i64 4
  %.pre29 = load i32, ptr %.phi.trans.insert, align 4, !tbaa !15
  br label %28

22:                                               ; preds = %16
  %23 = getelementptr inbounds i8, ptr %8, i64 4
  %24 = load i32, ptr %23, align 4, !tbaa !15
  %25 = add nsw i32 %18, 1
  %26 = tail call noundef i32 @_Z6insertiiii(i32 noundef %24, i32 noundef %25, i32 noundef %2, i32 noundef %3)
  %27 = getelementptr inbounds i8, ptr %10, i64 4
  store i32 %26, ptr %27, align 4, !tbaa !15
  %.pre = load i32, ptr %10, align 16, !tbaa !14
  br label %28

28:                                               ; preds = %22, %19
  %29 = phi i32 [ %26, %22 ], [ %.pre29, %19 ]
  %30 = phi i32 [ %.pre, %22 ], [ %21, %19 ]
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [3600180 x %struct.Node], ptr @tr, i64 0, i64 %31, i32 2
  %33 = load i64, ptr %32, align 8, !tbaa !12
  %34 = sext i32 %29 to i64
  %35 = getelementptr inbounds [3600180 x %struct.Node], ptr @tr, i64 0, i64 %34, i32 2
  %36 = load i64, ptr %35, align 8, !tbaa !12
  %37 = add nsw i64 %36, %33
  %38 = getelementptr inbounds i8, ptr %10, i64 8
  store i64 %37, ptr %38, align 8, !tbaa !12
  br label %39

39:                                               ; preds = %28, %12
  ret i32 %6
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

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" }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }

!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 = !{i64 0, i64 4, !5, i64 4, i64 4, !5, i64 8, i64 8, !10}
!10 = !{!11, !11, i64 0}
!11 = !{!"long long", !7, i64 0}
!12 = !{!13, !11, i64 8}
!13 = !{!"_ZTS4Node", !6, i64 0, !6, i64 4, !11, i64 8}
!14 = !{!13, !6, i64 0}
!15 = !{!13, !6, i64 4}


; Effective O3 passes: BDCEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,TailCallElimPass