; LLVM IR from ShahjalalShohag/code-library :: Number Theory/Floor Sum of  Arithmetic Progressions.ll (Z9floor_sumxxxx)

; 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 nounwind uwtable
declare dso_local noundef i64 @_Z5sumsqx(i64 noundef) #0

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z9floor_sumxxxx(i64 noundef %0, i64 noundef %1, i64 noundef %2, i64 noundef %3) #1 {
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca i32, align 4
  %12 = alloca i64, align 8
  store i64 %0, ptr %6, align 8, !tbaa !5
  store i64 %1, ptr %7, align 8, !tbaa !5
  store i64 %2, ptr %8, align 8, !tbaa !5
  store i64 %3, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %13 = load i64, ptr %7, align 8, !tbaa !5
  %14 = load i64, ptr %8, align 8, !tbaa !5
  %15 = sdiv i64 %13, %14
  %16 = load i64, ptr %9, align 8, !tbaa !5
  %17 = call noundef i64 @_Z5sumsqx(i64 noundef %16)
  %18 = mul nsw i64 %15, %17
  %19 = load i64, ptr %6, align 8, !tbaa !5
  %20 = load i64, ptr %8, align 8, !tbaa !5
  %21 = sdiv i64 %19, %20
  %22 = load i64, ptr %9, align 8, !tbaa !5
  %23 = mul nsw i64 %21, %22
  %24 = add nsw i64 %18, %23
  store i64 %24, ptr %10, align 8, !tbaa !5
  %25 = load i64, ptr %8, align 8, !tbaa !5
  %26 = load i64, ptr %7, align 8, !tbaa !5
  %27 = srem i64 %26, %25
  store i64 %27, ptr %7, align 8, !tbaa !5
  %28 = load i64, ptr %8, align 8, !tbaa !5
  %29 = load i64, ptr %6, align 8, !tbaa !5
  %30 = srem i64 %29, %28
  store i64 %30, ptr %6, align 8, !tbaa !5
  %31 = load i64, ptr %7, align 8, !tbaa !5
  %32 = icmp ne i64 %31, 0
  br i1 %32, label %35, label %33

33:                                               ; preds = %4
  %34 = load i64, ptr %10, align 8, !tbaa !5
  store i64 %34, ptr %5, align 8
  store i32 1, ptr %11, align 4
  br label %58

35:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  %36 = load i64, ptr %9, align 8, !tbaa !5
  %37 = load i64, ptr %7, align 8, !tbaa !5
  %38 = mul nsw i64 %36, %37
  %39 = load i64, ptr %6, align 8, !tbaa !5
  %40 = add nsw i64 %38, %39
  %41 = load i64, ptr %8, align 8, !tbaa !5
  %42 = sdiv i64 %40, %41
  store i64 %42, ptr %12, align 8, !tbaa !5
  %43 = load i64, ptr %10, align 8, !tbaa !5
  %44 = load i64, ptr %9, align 8, !tbaa !5
  %45 = sub nsw i64 %44, 1
  %46 = load i64, ptr %12, align 8, !tbaa !5
  %47 = mul nsw i64 %45, %46
  %48 = add nsw i64 %43, %47
  %49 = load i64, ptr %8, align 8, !tbaa !5
  %50 = sub nsw i64 %49, 1
  %51 = load i64, ptr %6, align 8, !tbaa !5
  %52 = sub nsw i64 %50, %51
  %53 = load i64, ptr %8, align 8, !tbaa !5
  %54 = load i64, ptr %7, align 8, !tbaa !5
  %55 = load i64, ptr %12, align 8, !tbaa !5
  %56 = call noundef i64 @_Z9floor_sumxxxx(i64 noundef %52, i64 noundef %53, i64 noundef %54, i64 noundef %55)
  %57 = sub nsw i64 %48, %56
  store i64 %57, ptr %5, align 8
  store i32 1, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  br label %58

58:                                               ; preds = %35, %33
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  %59 = load i64, ptr %5, align 8
  ret i64 %59
}

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


; 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 nounwind uwtable
declare dso_local noundef i64 @_Z5sumsqx(i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z9floor_sumxxxx(i64 noundef %0, i64 noundef %1, i64 noundef %2, i64 noundef %3) local_unnamed_addr #0 {
  %5 = sdiv i64 %1, %2
  %6 = tail call noundef i64 @_Z5sumsqx(i64 noundef %3)
  %7 = mul nsw i64 %6, %5
  %8 = sdiv i64 %0, %2
  %9 = srem i64 %0, %2
  %10 = mul nsw i64 %8, %3
  %11 = add nsw i64 %10, %7
  %12 = srem i64 %1, %2
  %.not = icmp eq i64 %12, 0
  br i1 %.not, label %common.ret26, label %13

common.ret26:                                     ; preds = %4, %13
  %common.ret26.op = phi i64 [ %23, %13 ], [ %11, %4 ]
  ret i64 %common.ret26.op

13:                                               ; preds = %4
  %14 = mul nsw i64 %12, %3
  %15 = add nsw i64 %9, %14
  %16 = sdiv i64 %15, %2
  %17 = add nsw i64 %3, -1
  %18 = mul nsw i64 %16, %17
  %19 = add nsw i64 %18, %11
  %20 = xor i64 %9, -1
  %21 = add i64 %20, %2
  %22 = tail call noundef i64 @_Z9floor_sumxxxx(i64 noundef %21, i64 noundef %2, i64 noundef %12, i64 noundef %16)
  %23 = sub i64 %19, %22
  br label %common.ret26
}

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

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


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