; LLVM IR from ravireddy07/cpp-book :: src/Coding-Blocks/FriendsPairingDP.ll (Z7friendsi)

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

@DParr = external dso_local global [100000 x i32], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z7friendsi(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  %4 = load i32, ptr %3, align 4, !tbaa !5
  %5 = icmp eq i32 %4, 0
  br i1 %5, label %9, label %6

6:                                                ; preds = %1
  %7 = load i32, ptr %3, align 4, !tbaa !5
  %8 = icmp eq i32 %7, 1
  br i1 %8, label %9, label %10

9:                                                ; preds = %6, %1
  store i32 1, ptr %2, align 4
  br label %49

10:                                               ; preds = %6
  %11 = load i32, ptr %3, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [100000 x i32], ptr @DParr, i64 0, i64 %12
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %15 = icmp ne i32 %14, 0
  br i1 %15, label %16, label %21

16:                                               ; preds = %10
  %17 = load i32, ptr %3, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [100000 x i32], ptr @DParr, i64 0, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !5
  store i32 %20, ptr %2, align 4
  br label %49

21:                                               ; preds = %10
  %22 = load i32, ptr %3, align 4, !tbaa !5
  %23 = icmp eq i32 %22, 2
  br i1 %23, label %24, label %25

24:                                               ; preds = %21
  store i32 2, ptr %2, align 4
  br label %49

25:                                               ; preds = %21
  %26 = load i32, ptr %3, align 4, !tbaa !5
  %27 = sub nsw i32 %26, 1
  %28 = call noundef i32 @_Z7friendsi(i32 noundef %27)
  %29 = load i32, ptr %3, align 4, !tbaa !5
  %30 = sub nsw i32 %29, 1
  %31 = load i32, ptr %3, align 4, !tbaa !5
  %32 = sub nsw i32 %31, 2
  %33 = call noundef i32 @_Z7friendsi(i32 noundef %32)
  %34 = mul nsw i32 %30, %33
  %35 = add nsw i32 %28, %34
  %36 = load i32, ptr %3, align 4, !tbaa !5
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [100000 x i32], ptr @DParr, i64 0, i64 %37
  store i32 %35, ptr %38, align 4, !tbaa !5
  %39 = load i32, ptr %3, align 4, !tbaa !5
  %40 = sub nsw i32 %39, 1
  %41 = call noundef i32 @_Z7friendsi(i32 noundef %40)
  %42 = load i32, ptr %3, align 4, !tbaa !5
  %43 = sub nsw i32 %42, 1
  %44 = load i32, ptr %3, align 4, !tbaa !5
  %45 = sub nsw i32 %44, 2
  %46 = call noundef i32 @_Z7friendsi(i32 noundef %45)
  %47 = mul nsw i32 %43, %46
  %48 = add nsw i32 %41, %47
  store i32 %48, ptr %2, align 4
  br label %49

49:                                               ; preds = %25, %24, %16, %9
  %50 = load i32, ptr %2, align 4
  ret i32 %50
}

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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}


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

@DParr = external dso_local local_unnamed_addr global [100000 x i32], align 16

; Function Attrs: mustprogress nofree nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_Z7friendsi(i32 noundef %0) local_unnamed_addr #0 {
  %or.cond = icmp ult i32 %0, 2
  br i1 %or.cond, label %common.ret16, label %2

2:                                                ; preds = %1
  %3 = sext i32 %0 to i64
  %4 = getelementptr inbounds [100000 x i32], ptr @DParr, i64 0, i64 %3
  %5 = load i32, ptr %4, align 4, !tbaa !5
  %.not = icmp eq i32 %5, 0
  br i1 %.not, label %6, label %common.ret16

6:                                                ; preds = %2
  %7 = icmp eq i32 %0, 2
  br i1 %7, label %common.ret16, label %8

common.ret16:                                     ; preds = %6, %2, %1, %8
  %common.ret16.op = phi i32 [ %18, %8 ], [ 1, %1 ], [ %5, %2 ], [ 2, %6 ]
  ret i32 %common.ret16.op

8:                                                ; preds = %6
  %9 = add nsw i32 %0, -1
  %10 = tail call noundef i32 @_Z7friendsi(i32 noundef %9)
  %11 = add nsw i32 %0, -2
  %12 = tail call noundef i32 @_Z7friendsi(i32 noundef %11)
  %13 = mul nsw i32 %12, %9
  %14 = add nsw i32 %13, %10
  store i32 %14, ptr %4, align 4, !tbaa !5
  %15 = tail call noundef i32 @_Z7friendsi(i32 noundef %9)
  %16 = tail call noundef i32 @_Z7friendsi(i32 noundef %11)
  %17 = mul nsw i32 %16, %9
  %18 = add nsw i32 %17, %15
  br label %common.ret16
}

attributes #0 = { mustprogress nofree nosync nounwind memory(readwrite, argmem: none, 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}


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