; 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 }
%struct.ThreadSlot = type { ptr, %struct.Thread }
%struct.Thread = type { i32, ptr, ptr, ptr, i32, i32 }

@currentProcess = external dso_local global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local void @nextThread() #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 9
  %4 = load ptr, ptr %3, align 8, !tbaa !9
  %5 = getelementptr inbounds %struct.ThreadSlot, ptr %4, i32 0, i32 1
  %6 = getelementptr inbounds %struct.Thread, ptr %5, i32 0, i32 4
  %7 = load i32, ptr %6, align 8, !tbaa !13
  %8 = icmp eq i32 %7, 2
  br i1 %8, label %9, label %16

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

16:                                               ; preds = %9, %0
  %17 = load ptr, ptr @currentProcess, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.ProcessSlot, ptr %17, i32 0, i32 1
  %19 = getelementptr inbounds %struct.Process, ptr %18, i32 0, i32 9
  %20 = load ptr, ptr %19, align 8, !tbaa !9
  %21 = getelementptr inbounds %struct.ThreadSlot, ptr %20, i32 0, i32 0
  %22 = load ptr, ptr %21, align 8, !tbaa !16
  %23 = icmp eq ptr %22, null
  br i1 %23, label %24, label %48

24:                                               ; preds = %16
  %25 = load ptr, ptr @currentProcess, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.ProcessSlot, ptr %25, i32 0, i32 1
  %27 = getelementptr inbounds %struct.Process, ptr %26, i32 0, i32 8
  %28 = load ptr, ptr %27, align 8, !tbaa !17
  %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 9
  store ptr %28, ptr %31, align 8, !tbaa !9
  %32 = load ptr, ptr @currentProcess, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.ProcessSlot, ptr %32, i32 0, i32 1
  %34 = getelementptr inbounds %struct.Process, ptr %33, i32 0, i32 9
  %35 = load ptr, ptr %34, align 8, !tbaa !9
  %36 = getelementptr inbounds %struct.ThreadSlot, ptr %35, i32 0, i32 1
  %37 = getelementptr inbounds %struct.Thread, ptr %36, i32 0, i32 4
  %38 = load i32, ptr %37, align 8, !tbaa !13
  %39 = icmp eq i32 %38, 3
  br i1 %39, label %40, label %47

40:                                               ; preds = %24
  %41 = load ptr, ptr @currentProcess, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.ProcessSlot, ptr %41, i32 0, i32 1
  %43 = getelementptr inbounds %struct.Process, ptr %42, i32 0, i32 9
  %44 = load ptr, ptr %43, align 8, !tbaa !9
  %45 = getelementptr inbounds %struct.ThreadSlot, ptr %44, i32 0, i32 1
  %46 = getelementptr inbounds %struct.Thread, ptr %45, i32 0, i32 4
  store i32 2, ptr %46, align 8, !tbaa !13
  br label %86

47:                                               ; preds = %24
  br label %48

48:                                               ; preds = %47, %16
  br label %49

49:                                               ; preds = %60, %48
  %50 = load ptr, ptr @currentProcess, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.ProcessSlot, ptr %50, i32 0, i32 1
  %52 = getelementptr inbounds %struct.Process, ptr %51, i32 0, i32 9
  %53 = load ptr, ptr %52, align 8, !tbaa !9
  %54 = getelementptr inbounds %struct.ThreadSlot, ptr %53, i32 0, i32 0
  %55 = load ptr, ptr %54, align 8, !tbaa !16
  %56 = getelementptr inbounds %struct.ThreadSlot, ptr %55, i32 0, i32 1
  %57 = getelementptr inbounds %struct.Thread, ptr %56, i32 0, i32 4
  %58 = load i32, ptr %57, align 8, !tbaa !13
  %59 = icmp ne i32 %58, 3
  br i1 %59, label %60, label %70

60:                                               ; preds = %49
  %61 = load ptr, ptr @currentProcess, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.ProcessSlot, ptr %61, i32 0, i32 1
  %63 = getelementptr inbounds %struct.Process, ptr %62, i32 0, i32 9
  %64 = load ptr, ptr %63, align 8, !tbaa !9
  %65 = getelementptr inbounds %struct.ThreadSlot, ptr %64, i32 0, i32 0
  %66 = load ptr, ptr %65, align 8, !tbaa !16
  %67 = load ptr, ptr @currentProcess, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.ProcessSlot, ptr %67, i32 0, i32 1
  %69 = getelementptr inbounds %struct.Process, ptr %68, i32 0, i32 9
  store ptr %66, ptr %69, align 8, !tbaa !9
  br label %49, !llvm.loop !18

70:                                               ; preds = %49
  %71 = load ptr, ptr @currentProcess, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.ProcessSlot, ptr %71, i32 0, i32 1
  %73 = getelementptr inbounds %struct.Process, ptr %72, i32 0, i32 9
  %74 = load ptr, ptr %73, align 8, !tbaa !9
  %75 = getelementptr inbounds %struct.ThreadSlot, ptr %74, i32 0, i32 0
  %76 = load ptr, ptr %75, align 8, !tbaa !16
  %77 = load ptr, ptr @currentProcess, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.ProcessSlot, ptr %77, i32 0, i32 1
  %79 = getelementptr inbounds %struct.Process, ptr %78, i32 0, i32 9
  store ptr %76, ptr %79, align 8, !tbaa !9
  %80 = load ptr, ptr @currentProcess, align 8, !tbaa !5
  %81 = getelementptr inbounds %struct.ProcessSlot, ptr %80, i32 0, i32 1
  %82 = getelementptr inbounds %struct.Process, ptr %81, i32 0, i32 9
  %83 = load ptr, ptr %82, align 8, !tbaa !9
  %84 = getelementptr inbounds %struct.ThreadSlot, ptr %83, i32 0, i32 1
  %85 = getelementptr inbounds %struct.Thread, ptr %84, i32 0, i32 4
  store i32 2, ptr %85, align 8, !tbaa !13
  br label %86

86:                                               ; preds = %70, %40
  ret void
}

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

!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, !6, i64 376}
!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 = !{!14, !12, i64 40}
!14 = !{!"ThreadSlot", !6, i64 0, !15, i64 8}
!15 = !{!"Thread", !12, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !12, i64 32, !12, i64 36}
!16 = !{!14, !6, i64 0}
!17 = !{!10, !6, i64 368}
!18 = distinct !{!18, !19}
!19 = !{!"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"

@currentProcess = 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 @nextThread() local_unnamed_addr #0 {
  %1 = load ptr, ptr @currentProcess, align 8, !tbaa !5
  %2 = getelementptr inbounds i8, ptr %1, i64 376
  %3 = load ptr, ptr %2, align 8, !tbaa !9
  %4 = getelementptr inbounds i8, ptr %3, i64 40
  %5 = load i32, ptr %4, align 8, !tbaa !13
  %6 = icmp eq i32 %5, 2
  br i1 %6, label %7, label %8

7:                                                ; preds = %0
  store i32 3, ptr %4, align 8, !tbaa !13
  br label %8

8:                                                ; preds = %7, %0
  %9 = load ptr, ptr %3, align 8, !tbaa !16
  %10 = icmp eq ptr %9, null
  br i1 %10, label %11, label %17

11:                                               ; preds = %8
  %12 = getelementptr inbounds i8, ptr %1, i64 368
  %13 = load ptr, ptr %12, align 8, !tbaa !17
  store ptr %13, ptr %2, align 8, !tbaa !9
  %14 = getelementptr inbounds i8, ptr %13, i64 40
  %15 = load i32, ptr %14, align 8, !tbaa !13
  %16 = icmp eq i32 %15, 3
  br i1 %16, label %26, label %._crit_edge7

._crit_edge7:                                     ; preds = %11
  %.pre = load ptr, ptr %13, align 8, !tbaa !16
  br label %17

17:                                               ; preds = %._crit_edge7, %8
  %18 = phi ptr [ %.pre, %._crit_edge7 ], [ %9, %8 ]
  %19 = getelementptr inbounds i8, ptr %18, i64 40
  %20 = load i32, ptr %19, align 8, !tbaa !13
  %.not3 = icmp eq i32 %20, 3
  br i1 %.not3, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %17, %.lr.ph
  %21 = phi ptr [ %22, %.lr.ph ], [ %18, %17 ]
  %22 = load ptr, ptr %21, align 8, !tbaa !16
  %23 = getelementptr inbounds i8, ptr %22, i64 40
  %24 = load i32, ptr %23, align 8, !tbaa !13
  %.not = icmp eq i32 %24, 3
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !18

._crit_edge:                                      ; preds = %.lr.ph, %17
  %.lcssa2 = phi ptr [ %18, %17 ], [ %22, %.lr.ph ]
  %25 = getelementptr inbounds i8, ptr %.lcssa2, i64 40
  store ptr %.lcssa2, ptr %2, align 8, !tbaa !9
  br label %26

26:                                               ; preds = %11, %._crit_edge
  %.sink = phi ptr [ %25, %._crit_edge ], [ %14, %11 ]
  store i32 2, ptr %.sink, align 8, !tbaa !13
  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 = !{!10, !6, i64 376}
!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 = !{!14, !12, i64 40}
!14 = !{!"ThreadSlot", !6, i64 0, !15, i64 8}
!15 = !{!"Thread", !12, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !12, i64 32, !12, i64 36}
!16 = !{!14, !6, i64 0}
!17 = !{!10, !6, i64 368}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: BDCEPass,DSEPass,EarlyCSEPass,GVNPass,GlobalOptPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SimplifyCFGPass