; LLVM IR from eyangch/competitive-programming :: USACOcontests/platinum/2021.01/sum_of_distances.ll (Z4calcxxRA200000_xS0_S0_S0_x)

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

@MOD = external dso_local global i64, align 8

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

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z4calcxxRA200000_xS0_S0_S0_x(i64 noundef %0, i64 noundef %1, ptr noundef nonnull align 8 dereferenceable(1600000) %2, ptr noundef nonnull align 8 dereferenceable(1600000) %3, ptr noundef nonnull align 8 dereferenceable(1600000) %4, ptr noundef nonnull align 8 dereferenceable(1600000) %5, i64 noundef %6) #1 {
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca ptr, align 8
  %12 = alloca ptr, align 8
  %13 = alloca ptr, align 8
  %14 = alloca ptr, align 8
  %15 = alloca i64, align 8
  %16 = alloca i64, align 8
  %17 = alloca i32, align 4
  store i64 %0, ptr %9, align 8, !tbaa !5
  store i64 %1, ptr %10, align 8, !tbaa !5
  store ptr %2, ptr %11, align 8, !tbaa !9
  store ptr %3, ptr %12, align 8, !tbaa !9
  store ptr %4, ptr %13, align 8, !tbaa !9
  store ptr %5, ptr %14, align 8, !tbaa !9
  store i64 %6, ptr %15, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #2
  store i64 1, ptr %16, align 8, !tbaa !5
  %18 = load i64, ptr %10, align 8, !tbaa !5
  %19 = sitofp i64 %18 to double
  %20 = fcmp oeq double %19, 1.000000e+15
  br i1 %20, label %21, label %22

21:                                               ; preds = %7
  store i64 0, ptr %8, align 8
  store i32 1, ptr %17, align 4
  br label %64

22:                                               ; preds = %7
  %23 = load ptr, ptr %11, align 8, !tbaa !9
  %24 = load i64, ptr %10, align 8, !tbaa !5
  %25 = getelementptr inbounds [200000 x i64], ptr %23, i64 0, i64 %24
  %26 = load i64, ptr %25, align 8, !tbaa !5
  store i64 %26, ptr %16, align 8, !tbaa !5
  %27 = load ptr, ptr %13, align 8, !tbaa !9
  %28 = load i64, ptr %10, align 8, !tbaa !5
  %29 = getelementptr inbounds [200000 x i64], ptr %27, i64 0, i64 %28
  %30 = load i64, ptr %29, align 8, !tbaa !5
  %31 = icmp ne i64 %30, 0
  br i1 %31, label %32, label %33

32:                                               ; preds = %22
  store i64 0, ptr %8, align 8
  store i32 1, ptr %17, align 4
  br label %64

33:                                               ; preds = %22
  %34 = load i64, ptr %10, align 8, !tbaa !5
  %35 = load i64, ptr %15, align 8, !tbaa !5
  %36 = icmp sge i64 %34, %35
  br i1 %36, label %37, label %57

37:                                               ; preds = %33
  %38 = load i64, ptr %16, align 8, !tbaa !5
  %39 = load ptr, ptr %12, align 8, !tbaa !9
  %40 = load i64, ptr %10, align 8, !tbaa !5
  %41 = load i64, ptr %15, align 8, !tbaa !5
  %42 = sub nsw i64 %40, %41
  %43 = getelementptr inbounds [200000 x i64], ptr %39, i64 0, i64 %42
  %44 = load i64, ptr %43, align 8, !tbaa !5
  %45 = mul nsw i64 %38, %44
  %46 = load i64, ptr @MOD, align 8, !tbaa !5
  %47 = srem i64 %45, %46
  store i64 %47, ptr %16, align 8, !tbaa !5
  %48 = load ptr, ptr %14, align 8, !tbaa !9
  %49 = load i64, ptr %10, align 8, !tbaa !5
  %50 = load i64, ptr %15, align 8, !tbaa !5
  %51 = sub nsw i64 %49, %50
  %52 = getelementptr inbounds [200000 x i64], ptr %48, i64 0, i64 %51
  %53 = load i64, ptr %52, align 8, !tbaa !5
  %54 = icmp ne i64 %53, 0
  br i1 %54, label %55, label %56

55:                                               ; preds = %37
  store i64 0, ptr %8, align 8
  store i32 1, ptr %17, align 4
  br label %64

56:                                               ; preds = %37
  br label %62

57:                                               ; preds = %33
  %58 = load i64, ptr %9, align 8, !tbaa !5
  %59 = icmp ne i64 %58, 0
  br i1 %59, label %60, label %61

60:                                               ; preds = %57
  store i64 0, ptr %8, align 8
  store i32 1, ptr %17, align 4
  br label %64

61:                                               ; preds = %57
  br label %62

62:                                               ; preds = %61, %56
  %63 = load i64, ptr %16, align 8, !tbaa !5
  store i64 %63, ptr %8, align 8
  store i32 1, ptr %17, align 4
  br label %64

64:                                               ; preds = %62, %60, %55, %32, %21
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #2
  %65 = load i64, ptr %8, align 8
  ret i64 %65
}

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

@MOD = external dso_local local_unnamed_addr global i64, align 8

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, inaccessiblemem: none) uwtable
define dso_local noundef i64 @_Z4calcxxRA200000_xS0_S0_S0_x(i64 noundef %0, i64 noundef %1, ptr nocapture noundef nonnull readonly align 8 dereferenceable(1600000) %2, ptr nocapture noundef nonnull readonly align 8 dereferenceable(1600000) %3, ptr nocapture noundef nonnull readonly align 8 dereferenceable(1600000) %4, ptr nocapture noundef nonnull readonly align 8 dereferenceable(1600000) %5, i64 noundef %6) local_unnamed_addr #0 {
  %8 = icmp eq i64 %1, 1000000000000000
  br i1 %8, label %26, label %9

9:                                                ; preds = %7
  %10 = getelementptr inbounds [200000 x i64], ptr %2, i64 0, i64 %1
  %11 = load i64, ptr %10, align 8, !tbaa !5
  %12 = getelementptr inbounds [200000 x i64], ptr %4, i64 0, i64 %1
  %13 = load i64, ptr %12, align 8, !tbaa !5
  %.not = icmp eq i64 %13, 0
  br i1 %.not, label %14, label %26

14:                                               ; preds = %9
  %.not19 = icmp slt i64 %1, %6
  br i1 %.not19, label %24, label %15

15:                                               ; preds = %14
  %16 = sub nsw i64 %1, %6
  %17 = getelementptr inbounds [200000 x i64], ptr %3, i64 0, i64 %16
  %18 = load i64, ptr %17, align 8, !tbaa !5
  %19 = mul nsw i64 %18, %11
  %20 = load i64, ptr @MOD, align 8, !tbaa !5
  %21 = srem i64 %19, %20
  %22 = getelementptr inbounds [200000 x i64], ptr %5, i64 0, i64 %16
  %23 = load i64, ptr %22, align 8, !tbaa !5
  %.not21 = icmp eq i64 %23, 0
  br i1 %.not21, label %25, label %26

24:                                               ; preds = %14
  %.not20 = icmp eq i64 %0, 0
  br i1 %.not20, label %25, label %26

25:                                               ; preds = %24, %15
  %.0 = phi i64 [ %21, %15 ], [ %11, %24 ]
  br label %26

26:                                               ; preds = %24, %15, %9, %7, %25
  %.016 = phi i64 [ %.0, %25 ], [ 0, %7 ], [ 0, %9 ], [ 0, %15 ], [ 0, %24 ]
  ret i64 %.016
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, inaccessiblemem: none) 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)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}


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