; LLVM IR from eyangch/competitive-programming :: Codeforces/1500/b.ll (Z5angryx)

; 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
@a = external dso_local global [500000 x i64], align 16
@compact = external dso_local global [1000000 x i64], align 16
@lid = external dso_local global [1000000 x i64], align 16
@lane_n = external dso_local global [500000 x i64], align 16
@lane_n2 = external dso_local global [500000 x i64], align 16
@lane_sz = external dso_local global i64, align 8

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

9:                                                ; preds = %65, %1
  %10 = load i64, ptr %4, align 8, !tbaa !5
  %11 = load i64, ptr @N, align 8, !tbaa !5
  %12 = icmp slt i64 %10, %11
  br i1 %12, label %14, label %13

13:                                               ; preds = %9
  store i32 2, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  br label %68

14:                                               ; preds = %9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %15 = load i64, ptr %2, align 8, !tbaa !5
  %16 = load i64, ptr @N, align 8, !tbaa !5
  %17 = sdiv i64 %15, %16
  %18 = load i64, ptr %4, align 8, !tbaa !5
  %19 = load i64, ptr %2, align 8, !tbaa !5
  %20 = load i64, ptr @N, align 8, !tbaa !5
  %21 = srem i64 %19, %20
  %22 = icmp slt i64 %18, %21
  %23 = zext i1 %22 to i64
  %24 = add nsw i64 %17, %23
  store i64 %24, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  %25 = load i64, ptr %4, align 8, !tbaa !5
  %26 = getelementptr inbounds [500000 x i64], ptr @lane_n, i64 0, i64 %25
  %27 = load i64, ptr %26, align 8, !tbaa !5
  store i64 %27, ptr %7, align 8, !tbaa !5
  %28 = load i64, ptr %7, align 8, !tbaa !5
  %29 = load i64, ptr %4, align 8, !tbaa !5
  %30 = getelementptr inbounds [500000 x i64], ptr @a, i64 0, i64 %29
  %31 = load i64, ptr %30, align 8, !tbaa !5
  %32 = getelementptr inbounds [1000000 x i64], ptr @lid, i64 0, i64 %31
  %33 = load i64, ptr %32, align 8, !tbaa !5
  %34 = icmp ne i64 %28, %33
  br i1 %34, label %35, label %36

35:                                               ; preds = %14
  store i32 4, ptr %5, align 4
  br label %62

36:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %37 = load i64, ptr %4, align 8, !tbaa !5
  %38 = getelementptr inbounds [500000 x i64], ptr @a, i64 0, i64 %37
  %39 = load i64, ptr %38, align 8, !tbaa !5
  %40 = getelementptr inbounds [1000000 x i64], ptr @compact, i64 0, i64 %39
  %41 = load i64, ptr %40, align 8, !tbaa !5
  %42 = load i64, ptr @lane_sz, align 8, !tbaa !5
  %43 = add nsw i64 %41, %42
  %44 = load i64, ptr %4, align 8, !tbaa !5
  %45 = getelementptr inbounds [500000 x i64], ptr @lane_n2, i64 0, i64 %44
  %46 = load i64, ptr %45, align 8, !tbaa !5
  %47 = sub nsw i64 %43, %46
  %48 = load i64, ptr @lane_sz, align 8, !tbaa !5
  %49 = srem i64 %47, %48
  store i64 %49, ptr %8, align 8, !tbaa !5
  %50 = load i64, ptr %6, align 8, !tbaa !5
  %51 = load i64, ptr @lane_sz, align 8, !tbaa !5
  %52 = sdiv i64 %50, %51
  %53 = load i64, ptr %8, align 8, !tbaa !5
  %54 = load i64, ptr %6, align 8, !tbaa !5
  %55 = load i64, ptr @lane_sz, align 8, !tbaa !5
  %56 = srem i64 %54, %55
  %57 = icmp slt i64 %53, %56
  %58 = zext i1 %57 to i64
  %59 = add nsw i64 %52, %58
  %60 = load i64, ptr %3, align 8, !tbaa !5
  %61 = add nsw i64 %60, %59
  store i64 %61, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  store i32 0, ptr %5, align 4
  br label %62

62:                                               ; preds = %36, %35
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  %63 = load i32, ptr %5, align 4
  switch i32 %63, label %72 [
    i32 0, label %64
    i32 4, label %65
  ]

64:                                               ; preds = %62
  br label %65

65:                                               ; preds = %64, %62
  %66 = load i64, ptr %4, align 8, !tbaa !5
  %67 = add nsw i64 %66, 1
  store i64 %67, ptr %4, align 8, !tbaa !5
  br label %9, !llvm.loop !9

68:                                               ; preds = %13
  %69 = load i64, ptr %2, align 8, !tbaa !5
  %70 = load i64, ptr %3, align 8, !tbaa !5
  %71 = sub nsw i64 %69, %70
  store i32 1, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  ret i64 %71

72:                                               ; preds = %62
  unreachable
}

; 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 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 #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 = !{!"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
@a = external dso_local local_unnamed_addr global [500000 x i64], align 16
@compact = external dso_local local_unnamed_addr global [1000000 x i64], align 16
@lid = external dso_local local_unnamed_addr global [1000000 x i64], align 16
@lane_n = external dso_local local_unnamed_addr global [500000 x i64], align 16
@lane_n2 = external dso_local local_unnamed_addr global [500000 x i64], align 16
@lane_sz = external dso_local local_unnamed_addr global i64, align 8

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i64 @_Z5angryx(i64 noundef %0) local_unnamed_addr #0 {
  %2 = load i64, ptr @N, align 8, !tbaa !5
  %3 = icmp sgt i64 %2, 0
  br i1 %3, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %1
  %4 = load i64, ptr @lane_sz, align 8
  br label %6

._crit_edge:                                      ; preds = %32, %1
  %.0.lcssa = phi i64 [ 0, %1 ], [ %.1, %32 ]
  %5 = sub nsw i64 %0, %.0.lcssa
  ret i64 %5

6:                                                ; preds = %.lr.ph, %32
  %.019 = phi i64 [ 0, %.lr.ph ], [ %.1, %32 ]
  %.01618 = phi i64 [ 0, %.lr.ph ], [ %33, %32 ]
  %7 = getelementptr inbounds [500000 x i64], ptr @lane_n, i64 0, i64 %.01618
  %8 = load i64, ptr %7, align 8, !tbaa !5
  %9 = getelementptr inbounds [500000 x i64], ptr @a, i64 0, i64 %.01618
  %10 = load i64, ptr %9, align 8, !tbaa !5
  %11 = getelementptr inbounds [1000000 x i64], ptr @lid, i64 0, i64 %10
  %12 = load i64, ptr %11, align 8, !tbaa !5
  %.not = icmp eq i64 %8, %12
  br i1 %.not, label %13, label %32

13:                                               ; preds = %6
  %14 = sdiv i64 %0, %2
  %15 = srem i64 %0, %2
  %16 = icmp slt i64 %.01618, %15
  %17 = zext i1 %16 to i64
  %18 = add nsw i64 %14, %17
  %19 = getelementptr inbounds [1000000 x i64], ptr @compact, i64 0, i64 %10
  %20 = load i64, ptr %19, align 8, !tbaa !5
  %21 = add nsw i64 %4, %20
  %22 = getelementptr inbounds [500000 x i64], ptr @lane_n2, i64 0, i64 %.01618
  %23 = load i64, ptr %22, align 8, !tbaa !5
  %24 = sub i64 %21, %23
  %25 = srem i64 %24, %4
  %26 = sdiv i64 %18, %4
  %27 = srem i64 %18, %4
  %28 = icmp slt i64 %25, %27
  %29 = zext i1 %28 to i64
  %30 = add i64 %26, %.019
  %31 = add i64 %30, %29
  br label %32

32:                                               ; preds = %6, %13
  %.1 = phi i64 [ %31, %13 ], [ %.019, %6 ]
  %33 = add nuw nsw i64 %.01618, 1
  %exitcond.not = icmp eq i64 %33, %2
  br i1 %exitcond.not, label %._crit_edge, label %6, !llvm.loop !9
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, 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 = !{!"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,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass