; LLVM IR from ShahjalalShohag/code-library :: Number Theory/LCM of Fibonacci Numbers.ll (Z3fibx)

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

@.str = external hidden unnamed_addr constant [7 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [105 x i8], align 1
@__PRETTY_FUNCTION__._Z3fibx = external hidden unnamed_addr constant [19 x i8], align 1

; 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 i32 @_Z3fibx(i64 noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca i64, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store i64 %0, ptr %3, align 8, !tbaa !5
  %10 = load i64, ptr %3, align 8, !tbaa !5
  %11 = icmp sge i64 %10, 0
  br i1 %11, label %12, label %13

12:                                               ; preds = %1
  br label %15

13:                                               ; preds = %1
  call void @__assert_fail(ptr noundef @.str, ptr noundef @.str.1, i32 noundef 16, ptr noundef @__PRETTY_FUNCTION__._Z3fibx) #4
  unreachable

14:                                               ; No predecessors!
  br label %15

15:                                               ; preds = %14, %12
  %16 = load i64, ptr %3, align 8, !tbaa !5
  %17 = icmp sle i64 %16, 1
  br i1 %17, label %18, label %21

18:                                               ; preds = %15
  %19 = load i64, ptr %3, align 8, !tbaa !5
  %20 = trunc i64 %19 to i32
  store i32 %20, ptr %2, align 4
  br label %81

21:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #5
  store i32 0, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #5
  store i32 1, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  %22 = load i64, ptr %3, align 8, !tbaa !5
  %23 = call i64 @llvm.ctlz.i64(i64 %22, i1 true)
  %24 = trunc i64 %23 to i32
  %25 = sub nsw i32 63, %24
  %26 = sub nsw i32 %25, 1
  %27 = zext i32 %26 to i64
  %28 = shl i64 1, %27
  store i64 %28, ptr %6, align 8, !tbaa !5
  br label %29

29:                                               ; preds = %76, %21
  %30 = load i64, ptr %6, align 8, !tbaa !5
  %31 = icmp ne i64 %30, 0
  br i1 %31, label %32, label %79

32:                                               ; preds = %29
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #5
  %33 = load i32, ptr %4, align 4, !tbaa !9
  %34 = sext i32 %33 to i64
  %35 = load i32, ptr %4, align 4, !tbaa !9
  %36 = sext i32 %35 to i64
  %37 = mul nsw i64 %34, %36
  %38 = load i32, ptr %5, align 4, !tbaa !9
  %39 = sext i32 %38 to i64
  %40 = load i32, ptr %5, align 4, !tbaa !9
  %41 = sext i32 %40 to i64
  %42 = mul nsw i64 %39, %41
  %43 = add nsw i64 %37, %42
  %44 = srem i64 %43, 1000000007
  %45 = trunc i64 %44 to i32
  store i32 %45, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #5
  %46 = load i32, ptr %4, align 4, !tbaa !9
  %47 = sext i32 %46 to i64
  %48 = mul nsw i64 2, %47
  %49 = load i32, ptr %5, align 4, !tbaa !9
  %50 = sext i32 %49 to i64
  %51 = add nsw i64 %48, %50
  %52 = load i32, ptr %5, align 4, !tbaa !9
  %53 = sext i32 %52 to i64
  %54 = mul nsw i64 %51, %53
  %55 = srem i64 %54, 1000000007
  %56 = trunc i64 %55 to i32
  store i32 %56, ptr %8, align 4, !tbaa !9
  %57 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %57, ptr %4, align 4, !tbaa !9
  %58 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %58, ptr %5, align 4, !tbaa !9
  %59 = load i64, ptr %3, align 8, !tbaa !5
  %60 = load i64, ptr %6, align 8, !tbaa !5
  %61 = and i64 %59, %60
  %62 = icmp ne i64 %61, 0
  br i1 %62, label %63, label %75

63:                                               ; preds = %32
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #5
  %64 = load i32, ptr %4, align 4, !tbaa !9
  %65 = load i32, ptr %5, align 4, !tbaa !9
  %66 = add nsw i32 %64, %65
  store i32 %66, ptr %9, align 4, !tbaa !9
  %67 = load i32, ptr %9, align 4, !tbaa !9
  %68 = icmp sge i32 %67, 1000000007
  br i1 %68, label %69, label %72

69:                                               ; preds = %63
  %70 = load i32, ptr %9, align 4, !tbaa !9
  %71 = sub nsw i32 %70, 1000000007
  store i32 %71, ptr %9, align 4, !tbaa !9
  br label %72

72:                                               ; preds = %69, %63
  %73 = load i32, ptr %5, align 4, !tbaa !9
  store i32 %73, ptr %4, align 4, !tbaa !9
  %74 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %74, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #5
  br label %75

75:                                               ; preds = %72, %32
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  br label %76

76:                                               ; preds = %75
  %77 = load i64, ptr %6, align 8, !tbaa !5
  %78 = ashr i64 %77, 1
  store i64 %78, ptr %6, align 8, !tbaa !5
  br label %29, !llvm.loop !11

79:                                               ; preds = %29
  %80 = load i32, ptr %5, align 4, !tbaa !9
  store i32 %80, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #5
  br label %81

81:                                               ; preds = %79, %18
  %82 = load i32, ptr %2, align 4
  ret i32 %82
}

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.ctlz.i64(i64, i1 immarg) #3

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 = { noreturn 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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { noreturn nounwind }
attributes #5 = { 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"

@.str = external hidden unnamed_addr constant [7 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [105 x i8], align 1
@__PRETTY_FUNCTION__._Z3fibx = external hidden unnamed_addr constant [19 x i8], align 1

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z3fibx(i64 noundef %0) local_unnamed_addr #0 {
  %2 = icmp sgt i64 %0, -1
  br i1 %2, label %4, label %3

3:                                                ; preds = %1
  tail call void @__assert_fail(ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 16, ptr noundef nonnull @__PRETTY_FUNCTION__._Z3fibx) #3
  unreachable

4:                                                ; preds = %1
  %5 = icmp ult i64 %0, 2
  br i1 %5, label %6, label %8

6:                                                ; preds = %4
  %7 = trunc nuw nsw i64 %0 to i32
  br label %.loopexit

8:                                                ; preds = %4
  %9 = tail call range(i64 1, 65) i64 @llvm.ctlz.i64(i64 %0, i1 true)
  %10 = sub nuw nsw i64 62, %9
  %11 = shl nuw nsw i64 1, %10
  br label %12

12:                                               ; preds = %8, %12
  %.02636 = phi i64 [ 0, %8 ], [ %.1, %12 ]
  %.02735 = phi i64 [ %11, %8 ], [ %28, %12 ]
  %.02834 = phi i32 [ 1, %8 ], [ %.129, %12 ]
  %13 = mul nsw i64 %.02636, %.02636
  %14 = sext i32 %.02834 to i64
  %15 = mul nsw i64 %14, %14
  %16 = add nuw nsw i64 %13, %15
  %17 = urem i64 %16, 1000000007
  %18 = shl nsw i64 %.02636, 1
  %19 = add nsw i64 %18, %14
  %20 = mul nsw i64 %19, %14
  %21 = srem i64 %20, 1000000007
  %22 = trunc nsw i64 %21 to i32
  %23 = and i64 %.02735, %0
  %.not33 = icmp eq i64 %23, 0
  %24 = trunc nuw nsw i64 %17 to i32
  %25 = add nsw i32 %22, %24
  %26 = icmp sgt i32 %25, 1000000006
  %27 = add nsw i32 %25, -1000000007
  %spec.select = select i1 %26, i32 %27, i32 %25
  %.129 = select i1 %.not33, i32 %22, i32 %spec.select
  %.1 = select i1 %.not33, i64 %17, i64 %21
  %28 = lshr i64 %.02735, 1
  %.not = icmp ult i64 %.02735, 2
  br i1 %.not, label %.loopexit, label %12, !llvm.loop !5

.loopexit:                                        ; preds = %12, %6
  %.025 = phi i32 [ %7, %6 ], [ %.129, %12 ]
  ret i32 %.025
}

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.ctlz.i64(i64, i1 immarg) #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 = { noreturn 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 #2 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #3 = { noreturn 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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


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