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

@kernelHeader = 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
define dso_local void @releaseHeapStruct(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 0, ptr %3, align 4, !tbaa !9
  br label %5

5:                                                ; preds = %19, %1
  %6 = load i32, ptr %3, align 4, !tbaa !9
  %7 = icmp slt i32 %6, 1000
  br i1 %7, label %8, label %22

8:                                                ; preds = %5
  %9 = load ptr, ptr %2, align 8, !tbaa !5
  %10 = load ptr, ptr @kernelHeader, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.kernelHeapHeader, ptr %10, i32 0, i32 14
  %12 = load i32, ptr %3, align 4, !tbaa !9
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [1000 x %struct.s_heapPage], ptr %11, i64 0, i64 %13
  %15 = icmp eq ptr %9, %14
  br i1 %15, label %16, label %18

16:                                               ; preds = %8
  %17 = load i32, ptr %3, align 4, !tbaa !9
  store i32 %17, ptr %4, align 4, !tbaa !9
  br label %22

18:                                               ; preds = %8
  br label %19

19:                                               ; preds = %18
  %20 = load i32, ptr %3, align 4, !tbaa !9
  %21 = add nsw i32 %20, 1
  store i32 %21, ptr %3, align 4, !tbaa !9
  br label %5, !llvm.loop !11

22:                                               ; preds = %16, %5
  store i32 0, ptr %3, align 4, !tbaa !9
  br label %23

23:                                               ; preds = %42, %22
  %24 = load i32, ptr %3, align 4, !tbaa !9
  %25 = icmp slt i32 %24, 10
  br i1 %25, label %26, label %45

26:                                               ; preds = %23
  %27 = load ptr, ptr @kernelHeader, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.kernelHeapHeader, ptr %27, i32 0, i32 13
  %29 = load i32, ptr %3, align 4, !tbaa !9
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [10 x i32], ptr %28, i64 0, i64 %30
  %32 = load i32, ptr %31, align 4, !tbaa !9
  %33 = icmp eq i32 %32, -1
  br i1 %33, label %34, label %41

34:                                               ; preds = %26
  %35 = load i32, ptr %4, align 4, !tbaa !9
  %36 = load ptr, ptr @kernelHeader, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.kernelHeapHeader, ptr %36, i32 0, i32 13
  %38 = load i32, ptr %3, align 4, !tbaa !9
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [10 x i32], ptr %37, i64 0, i64 %39
  store i32 %35, ptr %40, align 4, !tbaa !9
  br label %45

41:                                               ; preds = %26
  br label %42

42:                                               ; preds = %41
  %43 = load i32, ptr %3, align 4, !tbaa !9
  %44 = add nsw i32 %43, 1
  store i32 %44, ptr %3, align 4, !tbaa !9
  br label %23, !llvm.loop !13

45:                                               ; preds = %34, %23
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  ret void
}

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 = { 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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


; 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.s_heapPage = type { ptr, i32, i32, ptr }

@kernelHeader = external dso_local local_unnamed_addr global ptr, align 8

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @releaseHeapStruct(ptr noundef readnone %0) local_unnamed_addr #0 {
  %2 = load ptr, ptr @kernelHeader, align 8, !tbaa !5
  %3 = getelementptr inbounds i8, ptr %2, i64 74192
  br label %4

4:                                                ; preds = %19, %1
  %indvars.iv = phi i64 [ 0, %1 ], [ %indvars.iv.next.4, %19 ]
  %5 = getelementptr inbounds [1000 x %struct.s_heapPage], ptr %3, i64 0, i64 %indvars.iv
  %6 = icmp eq ptr %5, %0
  br i1 %6, label %.split.loop.exit, label %7

7:                                                ; preds = %4
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %8 = getelementptr inbounds [1000 x %struct.s_heapPage], ptr %3, i64 0, i64 %indvars.iv.next
  %9 = icmp eq ptr %8, %0
  br i1 %9, label %.split.loop.exit, label %10

10:                                               ; preds = %7
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %11 = getelementptr inbounds [1000 x %struct.s_heapPage], ptr %3, i64 0, i64 %indvars.iv.next.1
  %12 = icmp eq ptr %11, %0
  br i1 %12, label %.split.loop.exit, label %13

13:                                               ; preds = %10
  %indvars.iv.next.2 = add nuw nsw i64 %indvars.iv, 3
  %14 = getelementptr inbounds [1000 x %struct.s_heapPage], ptr %3, i64 0, i64 %indvars.iv.next.2
  %15 = icmp eq ptr %14, %0
  br i1 %15, label %.split.loop.exit, label %16

16:                                               ; preds = %13
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %17 = getelementptr inbounds [1000 x %struct.s_heapPage], ptr %3, i64 0, i64 %indvars.iv.next.3
  %18 = icmp eq ptr %17, %0
  br i1 %18, label %.split.loop.exit, label %19

19:                                               ; preds = %16
  %indvars.iv.next.4 = add nuw nsw i64 %indvars.iv, 5
  %exitcond.not.4 = icmp eq i64 %indvars.iv.next.4, 1000
  br i1 %exitcond.not.4, label %.split.loop.exit20, label %4, !llvm.loop !9

.split.loop.exit:                                 ; preds = %16, %13, %10, %7, %4
  %indvars.iv.lcssa = phi i64 [ %indvars.iv, %4 ], [ %indvars.iv.next, %7 ], [ %indvars.iv.next.1, %10 ], [ %indvars.iv.next.2, %13 ], [ %indvars.iv.next.3, %16 ]
  %20 = trunc nuw nsw i64 %indvars.iv.lcssa to i32
  br label %.split.loop.exit20

.split.loop.exit20:                               ; preds = %19, %.split.loop.exit
  %.09.lcssa = phi i32 [ %20, %.split.loop.exit ], [ 1000, %19 ]
  %21 = getelementptr inbounds i8, ptr %2, i64 74148
  %22 = load i32, ptr %21, align 4, !tbaa !11
  %23 = icmp eq i32 %22, -1
  br i1 %23, label %60, label %24

24:                                               ; preds = %.split.loop.exit20
  %25 = getelementptr inbounds i8, ptr %2, i64 74152
  %26 = load i32, ptr %25, align 4, !tbaa !11
  %27 = icmp eq i32 %26, -1
  br i1 %27, label %60, label %28

28:                                               ; preds = %24
  %29 = getelementptr inbounds i8, ptr %2, i64 74156
  %30 = load i32, ptr %29, align 4, !tbaa !11
  %31 = icmp eq i32 %30, -1
  br i1 %31, label %60, label %32

32:                                               ; preds = %28
  %33 = getelementptr inbounds i8, ptr %2, i64 74160
  %34 = load i32, ptr %33, align 4, !tbaa !11
  %35 = icmp eq i32 %34, -1
  br i1 %35, label %60, label %36

36:                                               ; preds = %32
  %37 = getelementptr inbounds i8, ptr %2, i64 74164
  %38 = load i32, ptr %37, align 4, !tbaa !11
  %39 = icmp eq i32 %38, -1
  br i1 %39, label %60, label %40

40:                                               ; preds = %36
  %41 = getelementptr inbounds i8, ptr %2, i64 74168
  %42 = load i32, ptr %41, align 4, !tbaa !11
  %43 = icmp eq i32 %42, -1
  br i1 %43, label %60, label %44

44:                                               ; preds = %40
  %45 = getelementptr inbounds i8, ptr %2, i64 74172
  %46 = load i32, ptr %45, align 4, !tbaa !11
  %47 = icmp eq i32 %46, -1
  br i1 %47, label %60, label %48

48:                                               ; preds = %44
  %49 = getelementptr inbounds i8, ptr %2, i64 74176
  %50 = load i32, ptr %49, align 4, !tbaa !11
  %51 = icmp eq i32 %50, -1
  br i1 %51, label %60, label %52

52:                                               ; preds = %48
  %53 = getelementptr inbounds i8, ptr %2, i64 74180
  %54 = load i32, ptr %53, align 4, !tbaa !11
  %55 = icmp eq i32 %54, -1
  br i1 %55, label %60, label %56

56:                                               ; preds = %52
  %57 = getelementptr inbounds i8, ptr %2, i64 74184
  %58 = load i32, ptr %57, align 4, !tbaa !11
  %59 = icmp eq i32 %58, -1
  br i1 %59, label %60, label %.loopexit

60:                                               ; preds = %56, %52, %48, %44, %40, %36, %32, %28, %24, %.split.loop.exit20
  %.lcssa = phi i64 [ 0, %.split.loop.exit20 ], [ 1, %24 ], [ 2, %28 ], [ 3, %32 ], [ 4, %36 ], [ 5, %40 ], [ 6, %44 ], [ 7, %48 ], [ 8, %52 ], [ 9, %56 ]
  %61 = getelementptr inbounds [10 x i32], ptr %21, i64 0, i64 %.lcssa
  store i32 %.09.lcssa, ptr %61, align 4, !tbaa !11
  br label %.loopexit

.loopexit:                                        ; preds = %56, %60
  ret void
}

attributes #0 = { nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass