; LLVM IR from DionysiosB/CodeForces :: 0900-0999 (940B-OurTanyaIsCryingOutLoud)

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z7getCostxRKxS0_S0_RSt3mapIxxSt4lessIxESaISt4pairIS_xEEE(i64 noundef %0, ptr noundef nonnull align 8 dereferenceable(8) %1, ptr noundef nonnull align 8 dereferenceable(8) %2, ptr noundef nonnull align 8 dereferenceable(8) %3, ptr noundef nonnull align 8 dereferenceable(48) %4) #0 {
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i64, align 8
  %13 = alloca i64, align 8
  %14 = alloca i64, align 8
  store i64 %0, ptr %7, align 8, !tbaa !5
  store ptr %1, ptr %8, align 8, !tbaa !9
  store ptr %2, ptr %9, align 8, !tbaa !9
  store ptr %3, ptr %10, align 8, !tbaa !9
  store ptr %4, ptr %11, align 8, !tbaa !9
  %15 = load i64, ptr %7, align 8, !tbaa !5
  %16 = icmp eq i64 %15, 1
  br i1 %16, label %17, label %18

17:                                               ; preds = %5
  store i64 0, ptr %6, align 8
  br label %114

18:                                               ; preds = %5
  %19 = load ptr, ptr %8, align 8, !tbaa !9
  %20 = load i64, ptr %19, align 8, !tbaa !5
  %21 = icmp sle i64 %20, 1
  br i1 %21, label %27, label %22

22:                                               ; preds = %18
  %23 = load ptr, ptr %8, align 8, !tbaa !9
  %24 = load i64, ptr %23, align 8, !tbaa !5
  %25 = load i64, ptr %7, align 8, !tbaa !5
  %26 = icmp sgt i64 %24, %25
  br i1 %26, label %27, label %33

27:                                               ; preds = %22, %18
  %28 = load ptr, ptr %9, align 8, !tbaa !9
  %29 = load i64, ptr %28, align 8, !tbaa !5
  %30 = load i64, ptr %7, align 8, !tbaa !5
  %31 = sub nsw i64 %30, 1
  %32 = mul nsw i64 %29, %31
  store i64 %32, ptr %6, align 8
  br label %114

33:                                               ; preds = %22
  %34 = load ptr, ptr %11, align 8, !tbaa !9
  %35 = call noundef i64 @_ZNKSt3mapIxxSt4lessIxESaISt4pairIKxxEEE5countERS3_(ptr noundef nonnull align 8 dereferenceable(48) %34, ptr noundef nonnull align 8 dereferenceable(8) %7)
  %36 = icmp ne i64 %35, 0
  br i1 %36, label %37, label %41

37:                                               ; preds = %33
  %38 = load ptr, ptr %11, align 8, !tbaa !9
  %39 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSt3mapIxxSt4lessIxESaISt4pairIKxxEEEixERS3_(ptr noundef nonnull align 8 dereferenceable(48) %38, ptr noundef nonnull align 8 dereferenceable(8) %7)
  %40 = load i64, ptr %39, align 8, !tbaa !5
  store i64 %40, ptr %6, align 8
  br label %114

41:                                               ; preds = %33
  %42 = load i64, ptr %7, align 8, !tbaa !5
  %43 = load ptr, ptr %8, align 8, !tbaa !9
  %44 = load i64, ptr %43, align 8, !tbaa !5
  %45 = srem i64 %42, %44
  %46 = icmp eq i64 %45, 0
  br i1 %46, label %47, label %89

47:                                               ; preds = %41
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #2
  %48 = load i64, ptr %7, align 8, !tbaa !5
  %49 = load ptr, ptr %8, align 8, !tbaa !9
  %50 = load i64, ptr %49, align 8, !tbaa !5
  %51 = sdiv i64 %48, %50
  %52 = load ptr, ptr %8, align 8, !tbaa !9
  %53 = load ptr, ptr %9, align 8, !tbaa !9
  %54 = load ptr, ptr %10, align 8, !tbaa !9
  %55 = load ptr, ptr %11, align 8, !tbaa !9
  %56 = call noundef i64 @_Z7getCostxRKxS0_S0_RSt3mapIxxSt4lessIxESaISt4pairIS_xEEE(i64 noundef %51, ptr noundef nonnull align 8 dereferenceable(8) %52, ptr noundef nonnull align 8 dereferenceable(8) %53, ptr noundef nonnull align 8 dereferenceable(8) %54, ptr noundef nonnull align 8 dereferenceable(48) %55)
  %57 = load ptr, ptr %10, align 8, !tbaa !9
  %58 = load i64, ptr %57, align 8, !tbaa !5
  %59 = load i64, ptr %7, align 8, !tbaa !5
  %60 = load i64, ptr %7, align 8, !tbaa !5
  %61 = load ptr, ptr %8, align 8, !tbaa !9
  %62 = load i64, ptr %61, align 8, !tbaa !5
  %63 = sdiv i64 %60, %62
  %64 = sub nsw i64 %59, %63
  %65 = load ptr, ptr %9, align 8, !tbaa !9
  %66 = load i64, ptr %65, align 8, !tbaa !5
  %67 = mul nsw i64 %64, %66
  %68 = icmp slt i64 %58, %67
  br i1 %68, label %69, label %72

69:                                               ; preds = %47
  %70 = load ptr, ptr %10, align 8, !tbaa !9
  %71 = load i64, ptr %70, align 8, !tbaa !5
  br label %82

72:                                               ; preds = %47
  %73 = load i64, ptr %7, align 8, !tbaa !5
  %74 = load i64, ptr %7, align 8, !tbaa !5
  %75 = load ptr, ptr %8, align 8, !tbaa !9
  %76 = load i64, ptr %75, align 8, !tbaa !5
  %77 = sdiv i64 %74, %76
  %78 = sub nsw i64 %73, %77
  %79 = load ptr, ptr %9, align 8, !tbaa !9
  %80 = load i64, ptr %79, align 8, !tbaa !5
  %81 = mul nsw i64 %78, %80
  br label %82

82:                                               ; preds = %72, %69
  %83 = phi i64 [ %71, %69 ], [ %81, %72 ]
  %84 = add nsw i64 %56, %83
  store i64 %84, ptr %12, align 8, !tbaa !5
  %85 = load i64, ptr %12, align 8, !tbaa !5
  %86 = load ptr, ptr %11, align 8, !tbaa !9
  %87 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSt3mapIxxSt4lessIxESaISt4pairIKxxEEEixERS3_(ptr noundef nonnull align 8 dereferenceable(48) %86, ptr noundef nonnull align 8 dereferenceable(8) %7)
  store i64 %85, ptr %87, align 8, !tbaa !5
  %88 = load i64, ptr %12, align 8, !tbaa !5
  store i64 %88, ptr %6, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #2
  br label %114

89:                                               ; preds = %41
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #2
  %90 = load ptr, ptr %8, align 8, !tbaa !9
  %91 = load i64, ptr %90, align 8, !tbaa !5
  %92 = load i64, ptr %7, align 8, !tbaa !5
  %93 = load ptr, ptr %8, align 8, !tbaa !9
  %94 = load i64, ptr %93, align 8, !tbaa !5
  %95 = sdiv i64 %92, %94
  %96 = mul nsw i64 %91, %95
  store i64 %96, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #2
  %97 = load i64, ptr %13, align 8, !tbaa !5
  %98 = load ptr, ptr %8, align 8, !tbaa !9
  %99 = load ptr, ptr %9, align 8, !tbaa !9
  %100 = load ptr, ptr %10, align 8, !tbaa !9
  %101 = load ptr, ptr %11, align 8, !tbaa !9
  %102 = call noundef i64 @_Z7getCostxRKxS0_S0_RSt3mapIxxSt4lessIxESaISt4pairIS_xEEE(i64 noundef %97, ptr noundef nonnull align 8 dereferenceable(8) %98, ptr noundef nonnull align 8 dereferenceable(8) %99, ptr noundef nonnull align 8 dereferenceable(8) %100, ptr noundef nonnull align 8 dereferenceable(48) %101)
  %103 = load i64, ptr %7, align 8, !tbaa !5
  %104 = load i64, ptr %13, align 8, !tbaa !5
  %105 = sub nsw i64 %103, %104
  %106 = load ptr, ptr %9, align 8, !tbaa !9
  %107 = load i64, ptr %106, align 8, !tbaa !5
  %108 = mul nsw i64 %105, %107
  %109 = add nsw i64 %102, %108
  store i64 %109, ptr %14, align 8, !tbaa !5
  %110 = load i64, ptr %14, align 8, !tbaa !5
  %111 = load ptr, ptr %11, align 8, !tbaa !9
  %112 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSt3mapIxxSt4lessIxESaISt4pairIKxxEEEixERS3_(ptr noundef nonnull align 8 dereferenceable(48) %111, ptr noundef nonnull align 8 dereferenceable(8) %7)
  store i64 %110, ptr %112, align 8, !tbaa !5
  %113 = load i64, ptr %14, align 8, !tbaa !5
  store i64 %113, ptr %6, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #2
  br label %114

