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

@allProcessForeground = external dso_local global i32, align 4
@currentProcess = external dso_local global ptr, align 8
@allProcess = external global i32, align 4
@numberOfTicks = external hidden global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local void @runScheduler() #0 {
  %1 = load ptr, ptr @currentProcess, align 8, !tbaa !5
  %2 = getelementptr inbounds %struct.ProcessSlot, ptr %1, i32 0, i32 1
  %3 = getelementptr inbounds %struct.Process, ptr %2, i32 0, i32 0
  %4 = load i32, ptr %3, align 8, !tbaa !9
  %5 = icmp eq i32 %4, 0
  br i1 %5, label %6, label %16

6:                                                ; preds = %0
  %7 = load i32, ptr @allProcess, align 4, !tbaa !13
  %8 = icmp sgt i32 %7, 1
  br i1 %8, label %9, label %16

9:                                                ; preds = %6
  %10 = load i32, ptr @allProcessForeground, align 4, !tbaa !13
  %11 = icmp sgt i32 %10, 0
  br i1 %11, label %12, label %16

12:                                               ; preds = %9
  %13 = load ptr, ptr @currentProcess, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.ProcessSlot, ptr %13, i32 0, i32 1
  %15 = getelementptr inbounds %struct.Process, ptr %14, i32 0, i32 3
  store i32 4, ptr %15, align 8, !tbaa !14
  br label %16

16:                                               ; preds = %12, %9, %6, %0
  %17 = load i32, ptr @numberOfTicks, align 4, !tbaa !13
  %18 = icmp slt i32 %17, 10
  br i1 %18, label %19, label %22

19:                                               ; preds = %16
  %20 = load i32, ptr @numberOfTicks, align 4, !tbaa !13
  %21 = add nsw i32 %20, 1
  store i32 %21, ptr @numberOfTicks, align 4, !tbaa !13
  call void (...) @disableTickInter()
  call void @checkIfThreadChange()
  call void (...) @enableTickInter()
  br label %50

22:                                               ; preds = %16
  store i32 0, ptr @numberOfTicks, align 4, !tbaa !13
  %23 = load ptr, ptr @currentProcess, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.ProcessSlot, ptr %23, i32 0, i32 1
  %25 = getelementptr inbounds %struct.Process, ptr %24, i32 0, i32 3
  %26 = load i32, ptr %25, align 8, !tbaa !14
  %27 = icmp eq i32 %26, 2
  br i1 %27, label %28, label %32

28:                                               ; preds = %22
  %29 = load ptr, ptr @currentProcess, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.ProcessSlot, ptr %29, i32 0, i32 1
  %31 = getelementptr inbounds %struct.Process, ptr %30, i32 0, i32 3
  store i32 3, ptr %31, align 8, !tbaa !14
  br label %32

32:                                               ; preds = %28, %22
  %33 = load ptr, ptr @currentProcess, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.ProcessSlot, ptr %33, i32 0, i32 0
  %35 = load ptr, ptr %34, align 8, !tbaa !15
  store ptr %35, ptr @currentProcess, align 8, !tbaa !5
  br label %36

36:                                               ; preds = %42, %32
  %37 = load ptr, ptr @currentProcess, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.ProcessSlot, ptr %37, i32 0, i32 1
  %39 = getelementptr inbounds %struct.Process, ptr %38, i32 0, i32 3
  %40 = load i32, ptr %39, align 8, !tbaa !14
  %41 = icmp ne i32 %40, 3
  br i1 %41, label %42, label %46

42:                                               ; preds = %36
  %43 = load ptr, ptr @currentProcess, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.ProcessSlot, ptr %43, i32 0, i32 0
  %45 = load ptr, ptr %44, align 8, !tbaa !15
  store ptr %45, ptr @currentProcess, align 8, !tbaa !5
  br label %36, !llvm.loop !16

46:                                               ; preds = %36
  %47 = load ptr, ptr @currentProcess, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.ProcessSlot, ptr %47, i32 0, i32 1
  %49 = getelementptr inbounds %struct.Process, ptr %48, i32 0, i32 3
  store i32 2, ptr %49, align 8, !tbaa !14
  br label %50

50:                                               ; preds = %46, %19
  ret void
}

declare void @disableTickInter(...) #1

declare void @enableTickInter(...) #1

; Function Attrs: nounwind uwtable
declare dso_local void @checkIfThreadChange() #0

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

!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 8}
!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}
!15 = !{!10, !6, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"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"

@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
@numberOfTicks = external hidden local_unnamed_addr global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local void @runScheduler() local_unnamed_addr #0 {
  %1 = load ptr, ptr @currentProcess, align 8, !tbaa !5
  %2 = getelementptr inbounds i8, ptr %1, i64 8
  %3 = load i32, ptr %2, align 8, !tbaa !9
  %4 = icmp eq i32 %3, 0
  %5 = load i32, ptr @allProcess, align 4
  %6 = icmp sgt i32 %5, 1
  %or.cond = select i1 %4, i1 %6, i1 false
  %7 = load i32, ptr @allProcessForeground, align 4
  %8 = icmp sgt i32 %7, 0
  %or.cond3 = select i1 %or.cond, i1 %8, i1 false
  br i1 %or.cond3, label %9, label %11

9:                                                ; preds = %0
  %10 = getelementptr inbounds i8, ptr %1, i64 136
  store i32 4, ptr %10, align 8, !tbaa !13
  br label %11

11:                                               ; preds = %9, %0
  %12 = load i32, ptr @numberOfTicks, align 4, !tbaa !14
  %13 = icmp slt i32 %12, 10
  br i1 %13, label %14, label %16

14:                                               ; preds = %11
  %15 = add nsw i32 %12, 1
  store i32 %15, ptr @numberOfTicks, align 4, !tbaa !14
  tail call void (...) @disableTickInter() #2
  tail call void @checkIfThreadChange()
  tail call void (...) @enableTickInter() #2
  br label %26

16:                                               ; preds = %11
  store i32 0, ptr @numberOfTicks, align 4, !tbaa !14
  %17 = getelementptr inbounds i8, ptr %1, i64 136
  %18 = load i32, ptr %17, align 8, !tbaa !13
  %19 = icmp eq i32 %18, 2
  br i1 %19, label %20, label %.preheader

20:                                               ; preds = %16
  store i32 3, ptr %17, align 8, !tbaa !13
  br label %.preheader

.preheader:                                       ; preds = %20, %16
  br label %21

21:                                               ; preds = %.preheader, %21
  %storemerge.in = phi ptr [ %storemerge, %21 ], [ %1, %.preheader ]
  %storemerge = load ptr, ptr %storemerge.in, align 8, !tbaa !15
  store ptr %storemerge, ptr @currentProcess, align 8, !tbaa !5
  %22 = getelementptr inbounds i8, ptr %storemerge, i64 136
  %23 = load i32, ptr %22, align 8, !tbaa !13
  %.not = icmp eq i32 %23, 3
  br i1 %.not, label %24, label %21, !llvm.loop !16

24:                                               ; preds = %21
  %25 = getelementptr inbounds i8, ptr %storemerge, i64 136
  store i32 2, ptr %25, align 8, !tbaa !13
  br label %26

26:                                               ; preds = %24, %14
  ret void
}

declare void @disableTickInter(...) local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
declare dso_local void @checkIfThreadChange() local_unnamed_addr #0

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 8}
!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, !12, i64 136}
!14 = !{!12, !12, i64 0}
!15 = !{!10, !6, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LICMPass,LoopUnrollPass,SimplifyCFGPass,TailCallElimPass