; LLVM IR from tangjz/acm-icpc :: projecteuler/379.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"

@mu = external dso_local global [1000001 x i32], align 16

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_Z6calc_3x(i64 noundef) #0

; 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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z4calcx(i64 noundef %0) #0 {
  %2 = alloca i64, align 8
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  store i64 %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  %6 = load i64, ptr %2, align 8, !tbaa !5
  store i64 %6, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  store i64 1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %7 = load i64, ptr %2, align 8, !tbaa !5
  %8 = sitofp i64 %7 to double
  %9 = call double @sqrt(double noundef %8) #3
  %10 = fptosi double %9 to i64
  store i64 %10, ptr %5, align 8, !tbaa !5
  br label %11

11:                                               ; preds = %36, %1
  %12 = load i64, ptr %4, align 8, !tbaa !5
  %13 = load i64, ptr %5, align 8, !tbaa !5
  %14 = icmp sle i64 %12, %13
  br i1 %14, label %16, label %15

15:                                               ; preds = %11
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  br label %39

16:                                               ; preds = %11
  %17 = load i64, ptr %4, align 8, !tbaa !5
  %18 = getelementptr inbounds [1000001 x i32], ptr @mu, i64 0, i64 %17
  %19 = load i32, ptr %18, align 4, !tbaa !9
  %20 = icmp ne i32 %19, 0
  br i1 %20, label %21, label %35

21:                                               ; preds = %16
  %22 = load i64, ptr %4, align 8, !tbaa !5
  %23 = getelementptr inbounds [1000001 x i32], ptr @mu, i64 0, i64 %22
  %24 = load i32, ptr %23, align 4, !tbaa !9
  %25 = sext i32 %24 to i64
  %26 = load i64, ptr %2, align 8, !tbaa !5
  %27 = load i64, ptr %4, align 8, !tbaa !5
  %28 = sdiv i64 %26, %27
  %29 = load i64, ptr %4, align 8, !tbaa !5
  %30 = sdiv i64 %28, %29
  %31 = call noundef i64 @_Z6calc_3x(i64 noundef %30)
  %32 = mul nsw i64 %25, %31
  %33 = load i64, ptr %3, align 8, !tbaa !5
  %34 = add nsw i64 %33, %32
  store i64 %34, ptr %3, align 8, !tbaa !5
  br label %35

35:                                               ; preds = %21, %16
  br label %36

36:                                               ; preds = %35
  %37 = load i64, ptr %4, align 8, !tbaa !5
  %38 = add nsw i64 %37, 1
  store i64 %38, ptr %4, align 8, !tbaa !5
  br label %11, !llvm.loop !11

39:                                               ; preds = %15
  %40 = load i64, ptr %3, align 8, !tbaa !5
  %41 = ashr i64 %40, 1
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  ret i64 %41
}

; Function Attrs: nounwind
declare double @sqrt(double noundef) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nounwind "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"

@mu = external dso_local local_unnamed_addr global [1000001 x i32], align 16

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_Z6calc_3x(i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef range(i64 -4611686018427387904, 4611686018427387904) i64 @_Z4calcx(i64 noundef %0) local_unnamed_addr #0 {
  %2 = sitofp i64 %0 to double
  %3 = tail call double @sqrt(double noundef %2) #2
  %4 = fptosi double %3 to i64
  %.not15 = icmp slt i64 %4, 1
  br i1 %.not15, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %15, %1
  %.0.lcssa = phi i64 [ %0, %1 ], [ %.1, %15 ]
  %5 = ashr i64 %.0.lcssa, 1
  ret i64 %5

.lr.ph:                                           ; preds = %1, %15
  %.017 = phi i64 [ %.1, %15 ], [ %0, %1 ]
  %.01216 = phi i64 [ %16, %15 ], [ 1, %1 ]
  %6 = getelementptr inbounds [1000001 x i32], ptr @mu, i64 0, i64 %.01216
  %7 = load i32, ptr %6, align 4, !tbaa !5
  %.not14 = icmp eq i32 %7, 0
  br i1 %.not14, label %15, label %8

8:                                                ; preds = %.lr.ph
  %9 = sext i32 %7 to i64
  %10 = sdiv i64 %0, %.01216
  %11 = sdiv i64 %10, %.01216
  %12 = tail call noundef i64 @_Z6calc_3x(i64 noundef %11)
  %13 = mul nsw i64 %12, %9
  %14 = add nsw i64 %13, %.017
  br label %15

15:                                               ; preds = %.lr.ph, %8
  %.1 = phi i64 [ %14, %8 ], [ %.017, %.lr.ph ]
  %16 = add nuw i64 %.01216, 1
  %exitcond.not = icmp eq i64 %.01216, %4
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !9
}

; Function Attrs: mustprogress nofree nounwind willreturn memory(write)
declare double @sqrt(double noundef) local_unnamed_addr #1

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 nofree nounwind willreturn memory(write) "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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass