; LLVM IR from tangjz/acm-icpc :: lydsy/3874.ll (Z4calcx)

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

%"struct.std::pair" = type { i64, i64 }

@n = external dso_local global i32, align 4
@a = external dso_local global [201 x %"struct.std::pair"], align 16
@m = external dso_local global i64, align 8
@dt = external dso_local global i64, align 8

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z4calcx(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 i32, align 4
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  store i64 %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  store i64 0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %10 = load i64, ptr @m, align 8, !tbaa !5
  %11 = load i64, ptr %2, align 8, !tbaa !5
  %12 = load i64, ptr @dt, align 8, !tbaa !5
  %13 = mul nsw i64 %11, %12
  %14 = sub nsw i64 %10, %13
  store i64 %14, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  store i32 0, ptr %5, align 4, !tbaa !9
  br label %15

15:                                               ; preds = %81, %1
  %16 = load i32, ptr %5, align 4, !tbaa !9
  %17 = load i32, ptr @n, align 4, !tbaa !9
  %18 = icmp slt i32 %16, %17
  br i1 %18, label %20, label %19

19:                                               ; preds = %15
  store i32 2, ptr %6, align 4
  br label %84

20:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %21 = load i32, ptr %5, align 4, !tbaa !9
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [201 x %"struct.std::pair"], ptr @a, i64 0, i64 %22
  %24 = getelementptr inbounds %"struct.std::pair", ptr %23, i32 0, i32 0
  %25 = load i64, ptr %24, align 16, !tbaa !11
  %26 = load i32, ptr %5, align 4, !tbaa !9
  %27 = icmp ne i32 %26, 0
  br i1 %27, label %28, label %35

28:                                               ; preds = %20
  %29 = load i32, ptr %5, align 4, !tbaa !9
  %30 = sub nsw i32 %29, 1
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [201 x %"struct.std::pair"], ptr @a, i64 0, i64 %31
  %33 = getelementptr inbounds %"struct.std::pair", ptr %32, i32 0, i32 0
  %34 = load i64, ptr %33, align 16, !tbaa !11
  br label %36

35:                                               ; preds = %20
  br label %36

36:                                               ; preds = %35, %28
  %37 = phi i64 [ %34, %28 ], [ -1, %35 ]
  %38 = sub nsw i64 %25, %37
  store i64 %38, ptr %7, align 8, !tbaa !5
  %39 = load i64, ptr %7, align 8, !tbaa !5
  %40 = load i64, ptr @m, align 8, !tbaa !5
  %41 = load i64, ptr %2, align 8, !tbaa !5
  %42 = sdiv i64 %40, %41
  %43 = icmp sle i64 %39, %42
  br i1 %43, label %44, label %48

44:                                               ; preds = %36
  %45 = load i64, ptr %2, align 8, !tbaa !5
  %46 = load i64, ptr %7, align 8, !tbaa !5
  %47 = mul nsw i64 %46, %45
  store i64 %47, ptr %7, align 8, !tbaa !5
  br label %51

48:                                               ; preds = %36
  %49 = load i64, ptr @m, align 8, !tbaa !5
  %50 = add nsw i64 %49, 1
  store i64 %50, ptr %7, align 8, !tbaa !5
  br label %51

51:                                               ; preds = %48, %44
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %52 = load i64, ptr %4, align 8, !tbaa !5
  %53 = load i32, ptr %5, align 4, !tbaa !9
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds [201 x %"struct.std::pair"], ptr @a, i64 0, i64 %54
  %56 = getelementptr inbounds %"struct.std::pair", ptr %55, i32 0, i32 1
  %57 = load i64, ptr %56, align 8, !tbaa !13
  %58 = sdiv i64 %52, %57
  store i64 %58, ptr %9, align 8, !tbaa !5
  %59 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %7, ptr noundef nonnull align 8 dereferenceable(8) %9)
  %60 = load i64, ptr %59, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  store i64 %60, ptr %8, align 8, !tbaa !5
  %61 = load i64, ptr %8, align 8, !tbaa !5
  %62 = load i64, ptr %3, align 8, !tbaa !5
  %63 = add nsw i64 %62, %61
  store i64 %63, ptr %3, align 8, !tbaa !5
  %64 = load i64, ptr %8, align 8, !tbaa !5
  %65 = load i32, ptr %5, align 4, !tbaa !9
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds [201 x %"struct.std::pair"], ptr @a, i64 0, i64 %66
  %68 = getelementptr inbounds %"struct.std::pair", ptr %67, i32 0, i32 1
  %69 = load i64, ptr %68, align 8, !tbaa !13
  %70 = mul nsw i64 %64, %69
  %71 = load i64, ptr %4, align 8, !tbaa !5
  %72 = sub nsw i64 %71, %70
  store i64 %72, ptr %4, align 8, !tbaa !5
  %73 = load i64, ptr %8, align 8, !tbaa !5
  %74 = load i64, ptr %7, align 8, !tbaa !5
  %75 = icmp slt i64 %73, %74
  br i1 %75, label %76, label %77

76:                                               ; preds = %51
  store i32 2, ptr %6, align 4
  br label %78

77:                                               ; preds = %51
  store i32 0, ptr %6, align 4
  br label %78

78:                                               ; preds = %77, %76
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  %79 = load i32, ptr %6, align 4
  switch i32 %79, label %84 [
    i32 0, label %80
  ]

80:                                               ; preds = %78
  br label %81

81:                                               ; preds = %80
  %82 = load i32, ptr %5, align 4, !tbaa !9
  %83 = add nsw i32 %82, 1
  store i32 %83, ptr %5, align 4, !tbaa !9
  br label %15, !llvm.loop !14

84:                                               ; preds = %78, %19
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  br label %85

85:                                               ; preds = %84
  %86 = load i64, ptr %3, align 8, !tbaa !5
  store i32 1, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  ret i64 %86
}

; 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 nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) #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 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 #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 = !{!"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 = !{!12, !6, i64 0}
!12 = !{!"_ZTSSt4pairIxxE", !6, i64 0, !6, i64 8}
!13 = !{!12, !6, i64 8}
!14 = distinct !{!14, !15}
!15 = !{!"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"

%"struct.std::pair" = type { i64, i64 }

@n = external dso_local local_unnamed_addr global i32, align 4
@a = external dso_local local_unnamed_addr global [201 x %"struct.std::pair"], align 16
@m = external dso_local local_unnamed_addr global i64, align 8
@dt = external dso_local local_unnamed_addr global i64, align 8

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z4calcx(i64 noundef %0) local_unnamed_addr #0 {
  %2 = alloca i64, align 8
  %3 = alloca i64, align 8
  %4 = load i64, ptr @m, align 8, !tbaa !5
  %5 = load i64, ptr @dt, align 8, !tbaa !5
  %6 = mul nsw i64 %5, %0
  %7 = sub nsw i64 %4, %6
  %8 = load i32, ptr @n, align 4, !tbaa !9
  %9 = icmp sgt i32 %8, 0
  br i1 %9, label %10, label %._crit_edge

10:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #3
  %11 = load i64, ptr @a, align 16, !tbaa !11
  %12 = add nsw i64 %11, 1
  %13 = sdiv i64 %4, %0
  %.not22.peel.not = icmp slt i64 %11, %13
  %14 = add nsw i64 %4, 1
  %15 = mul nsw i64 %12, %0
  %storemerge.peel = select i1 %.not22.peel.not, i64 %15, i64 %14
  store i64 %storemerge.peel, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #3
  %16 = load i64, ptr getelementptr inbounds (i8, ptr @a, i64 8), align 8, !tbaa !13
  %17 = sdiv i64 %7, %16
  store i64 %17, ptr %3, align 8, !tbaa !5
  %18 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %2, ptr noundef nonnull align 8 dereferenceable(8) %3)
  %19 = load i64, ptr %18, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #3
  %20 = load i64, ptr getelementptr inbounds (i8, ptr @a, i64 8), align 8, !tbaa !13
  %21 = load i64, ptr %2, align 8, !tbaa !5
  %.not23.peel = icmp sge i64 %19, %21
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #3
  %22 = load i32, ptr @n, align 4
  %23 = icmp sgt i32 %22, 1
  %or.cond = select i1 %.not23.peel, i1 %23, i1 false
  br i1 %or.cond, label %.lr.ph.peel.next, label %._crit_edge

.lr.ph.peel.next:                                 ; preds = %10
  %24 = mul nsw i64 %20, %19
  %25 = sub nsw i64 %7, %24
  br label %26

26:                                               ; preds = %.lr.ph.peel.next, %45
  %indvars.iv = phi i64 [ 1, %.lr.ph.peel.next ], [ %indvars.iv.next, %45 ]
  %.026 = phi i64 [ %19, %.lr.ph.peel.next ], [ %42, %45 ]
  %.02024 = phi i64 [ %25, %.lr.ph.peel.next ], [ %47, %45 ]
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #3
  %27 = getelementptr inbounds [201 x %"struct.std::pair"], ptr @a, i64 0, i64 %indvars.iv
  %28 = load i64, ptr %27, align 16, !tbaa !11
  %29 = add nsw i64 %indvars.iv, -1
  %30 = getelementptr inbounds [201 x %"struct.std::pair"], ptr @a, i64 0, i64 %29
  %31 = load i64, ptr %30, align 16, !tbaa !11
  %32 = sub nsw i64 %28, %31
  %33 = load i64, ptr @m, align 8, !tbaa !5
  %34 = sdiv i64 %33, %0
  %.not22 = icmp sgt i64 %32, %34
  %35 = add nsw i64 %33, 1
  %36 = mul nsw i64 %32, %0
  %storemerge = select i1 %.not22, i64 %35, i64 %36
  store i64 %storemerge, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #3
  %37 = getelementptr inbounds i8, ptr %27, i64 8
  %38 = load i64, ptr %37, align 8, !tbaa !13
  %39 = sdiv i64 %.02024, %38
  store i64 %39, ptr %3, align 8, !tbaa !5
  %40 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %2, ptr noundef nonnull align 8 dereferenceable(8) %3)
  %41 = load i64, ptr %40, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #3
  %42 = add nsw i64 %41, %.026
  %43 = load i64, ptr %37, align 8, !tbaa !13
  %44 = load i64, ptr %2, align 8, !tbaa !5
  %.not23 = icmp slt i64 %41, %44
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #3
  br i1 %.not23, label %._crit_edge, label %45

45:                                               ; preds = %26
  %46 = mul nsw i64 %43, %41
  %47 = sub nsw i64 %.02024, %46
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %48 = load i32, ptr @n, align 4, !tbaa !9
  %49 = sext i32 %48 to i64
  %50 = icmp slt i64 %indvars.iv.next, %49
  br i1 %50, label %26, label %._crit_edge, !llvm.loop !14

._crit_edge:                                      ; preds = %26, %45, %10, %1
  %.1 = phi i64 [ 0, %1 ], [ %19, %10 ], [ %42, %45 ], [ %42, %26 ]
  ret i64 %.1
}

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #2

; Function Attrs: mustprogress 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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint 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 #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 = !{!"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 = !{!12, !6, i64 0}
!12 = !{!"_ZTSSt4pairIxxE", !6, i64 0, !6, i64 8}
!13 = !{!12, !6, i64 8}
!14 = distinct !{!14, !15, !16}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!"llvm.loop.peeled.count", i32 1}


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