; 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: 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
declare dso_local ptr @allocate_new_block(i32 noundef) #1

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

10:                                               ; preds = %52, %2
  %11 = load ptr, ptr %6, align 8, !tbaa !5
  %12 = icmp ne ptr %11, null
  br i1 %12, label %13, label %56

13:                                               ; preds = %10
  %14 = load ptr, ptr %6, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct._kheap_block, ptr %14, i32 0, i32 2
  %16 = load ptr, ptr %15, align 1, !tbaa !11
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = icmp eq ptr %16, %17
  br i1 %18, label %19, label %52

19:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %20 = load i32, ptr %5, align 4, !tbaa !9
  %21 = call ptr @allocate_new_block(i32 noundef %20)
  store ptr %21, ptr %7, align 8, !tbaa !5
  %22 = load ptr, ptr %6, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct._kheap_block, ptr %22, i32 0, i32 0
  %24 = getelementptr inbounds %struct.anon, ptr %23, i32 0, i32 0
  %25 = load i32, ptr %24, align 1, !tbaa !14
  %26 = load i32, ptr %5, align 4, !tbaa !9
  %27 = icmp sgt i32 %25, %26
  br i1 %27, label %28, label %35

28:                                               ; preds = %19
  %29 = load ptr, ptr %7, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct._kheap_block, ptr %29, i32 0, i32 2
  %31 = load ptr, ptr %30, align 1, !tbaa !11
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = load i32, ptr %5, align 4, !tbaa !9
  %34 = call ptr @memcpy(ptr noundef %31, ptr noundef %32, i32 noundef %33)
  br label %45

35:                                               ; preds = %19
  %36 = load ptr, ptr %7, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct._kheap_block, ptr %36, i32 0, i32 2
  %38 = load ptr, ptr %37, align 1, !tbaa !11
  %39 = load ptr, ptr %4, align 8, !tbaa !5
  %40 = load ptr, ptr %6, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct._kheap_block, ptr %40, i32 0, i32 0
  %42 = getelementptr inbounds %struct.anon, ptr %41, i32 0, i32 0
  %43 = load i32, ptr %42, align 1, !tbaa !14
  %44 = call ptr @memcpy(ptr noundef %38, ptr noundef %39, i32 noundef %43)
  br label %45

45:                                               ; preds = %35, %28
  %46 = load ptr, ptr %6, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct._kheap_block, ptr %46, i32 0, i32 0
  %48 = getelementptr inbounds %struct.anon, ptr %47, i32 0, i32 1
  store i8 1, ptr %48, align 1, !tbaa !15
  %49 = load ptr, ptr %7, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct._kheap_block, ptr %49, i32 0, i32 2
  %51 = load ptr, ptr %50, align 1, !tbaa !11
  store ptr %51, ptr %3, align 8
  store i32 1, ptr %8, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  br label %57

52:                                               ; preds = %13
  %53 = load ptr, ptr %6, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct._kheap_block, ptr %53, i32 0, i32 1
  %55 = load ptr, ptr %54, align 1, !tbaa !16
  store ptr %55, ptr %6, align 8, !tbaa !5
  br label %10, !llvm.loop !17

56:                                               ; preds = %10
  store ptr null, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %57

57:                                               ; preds = %56, %45
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  %58 = load ptr, ptr %3, align 8
  ret ptr %58
}

declare ptr @memcpy(ptr noundef, ptr noundef, 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 = { "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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 16}
!12 = !{!"_kheap_block", !13, i64 0, !6, i64 8, !6, i64 16}
!13 = !{!"", !10, i64 0, !7, i64 4}
!14 = !{!12, !10, i64 0}
!15 = !{!12, !7, i64 4}
!16 = !{!12, !6, i64 8}
!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"

@g_head = external dso_local local_unnamed_addr global ptr, align 8

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

; Function Attrs: nounwind uwtable
define dso_local ptr @krealloc(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %.01520 = load ptr, ptr @g_head, align 8, !tbaa !5
  %.not21 = icmp eq ptr %.01520, null
  br i1 %.not21, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %2, %15
  %.01522 = phi ptr [ %.015, %15 ], [ %.01520, %2 ]
  %3 = getelementptr inbounds i8, ptr %.01522, i64 16
  %4 = load ptr, ptr %3, align 1, !tbaa !9
  %5 = icmp eq ptr %4, %0
  br i1 %5, label %6, label %15

6:                                                ; preds = %.lr.ph
  %7 = tail call ptr @allocate_new_block(i32 noundef %1)
  %8 = load i32, ptr %.01522, align 1, !tbaa !13
  %9 = getelementptr inbounds i8, ptr %7, i64 16
  %10 = load ptr, ptr %9, align 1, !tbaa !9
  %. = tail call i32 @llvm.smin.i32(i32 %8, i32 %1)
  %11 = tail call ptr @memcpy(ptr noundef %10, ptr noundef %0, i32 noundef %.) #3
  %12 = getelementptr inbounds i8, ptr %.01522, i64 4
  store i8 1, ptr %12, align 1, !tbaa !14
  %13 = getelementptr inbounds i8, ptr %7, i64 16
  %14 = load ptr, ptr %13, align 1, !tbaa !9
  br label %.loopexit

15:                                               ; preds = %.lr.ph
  %16 = getelementptr inbounds i8, ptr %.01522, i64 8
  %.015 = load ptr, ptr %16, align 1, !tbaa !5
  %.not = icmp eq ptr %.015, null
  br i1 %.not, label %.loopexit, label %.lr.ph, !llvm.loop !15

.loopexit:                                        ; preds = %15, %2, %6
  %.0 = phi ptr [ %14, %6 ], [ null, %2 ], [ null, %15 ]
  ret ptr %.0
}

declare ptr @memcpy(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smin.i32(i32, i32) #2

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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 16}
!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 = !{!10, !12, i64 0}
!14 = !{!10, !7, i64 4}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass