; LLVM IR from AlgoGenesis/C :: Miscellaneous Algorithms/Vortex Cache Replacement Algorithm (VCRA)

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

%struct.CacheEntry = type { i32, i32, i64 }

@cache = external dso_local global [5 x %struct.CacheEntry], 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: nounwind
declare i64 @time(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @replaceInCache(i32 noundef %0) #2 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  store i32 0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  store i32 1, ptr %4, align 4, !tbaa !5
  br label %5

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

8:                                                ; preds = %5
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  br label %51

9:                                                ; preds = %5
  %10 = load i32, ptr %4, align 4, !tbaa !5
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds [5 x %struct.CacheEntry], ptr @cache, i64 0, i64 %11
  %13 = getelementptr inbounds %struct.CacheEntry, ptr %12, i32 0, i32 1
  %14 = load i32, ptr %13, align 4, !tbaa !9
  %15 = load i32, ptr %3, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [5 x %struct.CacheEntry], ptr @cache, i64 0, i64 %16
  %18 = getelementptr inbounds %struct.CacheEntry, ptr %17, i32 0, i32 1
  %19 = load i32, ptr %18, align 4, !tbaa !9
  %20 = icmp slt i32 %14, %19
  br i1 %20, label %45, label %21

21:                                               ; preds = %9
  %22 = load i32, ptr %4, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [5 x %struct.CacheEntry], ptr @cache, i64 0, i64 %23
  %25 = getelementptr inbounds %struct.CacheEntry, ptr %24, i32 0, i32 1
  %26 = load i32, ptr %25, align 4, !tbaa !9
  %27 = load i32, ptr %3, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [5 x %struct.CacheEntry], ptr @cache, i64 0, i64 %28
  %30 = getelementptr inbounds %struct.CacheEntry, ptr %29, i32 0, i32 1
  %31 = load i32, ptr %30, align 4, !tbaa !9
  %32 = icmp eq i32 %26, %31
  br i1 %32, label %33, label %47

33:                                               ; preds = %21
  %34 = load i32, ptr %4, align 4, !tbaa !5
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [5 x %struct.CacheEntry], ptr @cache, i64 0, i64 %35
  %37 = getelementptr inbounds %struct.CacheEntry, ptr %36, i32 0, i32 2
  %38 = load i64, ptr %37, align 8, !tbaa !12
  %39 = load i32, ptr %3, align 4, !tbaa !5
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [5 x %struct.CacheEntry], ptr @cache, i64 0, i64 %40
  %42 = getelementptr inbounds %struct.CacheEntry, ptr %41, i32 0, i32 2
  %43 = load i64, ptr %42, align 8, !tbaa !12
  %44 = icmp slt i64 %38, %43
  br i1 %44, label %45, label %47

45:                                               ; preds = %33, %9
  %46 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %46, ptr %3, align 4, !tbaa !5
  br label %47

47:                                               ; preds = %45, %33, %21
  br label %48

48:                                               ; preds = %47
  %49 = load i32, ptr %4, align 4, !tbaa !5
  %50 = add nsw i32 %49, 1
  store i32 %50, ptr %4, align 4, !tbaa !5
  br label %5, !llvm.loop !13

51:                                               ; preds = %8
  %52 = load i32, ptr %2, align 4, !tbaa !5
  %53 = load i32, ptr %3, align 4, !tbaa !5
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds [5 x %struct.CacheEntry], ptr @cache, i64 0, i64 %54
  %56 = getelementptr inbounds %struct.CacheEntry, ptr %55, i32 0, i32 0
  store i32 %52, ptr %56, align 16, !tbaa !15
  %57 = load i32, ptr %3, align 4, !tbaa !5
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds [5 x %struct.CacheEntry], ptr @cache, i64 0, i64 %58
  %60 = getelementptr inbounds %struct.CacheEntry, ptr %59, i32 0, i32 1
  store i32 1, ptr %60, align 4, !tbaa !9
  %61 = call i64 @time(ptr noundef null) #3
  %62 = load i32, ptr %3, align 4, !tbaa !5
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds [5 x %struct.CacheEntry], ptr @cache, i64 0, i64 %63
  %65 = getelementptr inbounds %struct.CacheEntry, ptr %64, i32 0, i32 2
  store i64 %61, ptr %65, align 8, !tbaa !12
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 = { 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 #3 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 4}
!10 = !{!"", !6, i64 0, !6, i64 4, !11, i64 8}
!11 = !{!"long", !7, i64 0}
!12 = !{!10, !11, i64 8}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!10, !6, 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"

%struct.CacheEntry = type { i32, i32, i64 }

@cache = external dso_local local_unnamed_addr global [5 x %struct.CacheEntry], align 16

; Function Attrs: nounwind
declare i64 @time(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @replaceInCache(i32 noundef %0) local_unnamed_addr #1 {
  %2 = load i32, ptr getelementptr inbounds (i8, ptr @cache, i64 20), align 4, !tbaa !5
  %3 = load i32, ptr getelementptr inbounds (i8, ptr @cache, i64 4), align 4, !tbaa !5
  %4 = icmp slt i32 %2, %3
  br i1 %4, label %11, label %5

5:                                                ; preds = %1
  %6 = icmp eq i32 %2, %3
  br i1 %6, label %7, label %12

7:                                                ; preds = %5
  %8 = load i64, ptr getelementptr inbounds (i8, ptr @cache, i64 24), align 8, !tbaa !11
  %9 = load i64, ptr getelementptr inbounds (i8, ptr @cache, i64 8), align 8, !tbaa !11
  %10 = icmp slt i64 %8, %9
  br i1 %10, label %11, label %12

11:                                               ; preds = %7, %1
  br label %12

12:                                               ; preds = %5, %7, %11
  %13 = phi i32 [ %2, %11 ], [ %2, %7 ], [ %3, %5 ]
  %.1 = phi i32 [ 1, %11 ], [ 0, %7 ], [ 0, %5 ]
  %14 = load i32, ptr getelementptr inbounds (i8, ptr @cache, i64 36), align 4, !tbaa !5
  %15 = zext nneg i32 %.1 to i64
  %16 = icmp slt i32 %14, %13
  br i1 %16, label %24, label %17

17:                                               ; preds = %12
  %18 = icmp eq i32 %14, %13
  br i1 %18, label %19, label %25

19:                                               ; preds = %17
  %20 = load i64, ptr getelementptr inbounds (i8, ptr @cache, i64 40), align 8, !tbaa !11
  %21 = getelementptr inbounds [5 x %struct.CacheEntry], ptr @cache, i64 0, i64 %15, i32 2
  %22 = load i64, ptr %21, align 8, !tbaa !11
  %23 = icmp slt i64 %20, %22
  br i1 %23, label %24, label %25

24:                                               ; preds = %19, %12
  br label %25

25:                                               ; preds = %24, %19, %17
  %26 = phi i32 [ %14, %24 ], [ %13, %19 ], [ %13, %17 ]
  %.1.1 = phi i32 [ 2, %24 ], [ %.1, %19 ], [ %.1, %17 ]
  %27 = load i32, ptr getelementptr inbounds (i8, ptr @cache, i64 52), align 4, !tbaa !5
  %28 = zext nneg i32 %.1.1 to i64
  %29 = icmp slt i32 %27, %26
  br i1 %29, label %37, label %30

30:                                               ; preds = %25
  %31 = icmp eq i32 %27, %26
  br i1 %31, label %32, label %38

32:                                               ; preds = %30
  %33 = load i64, ptr getelementptr inbounds (i8, ptr @cache, i64 56), align 8, !tbaa !11
  %34 = getelementptr inbounds [5 x %struct.CacheEntry], ptr @cache, i64 0, i64 %28, i32 2
  %35 = load i64, ptr %34, align 8, !tbaa !11
  %36 = icmp slt i64 %33, %35
  br i1 %36, label %37, label %38

37:                                               ; preds = %32, %25
  br label %38

38:                                               ; preds = %37, %32, %30
  %39 = phi i32 [ %27, %37 ], [ %26, %32 ], [ %26, %30 ]
  %.1.2 = phi i32 [ 3, %37 ], [ %.1.1, %32 ], [ %.1.1, %30 ]
  %40 = load i32, ptr getelementptr inbounds (i8, ptr @cache, i64 68), align 4, !tbaa !5
  %41 = zext nneg i32 %.1.2 to i64
  %42 = icmp slt i32 %40, %39
  br i1 %42, label %50, label %43

43:                                               ; preds = %38
  %44 = icmp eq i32 %40, %39
  br i1 %44, label %45, label %51

45:                                               ; preds = %43
  %46 = load i64, ptr getelementptr inbounds (i8, ptr @cache, i64 72), align 8, !tbaa !11
  %47 = getelementptr inbounds [5 x %struct.CacheEntry], ptr @cache, i64 0, i64 %41, i32 2
  %48 = load i64, ptr %47, align 8, !tbaa !11
  %49 = icmp slt i64 %46, %48
  br i1 %49, label %50, label %51

50:                                               ; preds = %45, %38
  br label %51

51:                                               ; preds = %50, %45, %43
  %.1.3 = phi i32 [ 4, %50 ], [ %.1.2, %45 ], [ %.1.2, %43 ]
  %52 = zext nneg i32 %.1.3 to i64
  %53 = getelementptr inbounds [5 x %struct.CacheEntry], ptr @cache, i64 0, i64 %52
  store i32 %0, ptr %53, align 16, !tbaa !12
  %54 = getelementptr inbounds i8, ptr %53, i64 4
  store i32 1, ptr %54, align 4, !tbaa !5
  %55 = tail call i64 @time(ptr noundef null) #2
  %56 = getelementptr inbounds i8, ptr %53, i64 8
  store i64 %55, ptr %56, align 8, !tbaa !11
  ret void
}

attributes #0 = { 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 #1 = { 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, !7, i64 4}
!6 = !{!"", !7, i64 0, !7, i64 4, !10, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!6, !10, i64 8}
!12 = !{!6, !7, i64 0}


; Effective O3 passes: BDCEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LCSSAPass,LoopRotatePass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass