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

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

@tableProcess = external global ptr, align 8
@lastProcess = external 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 @removeFinishedProcess() #1 {
  %1 = alloca ptr, align 8
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #3
  %4 = load ptr, ptr @tableProcess, align 8, !tbaa !5
  %5 = getelementptr inbounds %struct.ProcessSlot, ptr %4, i32 0, i32 1
  %6 = getelementptr inbounds %struct.Process, ptr %5, i32 0, i32 3
  %7 = load i32, ptr %6, align 8, !tbaa !9
  %8 = icmp eq i32 %7, 5
  br i1 %8, label %9, label %18

9:                                                ; preds = %0
  %10 = load ptr, ptr @tableProcess, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.ProcessSlot, ptr %10, i32 0, i32 1
  call void @releasePage(ptr noundef byval(%struct.Process) align 8 %11)
  %12 = load ptr, ptr @tableProcess, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.ProcessSlot, ptr %12, i32 0, i32 0
  %14 = load ptr, ptr %13, align 8, !tbaa !13
  store ptr %14, ptr @tableProcess, align 8, !tbaa !5
  %15 = load ptr, ptr @tableProcess, align 8, !tbaa !5
  %16 = load ptr, ptr @lastProcess, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.ProcessSlot, ptr %16, i32 0, i32 0
  store ptr %15, ptr %17, align 8, !tbaa !13
  store i32 1, ptr %3, align 4
  br label %56

18:                                               ; preds = %0
  %19 = load ptr, ptr @tableProcess, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.ProcessSlot, ptr %19, i32 0, i32 0
  %21 = load ptr, ptr %20, align 8, !tbaa !13
  store ptr %21, ptr %1, align 8, !tbaa !5
  %22 = load ptr, ptr @tableProcess, align 8, !tbaa !5
  store ptr %22, ptr %2, align 8, !tbaa !5
  br label %23

23:                                               ; preds = %50, %18
  %24 = load ptr, ptr %1, align 8, !tbaa !5
  %25 = load ptr, ptr @tableProcess, align 8, !tbaa !5
  %26 = icmp ne ptr %24, %25
  br i1 %26, label %27, label %55

27:                                               ; preds = %23
  %28 = load ptr, ptr %1, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.ProcessSlot, ptr %28, i32 0, i32 1
  %30 = getelementptr inbounds %struct.Process, ptr %29, i32 0, i32 3
  %31 = load i32, ptr %30, align 8, !tbaa !9
  %32 = icmp eq i32 %31, 5
  br i1 %32, label %33, label %50

33:                                               ; preds = %27
  %34 = load ptr, ptr @lastProcess, align 8, !tbaa !5
  %35 = load ptr, ptr %1, align 8, !tbaa !5
  %36 = icmp eq ptr %34, %35
  br i1 %36, label %37, label %39

37:                                               ; preds = %33
  %38 = load ptr, ptr %2, align 8, !tbaa !5
  store ptr %38, ptr @lastProcess, align 8, !tbaa !5
  br label %39

39:                                               ; preds = %37, %33
  %40 = load ptr, ptr %1, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.ProcessSlot, ptr %40, i32 0, i32 1
  call void @changeProcessTree(ptr noundef byval(%struct.Process) align 8 %41)
  %42 = load ptr, ptr %1, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.ProcessSlot, ptr %42, i32 0, i32 1
  call void @releasePage(ptr noundef byval(%struct.Process) align 8 %43)
  %44 = load ptr, ptr %1, align 8, !tbaa !5
  call void @releaseStructs(ptr noundef %44)
  %45 = load ptr, ptr %1, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.ProcessSlot, ptr %45, i32 0, i32 0
  %47 = load ptr, ptr %46, align 8, !tbaa !13
  %48 = load ptr, ptr %2, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.ProcessSlot, ptr %48, i32 0, i32 0
  store ptr %47, ptr %49, align 8, !tbaa !13
  store i32 1, ptr %3, align 4
  br label %56

50:                                               ; preds = %27
  %51 = load ptr, ptr %1, align 8, !tbaa !5
  store ptr %51, ptr %2, align 8, !tbaa !5
  %52 = load ptr, ptr %1, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.ProcessSlot, ptr %52, i32 0, i32 0
  %54 = load ptr, ptr %53, align 8, !tbaa !13
  store ptr %54, ptr %1, align 8, !tbaa !5
  br label %23, !llvm.loop !14

55:                                               ; preds = %23
  store i32 0, ptr %3, align 4
  br label %56

56:                                               ; preds = %55, %39, %9
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #3
  %57 = load i32, ptr %3, align 4
  switch i32 %57, label %59 [
    i32 0, label %58
    i32 1, label %58
  ]

58:                                               ; preds = %56, %56
  ret void

59:                                               ; preds = %56
  unreachable
}

declare void @releasePage(ptr noundef byval(%struct.Process) align 8) #2

; Function Attrs: nounwind uwtable
declare dso_local void @changeProcessTree(ptr noundef byval(%struct.Process) align 8) #1

declare void @releaseStructs(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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !12, i64 136}
!10 = !{!"ProcessSlot", !6, i64 0, !11, i64 8}
!11 = !{!"Process", !12, i64 0, !12, i64 4, !7, i64 8, !12, i64 128, !12, i64 132, !7, i64 136, !7, i64 192, !12, i64 352, !6, i64 360, !6, i64 368, !6, i64 376, !7, i64 384, !12, i64 464, !6, i64 472, !6, i64 480}
!12 = !{!"int", !7, i64 0}
!13 = !{!10, !6, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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"

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

@tableProcess = external local_unnamed_addr global ptr, align 8
@lastProcess = external local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local void @removeFinishedProcess() local_unnamed_addr #0 {
  %1 = load ptr, ptr @tableProcess, align 8, !tbaa !5
  %2 = getelementptr inbounds i8, ptr %1, i64 136
  %3 = load i32, ptr %2, align 8, !tbaa !9
  %4 = icmp eq i32 %3, 5
  br i1 %4, label %5, label %.preheader

5:                                                ; preds = %0
  %6 = getelementptr inbounds i8, ptr %1, i64 8
  tail call void @releasePage(ptr noundef nonnull byval(%struct.Process) align 8 %6) #2
  %7 = load ptr, ptr @tableProcess, align 8, !tbaa !5
  %8 = load ptr, ptr %7, align 8, !tbaa !13
  store ptr %8, ptr @tableProcess, align 8, !tbaa !5
  %9 = load ptr, ptr @lastProcess, align 8, !tbaa !5
  store ptr %8, ptr %9, align 8, !tbaa !13
  br label %.loopexit

.preheader:                                       ; preds = %0, %10
  %.0 = phi ptr [ %.011, %10 ], [ %1, %0 ]
  %.011 = load ptr, ptr %.0, align 8, !tbaa !13
  %.not = icmp eq ptr %.011, %1
  br i1 %.not, label %.loopexit, label %10

10:                                               ; preds = %.preheader
  %11 = getelementptr inbounds i8, ptr %.011, i64 136
  %12 = load i32, ptr %11, align 8, !tbaa !9
  %13 = icmp eq i32 %12, 5
  br i1 %13, label %14, label %.preheader, !llvm.loop !14

14:                                               ; preds = %10
  %15 = getelementptr inbounds i8, ptr %.011, i64 8
  %16 = load ptr, ptr @lastProcess, align 8, !tbaa !5
  %17 = icmp eq ptr %16, %.011
  br i1 %17, label %18, label %19

18:                                               ; preds = %14
  store ptr %.0, ptr @lastProcess, align 8, !tbaa !5
  br label %19

19:                                               ; preds = %18, %14
  tail call void @changeProcessTree(ptr noundef nonnull byval(%struct.Process) align 8 %15)
  tail call void @releasePage(ptr noundef nonnull byval(%struct.Process) align 8 %15) #2
  tail call void @releaseStructs(ptr noundef nonnull %.011) #2
  %20 = load ptr, ptr %.011, align 8, !tbaa !13
  store ptr %20, ptr %.0, align 8, !tbaa !13
  br label %.loopexit

.loopexit:                                        ; preds = %.preheader, %19, %5
  ret void
}

declare void @releasePage(ptr noundef byval(%struct.Process) align 8) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @changeProcessTree(ptr noundef byval(%struct.Process) align 8) local_unnamed_addr #0

declare void @releaseStructs(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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !12, i64 136}
!10 = !{!"ProcessSlot", !6, i64 0, !11, i64 8}
!11 = !{!"Process", !12, i64 0, !12, i64 4, !7, i64 8, !12, i64 128, !12, i64 132, !7, i64 136, !7, i64 192, !12, i64 352, !6, i64 360, !6, i64 368, !6, i64 376, !7, i64 384, !12, i64 464, !6, i64 472, !6, i64 480}
!12 = !{!"int", !7, i64 0}
!13 = !{!10, !6, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


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