; LLVM IR from tangjz/acm-icpc :: lydsy/4805.ll (Z4calci)

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

@sz = external dso_local global i32, align 4
@que = external dso_local global [31623 x i32], align 16
@f = external dso_local global [1000001 x i64], align 16
@val = external dso_local global [31623 x i64], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z4calci(i32 noundef %0) #0 {
  %2 = alloca i64, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  %10 = load i32, ptr %3, align 4, !tbaa !5
  %11 = icmp slt i32 %10, 1000001
  br i1 %11, label %12, label %17

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

17:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  %18 = load i32, ptr @sz, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds i32, ptr @que, i64 %19
  %21 = call noundef ptr @_ZSt11lower_boundIPiiET_S1_S1_RKT0_(ptr noundef @que, ptr noundef %20, ptr noundef nonnull align 4 dereferenceable(4) %3)
  %22 = ptrtoint ptr %21 to i64
  %23 = sub i64 %22, ptrtoint (ptr @que to i64)
  %24 = sdiv exact i64 %23, 4
  %25 = trunc i64 %24 to i32
  store i32 %25, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %26 = load i32, ptr %4, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [31623 x i64], ptr @val, i64 0, i64 %27
  store ptr %28, ptr %5, align 8, !tbaa !11
  %29 = load ptr, ptr %5, align 8, !tbaa !11
  %30 = load i64, ptr %29, align 8, !tbaa !9
  %31 = icmp ne i64 %30, 0
  br i1 %31, label %32, label %35

32:                                               ; preds = %17
  %33 = load ptr, ptr %5, align 8, !tbaa !11
  %34 = load i64, ptr %33, align 8, !tbaa !9
  store i64 %34, ptr %2, align 8
  store i32 1, ptr %6, align 4
  br label %73

35:                                               ; preds = %17
  %36 = load i32, ptr %3, align 4, !tbaa !5
  %37 = sext i32 %36 to i64
  %38 = load i32, ptr %3, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = add nsw i64 %39, 1
  %41 = mul nsw i64 %37, %40
  %42 = ashr i64 %41, 1
  %43 = load ptr, ptr %5, align 8, !tbaa !11
  store i64 %42, ptr %43, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 2, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  br label %44

44:                                               ; preds = %67, %35
  %45 = load i32, ptr %7, align 4, !tbaa !5
  %46 = load i32, ptr %3, align 4, !tbaa !5
  %47 = icmp sle i32 %45, %46
  br i1 %47, label %49, label %48

48:                                               ; preds = %44
  store i32 2, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  br label %70

49:                                               ; preds = %44
  %50 = load i32, ptr %3, align 4, !tbaa !5
  %51 = load i32, ptr %7, align 4, !tbaa !5
  %52 = sdiv i32 %50, %51
  store i32 %52, ptr %9, align 4, !tbaa !5
  %53 = load i32, ptr %3, align 4, !tbaa !5
  %54 = load i32, ptr %9, align 4, !tbaa !5
  %55 = sdiv i32 %53, %54
  store i32 %55, ptr %8, align 4, !tbaa !5
  %56 = load i32, ptr %8, align 4, !tbaa !5
  %57 = load i32, ptr %7, align 4, !tbaa !5
  %58 = sub nsw i32 %56, %57
  %59 = sext i32 %58 to i64
  %60 = add nsw i64 %59, 1
  %61 = load i32, ptr %9, align 4, !tbaa !5
  %62 = call noundef i64 @_Z4calci(i32 noundef %61)
  %63 = mul nsw i64 %60, %62
  %64 = load ptr, ptr %5, align 8, !tbaa !11
  %65 = load i64, ptr %64, align 8, !tbaa !9
  %66 = sub nsw i64 %65, %63
  store i64 %66, ptr %64, align 8, !tbaa !9
  br label %67

67:                                               ; preds = %49
  %68 = load i32, ptr %8, align 4, !tbaa !5
  %69 = add nsw i32 %68, 1
  store i32 %69, ptr %7, align 4, !tbaa !5
  br label %44, !llvm.loop !13

70:                                               ; preds = %48
  %71 = load ptr, ptr %5, align 8, !tbaa !11
  %72 = load i64, ptr %71, align 8, !tbaa !9
  store i64 %72, ptr %2, align 8
  store i32 1, ptr %6, align 4
  br label %73

73:                                               ; preds = %70, %32
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  br label %74

74:                                               ; preds = %73, %12
  %75 = load i64, ptr %2, align 8
  ret i64 %75
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef ptr @_ZSt11lower_boundIPiiET_S1_S1_RKT0_(ptr noundef, ptr noundef, ptr noundef nonnull align 4 dereferenceable(4)) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

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" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint 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 #3 = { 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, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"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"

@sz = external dso_local local_unnamed_addr global i32, align 4
@que = external dso_local global [31623 x i32], align 16
@f = external dso_local local_unnamed_addr global [1000001 x i64], align 16
@val = external dso_local local_unnamed_addr global [31623 x i64], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z4calci(i32 noundef %0) local_unnamed_addr #0 {
  %2 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  %3 = icmp slt i32 %0, 1000001
  br i1 %3, label %4, label %8

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

8:                                                ; preds = %1
  %9 = load i32, ptr @sz, align 4, !tbaa !5
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds i32, ptr @que, i64 %10
  %12 = call noundef ptr @_ZSt11lower_boundIPiiET_S1_S1_RKT0_(ptr noundef nonnull @que, ptr noundef nonnull %11, ptr noundef nonnull align 4 dereferenceable(4) %2)
  %13 = ptrtoint ptr %12 to i64
  %14 = sub i64 %13, ptrtoint (ptr @que to i64)
  %sext = shl i64 %14, 30
  %15 = ashr i64 %sext, 32
  %16 = getelementptr inbounds [31623 x i64], ptr @val, i64 0, i64 %15
  %17 = load i64, ptr %16, align 8, !tbaa !9
  %.not = icmp eq i64 %17, 0
  br i1 %.not, label %18, label %._crit_edge

18:                                               ; preds = %8
  %19 = load i32, ptr %2, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = add nsw i64 %20, 1
  %22 = mul nsw i64 %21, %20
  %23 = ashr i64 %22, 1
  store i64 %23, ptr %16, align 8, !tbaa !9
  %.not1719 = icmp slt i32 %19, 2
  br i1 %.not1719, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %18, %.lr.ph
  %24 = phi i32 [ %33, %.lr.ph ], [ %19, %18 ]
  %.01320 = phi i32 [ %32, %.lr.ph ], [ 2, %18 ]
  %25 = udiv i32 %24, %.01320
  %26 = sdiv i32 %24, %25
  %27 = xor i32 %26, -1
  %28 = add i32 %.01320, %27
  %.neg = sext i32 %28 to i64
  %29 = call noundef i64 @_Z4calci(i32 noundef %25)
  %.neg18 = mul i64 %29, %.neg
  %30 = load i64, ptr %16, align 8, !tbaa !9
  %31 = add i64 %.neg18, %30
  store i64 %31, ptr %16, align 8, !tbaa !9
  %32 = add nuw nsw i32 %26, 1
  %33 = load i32, ptr %2, align 4, !tbaa !5
  %.not17.not = icmp slt i32 %26, %33
  br i1 %.not17.not, label %.lr.ph, label %._crit_edge, !llvm.loop !11

._crit_edge:                                      ; preds = %.lr.ph, %18, %8, %4
  %.1 = phi i64 [ %7, %4 ], [ %17, %8 ], [ %23, %18 ], [ %31, %.lr.ph ]
  ret i64 %.1
}

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef ptr @_ZSt11lower_boundIPiiET_S1_S1_RKT0_(ptr noundef, ptr noundef, ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #1

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" }
attributes #1 = { inlinehint 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 = !{!"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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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