; LLVM IR from imteekay/algorithms :: competitive-programming/uri/fibonacci_again.ll (Z8fast_fibxPx)

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

@a = external dso_local global i64, align 8
@b = external dso_local global i64, align 8
@c = external dso_local global i64, align 8
@d = external dso_local global i64, align 8

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

7:                                                ; preds = %2
  %8 = load ptr, ptr %4, align 8, !tbaa !9
  %9 = getelementptr inbounds i64, ptr %8, i64 0
  store i64 0, ptr %9, align 8, !tbaa !5
  %10 = load ptr, ptr %4, align 8, !tbaa !9
  %11 = getelementptr inbounds i64, ptr %10, i64 1
  store i64 1, ptr %11, align 8, !tbaa !5
  br label %63

12:                                               ; preds = %2
  %13 = load i64, ptr %3, align 8, !tbaa !5
  %14 = sdiv i64 %13, 2
  %15 = load ptr, ptr %4, align 8, !tbaa !9
  call void @_Z8fast_fibxPx(i64 noundef %14, ptr noundef %15)
  %16 = load ptr, ptr %4, align 8, !tbaa !9
  %17 = getelementptr inbounds i64, ptr %16, i64 0
  %18 = load i64, ptr %17, align 8, !tbaa !5
  store i64 %18, ptr @a, align 8, !tbaa !5
  %19 = load ptr, ptr %4, align 8, !tbaa !9
  %20 = getelementptr inbounds i64, ptr %19, i64 1
  %21 = load i64, ptr %20, align 8, !tbaa !5
  store i64 %21, ptr @b, align 8, !tbaa !5
  %22 = load i64, ptr @b, align 8, !tbaa !5
  %23 = mul nsw i64 2, %22
  %24 = load i64, ptr @a, align 8, !tbaa !5
  %25 = sub nsw i64 %23, %24
  store i64 %25, ptr @c, align 8, !tbaa !5
  %26 = load i64, ptr @c, align 8, !tbaa !5
  %27 = icmp slt i64 %26, 0
  br i1 %27, label %28, label %31

28:                                               ; preds = %12
  %29 = load i64, ptr @c, align 8, !tbaa !5
  %30 = add nsw i64 %29, 1000000007
  store i64 %30, ptr @c, align 8, !tbaa !5
  br label %31

31:                                               ; preds = %28, %12
  %32 = load i64, ptr @a, align 8, !tbaa !5
  %33 = load i64, ptr @c, align 8, !tbaa !5
  %34 = mul nsw i64 %32, %33
  %35 = srem i64 %34, 1000000007
  store i64 %35, ptr @c, align 8, !tbaa !5
  %36 = load i64, ptr @a, align 8, !tbaa !5
  %37 = load i64, ptr @a, align 8, !tbaa !5
  %38 = mul nsw i64 %36, %37
  %39 = load i64, ptr @b, align 8, !tbaa !5
  %40 = load i64, ptr @b, align 8, !tbaa !5
  %41 = mul nsw i64 %39, %40
  %42 = add nsw i64 %38, %41
  %43 = srem i64 %42, 1000000007
  store i64 %43, ptr @d, align 8, !tbaa !5
  %44 = load i64, ptr %3, align 8, !tbaa !5
  %45 = srem i64 %44, 2
  %46 = icmp eq i64 %45, 0
  br i1 %46, label %47, label %54

47:                                               ; preds = %31
  %48 = load i64, ptr @c, align 8, !tbaa !5
  %49 = load ptr, ptr %4, align 8, !tbaa !9
  %50 = getelementptr inbounds i64, ptr %49, i64 0
  store i64 %48, ptr %50, align 8, !tbaa !5
  %51 = load i64, ptr @d, align 8, !tbaa !5
  %52 = load ptr, ptr %4, align 8, !tbaa !9
  %53 = getelementptr inbounds i64, ptr %52, i64 1
  store i64 %51, ptr %53, align 8, !tbaa !5
  br label %63

54:                                               ; preds = %31
  %55 = load i64, ptr @d, align 8, !tbaa !5
  %56 = load ptr, ptr %4, align 8, !tbaa !9
  %57 = getelementptr inbounds i64, ptr %56, i64 0
  store i64 %55, ptr %57, align 8, !tbaa !5
  %58 = load i64, ptr @c, align 8, !tbaa !5
  %59 = load i64, ptr @d, align 8, !tbaa !5
  %60 = add nsw i64 %58, %59
  %61 = load ptr, ptr %4, align 8, !tbaa !9
  %62 = getelementptr inbounds i64, ptr %61, i64 1
  store i64 %60, ptr %62, align 8, !tbaa !5
  br label %63

63:                                               ; preds = %54, %47, %7
  ret void
}

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

!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 = !{!"any pointer", !7, i64 0}


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

@a = external dso_local local_unnamed_addr global i64, align 8
@b = external dso_local local_unnamed_addr global i64, align 8
@c = external dso_local local_unnamed_addr global i64, align 8
@d = external dso_local local_unnamed_addr global i64, align 8

; Function Attrs: mustprogress nofree nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @_Z8fast_fibxPx(i64 noundef %0, ptr nocapture noundef %1) local_unnamed_addr #0 {
  %3 = icmp eq i64 %0, 0
  br i1 %3, label %4, label %6

4:                                                ; preds = %2
  store i64 0, ptr %1, align 8, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %1, i64 8
  store i64 1, ptr %5, align 8, !tbaa !5
  br label %29

6:                                                ; preds = %2
  %7 = sdiv i64 %0, 2
  tail call void @_Z8fast_fibxPx(i64 noundef %7, ptr noundef %1)
  %8 = load i64, ptr %1, align 8, !tbaa !5
  store i64 %8, ptr @a, align 8, !tbaa !5
  %9 = getelementptr inbounds i8, ptr %1, i64 8
  %10 = load i64, ptr %9, align 8, !tbaa !5
  store i64 %10, ptr @b, align 8, !tbaa !5
  %11 = shl nsw i64 %10, 1
  %12 = sub nsw i64 %11, %8
  %13 = icmp slt i64 %12, 0
  %14 = add nsw i64 %12, 1000000007
  %spec.select = select i1 %13, i64 %14, i64 %12
  %15 = mul nsw i64 %spec.select, %8
  %16 = srem i64 %15, 1000000007
  store i64 %16, ptr @c, align 8, !tbaa !5
  %17 = mul nsw i64 %8, %8
  %18 = mul nsw i64 %10, %10
  %19 = add nuw nsw i64 %18, %17
  %20 = urem i64 %19, 1000000007
  store i64 %20, ptr @d, align 8, !tbaa !5
  %21 = and i64 %0, 1
  %22 = icmp eq i64 %21, 0
  br i1 %22, label %23, label %25

23:                                               ; preds = %6
  store i64 %16, ptr %1, align 8, !tbaa !5
  %24 = load i64, ptr @d, align 8, !tbaa !5
  store i64 %24, ptr %9, align 8, !tbaa !5
  br label %29

25:                                               ; preds = %6
  store i64 %20, ptr %1, align 8, !tbaa !5
  %26 = load i64, ptr @c, align 8, !tbaa !5
  %27 = load i64, ptr @d, align 8, !tbaa !5
  %28 = add nsw i64 %27, %26
  store i64 %28, ptr %9, align 8, !tbaa !5
  br label %29

29:                                               ; preds = %25, %23, %4
  ret void
}

attributes #0 = { mustprogress nofree nosync nounwind memory(readwrite, 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"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass