; LLVM IR from tangjz/acm-icpc :: self_training/2015-09-22/C.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"

; 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) #2
  store i64 0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  store i64 0, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  store i64 54, ptr %5, align 8, !tbaa !5
  br label %6

6:                                                ; preds = %37, %1
  %7 = load i64, ptr %5, align 8, !tbaa !5
  %8 = icmp sge i64 %7, 0
  br i1 %8, label %10, label %9

9:                                                ; preds = %6
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  br label %40

10:                                               ; preds = %6
  %11 = load i64, ptr %2, align 8, !tbaa !5
  %12 = load i64, ptr %5, align 8, !tbaa !5
  %13 = shl i64 1, %12
  %14 = and i64 %11, %13
  %15 = icmp ne i64 %14, 0
  br i1 %15, label %16, label %36

16:                                               ; preds = %10
  %17 = load i64, ptr %4, align 8, !tbaa !5
  %18 = load i64, ptr %5, align 8, !tbaa !5
  %19 = shl i64 1, %18
  %20 = mul nsw i64 %17, %19
  %21 = load i64, ptr %3, align 8, !tbaa !5
  %22 = add nsw i64 %21, %20
  store i64 %22, ptr %3, align 8, !tbaa !5
  %23 = load i64, ptr %5, align 8, !tbaa !5
  %24 = icmp ne i64 %23, 0
  br i1 %24, label %25, label %33

25:                                               ; preds = %16
  %26 = load i64, ptr %5, align 8, !tbaa !5
  %27 = load i64, ptr %5, align 8, !tbaa !5
  %28 = sub nsw i64 %27, 1
  %29 = shl i64 1, %28
  %30 = mul nsw i64 %26, %29
  %31 = load i64, ptr %3, align 8, !tbaa !5
  %32 = add nsw i64 %31, %30
  store i64 %32, ptr %3, align 8, !tbaa !5
  br label %33

33:                                               ; preds = %25, %16
  %34 = load i64, ptr %4, align 8, !tbaa !5
  %35 = add nsw i64 %34, 1
  store i64 %35, ptr %4, align 8, !tbaa !5
  br label %36

36:                                               ; preds = %33, %10
  br label %37

37:                                               ; preds = %36
  %38 = load i64, ptr %5, align 8, !tbaa !5
  %39 = add nsw i64 %38, -1
  store i64 %39, ptr %5, align 8, !tbaa !5
  br label %6, !llvm.loop !9

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

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


; 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 nofree norecurse nosync nounwind willreturn memory(none) uwtable
define dso_local noundef i64 @_Z4calcx(i64 noundef %0) local_unnamed_addr #0 {
  br label %3

2:                                                ; preds = %13
  ret i64 %.2

3:                                                ; preds = %1, %13
  %.019 = phi i64 [ 54, %1 ], [ %.pre-phi, %13 ]
  %.01318 = phi i64 [ 0, %1 ], [ %.1, %13 ]
  %.01417 = phi i64 [ 0, %1 ], [ %.2, %13 ]
  %4 = shl nuw i64 1, %.019
  %5 = and i64 %4, %0
  %.not = icmp eq i64 %5, 0
  br i1 %.not, label %._crit_edge, label %6

._crit_edge:                                      ; preds = %3
  %.pre = add nsw i64 %.019, -1
  br label %13

6:                                                ; preds = %3
  %7 = shl i64 %.01318, %.019
  %8 = add nsw i64 %7, %.01417
  %.not16 = icmp eq i64 %.019, 0
  %9 = add nsw i64 %.019, -1
  %10 = shl i64 %.019, %9
  %11 = select i1 %.not16, i64 0, i64 %10
  %.115 = add nsw i64 %8, %11
  %12 = add nsw i64 %.01318, 1
  br label %13

13:                                               ; preds = %._crit_edge, %6
  %.pre-phi = phi i64 [ %.pre, %._crit_edge ], [ %9, %6 ]
  %.2 = phi i64 [ %.01417, %._crit_edge ], [ %.115, %6 ]
  %.1 = phi i64 [ %.01318, %._crit_edge ], [ %12, %6 ]
  %.not20 = icmp eq i64 %.019, 0
  br i1 %.not20, label %2, label %3, !llvm.loop !5
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


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