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

@allProcessBackground = external dso_local global i32, align 4
@allProcessForeground = external dso_local global i32, align 4
@currentProcess = external dso_local global ptr, align 8
@allProcess = external global i32, align 4
@tableProcess = external global ptr, align 8
@lastProcess = external global ptr, align 8

declare void @enableTickInter(...) #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
declare dso_local void @removeFinishedProcess() #2

declare ptr @getProcessFromPid(i32 noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local void @exitMessage() #2

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

10:                                               ; preds = %1
  call void @exitMessage()
  store ptr null, ptr @tableProcess, align 8, !tbaa !9
  store ptr null, ptr @lastProcess, align 8, !tbaa !9
  store ptr null, ptr @currentProcess, align 8, !tbaa !9
  br label %11

11:                                               ; preds = %12, %10
  br label %12

12:                                               ; preds = %11
  br label %11

13:                                               ; preds = %1
  %14 = load ptr, ptr %4, align 8, !tbaa !9
  %15 = icmp eq ptr %14, null
  br i1 %15, label %16, label %17

16:                                               ; preds = %13
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %35

17:                                               ; preds = %13
  %18 = load ptr, ptr %4, align 8, !tbaa !9
  %19 = getelementptr inbounds %struct.ProcessSlot, ptr %18, i32 0, i32 1
  %20 = getelementptr inbounds %struct.Process, ptr %19, i32 0, i32 4
  %21 = load i32, ptr %20, align 4, !tbaa !11
  %22 = icmp eq i32 %21, 0
  br i1 %22, label %23, label %26

23:                                               ; preds = %17
  %24 = load i32, ptr @allProcessForeground, align 4, !tbaa !5
  %25 = add nsw i32 %24, -1
  store i32 %25, ptr @allProcessForeground, align 4, !tbaa !5
  br label %29

26:                                               ; preds = %17
  %27 = load i32, ptr @allProcessBackground, align 4, !tbaa !5
  %28 = add nsw i32 %27, -1
  store i32 %28, ptr @allProcessBackground, align 4, !tbaa !5
  br label %29

29:                                               ; preds = %26, %23
  %30 = load ptr, ptr %4, align 8, !tbaa !9
  %31 = getelementptr inbounds %struct.ProcessSlot, ptr %30, i32 0, i32 1
  %32 = getelementptr inbounds %struct.Process, ptr %31, i32 0, i32 3
  store i32 5, ptr %32, align 8, !tbaa !14
  %33 = load i32, ptr @allProcess, align 4, !tbaa !5
  %34 = add nsw i32 %33, -1
  store i32 %34, ptr @allProcess, align 4, !tbaa !5
  call void @removeFinishedProcess()
  call void (...) @enableTickInter()
  store i32 1, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %35

35:                                               ; preds = %29, %16
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  %36 = load i32, ptr %2, align 4
  ret i32 %36
}

attributes #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 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 #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 = !{!12, !6, i64 140}
!12 = !{!"ProcessSlot", !10, i64 0, !13, i64 8}
!13 = !{!"Process", !6, i64 0, !6, i64 4, !7, i64 8, !6, i64 128, !6, i64 132, !7, i64 136, !7, i64 192, !6, i64 352, !10, i64 360, !10, i64 368, !10, i64 376, !7, i64 384, !6, i64 464, !10, i64 472, !10, i64 480}
!14 = !{!12, !6, i64 136}


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

@allProcessBackground = external dso_local local_unnamed_addr global i32, align 4
@allProcessForeground = external dso_local local_unnamed_addr global i32, align 4
@currentProcess = external dso_local local_unnamed_addr global ptr, align 8
@allProcess = external local_unnamed_addr global i32, align 4
@tableProcess = external local_unnamed_addr global ptr, align 8
@lastProcess = external local_unnamed_addr global ptr, align 8

declare void @enableTickInter(...) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @removeFinishedProcess() local_unnamed_addr #1

declare ptr @getProcessFromPid(i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @exitMessage() local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 2) i32 @removeProcessFromTerminal(i32 noundef %0) local_unnamed_addr #1 {
  %2 = tail call ptr @getProcessFromPid(i32 noundef %0) #2
  %3 = icmp eq i32 %0, 0
  br i1 %3, label %4, label %6

4:                                                ; preds = %1
  tail call void @exitMessage()
  store ptr null, ptr @tableProcess, align 8, !tbaa !5
  store ptr null, ptr @lastProcess, align 8, !tbaa !5
  store ptr null, ptr @currentProcess, align 8, !tbaa !5
  br label %5

5:                                                ; preds = %5, %4
  br label %5

6:                                                ; preds = %1
  %7 = icmp eq ptr %2, null
  br i1 %7, label %17, label %8

8:                                                ; preds = %6
  %9 = getelementptr inbounds i8, ptr %2, i64 140
  %10 = load i32, ptr %9, align 4, !tbaa !9
  %11 = icmp eq i32 %10, 0
  %allProcessForeground.allProcessBackground = select i1 %11, ptr @allProcessForeground, ptr @allProcessBackground
  %12 = load i32, ptr %allProcessForeground.allProcessBackground, align 4, !tbaa !13
  %13 = add nsw i32 %12, -1
  store i32 %13, ptr %allProcessForeground.allProcessBackground, align 4, !tbaa !13
  %14 = getelementptr inbounds i8, ptr %2, i64 136
  store i32 5, ptr %14, align 8, !tbaa !14
  %15 = load i32, ptr @allProcess, align 4, !tbaa !13
  %16 = add nsw i32 %15, -1
  store i32 %16, ptr @allProcess, align 4, !tbaa !13
  tail call void @removeFinishedProcess()
  tail call void (...) @enableTickInter() #2
  br label %17

17:                                               ; preds = %6, %8
  %.0 = phi i32 [ 1, %8 ], [ -1, %6 ]
  ret i32 %.0
}

attributes #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 = { 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, !12, i64 140}
!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 = !{!12, !12, i64 0}
!14 = !{!10, !12, i64 136}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,SROAPass,SimplifyCFGPass,TailCallElimPass