; LLVM IR from tangjz/acm-icpc :: codeforces/complete/757G.ll (Z8tree_queii)

; 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.Info = type { i32, i32, i32, i32, i64, i64 }

@dis = external dso_local global [31 x [400001 x i64]], align 16
@tree = external dso_local global [20000001 x %struct.Info], 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 nounwind uwtable
define dso_local noundef i64 @_Z8tree_queii(i32 noundef %0, i32 noundef %1) #1 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  store i64 0, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 0, ptr %6, align 4, !tbaa !5
  br label %7

7:                                                ; preds = %80, %2
  %8 = load i32, ptr %3, align 4, !tbaa !5
  %9 = icmp ne i32 %8, 0
  br i1 %9, label %11, label %10

10:                                               ; preds = %7
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %83

11:                                               ; preds = %7
  %12 = load i32, ptr %6, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [31 x [400001 x i64]], ptr @dis, i64 0, i64 %13
  %15 = load i32, ptr %4, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [400001 x i64], ptr %14, i64 0, i64 %16
  %18 = load i64, ptr %17, align 8, !tbaa !9
  %19 = icmp slt i64 %18, 0
  br i1 %19, label %20, label %50

20:                                               ; preds = %11
  %21 = load i32, ptr %3, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [20000001 x %struct.Info], ptr @tree, i64 0, i64 %22
  %24 = getelementptr inbounds %struct.Info, ptr %23, i32 0, i32 5
  %25 = load i64, ptr %24, align 8, !tbaa !11
  %26 = load i32, ptr %3, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [20000001 x %struct.Info], ptr @tree, i64 0, i64 %27
  %29 = getelementptr inbounds %struct.Info, ptr %28, i32 0, i32 3
  %30 = load i32, ptr %29, align 4, !tbaa !13
  %31 = sext i32 %30 to i64
  %32 = load i32, ptr %6, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [31 x [400001 x i64]], ptr @dis, i64 0, i64 %33
  %35 = load i32, ptr %4, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [400001 x i64], ptr %34, i64 0, i64 %36
  %38 = load i64, ptr %37, align 8, !tbaa !9
  %39 = sub nsw i64 0, %38
  %40 = sub nsw i64 %39, 1
  %41 = mul nsw i64 %31, %40
  %42 = add nsw i64 %25, %41
  %43 = load i64, ptr %5, align 8, !tbaa !9
  %44 = add nsw i64 %43, %42
  store i64 %44, ptr %5, align 8, !tbaa !9
  %45 = load i32, ptr %3, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [20000001 x %struct.Info], ptr @tree, i64 0, i64 %46
  %48 = getelementptr inbounds %struct.Info, ptr %47, i32 0, i32 0
  %49 = load i32, ptr %48, align 16, !tbaa !14
  store i32 %49, ptr %3, align 4, !tbaa !5
  br label %79

50:                                               ; preds = %11
  %51 = load i32, ptr %3, align 4, !tbaa !5
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds [20000001 x %struct.Info], ptr @tree, i64 0, i64 %52
  %54 = getelementptr inbounds %struct.Info, ptr %53, i32 0, i32 4
  %55 = load i64, ptr %54, align 16, !tbaa !15
  %56 = load i32, ptr %3, align 4, !tbaa !5
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds [20000001 x %struct.Info], ptr @tree, i64 0, i64 %57
  %59 = getelementptr inbounds %struct.Info, ptr %58, i32 0, i32 2
  %60 = load i32, ptr %59, align 8, !tbaa !16
  %61 = sext i32 %60 to i64
  %62 = load i32, ptr %6, align 4, !tbaa !5
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds [31 x [400001 x i64]], ptr @dis, i64 0, i64 %63
  %65 = load i32, ptr %4, align 4, !tbaa !5
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds [400001 x i64], ptr %64, i64 0, i64 %66
  %68 = load i64, ptr %67, align 8, !tbaa !9
  %69 = sub nsw i64 %68, 1
  %70 = mul nsw i64 %61, %69
  %71 = add nsw i64 %55, %70
  %72 = load i64, ptr %5, align 8, !tbaa !9
  %73 = add nsw i64 %72, %71
  store i64 %73, ptr %5, align 8, !tbaa !9
  %74 = load i32, ptr %3, align 4, !tbaa !5
  %75 = sext i32 %74 to i64
  %76 = getelementptr inbounds [20000001 x %struct.Info], ptr @tree, i64 0, i64 %75
  %77 = getelementptr inbounds %struct.Info, ptr %76, i32 0, i32 1
  %78 = load i32, ptr %77, align 4, !tbaa !17
  store i32 %78, ptr %3, align 4, !tbaa !5
  br label %79

79:                                               ; preds = %50, %20
  br label %80

80:                                               ; preds = %79
  %81 = load i32, ptr %6, align 4, !tbaa !5
  %82 = add nsw i32 %81, 1
  store i32 %82, ptr %6, align 4, !tbaa !5
  br label %7, !llvm.loop !18

83:                                               ; preds = %10
  %84 = load i64, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  ret i64 %84
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = !{!10, !10, i64 0}
!10 = !{!"long long", !7, i64 0}
!11 = !{!12, !10, i64 24}
!12 = !{!"_ZTS4Info", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12, !10, i64 16, !10, i64 24}
!13 = !{!12, !6, i64 12}
!14 = !{!12, !6, i64 0}
!15 = !{!12, !10, i64 16}
!16 = !{!12, !6, i64 8}
!17 = !{!12, !6, i64 4}
!18 = distinct !{!18, !19}
!19 = !{!"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"

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

@dis = external dso_local local_unnamed_addr global [31 x [400001 x i64]], align 16
@tree = external dso_local local_unnamed_addr global [20000001 x %struct.Info], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, inaccessiblemem: none) uwtable
define dso_local noundef i64 @_Z8tree_queii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %.not22 = icmp eq i32 %0, 0
  br i1 %.not22, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %2
  %3 = sext i32 %1 to i64
  br label %4

._crit_edge:                                      ; preds = %29, %2
  %.017.lcssa = phi i64 [ 0, %2 ], [ %.1, %29 ]
  ret i64 %.017.lcssa

4:                                                ; preds = %.lr.ph, %29
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %29 ]
  %.01724 = phi i64 [ 0, %.lr.ph ], [ %.1, %29 ]
  %.01823 = phi i32 [ %0, %.lr.ph ], [ %.119, %29 ]
  %5 = getelementptr inbounds [31 x [400001 x i64]], ptr @dis, i64 0, i64 %indvars.iv, i64 %3
  %6 = load i64, ptr %5, align 8, !tbaa !5
  %7 = icmp slt i64 %6, 0
  %8 = sext i32 %.01823 to i64
  %9 = getelementptr inbounds [20000001 x %struct.Info], ptr @tree, i64 0, i64 %8
  br i1 %7, label %10, label %19

10:                                               ; preds = %4
  %11 = getelementptr inbounds i8, ptr %9, i64 24
  %12 = load i64, ptr %11, align 8, !tbaa !9
  %13 = getelementptr inbounds i8, ptr %9, i64 12
  %14 = load i32, ptr %13, align 4, !tbaa !12
  %15 = sext i32 %14 to i64
  %16 = xor i64 %6, -1
  %17 = mul nsw i64 %15, %16
  %18 = add nsw i64 %17, %12
  br label %29

19:                                               ; preds = %4
  %20 = getelementptr inbounds i8, ptr %9, i64 16
  %21 = load i64, ptr %20, align 16, !tbaa !13
  %22 = getelementptr inbounds i8, ptr %9, i64 8
  %23 = load i32, ptr %22, align 8, !tbaa !14
  %24 = sext i32 %23 to i64
  %25 = add nsw i64 %6, -1
  %26 = mul nsw i64 %25, %24
  %27 = add nsw i64 %26, %21
  %28 = getelementptr inbounds i8, ptr %9, i64 4
  br label %29

29:                                               ; preds = %10, %19
  %.119.in = phi ptr [ %9, %10 ], [ %28, %19 ]
  %.pn = phi i64 [ %18, %10 ], [ %27, %19 ]
  %.1 = add nsw i64 %.pn, %.01724
  %.119 = load i32, ptr %.119.in, align 4, !tbaa !15
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %.not = icmp eq i32 %.119, 0
  br i1 %.not, label %._crit_edge, label %4, !llvm.loop !16
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, 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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !6, i64 24}
!10 = !{!"_ZTS4Info", !11, i64 0, !11, i64 4, !11, i64 8, !11, i64 12, !6, i64 16, !6, i64 24}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !11, i64 12}
!13 = !{!10, !6, i64 16}
!14 = !{!10, !11, i64 8}
!15 = !{!11, !11, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


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