; LLVM IR from ShahjalalShohag/code-library :: Number Theory/Fibonacci Number Faster.ll (Z3fibxi)

; 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 [104 x i8], align 1
@__PRETTY_FUNCTION__._Z3fibxi = external hidden unnamed_addr constant [24 x i8], align 1

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z3fibxi(i64 noundef %0, i32 noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca i64, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store i64 %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  %12 = load i64, ptr %4, align 8, !tbaa !5
  %13 = icmp sge i64 %12, 0
  br i1 %13, label %14, label %15

14:                                               ; preds = %2
  br label %17

15:                                               ; preds = %2
  call void @__assert_fail(ptr noundef @.str, ptr noundef @.str.1, i32 noundef 5, ptr noundef @__PRETTY_FUNCTION__._Z3fibxi) #4
  unreachable

16:                                               ; No predecessors!
  br label %17

17:                                               ; preds = %16, %14
  %18 = load i64, ptr %4, align 8, !tbaa !5
  %19 = icmp sle i64 %18, 1
  br i1 %19, label %20, label %23

20:                                               ; preds = %17
  %21 = load i64, ptr %4, align 8, !tbaa !5
  %22 = trunc i64 %21 to i32
  store i32 %22, ptr %3, align 4
  br label %89

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

31:                                               ; preds = %84, %23
  %32 = load i64, ptr %8, align 8, !tbaa !5
  %33 = icmp ne i64 %32, 0
  br i1 %33, label %34, label %87

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

69:                                               ; preds = %34
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #5
  %70 = load i32, ptr %6, align 4, !tbaa !9
  %71 = load i32, ptr %7, align 4, !tbaa !9
  %72 = add nsw i32 %70, %71
  store i32 %72, ptr %11, align 4, !tbaa !9
  %73 = load i32, ptr %11, align 4, !tbaa !9
  %74 = load i32, ptr %5, align 4, !tbaa !9
  %75 = icmp sge i32 %73, %74
  br i1 %75, label %76, label %80

76:                                               ; preds = %69
  %77 = load i32, ptr %5, align 4, !tbaa !9
  %78 = load i32, ptr %11, align 4, !tbaa !9
  %79 = sub nsw i32 %78, %77
  store i32 %79, ptr %11, align 4, !tbaa !9
  br label %80

80:                                               ; preds = %76, %69
  %81 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %81, ptr %6, align 4, !tbaa !9
  %82 = load i32, ptr %11, align 4, !tbaa !9
  store i32 %82, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #5
  br label %83

83:                                               ; preds = %80, %34
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #5
  br label %84

84:                                               ; preds = %83
  %85 = load i64, ptr %8, align 8, !tbaa !5
  %86 = ashr i64 %85, 1
  store i64 %86, ptr %8, align 8, !tbaa !5
  br label %31, !llvm.loop !11

87:                                               ; preds = %31
  %88 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %88, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #5
  br label %89

89:                                               ; preds = %87, %20
  %90 = load i32, ptr %3, align 4
  ret i32 %90
}

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

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

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

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 [104 x i8], align 1
@__PRETTY_FUNCTION__._Z3fibxi = external hidden unnamed_addr constant [24 x i8], align 1

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

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

5:                                                ; preds = %2
  %6 = icmp ult i64 %0, 2
  br i1 %6, label %7, label %9

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

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

14:                                               ; preds = %9, %14
  %.03042 = phi i64 [ 0, %9 ], [ %.1, %14 ]
  %.03141 = phi i64 [ %12, %9 ], [ %29, %14 ]
  %.03240 = phi i32 [ 1, %9 ], [ %.133, %14 ]
  %15 = mul nsw i64 %.03042, %.03042
  %16 = sext i32 %.03240 to i64
  %17 = mul nsw i64 %16, %16
  %18 = add nuw nsw i64 %15, %17
  %19 = srem i64 %18, %13
  %20 = shl nsw i64 %.03042, 1
  %21 = add nsw i64 %20, %16
  %22 = mul nsw i64 %21, %16
  %23 = srem i64 %22, %13
  %24 = trunc nsw i64 %23 to i32
  %25 = and i64 %.03141, %0
  %.not38 = icmp eq i64 %25, 0
  %26 = trunc nsw i64 %19 to i32
  %27 = add nsw i32 %24, %26
  %.not39 = icmp slt i32 %27, %1
  %28 = select i1 %.not39, i32 0, i32 %1
  %spec.select = sub nsw i32 %27, %28
  %.133 = select i1 %.not38, i32 %24, i32 %spec.select
  %.1 = select i1 %.not38, i64 %19, i64 %23
  %29 = lshr i64 %.03141, 1
  %.not = icmp ult i64 %.03141, 2
  br i1 %.not, label %.loopexit, label %14, !llvm.loop !5

.loopexit:                                        ; preds = %14, %7
  %.029 = phi i32 [ %8, %7 ], [ %.133, %14 ]
  ret i32 %.029
}

; 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,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass