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

; 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 void @_Z13updateSegTreexxxxxx(i64 noundef, i64 noundef, i64 noundef, i64 noundef, i64 noundef, i64 noundef) #1

; Function Attrs: mustprogress uwtable
define dso_local void @_Z11chainUpdatexxxx(i64 noundef %0, i64 noundef %1, i64 noundef %2, i64 noundef %3) #1 {
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  store i64 %0, ptr %5, align 8, !tbaa !5
  store i64 %1, ptr %6, align 8, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !5
  store i64 %3, ptr %8, align 8, !tbaa !5
  br label %10

10:                                               ; preds = %30, %4
  %11 = load i64, ptr %5, 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 %6, 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 %30

18:                                               ; preds = %10
  %19 = load i64, ptr %6, 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 %8, align 8, !tbaa !5
  %23 = add nsw i64 %21, %22
  %24 = load i64, ptr %5, 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 %7, align 8, !tbaa !5
  %29 = load i64, ptr @n, align 8, !tbaa !5
  call void @_Z13updateSegTreexxxxxx(i64 noundef %23, i64 noundef %27, i64 noundef %28, i64 noundef 1, i64 noundef 0, i64 noundef %29)
  ret void

30:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  %31 = load i64, ptr %5, align 8, !tbaa !5
  %32 = getelementptr inbounds [524288 x i64], ptr @chain, i64 0, i64 %31
  %33 = load i64, ptr %32, align 8, !tbaa !5
  %34 = getelementptr inbounds [524288 x i64], ptr @chainHead, i64 0, i64 %33
  %35 = load i64, ptr %34, align 8, !tbaa !5
  store i64 %35, ptr %9, align 8, !tbaa !5
  %36 = load i64, ptr %9, align 8, !tbaa !5
  %37 = getelementptr inbounds [524288 x i64], ptr @pos, i64 0, i64 %36
  %38 = load i64, ptr %37, align 8, !tbaa !5
  %39 = load i64, ptr %5, 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 = add nsw i64 %41, 1
  %43 = load i64, ptr %7, align 8, !tbaa !5
  %44 = load i64, ptr @n, align 8, !tbaa !5
  call void @_Z13updateSegTreexxxxxx(i64 noundef %38, i64 noundef %42, i64 noundef %43, i64 noundef 1, i64 noundef 0, i64 noundef %44)
  %45 = load i64, ptr %9, align 8, !tbaa !5
  %46 = getelementptr inbounds [524288 x i64], ptr @p, i64 0, i64 %45
  %47 = load i64, ptr %46, align 8, !tbaa !5
  store i64 %47, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  br label %10, !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 void @_Z13updateSegTreexxxxxx(i64 noundef, i64 noundef, i64 noundef, i64 noundef, i64 noundef, i64 noundef) local_unnamed_addr #0

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

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

.lr.ph:                                           ; preds = %4, %.lr.ph
  %17 = phi i64 [ %29, %.lr.ph ], [ %7, %4 ]
  %.012 = phi i64 [ %27, %.lr.ph ], [ %0, %4 ]
  %18 = getelementptr inbounds [524288 x i64], ptr @chainHead, i64 0, i64 %17
  %19 = load i64, ptr %18, 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 = getelementptr inbounds [524288 x i64], ptr @pos, i64 0, i64 %.012
  %23 = load i64, ptr %22, align 8, !tbaa !5
  %24 = add nsw i64 %23, 1
  %25 = load i64, ptr @n, align 8, !tbaa !5
  tail call void @_Z13updateSegTreexxxxxx(i64 noundef %21, i64 noundef %24, i64 noundef %2, i64 noundef 1, i64 noundef 0, i64 noundef %25)
  %26 = getelementptr inbounds [524288 x i64], ptr @p, i64 0, i64 %19
  %27 = load i64, ptr %26, align 8, !tbaa !5
  %28 = getelementptr inbounds [524288 x i64], ptr @chain, i64 0, i64 %27
  %29 = load i64, ptr %28, align 8, !tbaa !5
  %30 = load i64, ptr %5, align 8, !tbaa !5
  %31 = icmp eq i64 %29, %30
  br i1 %31, 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,SROAPass,SimplifyCFGPass,TailCallElimPass