; LLVM IR from tangjz/acm-icpc :: lydsy/5455.ll (Z1Gx)

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

@mu = external dso_local global [5000001 x i32], align 16
@g = external dso_local global [5000001 x i32], align 16
@nLim = external dso_local global i32, align 4

; 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 @_Z1Gx(i64 noundef %0) #1 {
  %2 = alloca i64, align 8
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  store i64 %0, ptr %3, align 8, !tbaa !5
  %7 = load i64, ptr %3, align 8, !tbaa !5
  %8 = load i32, ptr @nLim, align 4, !tbaa !9
  %9 = sext i32 %8 to i64
  %10 = icmp slt i64 %7, %9
  br i1 %10, label %11, label %16

11:                                               ; preds = %1
  %12 = load i64, ptr %3, align 8, !tbaa !5
  %13 = getelementptr inbounds [5000001 x i32], ptr @g, i64 0, i64 %12
  %14 = load i32, ptr %13, align 4, !tbaa !9
  %15 = sext i32 %14 to i64
  store i64 %15, ptr %2, align 8
  br label %57

16:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  store i64 0, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  store i64 1, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  store i64 1, ptr %6, align 8, !tbaa !5
  br label %17

17:                                               ; preds = %47, %16
  %18 = load i64, ptr %6, align 8, !tbaa !5
  %19 = load i64, ptr %3, align 8, !tbaa !5
  %20 = icmp sle i64 %18, %19
  br i1 %20, label %22, label %21

21:                                               ; preds = %17
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  br label %55

22:                                               ; preds = %17
  %23 = load i64, ptr %5, align 8, !tbaa !5
  %24 = getelementptr inbounds [5000001 x i32], ptr @mu, i64 0, i64 %23
  %25 = load i32, ptr %24, align 4, !tbaa !9
  %26 = icmp ne i32 %25, 0
  br i1 %26, label %27, label %46

27:                                               ; preds = %22
  %28 = load i64, ptr %5, align 8, !tbaa !5
  %29 = getelementptr inbounds [5000001 x i32], ptr @mu, i64 0, i64 %28
  %30 = load i32, ptr %29, align 4, !tbaa !9
  %31 = icmp sgt i32 %30, 0
  br i1 %31, label %32, label %38

32:                                               ; preds = %27
  %33 = load i64, ptr %3, align 8, !tbaa !5
  %34 = load i64, ptr %6, align 8, !tbaa !5
  %35 = sdiv i64 %33, %34
  %36 = load i64, ptr %4, align 8, !tbaa !5
  %37 = add nsw i64 %36, %35
  store i64 %37, ptr %4, align 8, !tbaa !5
  br label %44

38:                                               ; preds = %27
  %39 = load i64, ptr %3, align 8, !tbaa !5
  %40 = load i64, ptr %6, align 8, !tbaa !5
  %41 = sdiv i64 %39, %40
  %42 = load i64, ptr %4, align 8, !tbaa !5
  %43 = sub nsw i64 %42, %41
  store i64 %43, ptr %4, align 8, !tbaa !5
  br label %44

44:                                               ; preds = %38, %32
  %45 = phi ptr [ %4, %32 ], [ %4, %38 ]
  br label %46

46:                                               ; preds = %44, %22
  br label %47

47:                                               ; preds = %46
  %48 = load i64, ptr %5, align 8, !tbaa !5
  %49 = shl i64 %48, 1
  %50 = or i64 %49, 1
  %51 = load i64, ptr %6, align 8, !tbaa !5
  %52 = add nsw i64 %51, %50
  store i64 %52, ptr %6, align 8, !tbaa !5
  %53 = load i64, ptr %5, align 8, !tbaa !5
  %54 = add nsw i64 %53, 1
  store i64 %54, ptr %5, align 8, !tbaa !5
  br label %17, !llvm.loop !11

55:                                               ; preds = %21
  %56 = load i64, ptr %4, align 8, !tbaa !5
  store i64 %56, ptr %2, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  br label %57

57:                                               ; preds = %55, %11
  %58 = load i64, ptr %2, align 8
  ret i64 %58
}

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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"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"

@mu = external dso_local local_unnamed_addr global [5000001 x i32], align 16
@g = external dso_local local_unnamed_addr global [5000001 x i32], align 16
@nLim = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i64 @_Z1Gx(i64 noundef %0) local_unnamed_addr #0 {
  %2 = load i32, ptr @nLim, align 4, !tbaa !5
  %3 = sext i32 %2 to i64
  %4 = icmp sgt i64 %3, %0
  br i1 %4, label %5, label %.preheader

.preheader:                                       ; preds = %1
  %.not24 = icmp slt i64 %0, 1
  br i1 %.not24, label %.loopexit, label %.lr.ph

5:                                                ; preds = %1
  %6 = getelementptr inbounds [5000001 x i32], ptr @g, i64 0, i64 %0
  %7 = load i32, ptr %6, align 4, !tbaa !5
  %8 = sext i32 %7 to i64
  br label %.loopexit

.lr.ph:                                           ; preds = %.preheader, %18
  %.027 = phi i64 [ %21, %18 ], [ 1, %.preheader ]
  %.01726 = phi i64 [ %22, %18 ], [ 1, %.preheader ]
  %.01825 = phi i64 [ %.1, %18 ], [ 0, %.preheader ]
  %9 = getelementptr inbounds [5000001 x i32], ptr @mu, i64 0, i64 %.01726
  %10 = load i32, ptr %9, align 4, !tbaa !5
  %.not23 = icmp eq i32 %10, 0
  br i1 %.not23, label %18, label %11

11:                                               ; preds = %.lr.ph
  %12 = icmp sgt i32 %10, 0
  %13 = sdiv i64 %0, %.027
  br i1 %12, label %14, label %16

14:                                               ; preds = %11
  %15 = add nsw i64 %13, %.01825
  br label %18

16:                                               ; preds = %11
  %17 = sub nsw i64 %.01825, %13
  br label %18

18:                                               ; preds = %14, %16, %.lr.ph
  %.1 = phi i64 [ %15, %14 ], [ %17, %16 ], [ %.01825, %.lr.ph ]
  %19 = shl nuw i64 %.01726, 1
  %20 = or disjoint i64 %19, 1
  %21 = add nsw i64 %20, %.027
  %22 = add nuw nsw i64 %.01726, 1
  %.not = icmp sgt i64 %21, %0
  br i1 %.not, label %.loopexit, label %.lr.ph, !llvm.loop !9

.loopexit:                                        ; preds = %18, %.preheader, %5
  %.019 = phi i64 [ %8, %5 ], [ 0, %.preheader ], [ %.1, %18 ]
  ret i64 %.019
}

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 = !{!"int", !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,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass