; LLVM IR from VastoLorde95/Competitive-Programming :: CodeChef/SHORT/COOK65/Problem E/E.ll (Z10chainQueryxxx)

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

@n = external dso_local global i64, align 8
@chainHead = external dso_local global [524288 x i64], align 16
@pos = external dso_local global [524288 x i64], align 16
@chain = external dso_local global [524288 x i64], align 16
@p = external dso_local global [19 x [524288 x i64]], 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
declare dso_local noundef i64 @_Z12querySegTreexxxxxx(i64 noundef, i64 noundef, i64 noundef, i64 noundef, i64 noundef, i64 noundef) #1

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z10chainQueryxxx(i64 noundef %0, i64 noundef %1, i64 noundef %2) #1 {
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  store i64 %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !5
  store i64 %2, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  store i64 0, ptr %7, align 8, !tbaa !5
  br label %9

9:                                                ; preds = %33, %3
  br label %10

10:                                               ; preds = %9
  %11 = load i64, ptr %4, align 8, !tbaa !5
  %12 = getelementptr inbounds [524288 x i64], ptr @chain, i64 0, i64 %11
  %13 = load i64, ptr %12, align 8, !tbaa !5
  %14 = load i64, ptr %5, align 8, !tbaa !5
  %15 = getelementptr inbounds [524288 x i64], ptr @chain, i64 0, i64 %14
  %16 = load i64, ptr %15, align 8, !tbaa !5
  %17 = icmp eq i64 %13, %16
  br i1 %17, label %18, label %33

18:                                               ; preds = %10
  %19 = load i64, ptr %5, align 8, !tbaa !5
  %20 = getelementptr inbounds [524288 x i64], ptr @pos, i64 0, i64 %19
  %21 = load i64, ptr %20, align 8, !tbaa !5
  %22 = load i64, ptr %6, align 8, !tbaa !5
  %23 = add nsw i64 %21, %22
  %24 = load i64, ptr %4, align 8, !tbaa !5
  %25 = getelementptr inbounds [524288 x i64], ptr @pos, i64 0, i64 %24
  %26 = load i64, ptr %25, align 8, !tbaa !5
  %27 = add nsw i64 %26, 1
  %28 = load i64, ptr @n, align 8, !tbaa !5
  %29 = call noundef i64 @_Z12querySegTreexxxxxx(i64 noundef %23, i64 noundef %27, i64 noundef 1, i64 noundef 0, i64 noundef %28, i64 noundef 0)
  %30 = load i64, ptr %7, align 8, !tbaa !5
  %31 = add nsw i64 %30, %29
  store i64 %31, ptr %7, align 8, !tbaa !5
  %32 = load i64, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  ret i64 %32

33:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %34 = load i64, ptr %4, align 8, !tbaa !5
  %35 = getelementptr inbounds [524288 x i64], ptr @chain, i64 0, i64 %34
  %36 = load i64, ptr %35, align 8, !tbaa !5
  %37 = getelementptr inbounds [524288 x i64], ptr @chainHead, i64 0, i64 %36
  %38 = load i64, ptr %37, align 8, !tbaa !5
  store i64 %38, ptr %8, align 8, !tbaa !5
  %39 = load i64, ptr %8, align 8, !tbaa !5
  %40 = getelementptr inbounds [524288 x i64], ptr @pos, i64 0, i64 %39
  %41 = load i64, ptr %40, align 8, !tbaa !5
  %42 = load i64, ptr %4, align 8, !tbaa !5
  %43 = getelementptr inbounds [524288 x i64], ptr @pos, i64 0, i64 %42
  %44 = load i64, ptr %43, align 8, !tbaa !5
  %45 = add nsw i64 %44, 1
  %46 = load i64, ptr @n, align 8, !tbaa !5
  %47 = call noundef i64 @_Z12querySegTreexxxxxx(i64 noundef %41, i64 noundef %45, i64 noundef 1, i64 noundef 0, i64 noundef %46, i64 noundef 0)
  %48 = load i64, ptr %7, align 8, !tbaa !5
  %49 = add nsw i64 %48, %47
  store i64 %49, ptr %7, align 8, !tbaa !5
  %50 = load i64, ptr %8, align 8, !tbaa !5
  %51 = getelementptr inbounds [524288 x i64], ptr @p, i64 0, i64 %50
  %52 = load i64, ptr %51, align 8, !tbaa !5
  store i64 %52, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  br label %9, !llvm.loop !9
}

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 = { 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 = !{!"long long", !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"

@n = external dso_local local_unnamed_addr global i64, align 8
@chainHead = external dso_local local_unnamed_addr global [524288 x i64], align 16
@pos = external dso_local local_unnamed_addr global [524288 x i64], align 16
@chain = external dso_local local_unnamed_addr global [524288 x i64], align 16
@p = external dso_local local_unnamed_addr global [19 x [524288 x i64]], align 16

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_Z12querySegTreexxxxxx(i64 noundef, i64 noundef, i64 noundef, i64 noundef, i64 noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z10chainQueryxxx(i64 noundef %0, i64 noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = getelementptr inbounds [524288 x i64], ptr @chain, i64 0, i64 %1
  %5 = getelementptr inbounds [524288 x i64], ptr @chain, i64 0, i64 %0
  %6 = load i64, ptr %5, align 8, !tbaa !5
  %7 = load i64, ptr %4, align 8, !tbaa !5
  %8 = icmp eq i64 %6, %7
  br i1 %8, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %3
  %.012.lcssa = phi i64 [ 0, %3 ], [ %28, %.lr.ph ]
  %.0.lcssa = phi i64 [ %0, %3 ], [ %30, %.lr.ph ]
  %9 = getelementptr inbounds [524288 x i64], ptr @pos, i64 0, i64 %1
  %10 = load i64, ptr %9, align 8, !tbaa !5
  %11 = add nsw i64 %10, %2
  %12 = getelementptr inbounds [524288 x i64], ptr @pos, i64 0, i64 %.0.lcssa
  %13 = load i64, ptr %12, align 8, !tbaa !5
  %14 = add nsw i64 %13, 1
  %15 = load i64, ptr @n, align 8, !tbaa !5
  %16 = tail call noundef i64 @_Z12querySegTreexxxxxx(i64 noundef %11, i64 noundef %14, i64 noundef 1, i64 noundef 0, i64 noundef %15, i64 noundef 0)
  %17 = add nsw i64 %16, %.012.lcssa
  ret i64 %17

.lr.ph:                                           ; preds = %3, %.lr.ph
  %18 = phi i64 [ %32, %.lr.ph ], [ %6, %3 ]
  %.015 = phi i64 [ %30, %.lr.ph ], [ %0, %3 ]
  %.01214 = phi i64 [ %28, %.lr.ph ], [ 0, %3 ]
  %19 = getelementptr inbounds [524288 x i64], ptr @chainHead, i64 0, i64 %18
  %20 = load i64, ptr %19, align 8, !tbaa !5
  %21 = getelementptr inbounds [524288 x i64], ptr @pos, i64 0, i64 %20
  %22 = load i64, ptr %21, align 8, !tbaa !5
  %23 = getelementptr inbounds [524288 x i64], ptr @pos, i64 0, i64 %.015
  %24 = load i64, ptr %23, align 8, !tbaa !5
  %25 = add nsw i64 %24, 1
  %26 = load i64, ptr @n, align 8, !tbaa !5
  %27 = tail call noundef i64 @_Z12querySegTreexxxxxx(i64 noundef %22, i64 noundef %25, i64 noundef 1, i64 noundef 0, i64 noundef %26, i64 noundef 0)
  %28 = add nsw i64 %27, %.01214
  %29 = getelementptr inbounds [524288 x i64], ptr @p, i64 0, i64 %20
  %30 = load i64, ptr %29, align 8, !tbaa !5
  %31 = getelementptr inbounds [524288 x i64], ptr @chain, i64 0, i64 %30
  %32 = load i64, ptr %31, align 8, !tbaa !5
  %33 = load i64, ptr %4, align 8, !tbaa !5
  %34 = icmp eq i64 %32, %33
  br i1 %34, label %._crit_edge, label %.lr.ph, !llvm.loop !9
}

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

!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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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