; LLVM IR from xing1357/SimpleOS :: src (kheap)

; 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._kheap_block = type { %struct.anon, ptr, ptr }
%struct.anon = type { i32, i8 }

@g_head = external dso_local global ptr, align 8

; Function Attrs: nounwind uwtable
declare dso_local ptr @kbrk(i32 noundef) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

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

6:                                                ; preds = %11, %1
  %7 = load ptr, ptr %3, align 8, !tbaa !9
  %8 = getelementptr inbounds %struct._kheap_block, ptr %7, i32 0, i32 1
  %9 = load ptr, ptr %8, align 1, !tbaa !11
  %10 = icmp ne ptr %9, null
  br i1 %10, label %11, label %15

11:                                               ; preds = %6
  %12 = load ptr, ptr %3, align 8, !tbaa !9
  %13 = getelementptr inbounds %struct._kheap_block, ptr %12, i32 0, i32 1
  %14 = load ptr, ptr %13, align 1, !tbaa !11
  store ptr %14, ptr %3, align 8, !tbaa !9
  br label %6, !llvm.loop !14

15:                                               ; preds = %6
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  %16 = call ptr @kbrk(i32 noundef 24)
  store ptr %16, ptr %4, align 8, !tbaa !9
  %17 = load ptr, ptr %4, align 8, !tbaa !9
  %18 = getelementptr inbounds %struct._kheap_block, ptr %17, i32 0, i32 0
  %19 = getelementptr inbounds %struct.anon, ptr %18, i32 0, i32 1
  store i8 0, ptr %19, align 1, !tbaa !16
  %20 = load i32, ptr %2, align 4, !tbaa !5
  %21 = load ptr, ptr %4, align 8, !tbaa !9
  %22 = getelementptr inbounds %struct._kheap_block, ptr %21, i32 0, i32 0
  %23 = getelementptr inbounds %struct.anon, ptr %22, i32 0, i32 0
  store i32 %20, ptr %23, align 1, !tbaa !17
  %24 = load i32, ptr %2, align 4, !tbaa !5
  %25 = call ptr @kbrk(i32 noundef %24)
  %26 = load ptr, ptr %4, align 8, !tbaa !9
  %27 = getelementptr inbounds %struct._kheap_block, ptr %26, i32 0, i32 2
  store ptr %25, ptr %27, align 1, !tbaa !18
  %28 = load ptr, ptr %4, align 8, !tbaa !9
  %29 = getelementptr inbounds %struct._kheap_block, ptr %28, i32 0, i32 1
  store ptr null, ptr %29, align 1, !tbaa !11
  %30 = load ptr, ptr %4, align 8, !tbaa !9
  %31 = load ptr, ptr %3, align 8, !tbaa !9
  %32 = getelementptr inbounds %struct._kheap_block, ptr %31, i32 0, i32 1
  store ptr %30, ptr %32, align 1, !tbaa !11
  %33 = load ptr, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  ret ptr %33
}

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"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, !10, i64 8}
!12 = !{!"_kheap_block", !13, i64 0, !10, i64 8, !10, i64 16}
!13 = !{!"", !6, i64 0, !7, i64 4}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!12, !7, i64 4}
!17 = !{!12, !6, i64 0}
!18 = !{!12, !10, i64 16}


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

@g_head = external dso_local local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
declare dso_local ptr @kbrk(i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef ptr @allocate_new_block(i32 noundef %0) local_unnamed_addr #0 {
  %2 = load ptr, ptr @g_head, align 8, !tbaa !5
  br label %3

3:                                                ; preds = %3, %1
  %.0 = phi ptr [ %2, %1 ], [ %5, %3 ]
  %4 = getelementptr inbounds i8, ptr %.0, i64 8
  %5 = load ptr, ptr %4, align 1, !tbaa !9
  %.not = icmp eq ptr %5, null
  br i1 %.not, label %6, label %3, !llvm.loop !13

6:                                                ; preds = %3
  %7 = getelementptr inbounds i8, ptr %.0, i64 8
  %8 = tail call ptr @kbrk(i32 noundef 24)
  %9 = getelementptr inbounds i8, ptr %8, i64 4
  store i8 0, ptr %9, align 1, !tbaa !15
  store i32 %0, ptr %8, align 1, !tbaa !16
  %10 = tail call ptr @kbrk(i32 noundef %0)
  %11 = getelementptr inbounds i8, ptr %8, i64 16
  store ptr %10, ptr %11, align 1, !tbaa !17
  %12 = getelementptr inbounds i8, ptr %8, i64 8
  store ptr null, ptr %12, align 1, !tbaa !9
  store ptr %8, ptr %7, align 1, !tbaa !9
  ret ptr %8
}

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

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


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