; LLVM IR from abhisheknaiidu/dsa :: Other/Hackerrank/Fibonacci-Finding.ll (Z6getfibx)

; 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 [3 x i64], align 16
@I = external dso_local global [3 x [3 x i64]], align 16
@T = external dso_local global [3 x [3 x i64]], align 16

; 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
declare dso_local void @_Z3mulPA3_xS0_x(ptr noundef, ptr noundef, i64 noundef) #1

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z6getfibx(i64 noundef %0) #1 {
  %2 = alloca i64, align 8
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  store i64 %0, ptr %3, align 8, !tbaa !5
  %5 = load i64, ptr %3, align 8, !tbaa !5
  %6 = icmp sle i64 %5, 2
  br i1 %6, label %7, label %11

7:                                                ; preds = %1
  %8 = load i64, ptr %3, align 8, !tbaa !5
  %9 = getelementptr inbounds [3 x i64], ptr @a, i64 0, i64 %8
  %10 = load i64, ptr %9, align 8, !tbaa !5
  store i64 %10, ptr %2, align 8
  br label %38

11:                                               ; preds = %1
  store i64 1, ptr getelementptr inbounds ([3 x i64], ptr getelementptr inbounds ([3 x [3 x i64]], ptr @I, i64 0, i64 2), i64 0, i64 2), align 16, !tbaa !5
  store i64 1, ptr getelementptr inbounds ([3 x i64], ptr getelementptr inbounds ([3 x [3 x i64]], ptr @I, i64 0, i64 1), i64 0, i64 1), align 8, !tbaa !5
  store i64 0, ptr getelementptr inbounds ([3 x i64], ptr getelementptr inbounds ([3 x [3 x i64]], ptr @I, i64 0, i64 2), i64 0, i64 1), align 8, !tbaa !5
  store i64 0, ptr getelementptr inbounds ([3 x i64], ptr getelementptr inbounds ([3 x [3 x i64]], ptr @I, i64 0, i64 1), i64 0, i64 2), align 8, !tbaa !5
  store i64 0, ptr getelementptr inbounds ([3 x i64], ptr getelementptr inbounds ([3 x [3 x i64]], ptr @T, i64 0, i64 1), i64 0, i64 1), align 8, !tbaa !5
  store i64 1, ptr getelementptr inbounds ([3 x i64], ptr getelementptr inbounds ([3 x [3 x i64]], ptr @T, i64 0, i64 2), i64 0, i64 1), align 8, !tbaa !5
  store i64 1, ptr getelementptr inbounds ([3 x i64], ptr getelementptr inbounds ([3 x [3 x i64]], ptr @T, i64 0, i64 2), i64 0, i64 2), align 16, !tbaa !5
  store i64 1, ptr getelementptr inbounds ([3 x i64], ptr getelementptr inbounds ([3 x [3 x i64]], ptr @T, i64 0, i64 1), i64 0, i64 2), align 8, !tbaa !5
  %12 = load i64, ptr %3, align 8, !tbaa !5
  %13 = sub nsw i64 %12, 1
  store i64 %13, ptr %3, align 8, !tbaa !5
  br label %14

14:                                               ; preds = %27, %11
  %15 = load i64, ptr %3, align 8, !tbaa !5
  %16 = icmp ne i64 %15, 0
  br i1 %16, label %17, label %28

17:                                               ; preds = %14
  %18 = load i64, ptr %3, align 8, !tbaa !5
  %19 = srem i64 %18, 2
  %20 = icmp ne i64 %19, 0
  br i1 %20, label %21, label %24

21:                                               ; preds = %17
  call void @_Z3mulPA3_xS0_x(ptr noundef @I, ptr noundef @T, i64 noundef 2)
  %22 = load i64, ptr %3, align 8, !tbaa !5
  %23 = add nsw i64 %22, -1
  store i64 %23, ptr %3, align 8, !tbaa !5
  br label %27

24:                                               ; preds = %17
  call void @_Z3mulPA3_xS0_x(ptr noundef @T, ptr noundef @T, i64 noundef 2)
  %25 = load i64, ptr %3, align 8, !tbaa !5
  %26 = sdiv i64 %25, 2
  store i64 %26, ptr %3, align 8, !tbaa !5
  br label %27

27:                                               ; preds = %24, %21
  br label %14, !llvm.loop !9

28:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  %29 = load i64, ptr getelementptr inbounds ([3 x i64], ptr @a, i64 0, i64 1), align 8, !tbaa !5
  %30 = load i64, ptr getelementptr inbounds ([3 x i64], ptr getelementptr inbounds ([3 x [3 x i64]], ptr @I, i64 0, i64 1), i64 0, i64 1), align 8, !tbaa !5
  %31 = mul nsw i64 %29, %30
  %32 = load i64, ptr getelementptr inbounds ([3 x i64], ptr @a, i64 0, i64 2), align 16, !tbaa !5
  %33 = load i64, ptr getelementptr inbounds ([3 x i64], ptr getelementptr inbounds ([3 x [3 x i64]], ptr @I, i64 0, i64 2), i64 0, i64 1), align 8, !tbaa !5
  %34 = mul nsw i64 %32, %33
  %35 = add nsw i64 %31, %34
  %36 = srem i64 %35, 1000000007
  store i64 %36, ptr %4, align 8, !tbaa !5
  %37 = load i64, ptr %4, align 8, !tbaa !5
  store i64 %37, ptr %2, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  br label %38

38:                                               ; preds = %28, %7
  %39 = load i64, ptr %2, align 8
  ret i64 %39
}

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 = { 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"

@a = external dso_local local_unnamed_addr global [3 x i64], align 16
@I = external dso_local global [3 x [3 x i64]], align 16
@T = external dso_local global [3 x [3 x i64]], align 16

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_Z3mulPA3_xS0_x(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z6getfibx(i64 noundef %0) local_unnamed_addr #0 {
  %2 = icmp slt i64 %0, 3
  br i1 %2, label %3, label %.lr.ph.preheader

3:                                                ; preds = %1
  %4 = getelementptr inbounds [3 x i64], ptr @a, i64 0, i64 %0
  %5 = load i64, ptr %4, align 8, !tbaa !5
  br label %19

.lr.ph.preheader:                                 ; preds = %1
  store i64 1, ptr getelementptr inbounds (i8, ptr @I, i64 64), align 16, !tbaa !5
  store i64 1, ptr getelementptr inbounds (i8, ptr @I, i64 32), align 16, !tbaa !5
  store i64 0, ptr getelementptr inbounds (i8, ptr @I, i64 56), align 8, !tbaa !5
  store i64 0, ptr getelementptr inbounds (i8, ptr @I, i64 40), align 8, !tbaa !5
  store i64 0, ptr getelementptr inbounds (i8, ptr @T, i64 32), align 16, !tbaa !5
  store i64 1, ptr getelementptr inbounds (i8, ptr @T, i64 56), align 8, !tbaa !5
  store i64 1, ptr getelementptr inbounds (i8, ptr @T, i64 64), align 16, !tbaa !5
  store i64 1, ptr getelementptr inbounds (i8, ptr @T, i64 40), align 8, !tbaa !5
  %6 = add nsw i64 %0, -1
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %12
  %.0914 = phi i64 [ %.1, %12 ], [ %6, %.lr.ph.preheader ]
  %7 = and i64 %.0914, 1
  %.not12 = icmp eq i64 %7, 0
  br i1 %.not12, label %10, label %8

8:                                                ; preds = %.lr.ph
  tail call void @_Z3mulPA3_xS0_x(ptr noundef nonnull @I, ptr noundef nonnull @T, i64 noundef 2)
  %9 = add nsw i64 %.0914, -1
  br label %12

10:                                               ; preds = %.lr.ph
  tail call void @_Z3mulPA3_xS0_x(ptr noundef nonnull @T, ptr noundef nonnull @T, i64 noundef 2)
  %11 = lshr exact i64 %.0914, 1
  br label %12

12:                                               ; preds = %10, %8
  %.1 = phi i64 [ %9, %8 ], [ %11, %10 ]
  %.not = icmp eq i64 %.1, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !9

._crit_edge:                                      ; preds = %12
  %.pre = load i64, ptr getelementptr inbounds (i8, ptr @I, i64 32), align 16, !tbaa !5
  %.pre15 = load i64, ptr getelementptr inbounds (i8, ptr @I, i64 56), align 8, !tbaa !5
  %13 = load i64, ptr getelementptr inbounds (i8, ptr @a, i64 8), align 8, !tbaa !5
  %14 = mul nsw i64 %.pre, %13
  %15 = load i64, ptr getelementptr inbounds (i8, ptr @a, i64 16), align 16, !tbaa !5
  %16 = mul nsw i64 %.pre15, %15
  %17 = add nsw i64 %16, %14
  %18 = srem i64 %17, 1000000007
  br label %19

19:                                               ; preds = %._crit_edge, %3
  %.0 = phi i64 [ %5, %3 ], [ %18, %._crit_edge ]
  ret i64 %.0
}

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

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


; Effective O3 passes: GVNPass,GlobalOptPass,IPSCCPPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass