; LLVM IR from rociodellasala/TP3_SO :: Kernel (heap)

; 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.kernelHeapHeader = type { i32, i32, [10 x i32], [50 x %struct.ProcessSlot], i32, [10 x i32], [100 x %struct.ThreadSlot], i32, [100 x %struct.s_mutex], i32, [10 x i32], [500 x %struct.s_pipe], i32, [10 x i32], [1000 x %struct.s_heapPage] }
%struct.ProcessSlot = type { ptr, %struct.Process }
%struct.Process = type { i32, i32, [30 x i32], i32, i32, [50 x i8], [20 x ptr], i32, ptr, ptr, ptr, [10 x ptr], i32, ptr, ptr }
%struct.ThreadSlot = type { ptr, %struct.Thread }
%struct.Thread = type { i32, ptr, ptr, ptr, i32, i32 }
%struct.s_mutex = type { i8, i8, [20 x i8], [10 x i32], [10 x i32] }
%struct.s_pipe = type { i32, i32, i32, i8, i8, i8, i8, i8, [40 x i8], i32, i32 }
%struct.s_heapPage = type { ptr, i32, i32, ptr }

@.str = external hidden unnamed_addr constant [12 x i8], align 1
@kernelHeader = external dso_local global ptr, align 8

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

5:                                                ; preds = %29, %0
  %6 = load i32, ptr %2, align 4, !tbaa !5
  %7 = icmp slt i32 %6, 10
  br i1 %7, label %8, label %32

8:                                                ; preds = %5
  %9 = load ptr, ptr @kernelHeader, align 8, !tbaa !9
  %10 = getelementptr inbounds %struct.kernelHeapHeader, ptr %9, i32 0, i32 2
  %11 = load i32, ptr %2, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [10 x i32], ptr %10, i64 0, i64 %12
  store i32 -1, ptr %13, align 4, !tbaa !5
  %14 = load ptr, ptr @kernelHeader, align 8, !tbaa !9
  %15 = getelementptr inbounds %struct.kernelHeapHeader, ptr %14, i32 0, i32 5
  %16 = load i32, ptr %2, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [10 x i32], ptr %15, i64 0, i64 %17
  store i32 -1, ptr %18, align 4, !tbaa !5
  %19 = load ptr, ptr @kernelHeader, align 8, !tbaa !9
  %20 = getelementptr inbounds %struct.kernelHeapHeader, ptr %19, i32 0, i32 10
  %21 = load i32, ptr %2, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [10 x i32], ptr %20, i64 0, i64 %22
  store i32 -1, ptr %23, align 4, !tbaa !5
  %24 = load ptr, ptr @kernelHeader, align 8, !tbaa !9
  %25 = getelementptr inbounds %struct.kernelHeapHeader, ptr %24, i32 0, i32 13
  %26 = load i32, ptr %2, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [10 x i32], ptr %25, i64 0, i64 %27
  store i32 -1, ptr %28, align 4, !tbaa !5
  br label %29

29:                                               ; preds = %8
  %30 = load i32, ptr %2, align 4, !tbaa !5
  %31 = add nsw i32 %30, 1
  store i32 %31, ptr %2, align 4, !tbaa !5
  br label %5, !llvm.loop !11

32:                                               ; preds = %5
  %33 = load ptr, ptr @kernelHeader, align 8, !tbaa !9
  %34 = getelementptr inbounds %struct.kernelHeapHeader, ptr %33, i32 0, i32 1
  store i32 0, ptr %34, align 4, !tbaa !13
  %35 = load ptr, ptr @kernelHeader, align 8, !tbaa !9
  %36 = getelementptr inbounds %struct.kernelHeapHeader, ptr %35, i32 0, i32 4
  store i32 0, ptr %36, align 8, !tbaa !15
  %37 = load ptr, ptr @kernelHeader, align 8, !tbaa !9
  %38 = getelementptr inbounds %struct.kernelHeapHeader, ptr %37, i32 0, i32 7
  store i32 0, ptr %38, align 8, !tbaa !16
  %39 = load ptr, ptr @kernelHeader, align 8, !tbaa !9
  %40 = getelementptr inbounds %struct.kernelHeapHeader, ptr %39, i32 0, i32 9
  store i32 0, ptr %40, align 4, !tbaa !17
  %41 = load ptr, ptr @kernelHeader, align 8, !tbaa !9
  %42 = getelementptr inbounds %struct.kernelHeapHeader, ptr %41, i32 0, i32 12
  store i32 0, ptr %42, align 8, !tbaa !18
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  ret void
}

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

declare ptr @allocPage(i32 noundef, ptr noundef) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!14, !6, i64 4}
!14 = !{!"kernelHeapHeader", !6, i64 0, !6, i64 4, !7, i64 8, !7, i64 48, !6, i64 24848, !7, i64 24852, !7, i64 24896, !6, i64 29696, !7, i64 29700, !6, i64 40100, !7, i64 40104, !7, i64 40144, !6, i64 74144, !7, i64 74148, !7, i64 74192}
!15 = !{!14, !6, i64 24848}
!16 = !{!14, !6, i64 29696}
!17 = !{!14, !6, i64 40100}
!18 = !{!14, !6, i64 74144}


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

@.str = external hidden unnamed_addr constant [12 x i8], align 1
@kernelHeader = external dso_local local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local void @initializeKernelHeap() local_unnamed_addr #0 {
  %1 = tail call ptr @allocPage(i32 noundef 98192, ptr noundef nonnull @.str) #3
  store ptr %1, ptr @kernelHeader, align 8, !tbaa !5
  %2 = getelementptr inbounds i8, ptr %1, i64 8
  %3 = getelementptr inbounds i8, ptr %1, i64 24852
  %4 = getelementptr inbounds i8, ptr %1, i64 40104
  %5 = getelementptr inbounds i8, ptr %1, i64 74148
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(40) %2, i8 -1, i64 40, i1 false), !tbaa !9
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(40) %3, i8 -1, i64 40, i1 false), !tbaa !9
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(40) %4, i8 -1, i64 40, i1 false), !tbaa !9
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(40) %5, i8 -1, i64 40, i1 false), !tbaa !9
  %6 = getelementptr inbounds i8, ptr %1, i64 4
  store i32 0, ptr %6, align 4, !tbaa !11
  %7 = getelementptr inbounds i8, ptr %1, i64 24848
  store i32 0, ptr %7, align 8, !tbaa !13
  %8 = getelementptr inbounds i8, ptr %1, i64 29696
  store i32 0, ptr %8, align 8, !tbaa !14
  %9 = getelementptr inbounds i8, ptr %1, i64 40100
  store i32 0, ptr %9, align 4, !tbaa !15
  %10 = getelementptr inbounds i8, ptr %1, i64 74144
  store i32 0, ptr %10, align 8, !tbaa !16
  ret void
}

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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #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 nounwind willreturn memory(argmem: write) }
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, !10, i64 4}
!12 = !{!"kernelHeapHeader", !10, i64 0, !10, i64 4, !7, i64 8, !7, i64 48, !10, i64 24848, !7, i64 24852, !7, i64 24896, !10, i64 29696, !7, i64 29700, !10, i64 40100, !7, i64 40104, !7, i64 40144, !10, i64 74144, !7, i64 74148, !7, i64 74192}
!13 = !{!12, !10, i64 24848}
!14 = !{!12, !10, i64 29696}
!15 = !{!12, !10, i64 40100}
!16 = !{!12, !10, i64 74144}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,LoopIdiomRecognizePass,LoopRotatePass,SROAPass,SimplifyCFGPass,TailCallElimPass