; 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

declare void @vbe_set_color(i64 noundef) #1

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

; 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 @schedule() #2 {
  %1 = alloca ptr, align 8
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #3
  %6 = call ptr @thread_get_current()
  store ptr %6, ptr %1, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  %7 = load ptr, ptr @ready_threads, align 8, !tbaa !9
  %8 = icmp eq ptr %7, null
  %9 = zext i1 %8 to i32
  %10 = icmp eq i32 %9, 0
  %11 = zext i1 %10 to i32
  store i32 %11, ptr %2, align 4, !tbaa !11
  %12 = load i32, ptr %2, align 4, !tbaa !11
  %13 = icmp ne i32 %12, 0
  br i1 %13, label %17, label %14

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

15:                                               ; preds = %16, %14
  br label %16

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

17:                                               ; preds = %0
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  %18 = load ptr, ptr @ready_threads, align 8, !tbaa !9
  store ptr %18, ptr %3, align 8, !tbaa !5
  br label %19

19:                                               ; preds = %17
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.thread, ptr %20, i32 0, i32 6
  %22 = getelementptr inbounds %struct.anon.0, ptr %21, i32 0, i32 0
  %23 = load ptr, ptr %22, align 8, !tbaa !15
  %24 = icmp ne ptr %23, null
  br i1 %24, label %25, label %36

25:                                               ; preds = %19
  %26 = load ptr, ptr %3, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.thread, ptr %26, i32 0, i32 6
  %28 = getelementptr inbounds %struct.anon.0, ptr %27, i32 0, i32 1
  %29 = load ptr, ptr %28, align 8, !tbaa !18
  %30 = load ptr, ptr %3, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.thread, ptr %30, i32 0, i32 6
  %32 = getelementptr inbounds %struct.anon.0, ptr %31, i32 0, i32 0
  %33 = load ptr, ptr %32, align 8, !tbaa !15
  %34 = getelementptr inbounds %struct.thread, ptr %33, i32 0, i32 6
  %35 = getelementptr inbounds %struct.anon.0, ptr %34, i32 0, i32 1
  store ptr %29, ptr %35, align 8, !tbaa !18
  br label %41

36:                                               ; preds = %19
  %37 = load ptr, ptr %3, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.thread, ptr %37, i32 0, i32 6
  %39 = getelementptr inbounds %struct.anon.0, ptr %38, i32 0, i32 1
  %40 = load ptr, ptr %39, align 8, !tbaa !18
  store ptr %40, ptr getelementptr inbounds (%struct.anon, ptr @ready_threads, i32 0, i32 1), align 8, !tbaa !19
  br label %41

41:                                               ; preds = %36, %25
  %42 = load ptr, ptr %3, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.thread, ptr %42, i32 0, i32 6
  %44 = getelementptr inbounds %struct.anon.0, ptr %43, i32 0, i32 0
  %45 = load ptr, ptr %44, align 8, !tbaa !15
  %46 = load ptr, ptr %3, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.thread, ptr %46, i32 0, i32 6
  %48 = getelementptr inbounds %struct.anon.0, ptr %47, i32 0, i32 1
  %49 = load ptr, ptr %48, align 8, !tbaa !18
  store ptr %45, ptr %49, align 8, !tbaa !5
  br label %50

50:                                               ; preds = %41
  br label %51

51:                                               ; preds = %50
  br label %52

52:                                               ; preds = %51
  %53 = load ptr, ptr %3, 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
  store ptr null, ptr %55, align 8, !tbaa !15
  %56 = load ptr, ptr getelementptr inbounds (%struct.anon, ptr @ready_threads, i32 0, i32 1), align 8, !tbaa !19
  %57 = load ptr, ptr %3, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.thread, ptr %57, i32 0, i32 6
  %59 = getelementptr inbounds %struct.anon.0, ptr %58, i32 0, i32 1
  store ptr %56, ptr %59, align 8, !tbaa !18
  %60 = load ptr, ptr %3, align 8, !tbaa !5
  %61 = load ptr, ptr getelementptr inbounds (%struct.anon, ptr @ready_threads, i32 0, i32 1), align 8, !tbaa !19
  store ptr %60, ptr %61, align 8, !tbaa !5
  %62 = load ptr, ptr %3, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.thread, ptr %62, i32 0, i32 6
  %64 = getelementptr inbounds %struct.anon.0, ptr %63, i32 0, i32 0
  store ptr %64, ptr getelementptr inbounds (%struct.anon, ptr @ready_threads, i32 0, i32 1), align 8, !tbaa !19
  br label %65

