; LLVM IR from tangjz/acm-icpc :: projecteuler/379.ll (Z6calc_3x)

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

@_ZZ6calc_3xE5lastn = external hidden global i64, align 8
@_ZZ6calc_3xE3ret = external hidden global i64, align 8

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z6calc_3x(i64 noundef %0) #0 {
  %2 = alloca i64, align 8
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca i32, align 4
  %6 = alloca i64, align 8
  store i64 %0, ptr %3, align 8, !tbaa !5
  %7 = load i64, ptr @_ZZ6calc_3xE5lastn, align 8, !tbaa !5
  %8 = load i64, ptr %3, align 8, !tbaa !5
  %9 = icmp eq i64 %7, %8
  br i1 %9, label %10, label %12

10:                                               ; preds = %1
  %11 = load i64, ptr @_ZZ6calc_3xE3ret, align 8, !tbaa !5
  store i64 %11, ptr %2, align 8
  br label %67

12:                                               ; preds = %1
  %13 = load i64, ptr %3, align 8, !tbaa !5
  store i64 %13, ptr @_ZZ6calc_3xE5lastn, align 8, !tbaa !5
  store i64 0, ptr @_ZZ6calc_3xE3ret, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  store i64 1, ptr %4, align 8, !tbaa !5
  br label %14

14:                                               ; preds = %62, %12
  %15 = load i64, ptr %4, align 8, !tbaa !5
  %16 = load i64, ptr %4, align 8, !tbaa !5
  %17 = mul nsw i64 %15, %16
  %18 = load i64, ptr %4, align 8, !tbaa !5
  %19 = mul nsw i64 %17, %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 = %14
  store i32 2, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  br label %65

23:                                               ; preds = %14
  %24 = load i64, ptr %3, align 8, !tbaa !5
  %25 = load i64, ptr %4, align 8, !tbaa !5
  %26 = sdiv i64 %24, %25
  %27 = load i64, ptr %4, align 8, !tbaa !5
  %28 = sdiv i64 %26, %27
  %29 = load i64, ptr %4, align 8, !tbaa !5
  %30 = sub nsw i64 %28, %29
  %31 = mul nsw i64 3, %30
  %32 = add nsw i64 1, %31
  %33 = load i64, ptr @_ZZ6calc_3xE3ret, align 8, !tbaa !5
  %34 = add nsw i64 %33, %32
  store i64 %34, ptr @_ZZ6calc_3xE3ret, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %35 = load i64, ptr %4, align 8, !tbaa !5
  %36 = add nsw i64 %35, 1
  store i64 %36, ptr %6, align 8, !tbaa !5
  br label %37

37:                                               ; preds = %58, %23
  %38 = load i64, ptr %4, align 8, !tbaa !5
  %39 = load i64, ptr %6, align 8, !tbaa !5
  %40 = mul nsw i64 %38, %39
  %41 = load i64, ptr %6, align 8, !tbaa !5
  %42 = mul nsw i64 %40, %41
  %43 = load i64, ptr %3, align 8, !tbaa !5
  %44 = icmp sle i64 %42, %43
  br i1 %44, label %46, label %45

45:                                               ; preds = %37
  store i32 5, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  br label %61

46:                                               ; preds = %37
  %47 = load i64, ptr %3, align 8, !tbaa !5
  %48 = load i64, ptr %4, align 8, !tbaa !5
  %49 = sdiv i64 %47, %48
  %50 = load i64, ptr %6, align 8, !tbaa !5
  %51 = sdiv i64 %49, %50
  %52 = load i64, ptr %6, align 8, !tbaa !5
  %53 = sub nsw i64 %51, %52
  %54 = mul nsw i64 6, %53
  %55 = add nsw i64 3, %54
  %56 = load i64, ptr @_ZZ6calc_3xE3ret, align 8, !tbaa !5
  %57 = add nsw i64 %56, %55
  store i64 %57, ptr @_ZZ6calc_3xE3ret, align 8, !tbaa !5
  br label %58

58:                                               ; preds = %46
  %59 = load i64, ptr %6, align 8, !tbaa !5
  %60 = add nsw i64 %59, 1
  store i64 %60, ptr %6, align 8, !tbaa !5
  br label %37, !llvm.loop !9

61:                                               ; preds = %45
  br label %62

62:                                               ; preds = %61
  %63 = load i64, ptr %4, align 8, !tbaa !5
  %64 = add nsw i64 %63, 1
  store i64 %64, ptr %4, align 8, !tbaa !5
  br label %14, !llvm.loop !11

65:                                               ; preds = %22
  %66 = load i64, ptr @_ZZ6calc_3xE3ret, align 8, !tbaa !5
  store i64 %66, ptr %2, align 8
  br label %67

67:                                               ; preds = %65, %10
  %68 = load i64, ptr %2, align 8
  ret i64 %68
}

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

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


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

@_ZZ6calc_3xE5lastn = external hidden local_unnamed_addr global i64, align 8
@_ZZ6calc_3xE3ret = external hidden local_unnamed_addr global i64, align 8

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i64 @_Z6calc_3x(i64 noundef %0) local_unnamed_addr #0 {
  %2 = load i64, ptr @_ZZ6calc_3xE5lastn, align 8, !tbaa !5
  %3 = icmp eq i64 %2, %0
  br i1 %3, label %..loopexit26_crit_edge36, label %4

..loopexit26_crit_edge36:                         ; preds = %1
  %.023.pre = load i64, ptr @_ZZ6calc_3xE3ret, align 8, !tbaa !5
  br label %.loopexit26

4:                                                ; preds = %1
  store i64 %0, ptr @_ZZ6calc_3xE5lastn, align 8, !tbaa !5
  store i64 0, ptr @_ZZ6calc_3xE3ret, align 8, !tbaa !5
  %.not32 = icmp slt i64 %0, 1
  br i1 %.not32, label %.loopexit26, label %.lr.ph35

.loopexit:                                        ; preds = %.lr.ph, %.lr.ph35
  %.lcssa31 = phi i64 [ %12, %.lr.ph35 ], [ %21, %.lr.ph ]
  %5 = mul nuw nsw i64 %13, %13
  %6 = mul nuw nsw i64 %5, %13
  %.not = icmp sgt i64 %6, %0
  br i1 %.not, label %..loopexit26_crit_edge, label %.lr.ph35, !llvm.loop !9

.lr.ph35:                                         ; preds = %4, %.loopexit
  %.02234 = phi i64 [ %13, %.loopexit ], [ 1, %4 ]
  %.lcssa3033 = phi i64 [ %.lcssa31, %.loopexit ], [ 0, %4 ]
  %7 = udiv i64 %0, %.02234
  %8 = udiv i64 %7, %.02234
  %9 = sub nsw i64 %8, %.02234
  %10 = mul nsw i64 %9, 3
  %11 = add i64 %.lcssa3033, 1
  %12 = add i64 %11, %10
  %13 = add nuw nsw i64 %.02234, 1
  %14 = mul nuw nsw i64 %13, %.02234
  %15 = mul nuw nsw i64 %14, %13
  %.not2527 = icmp sgt i64 %15, %0
  br i1 %.not2527, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph35, %.lr.ph
  %.028 = phi i64 [ %22, %.lr.ph ], [ %13, %.lr.ph35 ]
  %16 = phi i64 [ %21, %.lr.ph ], [ %12, %.lr.ph35 ]
  %17 = udiv i64 %7, %.028
  %18 = sub nsw i64 %17, %.028
  %19 = mul nsw i64 %18, 6
  %20 = add i64 %16, 3
  %21 = add i64 %20, %19
  %22 = add nuw nsw i64 %.028, 1
  %23 = mul nuw nsw i64 %22, %.02234
  %24 = mul nuw nsw i64 %23, %22
  %.not25 = icmp sgt i64 %24, %0
  br i1 %.not25, label %.loopexit, label %.lr.ph, !llvm.loop !11

..loopexit26_crit_edge:                           ; preds = %.loopexit
  store i64 %.lcssa31, ptr @_ZZ6calc_3xE3ret, align 8, !tbaa !5
  br label %.loopexit26

.loopexit26:                                      ; preds = %..loopexit26_crit_edge36, %4, %..loopexit26_crit_edge
  %.023 = phi i64 [ %.023.pre, %..loopexit26_crit_edge36 ], [ 0, %4 ], [ %.lcssa31, %..loopexit26_crit_edge ]
  ret i64 %.023
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, 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"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


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