; 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
@.str = external hidden unnamed_addr constant [16 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [74 x i8], align 1

; 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_alloc() #1 {
  %1 = alloca i32, align 4
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #3
  %6 = load ptr, ptr @free_frames, align 8, !tbaa !5
  %7 = icmp eq ptr %6, null
  br i1 %7, label %8, label %9

8:                                                ; preds = %0
  store i32 0, ptr %1, align 4
  store i32 1, ptr %3, align 4
  br label %78

9:                                                ; preds = %0
  %10 = load ptr, ptr @free_frames, align 8, !tbaa !5
  store ptr %10, ptr %2, align 8, !tbaa !10
  br label %11

11:                                               ; preds = %9
  %12 = load ptr, ptr @free_frames, align 8, !tbaa !5
  %13 = load ptr, ptr %2, align 8, !tbaa !10
  %14 = icmp eq ptr %12, %13
  br i1 %14, label %15, label %23

15:                                               ; preds = %11
  br label %16

16:                                               ; preds = %15
  %17 = load ptr, ptr @free_frames, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.frame, ptr %17, i32 0, i32 2
  %19 = getelementptr inbounds %struct.anon.1, ptr %18, i32 0, i32 0
  %20 = load ptr, ptr %19, align 8, !tbaa !11
  store ptr %20, ptr @free_frames, align 8, !tbaa !5
  br label %21

21:                                               ; preds = %16
  br label %22

22:                                               ; preds = %21
  br label %48

23:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %24 = load ptr, ptr @free_frames, align 8, !tbaa !5
  store ptr %24, ptr %4, align 8, !tbaa !10
  br label %25

25:                                               ; preds = %32, %23
  %26 = load ptr, ptr %4, align 8, !tbaa !10
  %27 = getelementptr inbounds %struct.frame, ptr %26, i32 0, i32 2
  %28 = getelementptr inbounds %struct.anon.1, ptr %27, i32 0, i32 0
  %29 = load ptr, ptr %28, align 8, !tbaa !11
  %30 = load ptr, ptr %2, align 8, !tbaa !10
  %31 = icmp ne ptr %29, %30
  br i1 %31, label %32, label %37

32:                                               ; preds = %25
  %33 = load ptr, ptr %4, align 8, !tbaa !10
  %34 = getelementptr inbounds %struct.frame, ptr %33, i32 0, i32 2
  %35 = getelementptr inbounds %struct.anon.1, ptr %34, i32 0, i32 0
  %36 = load ptr, ptr %35, align 8, !tbaa !11
  store ptr %36, ptr %4, align 8, !tbaa !10
  br label %25, !llvm.loop !14

37:                                               ; preds = %25
  %38 = load ptr, ptr %4, align 8, !tbaa !10
  %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 !11
  %42 = getelementptr inbounds %struct.frame, ptr %41, i32 0, i32 2
  %43 = getelementptr inbounds %struct.anon.1, ptr %42, i32 0, i32 0
  %44 = load ptr, ptr %43, align 8, !tbaa !11
  %45 = load ptr, ptr %4, align 8, !tbaa !10
  %46 = getelementptr inbounds %struct.frame, ptr %45, i32 0, i32 2
  %47 = getelementptr inbounds %struct.anon.1, ptr %46, i32 0, i32 0
  store ptr %44, ptr %47, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  br label %48

48:                                               ; preds = %37, %22
  br label %49

49:                                               ; preds = %48
  br label %50

50:                                               ; preds = %49
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %51 = load ptr, ptr %2, align 8, !tbaa !10
  %52 = getelementptr inbounds %struct.frame, ptr %51, i32 0, i32 1
  %53 = load i64, ptr %52, align 8, !tbaa !16
  %54 = icmp eq i64 %53, 0
  %55 = zext i1 %54 to i32
  store i32 %55, ptr %5, align 4, !tbaa !17
  %56 = load i32, ptr %5, align 4, !tbaa !17
  %57 = icmp ne i32 %56, 0
  br i1 %57, label %61, label %58

58:                                               ; preds = %50
  call void asm sideeffect "cli\0A", "~{dirflag},~{fpsr},~{flags}"() #3, !srcloc !19
  call void @vbe_set_color(i64 noundef 11141120)
  call void (ptr, ...) @printf(ptr noundef @.str, ptr noundef @.str.1, i32 noundef 140)
  br label %59

59:                                               ; preds = %60, %58
  br label %60

60:                                               ; preds = %59
  call void asm sideeffect "hlt", "~{dirflag},~{fpsr},~{flags}"() #3, !srcloc !20
  br label %59

61:                                               ; preds = %50
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  %62 = load ptr, ptr %2, align 8, !tbaa !10
  %63 = getelementptr inbounds %struct.frame, ptr %62, i32 0, i32 1
  %64 = load i64, ptr %63, align 8, !tbaa !16
  %65 = add i64 %64, 1
  store i64 %65, ptr %63, align 8, !tbaa !16
  br label %66

66:                                               ; preds = %61
  %67 = load ptr, ptr @used_frames, align 8, !tbaa !5
  %68 = load ptr, ptr %2, align 8, !tbaa !10
  %69 = getelementptr inbounds %struct.frame, ptr %68, i32 0, i32 2
  %70 = getelementptr inbounds %struct.anon.1, ptr %69, i32 0, i32 0
  store ptr %67, ptr %70, align 8, !tbaa !11
  %71 = load ptr, ptr %2, align 8, !tbaa !10
  store ptr %71, ptr @used_frames, align 8, !tbaa !5
  br label %72

72:                                               ; preds = %66
  br label %73

73:                                               ; preds = %72
  %74 = load ptr, ptr %2, align 8, !tbaa !10
  %75 = getelementptr inbounds %struct.frame, ptr %74, i32 0, i32 0
  %76 = load i64, ptr %75, align 8, !tbaa !21
  %77 = trunc i64 %76 to i32
  store i32 %77, ptr %1, align 4
  store i32 1, ptr %3, align 4
  br label %78

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

declare void @vbe_set_color(i64 noundef) #2

declare void @printf(ptr 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 = { "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, !7, i64 0}
!6 = !{!"", !7, i64 0}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!7, !7, i64 0}
!11 = !{!12, !7, i64 16}
!12 = !{!"frame", !13, i64 0, !13, i64 8, !6, i64 16}
!13 = !{!"long", !8, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!12, !13, i64 8}
!17 = !{!18, !18, i64 0}
!18 = !{!"int", !8, i64 0}
!19 = !{i64 2147555175}
!20 = !{i64 2147555341}
!21 = !{!12, !13, 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.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
@.str = external hidden unnamed_addr constant [16 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [74 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local i32 @frame_alloc() local_unnamed_addr #0 {
  %1 = load ptr, ptr @free_frames, align 8, !tbaa !5
  %2 = icmp eq ptr %1, null
  br i1 %2, label %15, label %3

3:                                                ; preds = %0
  %4 = getelementptr inbounds i8, ptr %1, i64 16
  %5 = load ptr, ptr %4, align 8, !tbaa !10
  store ptr %5, ptr @free_frames, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %1, i64 8
  %7 = load i64, ptr %6, align 8, !tbaa !13
  %8 = icmp eq i64 %7, 0
  br i1 %8, label %11, label %9

9:                                                ; preds = %3
  tail call void asm sideeffect "cli\0A", "~{dirflag},~{fpsr},~{flags}"() #2, !srcloc !14
  tail call void @vbe_set_color(i64 noundef 11141120) #2
  tail call void (ptr, ...) @printf(ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 140) #2
  br label %10

10:                                               ; preds = %10, %9
  tail call void asm sideeffect "hlt", "~{dirflag},~{fpsr},~{flags}"() #2, !srcloc !15
  br label %10

11:                                               ; preds = %3
  store i64 1, ptr %6, align 8, !tbaa !13
  %12 = load ptr, ptr @used_frames, align 8, !tbaa !5
  store ptr %12, ptr %4, align 8, !tbaa !10
  store ptr %1, ptr @used_frames, align 8, !tbaa !5
  %13 = load i64, ptr %1, align 8, !tbaa !16
  %14 = trunc i64 %13 to i32
  br label %15

15:                                               ; preds = %0, %11
  %.0 = phi i32 [ %14, %11 ], [ 0, %0 ]
  ret i32 %.0
}

declare void @vbe_set_color(i64 noundef) local_unnamed_addr #1

declare void @printf(ptr 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 = { "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 0}
!6 = !{!"", !7, i64 0}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!11, !7, i64 16}
!11 = !{!"frame", !12, i64 0, !12, i64 8, !6, i64 16}
!12 = !{!"long", !8, i64 0}
!13 = !{!11, !12, i64 8}
!14 = !{i64 2147555175}
!15 = !{i64 2147555341}
!16 = !{!11, !12, i64 0}


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