; LLVM IR from narke/Aragveli :: src/kernel/memory (frame)

; 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.anon = type { ptr }
%struct.anon.0 = type { ptr }
%struct.frame = type { i64, i64, %struct.anon.1 }
%struct.anon.1 = type { ptr }

@free_frames = external dso_local global %struct.anon, align 8
@used_frames = external dso_local global %struct.anon.0, align 8

; 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 uwtable
define dso_local i32 @frame_free(i32 noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  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
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %8 = load i32, ptr %3, align 4, !tbaa !5
  %9 = call ptr @frame_at_address(i32 noundef %8)
  store ptr %9, ptr %5, align 8, !tbaa !9
  %10 = load ptr, ptr %5, align 8, !tbaa !9
  %11 = icmp ne ptr %10, null
  br i1 %11, label %13, label %12

12:                                               ; preds = %1
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %73

13:                                               ; preds = %1
  %14 = load ptr, ptr %5, align 8, !tbaa !9
  %15 = getelementptr inbounds %struct.frame, ptr %14, i32 0, i32 1
  %16 = load i64, ptr %15, align 8, !tbaa !11
  %17 = add i64 %16, -1
  store i64 %17, ptr %15, align 8, !tbaa !11
  %18 = load ptr, ptr %5, align 8, !tbaa !9
  %19 = getelementptr inbounds %struct.frame, ptr %18, i32 0, i32 1
  %20 = load i64, ptr %19, align 8, !tbaa !11
  %21 = icmp eq i64 %20, 0
  br i1 %21, label %22, label %71

22:                                               ; preds = %13
  br label %23

23:                                               ; preds = %22
  %24 = load ptr, ptr @used_frames, align 8, !tbaa !15
  %25 = load ptr, ptr %5, align 8, !tbaa !9
  %26 = icmp eq ptr %24, %25
  br i1 %26, label %27, label %35

27:                                               ; preds = %23
  br label %28

28:                                               ; preds = %27
  %29 = load ptr, ptr @used_frames, align 8, !tbaa !15
  %30 = getelementptr inbounds %struct.frame, ptr %29, i32 0, i32 2
  %31 = getelementptr inbounds %struct.anon.1, ptr %30, i32 0, i32 0
  %32 = load ptr, ptr %31, align 8, !tbaa !16
  store ptr %32, ptr @used_frames, align 8, !tbaa !15
  br label %33

33:                                               ; preds = %28
  br label %34

34:                                               ; preds = %33
  br label %60

35:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %36 = load ptr, ptr @used_frames, align 8, !tbaa !15
  store ptr %36, ptr %7, align 8, !tbaa !9
  br label %37

37:                                               ; preds = %44, %35
  %38 = load ptr, ptr %7, align 8, !tbaa !9
  %39 = getelementptr inbounds %struct.frame, ptr %38, i32 0, i32 2
  %40 = getelementptr inbounds %struct.anon.1, ptr %39, i32 0, i32 0
  %41 = load ptr, ptr %40, align 8, !tbaa !16
  %42 = load ptr, ptr %5, align 8, !tbaa !9
  %43 = icmp ne ptr %41, %42
  br i1 %43, label %44, label %49

44:                                               ; preds = %37
  %45 = load ptr, ptr %7, align 8, !tbaa !9
  %46 = getelementptr inbounds %struct.frame, ptr %45, i32 0, i32 2
  %47 = getelementptr inbounds %struct.anon.1, ptr %46, i32 0, i32 0
  %48 = load ptr, ptr %47, align 8, !tbaa !16
  store ptr %48, ptr %7, align 8, !tbaa !9
  br label %37, !llvm.loop !17

49:                                               ; preds = %37
  %50 = load ptr, ptr %7, align 8, !tbaa !9
  %51 = getelementptr inbounds %struct.frame, ptr %50, i32 0, i32 2
  %52 = getelementptr inbounds %struct.anon.1, ptr %51, i32 0, i32 0
  %53 = load ptr, ptr %52, align 8, !tbaa !16
  %54 = getelementptr inbounds %struct.frame, ptr %53, i32 0, i32 2
  %55 = getelementptr inbounds %struct.anon.1, ptr %54, i32 0, i32 0
  %56 = load ptr, ptr %55, align 8, !tbaa !16
  %57 = load ptr, ptr %7, align 8, !tbaa !9
  %58 = getelementptr inbounds %struct.frame, ptr %57, i32 0, i32 2
  %59 = getelementptr inbounds %struct.anon.1, ptr %58, i32 0, i32 0
  store ptr %56, ptr %59, align 8, !tbaa !16
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  br label %60

60:                                               ; preds = %49, %34
  br label %61

61:                                               ; preds = %60
  br label %62

62:                                               ; preds = %61
  br label %63

63:                                               ; preds = %62
  %64 = load ptr, ptr @free_frames, align 8, !tbaa !15
  %65 = load ptr, ptr %5, align 8, !tbaa !9
  %66 = getelementptr inbounds %struct.frame, ptr %65, i32 0, i32 2
  %67 = getelementptr inbounds %struct.anon.1, ptr %66, i32 0, i32 0
  store ptr %64, ptr %67, align 8, !tbaa !16
  %68 = load ptr, ptr %5, align 8, !tbaa !9
  store ptr %68, ptr @free_frames, align 8, !tbaa !15
  br label %69

69:                                               ; preds = %63
  br label %70

70:                                               ; preds = %69
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %71

71:                                               ; preds = %70, %13
  %72 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %72, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %73

73:                                               ; preds = %71, %12
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  %74 = load i32, ptr %2, align 4
  ret i32 %74
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @frame_at_address(i32 noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { inlinehint 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, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !13, i64 8}
!12 = !{!"frame", !13, i64 0, !13, i64 8, !14, i64 16}
!13 = !{!"long", !7, i64 0}
!14 = !{!"", !10, i64 0}
!15 = !{!14, !10, i64 0}
!16 = !{!12, !10, i64 16}
!17 = distinct !{!17, !18}
!18 = !{!"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"

%struct.anon = type { ptr }
%struct.anon.0 = type { ptr }

@free_frames = external dso_local local_unnamed_addr global %struct.anon, align 8
@used_frames = external dso_local local_unnamed_addr global %struct.anon.0, align 8

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 2) i32 @frame_free(i32 noundef %0) local_unnamed_addr #0 {
  %2 = tail call ptr @frame_at_address(i32 noundef %0)
  %.not = icmp eq ptr %2, null
  br i1 %.not, label %20, label %3

3:                                                ; preds = %1
  %4 = getelementptr inbounds i8, ptr %2, i64 8
  %5 = load i64, ptr %4, align 8, !tbaa !5
  %6 = add i64 %5, -1
  store i64 %6, ptr %4, align 8, !tbaa !5
  %7 = icmp eq i64 %6, 0
  br i1 %7, label %8, label %20

8:                                                ; preds = %3
  %9 = load ptr, ptr @used_frames, align 8, !tbaa !12
  %10 = icmp eq ptr %9, %2
  br i1 %10, label %15, label %.preheader

.preheader:                                       ; preds = %8, %.preheader
  %.0 = phi ptr [ %12, %.preheader ], [ %9, %8 ]
  %11 = getelementptr inbounds i8, ptr %.0, i64 16
  %12 = load ptr, ptr %11, align 8, !tbaa !13
  %.not19 = icmp eq ptr %12, %2
  br i1 %.not19, label %13, label %.preheader, !llvm.loop !14

13:                                               ; preds = %.preheader
  %14 = getelementptr inbounds i8, ptr %.0, i64 16
  br label %15

15:                                               ; preds = %8, %13
  %.sink = phi ptr [ %12, %13 ], [ %9, %8 ]
  %used_frames.sink = phi ptr [ %14, %13 ], [ @used_frames, %8 ]
  %16 = getelementptr inbounds i8, ptr %.sink, i64 16
  %17 = load ptr, ptr %16, align 8, !tbaa !13
  store ptr %17, ptr %used_frames.sink, align 8, !tbaa !12
  %18 = load ptr, ptr @free_frames, align 8, !tbaa !12
  %19 = getelementptr inbounds i8, ptr %2, i64 16
  store ptr %18, ptr %19, align 8, !tbaa !13
  store ptr %2, ptr @free_frames, align 8, !tbaa !12
  br label %20

20:                                               ; preds = %3, %15, %1
  %.013 = phi i32 [ -1, %1 ], [ 0, %15 ], [ 1, %3 ]
  ret i32 %.013
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @frame_at_address(i32 noundef) local_unnamed_addr #1

attributes #0 = { 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 = { inlinehint 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, !7, i64 8}
!6 = !{!"frame", !7, i64 0, !7, i64 8, !10, i64 16}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"", !11, i64 0}
!11 = !{!"any pointer", !8, i64 0}
!12 = !{!10, !11, i64 0}
!13 = !{!6, !11, i64 16}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LICMPass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass