; LLVM IR from tangjz/acm-icpc :: 51nod/1239.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"

%"class.std::map" = type { %"class.std::_Rb_tree" }
%"class.std::_Rb_tree" = type { %"struct.std::_Rb_tree<long long, std::pair<const long long, int>, std::_Select1st<std::pair<const long long, int>>, std::less<long long>>::_Rb_tree_impl" }
%"struct.std::_Rb_tree<long long, std::pair<const long long, int>, std::_Select1st<std::pair<const long long, int>>, std::less<long long>>::_Rb_tree_impl" = type { [8 x i8], %"struct.std::_Rb_tree_header" }
%"struct.std::_Rb_tree_header" = type { %"struct.std::_Rb_tree_node_base", i64 }
%"struct.std::_Rb_tree_node_base" = type { i32, ptr, ptr, ptr }

@Hash = external dso_local global %"class.std::map", align 8
@f = external dso_local global [4641589 x i32], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z4calcx(i64 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i64, align 8
  %4 = alloca i32, align 4
  %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 = icmp slt i64 %7, 4641589
  br i1 %8, label %9, label %13

9:                                                ; preds = %1
  %10 = load i64, ptr %3, align 8, !tbaa !5
  %11 = getelementptr inbounds [4641589 x i32], ptr @f, i64 0, i64 %10
  %12 = load i32, ptr %11, align 4, !tbaa !9
  store i32 %12, ptr %2, align 4
  br label %60

13:                                               ; preds = %1
  %14 = call noundef i64 @_ZNKSt3mapIxiSt4lessIxESaISt4pairIKxiEEE5countERS3_(ptr noundef nonnull align 8 dereferenceable(48) @Hash, ptr noundef nonnull align 8 dereferenceable(8) %3)
  %15 = icmp ne i64 %14, 0
  br i1 %15, label %16, label %19

16:                                               ; preds = %13
  %17 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt3mapIxiSt4lessIxESaISt4pairIKxiEEEixERS3_(ptr noundef nonnull align 8 dereferenceable(48) @Hash, ptr noundef nonnull align 8 dereferenceable(8) %3)
  %18 = load i32, ptr %17, align 4, !tbaa !9
  store i32 %18, ptr %2, align 4
  br label %60

19:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  %20 = load i64, ptr %3, align 8, !tbaa !5
  %21 = srem i64 %20, 1000000007
  %22 = load i64, ptr %3, align 8, !tbaa !5
  %23 = add nsw i64 %22, 1
  %24 = srem i64 %23, 1000000007
  %25 = mul nsw i64 %21, %24
  %26 = srem i64 %25, 1000000007
  %27 = mul nsw i64 %26, 500000004
  %28 = srem i64 %27, 1000000007
  %29 = trunc i64 %28 to i32
  store i32 %29, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  store i64 2, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  br label %30

30:                                               ; preds = %54, %19
  %31 = load i64, ptr %5, align 8, !tbaa !5
  %32 = load i64, ptr %3, align 8, !tbaa !5
  %33 = icmp sle i64 %31, %32
  br i1 %33, label %35, label %34

34:                                               ; preds = %30
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  br label %57

35:                                               ; preds = %30
  %36 = load i64, ptr %3, align 8, !tbaa !5
  %37 = load i64, ptr %3, align 8, !tbaa !5
  %38 = load i64, ptr %5, align 8, !tbaa !5
  %39 = sdiv i64 %37, %38
  %40 = sdiv i64 %36, %39
  store i64 %40, ptr %6, align 8, !tbaa !5
  %41 = load i64, ptr %6, align 8, !tbaa !5
  %42 = load i64, ptr %5, align 8, !tbaa !5
  %43 = sub nsw i64 %41, %42
  %44 = add nsw i64 %43, 1
  %45 = srem i64 %44, 1000000007
  %46 = load i64, ptr %3, align 8, !tbaa !5
  %47 = load i64, ptr %5, align 8, !tbaa !5
  %48 = sdiv i64 %46, %47
  %49 = call noundef i32 @_Z4calcx(i64 noundef %48)
  %50 = sext i32 %49 to i64
  %51 = mul nsw i64 %45, %50
  %52 = srem i64 %51, 1000000007
  %53 = trunc i64 %52 to i32
  call void @_Z3decRii(ptr noundef nonnull align 4 dereferenceable(4) %4, i32 noundef %53)
  br label %54

54:                                               ; preds = %35
  %55 = load i64, ptr %6, align 8, !tbaa !5
  %56 = add nsw i64 %55, 1
  store i64 %56, ptr %5, align 8, !tbaa !5
  br label %30, !llvm.loop !11

57:                                               ; preds = %34
  %58 = load i32, ptr %4, align 4, !tbaa !9
  %59 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt3mapIxiSt4lessIxESaISt4pairIKxiEEEixERS3_(ptr noundef nonnull align 8 dereferenceable(48) @Hash, ptr noundef nonnull align 8 dereferenceable(8) %3)
  store i32 %58, ptr %59, align 4, !tbaa !9
  store i32 %58, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  br label %60

60:                                               ; preds = %57, %16, %9
  %61 = load i32, ptr %2, align 4
  ret i32 %61
}

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_ZNKSt3mapIxiSt4lessIxESaISt4pairIKxiEEE5countERS3_(ptr noundef nonnull align 8 dereferenceable(48), ptr noundef nonnull align 8 dereferenceable(8)) #0 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt3mapIxiSt4lessIxESaISt4pairIKxiEEEixERS3_(ptr noundef nonnull align 8 dereferenceable(48), ptr noundef nonnull align 8 dereferenceable(8)) #0 align 2

; 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 void @_Z3decRii(ptr noundef nonnull align 4 dereferenceable(4), i32 noundef) #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 = 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"

%"class.std::map" = type { %"class.std::_Rb_tree" }
%"class.std::_Rb_tree" = type { %"struct.std::_Rb_tree<long long, std::pair<const long long, int>, std::_Select1st<std::pair<const long long, int>>, std::less<long long>>::_Rb_tree_impl" }
%"struct.std::_Rb_tree<long long, std::pair<const long long, int>, std::_Select1st<std::pair<const long long, int>>, std::less<long long>>::_Rb_tree_impl" = type { [8 x i8], %"struct.std::_Rb_tree_header" }
%"struct.std::_Rb_tree_header" = type { %"struct.std::_Rb_tree_node_base", i64 }
%"struct.std::_Rb_tree_node_base" = type { i32, ptr, ptr, ptr }

@Hash = external dso_local global %"class.std::map", align 8
@f = external dso_local local_unnamed_addr global [4641589 x i32], align 16

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

5:                                                ; preds = %1
  %6 = getelementptr inbounds [4641589 x i32], ptr @f, i64 0, i64 %0
  %7 = load i32, ptr %6, align 4, !tbaa !9
  br label %37

8:                                                ; preds = %1
  %9 = call noundef i64 @_ZNKSt3mapIxiSt4lessIxESaISt4pairIKxiEEE5countERS3_(ptr noundef nonnull align 8 dereferenceable(48) @Hash, ptr noundef nonnull align 8 dereferenceable(8) %2)
  %.not = icmp eq i64 %9, 0
  br i1 %.not, label %13, label %10

10:                                               ; preds = %8
  %11 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt3mapIxiSt4lessIxESaISt4pairIKxiEEEixERS3_(ptr noundef nonnull align 8 dereferenceable(48) @Hash, ptr noundef nonnull align 8 dereferenceable(8) %2)
  %12 = load i32, ptr %11, align 4, !tbaa !9
  br label %37

13:                                               ; preds = %8
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %3) #3
  %14 = load i64, ptr %2, align 8, !tbaa !5
  %15 = srem i64 %14, 1000000007
  %16 = add nsw i64 %14, 1
  %17 = srem i64 %16, 1000000007
  %18 = mul nsw i64 %17, %15
  %19 = srem i64 %18, 1000000007
  %20 = mul nsw i64 %19, 500000004
  %21 = srem i64 %20, 1000000007
  %22 = trunc nsw i64 %21 to i32
  store i32 %22, ptr %3, align 4, !tbaa !9
  %.not1011 = icmp slt i64 %14, 2
  br i1 %.not1011, label %._crit_edge, label %.lr.ph

._crit_edge.loopexit:                             ; preds = %.lr.ph
  %.pre = load i32, ptr %3, align 4, !tbaa !9
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %13
  %23 = phi i32 [ %.pre, %._crit_edge.loopexit ], [ %22, %13 ]
  %24 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt3mapIxiSt4lessIxESaISt4pairIKxiEEEixERS3_(ptr noundef nonnull align 8 dereferenceable(48) @Hash, ptr noundef nonnull align 8 dereferenceable(8) %2)
  store i32 %23, ptr %24, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %3) #3
  br label %37

.lr.ph:                                           ; preds = %13, %.lr.ph
  %25 = phi i64 [ %36, %.lr.ph ], [ %14, %13 ]
  %.0712 = phi i64 [ %35, %.lr.ph ], [ 2, %13 ]
  %26 = udiv i64 %25, %.0712
  %27 = sdiv i64 %25, %26
  %reass.sub = sub i64 %27, %.0712
  %28 = add i64 %reass.sub, 1
  %29 = srem i64 %28, 1000000007
  %30 = call noundef i32 @_Z4calcx(i64 noundef %26)
  %31 = sext i32 %30 to i64
  %32 = mul nsw i64 %29, %31
  %33 = srem i64 %32, 1000000007
  %34 = trunc nsw i64 %33 to i32
  call void @_Z3decRii(ptr noundef nonnull align 4 dereferenceable(4) %3, i32 noundef %34)
  %35 = add nuw nsw i64 %27, 1
  %36 = load i64, ptr %2, align 8, !tbaa !5
  %.not10.not = icmp slt i64 %27, %36
  br i1 %.not10.not, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !11

37:                                               ; preds = %._crit_edge, %10, %5
  %.0 = phi i32 [ %7, %5 ], [ %12, %10 ], [ %23, %._crit_edge ]
  ret i32 %.0
}

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_ZNKSt3mapIxiSt4lessIxESaISt4pairIKxiEEE5countERS3_(ptr noundef nonnull align 8 dereferenceable(48), ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt3mapIxiSt4lessIxESaISt4pairIKxiEEEixERS3_(ptr noundef nonnull align 8 dereferenceable(48), ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #0 align 2

; 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 void @_Z3decRii(ptr noundef nonnull align 4 dereferenceable(4), i32 noundef) 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 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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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