; LLVM IR from narke/Aragveli :: src/kernel/process (sched_rr)

; 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.anon = type { ptr, ptr }
%struct.thread = type { i32, i64, [32 x i8], i32, ptr, i8, %struct.anon.0 }
%struct.anon.0 = type { ptr, ptr }

@ready_threads = external dso_local global %struct.anon, align 8
@.str = external hidden unnamed_addr constant [16 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [78 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local void @scheduler_insert_thread(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  %5 = load ptr, ptr %2, align 8, !tbaa !5
  %6 = getelementptr inbounds %struct.thread, ptr %5, i32 0, i32 3
  %7 = load i32, ptr %6, align 8, !tbaa !9
  %8 = icmp eq i32 %7, 1
  br i1 %8, label %9, label %10

9:                                                ; preds = %1
  br label %73

10:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  %11 = load ptr, ptr %2, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.thread, ptr %11, i32 0, i32 3
  %13 = load i32, ptr %12, align 8, !tbaa !9
  %14 = icmp eq i32 %13, 0
  br i1 %14, label %25, label %15

15:                                               ; preds = %10
  %16 = load ptr, ptr %2, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.thread, ptr %16, i32 0, i32 3
  %18 = load i32, ptr %17, align 8, !tbaa !9
  %19 = icmp eq i32 %18, 2
  br i1 %19, label %25, label %20

20:                                               ; preds = %15
  %21 = load ptr, ptr %2, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.thread, ptr %21, i32 0, i32 3
  %23 = load i32, ptr %22, align 8, !tbaa !9
  %24 = icmp eq i32 %23, 3
  br label %25

25:                                               ; preds = %20, %15, %10
  %26 = phi i1 [ true, %15 ], [ true, %10 ], [ %24, %20 ]
  %27 = zext i1 %26 to i32
  store i32 %27, ptr %3, align 4, !tbaa !14
  %28 = load i32, ptr %3, align 4, !tbaa !14
  %29 = icmp ne i32 %28, 0
  br i1 %29, label %33, label %30

30:                                               ; preds = %25
  call void asm sideeffect "cli\0A", "~{dirflag},~{fpsr},~{flags}"() #3, !srcloc !15
  call void @vbe_set_color(i64 noundef 11141120)
  call void (ptr, ...) @printf(ptr noundef @.str, ptr noundef @.str.1, i32 noundef 34)
  br label %31

31:                                               ; preds = %32, %30
  br label %32

32:                                               ; preds = %31
  call void asm sideeffect "hlt", "~{dirflag},~{fpsr},~{flags}"() #3, !srcloc !16
  br label %31

33:                                               ; preds = %25
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  %34 = load ptr, ptr %2, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.thread, ptr %34, i32 0, i32 3
  store i32 1, ptr %35, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %36 = load ptr, ptr @ready_threads, align 8, !tbaa !17
  store ptr %36, ptr %4, align 8, !tbaa !5
  br label %37

37:                                               ; preds = %52, %33
  %38 = load ptr, ptr %4, align 8, !tbaa !5
  %39 = icmp ne ptr %38, null
  br i1 %39, label %40, label %57

40:                                               ; preds = %37
  %41 = load ptr, ptr %2, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.thread, ptr %41, i32 0, i32 5
  %43 = load i8, ptr %42, align 8, !tbaa !18
  %44 = zext i8 %43 to i32
  %45 = load ptr, ptr %4, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.thread, ptr %45, i32 0, i32 5
  %47 = load i8, ptr %46, align 8, !tbaa !18
  %48 = zext i8 %47 to i32
  %49 = icmp sgt i32 %44, %48
  br i1 %49, label %50, label %51

50:                                               ; preds = %40
  br label %57

51:                                               ; preds = %40
  br label %52

52:                                               ; preds = %51
  %53 = load ptr, ptr %4, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.thread, ptr %53, i32 0, i32 6
  %55 = getelementptr inbounds %struct.anon.0, ptr %54, i32 0, i32 0
  %56 = load ptr, ptr %55, align 8, !tbaa !19
  store ptr %56, ptr %4, align 8, !tbaa !5
  br label %37, !llvm.loop !20

57:                                               ; preds = %50, %37
  br label %58

58:                                               ; preds = %57
  %59 = load ptr, ptr %2, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.thread, ptr %59, i32 0, i32 6
  %61 = getelementptr inbounds %struct.anon.0, ptr %60, i32 0, i32 0
  store ptr null, ptr %61, align 8, !tbaa !19
  %62 = load ptr, ptr getelementptr inbounds (%struct.anon, ptr @ready_threads, i32 0, i32 1), align 8, !tbaa !22
  %63 = load ptr, ptr %2, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.thread, ptr %63, i32 0, i32 6
  %65 = getelementptr inbounds %struct.anon.0, ptr %64, i32 0, i32 1
  store ptr %62, ptr %65, align 8, !tbaa !23
  %66 = load ptr, ptr %2, align 8, !tbaa !5
  %67 = load ptr, ptr getelementptr inbounds (%struct.anon, ptr @ready_threads, i32 0, i32 1), align 8, !tbaa !22
  store ptr %66, ptr %67, align 8, !tbaa !5
  %68 = load ptr, ptr %2, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.thread, ptr %68, i32 0, i32 6
  %70 = getelementptr inbounds %struct.anon.0, ptr %69, i32 0, i32 0
  store ptr %70, ptr getelementptr inbounds (%struct.anon, ptr @ready_threads, i32 0, i32 1), align 8, !tbaa !22
  br label %71

71:                                               ; preds = %58
  br label %72

72:                                               ; preds = %71
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  br label %73

73:                                               ; preds = %72, %9
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

declare void @vbe_set_color(i64 noundef) #2

declare void @printf(ptr noundef, ...) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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, !11, i64 48}
!10 = !{!"thread", !11, i64 0, !12, i64 8, !7, i64 16, !11, i64 48, !6, i64 56, !7, i64 64, !13, i64 72}
!11 = !{!"int", !7, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!"", !6, i64 0, !6, i64 8}
!14 = !{!11, !11, i64 0}
!15 = !{i64 2147553169}
!16 = !{i64 2147553335}
!17 = !{!13, !6, i64 0}
!18 = !{!10, !7, i64 64}
!19 = !{!10, !6, i64 72}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}
!22 = !{!13, !6, i64 8}
!23 = !{!10, !6, i64 80}


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

@ready_threads = external dso_local local_unnamed_addr global %struct.anon, align 8
@.str = external hidden unnamed_addr constant [16 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [78 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local void @scheduler_insert_thread(ptr noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 48
  %3 = load i32, ptr %2, align 8, !tbaa !5
  switch i32 %3, label %.critedge [
    i32 1, label %8
    i32 0, label %switch.edge
    i32 2, label %switch.edge
    i32 3, label %switch.edge
  ]

.critedge:                                        ; preds = %1
  tail call void asm sideeffect "cli\0A", "~{dirflag},~{fpsr},~{flags}"() #2, !srcloc !13
  tail call void @vbe_set_color(i64 noundef 11141120) #2
  tail call void (ptr, ...) @printf(ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 34) #2
  br label %4

4:                                                ; preds = %4, %.critedge
  tail call void asm sideeffect "hlt", "~{dirflag},~{fpsr},~{flags}"() #2, !srcloc !14
  br label %4

switch.edge:                                      ; preds = %1, %1, %1
  store i32 1, ptr %2, align 8, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %0, i64 72
  store ptr null, ptr %5, align 8, !tbaa !15
  %6 = load ptr, ptr getelementptr inbounds (i8, ptr @ready_threads, i64 8), align 8, !tbaa !16
  %7 = getelementptr inbounds i8, ptr %0, i64 80
  store ptr %6, ptr %7, align 8, !tbaa !17
  store ptr %0, ptr %6, align 8, !tbaa !18
  store ptr %5, ptr getelementptr inbounds (i8, ptr @ready_threads, i64 8), align 8, !tbaa !16
  br label %8

8:                                                ; preds = %1, %switch.edge
  ret void
}

declare void @vbe_set_color(i64 noundef) local_unnamed_addr #1

declare void @printf(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, !7, i64 48}
!6 = !{!"thread", !7, i64 0, !10, i64 8, !8, i64 16, !7, i64 48, !11, i64 56, !8, i64 64, !12, i64 72}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"any pointer", !8, i64 0}
!12 = !{!"", !11, i64 0, !11, i64 8}
!13 = !{i64 2147553169}
!14 = !{i64 2147553335}
!15 = !{!6, !11, i64 72}
!16 = !{!12, !11, i64 8}
!17 = !{!6, !11, i64 80}
!18 = !{!11, !11, i64 0}


; Effective O3 passes: ADCEPass,BDCEPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,SROAPass,SimplifyCFGPass,TailCallElimPass