65:                                               ; preds = %52
  br label %66

66:                                               ; preds = %65
  %67 = load ptr, ptr %3, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.thread, ptr %67, i32 0, i32 3
  store i32 1, ptr %68, align 8, !tbaa !20
  %69 = load ptr, ptr %3, align 8, !tbaa !5
  call void @thread_set_current(ptr noundef %69)
  %70 = load ptr, ptr %1, align 8, !tbaa !5
  %71 = load ptr, ptr %3, align 8, !tbaa !5
  %72 = icmp ne ptr %70, %71
  br i1 %72, label %73, label %100

73:                                               ; preds = %66
  %74 = load ptr, ptr %1, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.thread, ptr %74, i32 0, i32 4
  %76 = load ptr, ptr %3, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.thread, ptr %76, i32 0, i32 4
  %78 = load ptr, ptr %77, align 8, !tbaa !21
  call void @cpu_context_switch(ptr noundef %75, ptr noundef %78)
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  %79 = load ptr, ptr %1, align 8, !tbaa !5
  %80 = call ptr @thread_get_current()
  %81 = icmp eq ptr %79, %80
  %82 = zext i1 %81 to i32
  store i32 %82, ptr %4, align 4, !tbaa !11
  %83 = load i32, ptr %4, align 4, !tbaa !11
  %84 = icmp ne i32 %83, 0
  br i1 %84, label %88, label %85

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

86:                                               ; preds = %87, %85
  br label %87

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

88:                                               ; preds = %73
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %89 = load ptr, ptr %1, align 8, !tbaa !5
  %90 = getelementptr inbounds %struct.thread, ptr %89, i32 0, i32 3
  %91 = load i32, ptr %90, align 8, !tbaa !20
  %92 = icmp eq i32 %91, 2
  %93 = zext i1 %92 to i32
  store i32 %93, ptr %5, align 4, !tbaa !11
  %94 = load i32, ptr %5, align 4, !tbaa !11
  %95 = icmp ne i32 %94, 0
  br i1 %95, label %99, label %96

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

97:                                               ; preds = %98, %96
  br label %98

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

99:                                               ; preds = %88
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  br label %100

100:                                              ; preds = %99, %66
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #3
  ret void
}

declare ptr @thread_get_current() #1

declare void @thread_set_current(ptr noundef) #1

declare void @cpu_context_switch(ptr noundef, ptr noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 0}
!10 = !{!"", !6, i64 0, !6, i64 8}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{i64 2147554474}
!14 = !{i64 2147554640}
!15 = !{!16, !6, i64 72}
!16 = !{!"thread", !12, i64 0, !17, i64 8, !7, i64 16, !12, i64 48, !6, i64 56, !7, i64 64, !10, i64 72}
!17 = !{!"long", !7, i64 0}
!18 = !{!16, !6, i64 80}
!19 = !{!10, !6, i64 8}
!20 = !{!16, !12, i64 48}
!21 = !{!16, !6, i64 56}
!22 = !{i64 2147555753}
!23 = !{i64 2147555919}
!24 = !{i64 2147556201}
!25 = !{i64 2147556367}


; 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

declare void @vbe_set_color(i64 noundef) local_unnamed_addr #0

declare void @printf(ptr noundef, ...) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @schedule() local_unnamed_addr #1 {
  %1 = tail call ptr @thread_get_current() #2
  %2 = load ptr, ptr @ready_threads, align 8, !tbaa !5
  %.not = icmp eq ptr %2, null
  br i1 %.not, label %3, label %5

3:                                                ; preds = %0
  tail call void asm sideeffect "cli\0A", "~{dirflag},~{fpsr},~{flags}"() #2, !srcloc !10
  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 66) #2
  br label %4

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

5:                                                ; preds = %0
  %6 = getelementptr inbounds i8, ptr %2, i64 72
  %7 = load ptr, ptr %6, align 8, !tbaa !12
  %.not22 = icmp eq ptr %7, null
  %8 = getelementptr inbounds i8, ptr %2, i64 80
  %9 = load ptr, ptr %8, align 8, !tbaa !16
  %10 = getelementptr inbounds i8, ptr %7, i64 80
  %.sink = select i1 %.not22, ptr getelementptr inbounds (i8, ptr @ready_threads, i64 8), ptr %10
  store ptr %9, ptr %.sink, align 8, !tbaa !17
  %11 = getelementptr inbounds i8, ptr %2, i64 80
  store ptr %7, ptr %9, align 8, !tbaa !18
  store ptr null, ptr %6, align 8, !tbaa !12
  %12 = load ptr, ptr getelementptr inbounds (i8, ptr @ready_threads, i64 8), align 8, !tbaa !17
  store ptr %12, ptr %11, align 8, !tbaa !16
  store ptr %2, ptr %12, align 8, !tbaa !18
  store ptr %6, ptr getelementptr inbounds (i8, ptr @ready_threads, i64 8), align 8, !tbaa !17
  %13 = getelementptr inbounds i8, ptr %2, i64 48
  store i32 1, ptr %13, align 8, !tbaa !19
  tail call void @thread_set_current(ptr noundef nonnull %2) #2
  %.not23 = icmp eq ptr %1, %2
  br i1 %.not23, label %28, label %14

14:                                               ; preds = %5
  %15 = getelementptr inbounds i8, ptr %1, i64 56
  %16 = getelementptr inbounds i8, ptr %2, i64 56
  %17 = load ptr, ptr %16, align 8, !tbaa !20
  tail call void @cpu_context_switch(ptr noundef nonnull %15, ptr noundef %17) #2
  %18 = tail call ptr @thread_get_current() #2
  %19 = icmp eq ptr %1, %18
  br i1 %19, label %22, label %20

20:                                               ; preds = %14
  tail call void asm sideeffect "cli\0A", "~{dirflag},~{fpsr},~{flags}"() #2, !srcloc !21
  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 82) #2
  br label %21

21:                                               ; preds = %21, %20
  tail call void asm sideeffect "hlt", "~{dirflag},~{fpsr},~{flags}"() #2, !srcloc !22
  br label %21

22:                                               ; preds = %14
  %23 = getelementptr inbounds i8, ptr %1, i64 48
  %24 = load i32, ptr %23, align 8, !tbaa !19
  %25 = icmp eq i32 %24, 2
  br i1 %25, label %28, label %26

26:                                               ; preds = %22
  tail call void asm sideeffect "cli\0A", "~{dirflag},~{fpsr},~{flags}"() #2, !srcloc !23
  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 83) #2
  br label %27

27:                                               ; preds = %27, %26
  tail call void asm sideeffect "hlt", "~{dirflag},~{fpsr},~{flags}"() #2, !srcloc !24
  br label %27

28:                                               ; preds = %22, %5
  ret void
}

declare ptr @thread_get_current() local_unnamed_addr #0

declare void @thread_set_current(ptr noundef) local_unnamed_addr #0

declare void @cpu_context_switch(ptr noundef, ptr noundef) local_unnamed_addr #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, !7, i64 0}
!6 = !{!"", !7, i64 0, !7, i64 8}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{i64 2147554474}
!11 = !{i64 2147554640}
!12 = !{!13, !7, i64 72}
!13 = !{!"thread", !14, i64 0, !15, i64 8, !8, i64 16, !14, i64 48, !7, i64 56, !8, i64 64, !6, i64 72}
!14 = !{!"int", !8, i64 0}
!15 = !{!"long", !8, i64 0}
!16 = !{!13, !7, i64 80}
!17 = !{!6, !7, i64 8}
!18 = !{!7, !7, i64 0}
!19 = !{!13, !14, i64 48}
!20 = !{!13, !7, i64 56}
!21 = !{i64 2147555753}
!22 = !{i64 2147555919}
!23 = !{i64 2147556201}
!24 = !{i64 2147556367}


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