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

@_ZZ4calcxE6last_n = external hidden global i64, align 8
@_ZZ4calcxE3ret = external hidden global i64, align 8

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_Z5iSqrtx(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 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
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  store i64 %0, ptr %3, align 8, !tbaa !5
  %8 = load i64, ptr %3, align 8, !tbaa !5
  %9 = load i64, ptr @_ZZ4calcxE6last_n, align 8, !tbaa !5
  %10 = icmp eq i64 %8, %9
  br i1 %10, label %11, label %13

11:                                               ; preds = %1
  %12 = load i64, ptr @_ZZ4calcxE3ret, align 8, !tbaa !5
  store i64 %12, ptr %2, align 8
  br label %46

13:                                               ; preds = %1
  %14 = load i64, ptr %3, align 8, !tbaa !5
  store i64 %14, ptr @_ZZ4calcxE6last_n, align 8, !tbaa !5
  store i64 0, ptr @_ZZ4calcxE3ret, 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
  store i64 1, ptr %5, align 8, !tbaa !5
  br label %15

15:                                               ; preds = %37, %13
  %16 = load i64, ptr %5, align 8, !tbaa !5
  %17 = load i64, ptr %4, align 8, !tbaa !5
  %18 = shl i64 %17, 2
  %19 = add nsw i64 %16, %18
  %20 = load i64, ptr %3, align 8, !tbaa !5
  %21 = icmp sle i64 %19, %20
  br i1 %21, label %23, label %22

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

23:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %24 = load i64, ptr %3, align 8, !tbaa !5
  %25 = shl i64 %24, 1
  %26 = load i64, ptr %5, align 8, !tbaa !5
  %27 = sub nsw i64 %25, %26
  %28 = call noundef i64 @_Z5iSqrtx(i64 noundef %27)
  %29 = add nsw i64 %28, 1
  %30 = ashr i64 %29, 1
  %31 = load i64, ptr %4, align 8, !tbaa !5
  %32 = sub nsw i64 %30, %31
  store i64 %32, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  store i64 0, ptr %7, align 8, !tbaa !5
  %33 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %6, ptr noundef nonnull align 8 dereferenceable(8) %7)
  %34 = load i64, ptr %33, align 8, !tbaa !5
  %35 = load i64, ptr @_ZZ4calcxE3ret, align 8, !tbaa !5
  %36 = add nsw i64 %35, %34
  store i64 %36, ptr @_ZZ4calcxE3ret, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  br label %37

37:                                               ; preds = %23
  %38 = load i64, ptr %4, align 8, !tbaa !5
  %39 = shl i64 %38, 3
  %40 = load i64, ptr %5, align 8, !tbaa !5
  %41 = add nsw i64 %40, %39
  store i64 %41, ptr %5, align 8, !tbaa !5
  %42 = load i64, ptr %4, align 8, !tbaa !5
  %43 = add nsw i64 %42, 1
  store i64 %43, ptr %4, align 8, !tbaa !5
  br label %15, !llvm.loop !9

44:                                               ; preds = %22
  %45 = load i64, ptr @_ZZ4calcxE3ret, align 8, !tbaa !5
  store i64 %45, ptr %2, align 8
  br label %46

46:                                               ; preds = %44, %11
  %47 = load i64, ptr %2, align 8
  ret i64 %47
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) #2

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 = { inlinehint 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 #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 = distinct !{!9, !10}
!10 = !{!"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"

@_ZZ4calcxE6last_n = external hidden local_unnamed_addr global i64, align 8
@_ZZ4calcxE3ret = external hidden local_unnamed_addr global i64, align 8

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

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

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z4calcx(i64 noundef %0) local_unnamed_addr #0 {
  %2 = alloca i64, align 8
  %3 = alloca i64, align 8
  %4 = load i64, ptr @_ZZ4calcxE6last_n, align 8, !tbaa !5
  %5 = icmp eq i64 %4, %0
  br i1 %5, label %..loopexit_crit_edge, label %6

..loopexit_crit_edge:                             ; preds = %1
  %.013.pre = load i64, ptr @_ZZ4calcxE3ret, align 8, !tbaa !5
  br label %.loopexit

6:                                                ; preds = %1
  store i64 %0, ptr @_ZZ4calcxE6last_n, align 8, !tbaa !5
  store i64 0, ptr @_ZZ4calcxE3ret, align 8, !tbaa !5
  %.not15 = icmp slt i64 %0, 5
  br i1 %.not15, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %6
  %7 = shl nuw i64 %0, 1
  br label %8

8:                                                ; preds = %.lr.ph, %8
  %.017 = phi i64 [ 1, %.lr.ph ], [ %19, %8 ]
  %.01216 = phi i64 [ 1, %.lr.ph ], [ %20, %8 ]
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #3
  %9 = sub nsw i64 %7, %.017
  %10 = call noundef i64 @_Z5iSqrtx(i64 noundef %9)
  %11 = add nsw i64 %10, 1
  %12 = ashr i64 %11, 1
  %13 = sub nsw i64 %12, %.01216
  store i64 %13, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #3
  store i64 0, ptr %3, align 8, !tbaa !5
  %14 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %2, ptr noundef nonnull align 8 dereferenceable(8) %3)
  %15 = load i64, ptr %14, align 8, !tbaa !5
  %16 = load i64, ptr @_ZZ4calcxE3ret, align 8, !tbaa !5
  %17 = add nsw i64 %16, %15
  store i64 %17, ptr @_ZZ4calcxE3ret, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #3
  %18 = shl i64 %.01216, 3
  %19 = add nsw i64 %.017, %18
  %20 = add nuw nsw i64 %.01216, 1
  %21 = shl i64 %20, 2
  %22 = add nsw i64 %19, %21
  %.not = icmp sgt i64 %22, %0
  br i1 %.not, label %.loopexit, label %8, !llvm.loop !9

.loopexit:                                        ; preds = %8, %..loopexit_crit_edge, %6
  %.013 = phi i64 [ %.013.pre, %..loopexit_crit_edge ], [ 0, %6 ], [ %17, %8 ]
  ret i64 %.013
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #2

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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint 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 #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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass