; LLVM IR from tangjz/acm-icpc :: 51nod/1237.ll (Z7calc_mux)

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

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_Z3decRii(ptr noundef nonnull align 4 dereferenceable(4), i32 noundef) #0

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z7calc_mux(i64 noundef %0) #1 {
  %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 %50

13:                                               ; preds = %1
  %14 = call noundef i64 @_ZNKSt3mapIxiSt4lessIxESaISt4pairIKxiEEE5countERS3_(ptr noundef nonnull align 8 dereferenceable(48) @Hash1, 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) @Hash1, 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 %50

19:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  store i32 1, 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 %20

20:                                               ; preds = %44, %19
  %21 = load i64, ptr %5, align 8, !tbaa !5
  %22 = load i64, ptr %3, align 8, !tbaa !5
  %23 = icmp sle i64 %21, %22
  br i1 %23, label %25, label %24

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

25:                                               ; preds = %20
  %26 = load i64, ptr %3, align 8, !tbaa !5
  %27 = load i64, ptr %3, align 8, !tbaa !5
  %28 = load i64, ptr %5, align 8, !tbaa !5
  %29 = sdiv i64 %27, %28
  %30 = sdiv i64 %26, %29
  store i64 %30, ptr %6, align 8, !tbaa !5
  %31 = load i64, ptr %6, align 8, !tbaa !5
  %32 = load i64, ptr %5, align 8, !tbaa !5
  %33 = sub nsw i64 %31, %32
  %34 = add nsw i64 %33, 1
  %35 = srem i64 %34, 1000000007
  %36 = load i64, ptr %3, align 8, !tbaa !5
  %37 = load i64, ptr %5, align 8, !tbaa !5
  %38 = sdiv i64 %36, %37
  %39 = call noundef i32 @_Z7calc_mux(i64 noundef %38)
  %40 = sext i32 %39 to i64
  %41 = mul nsw i64 %35, %40
  %42 = srem i64 %41, 1000000007
  %43 = trunc i64 %42 to i32
  call void @_Z3decRii(ptr noundef nonnull align 4 dereferenceable(4) %4, i32 noundef %43)
  br label %44

44:                                               ; preds = %25
  %45 = load i64, ptr %6, align 8, !tbaa !5
  %46 = add nsw i64 %45, 1
  store i64 %46, ptr %5, align 8, !tbaa !5
  br label %20, !llvm.loop !11

47:                                               ; preds = %24
  %48 = load i32, ptr %4, align 4, !tbaa !9
  %49 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt3mapIxiSt4lessIxESaISt4pairIKxiEEEixERS3_(ptr noundef nonnull align 8 dereferenceable(48) @Hash1, ptr noundef nonnull align 8 dereferenceable(8) %3)
  store i32 %48, ptr %49, align 4, !tbaa !9
  store i32 %48, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  br label %50

50:                                               ; preds = %47, %16, %9
  %51 = load i32, ptr %2, align 4
  ret i32 %51
}

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_ZNKSt3mapIxiSt4lessIxESaISt4pairIKxiEEE5countERS3_(ptr noundef nonnull align 8 dereferenceable(48), ptr noundef nonnull align 8 dereferenceable(8)) #1 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)) #1 align 2

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

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

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 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 }

@Hash1 = 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 nounwind uwtable
declare dso_local void @_Z3decRii(ptr noundef nonnull align 4 dereferenceable(4), i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z7calc_mux(i64 noundef %0) local_unnamed_addr #1 {
  %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 %29

8:                                                ; preds = %1
  %9 = call noundef i64 @_ZNKSt3mapIxiSt4lessIxESaISt4pairIKxiEEE5countERS3_(ptr noundef nonnull align 8 dereferenceable(48) @Hash1, 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) @Hash1, ptr noundef nonnull align 8 dereferenceable(8) %2)
  %12 = load i32, ptr %11, align 4, !tbaa !9
  br label %29

13:                                               ; preds = %8
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %3) #3
  store i32 1, ptr %3, align 4, !tbaa !9
  %14 = load i64, ptr %2, align 8, !tbaa !5
  %.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
  %15 = phi i32 [ %.pre, %._crit_edge.loopexit ], [ 1, %13 ]
  %16 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt3mapIxiSt4lessIxESaISt4pairIKxiEEEixERS3_(ptr noundef nonnull align 8 dereferenceable(48) @Hash1, ptr noundef nonnull align 8 dereferenceable(8) %2)
  store i32 %15, ptr %16, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %3) #3
  br label %29

.lr.ph:                                           ; preds = %13, %.lr.ph
  %17 = phi i64 [ %28, %.lr.ph ], [ %14, %13 ]
  %.0712 = phi i64 [ %27, %.lr.ph ], [ 2, %13 ]
  %18 = udiv i64 %17, %.0712
  %19 = sdiv i64 %17, %18
  %reass.sub = sub i64 %19, %.0712
  %20 = add i64 %reass.sub, 1
  %21 = srem i64 %20, 1000000007
  %22 = call noundef i32 @_Z7calc_mux(i64 noundef %18)
  %23 = sext i32 %22 to i64
  %24 = mul nsw i64 %21, %23
  %25 = srem i64 %24, 1000000007
  %26 = trunc nsw i64 %25 to i32
  call void @_Z3decRii(ptr noundef nonnull align 4 dereferenceable(4) %3, i32 noundef %26)
  %27 = add nuw nsw i64 %19, 1
  %28 = load i64, ptr %2, align 8, !tbaa !5
  %.not10.not = icmp slt i64 %19, %28
  br i1 %.not10.not, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !11

29:                                               ; preds = %._crit_edge, %10, %5
  %.0 = phi i32 [ %7, %5 ], [ %12, %10 ], [ %15, %._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 #1 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 #1 align 2

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

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

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 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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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