114:                                              ; preds = %89, %82, %37, %27, %17
  %115 = load i64, ptr %6, align 8
  ret i64 %115
}

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_ZNKSt3mapIxxSt4lessIxESaISt4pairIKxxEEE5countERS3_(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 8 dereferenceable(8) ptr @_ZNSt3mapIxxSt4lessIxESaISt4pairIKxxEEEixERS3_(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: 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 = { 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 = !{!"any pointer", !7, i64 0}


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

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z7getCostxRKxS0_S0_RSt3mapIxxSt4lessIxESaISt4pairIS_xEEE(i64 noundef %0, ptr noundef nonnull align 8 dereferenceable(8) %1, ptr noundef nonnull align 8 dereferenceable(8) %2, ptr noundef nonnull align 8 dereferenceable(8) %3, ptr noundef nonnull align 8 dereferenceable(48) %4) local_unnamed_addr #0 {
  %6 = alloca i64, align 8
  store i64 %0, ptr %6, align 8, !tbaa !5
  %7 = icmp eq i64 %0, 1
  br i1 %7, label %47, label %8

8:                                                ; preds = %5
  %9 = load i64, ptr %1, align 8, !tbaa !5
  %10 = icmp slt i64 %9, 2
  %11 = icmp sgt i64 %9, %0
  %or.cond = or i1 %10, %11
  br i1 %or.cond, label %12, label %16

12:                                               ; preds = %8
  %13 = load i64, ptr %2, align 8, !tbaa !5
  %14 = add nsw i64 %0, -1
  %15 = mul nsw i64 %13, %14
  br label %47

16:                                               ; preds = %8
  %17 = call noundef i64 @_ZNKSt3mapIxxSt4lessIxESaISt4pairIKxxEEE5countERS3_(ptr noundef nonnull align 8 dereferenceable(48) %4, ptr noundef nonnull align 8 dereferenceable(8) %6)
  %.not = icmp eq i64 %17, 0
  br i1 %.not, label %21, label %18

18:                                               ; preds = %16
  %19 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSt3mapIxxSt4lessIxESaISt4pairIKxxEEEixERS3_(ptr noundef nonnull align 8 dereferenceable(48) %4, ptr noundef nonnull align 8 dereferenceable(8) %6)
  %20 = load i64, ptr %19, align 8, !tbaa !5
  br label %47

21:                                               ; preds = %16
  %22 = load i64, ptr %6, align 8, !tbaa !5
  %.fr = freeze i64 %22
  %23 = load i64, ptr %1, align 8, !tbaa !5
  %24 = srem i64 %.fr, %23
  %25 = sdiv i64 %.fr, %23
  %26 = icmp eq i64 %24, 0
  br i1 %26, label %27, label %38

27:                                               ; preds = %21
  %28 = call noundef i64 @_Z7getCostxRKxS0_S0_RSt3mapIxxSt4lessIxESaISt4pairIS_xEEE(i64 noundef %25, ptr noundef nonnull align 8 dereferenceable(8) %1, ptr noundef nonnull align 8 dereferenceable(8) %2, ptr noundef nonnull align 8 dereferenceable(8) %3, ptr noundef nonnull align 8 dereferenceable(48) %4)
  %29 = load i64, ptr %3, align 8, !tbaa !5
  %30 = load i64, ptr %6, align 8, !tbaa !5
  %31 = load i64, ptr %1, align 8, !tbaa !5
  %32 = sdiv i64 %30, %31
  %33 = sub nsw i64 %30, %32
  %34 = load i64, ptr %2, align 8, !tbaa !5
  %35 = mul nsw i64 %33, %34
  %. = call i64 @llvm.smin.i64(i64 %29, i64 %35)
  %36 = add nsw i64 %., %28
  %37 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSt3mapIxxSt4lessIxESaISt4pairIKxxEEEixERS3_(ptr noundef nonnull align 8 dereferenceable(48) %4, ptr noundef nonnull align 8 dereferenceable(8) %6)
  store i64 %36, ptr %37, align 8, !tbaa !5
  br label %47

38:                                               ; preds = %21
  %39 = sub nsw i64 %.fr, %24
  %40 = call noundef i64 @_Z7getCostxRKxS0_S0_RSt3mapIxxSt4lessIxESaISt4pairIS_xEEE(i64 noundef %39, ptr noundef nonnull align 8 dereferenceable(8) %1, ptr noundef nonnull align 8 dereferenceable(8) %2, ptr noundef nonnull align 8 dereferenceable(8) %3, ptr noundef nonnull align 8 dereferenceable(48) %4)
  %41 = load i64, ptr %6, align 8, !tbaa !5
  %42 = sub nsw i64 %41, %39
  %43 = load i64, ptr %2, align 8, !tbaa !5
  %44 = mul nsw i64 %42, %43
  %45 = add nsw i64 %44, %40
  %46 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSt3mapIxxSt4lessIxESaISt4pairIKxxEEEixERS3_(ptr noundef nonnull align 8 dereferenceable(48) %4, ptr noundef nonnull align 8 dereferenceable(8) %6)
  store i64 %45, ptr %46, align 8, !tbaa !5
  br label %47

47:                                               ; preds = %5, %38, %27, %18, %12
  %.0 = phi i64 [ %15, %12 ], [ %20, %18 ], [ %36, %27 ], [ %45, %38 ], [ 0, %5 ]
  ret i64 %.0
}

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.smin.i64(i64, i64) #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 speculatable willreturn memory(none) }

!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"}


; Effective O3 passes: DivRemPairsPass,EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,ReassociatePass,SROAPass,SimplifyCFGPass