; LLVM IR from s-matyukevich/raspberry-pi-os :: exercises/lesson04/1/avenito/src (sched)

; 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.task_struct = type { %struct.cpu_context, i64, i64, i64, i64 }
%struct.cpu_context = type { i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64 }

@current = external dso_local global ptr, align 8
@task = external dso_local global [3 x ptr], align 16
@.str = external hidden unnamed_addr constant [42 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [26 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [25 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [30 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [23 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [35 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [16 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

; 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 @switch_to(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  call void (ptr, ...) @tfp_printf(ptr noundef @.str)
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  store i32 0, ptr %4, align 4, !tbaa !9
  br label %7

7:                                                ; preds = %33, %1
  %8 = load i32, ptr %4, align 4, !tbaa !9
  %9 = icmp slt i32 %8, 3
  br i1 %9, label %11, label %10

10:                                               ; preds = %7
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  br label %36

11:                                               ; preds = %7
  %12 = load i32, ptr %4, align 4, !tbaa !9
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [3 x ptr], ptr @task, i64 0, i64 %13
  %15 = load ptr, ptr %14, align 8, !tbaa !5
  store ptr %15, ptr %3, align 8, !tbaa !5
  %16 = load i32, ptr %4, align 4, !tbaa !9
  %17 = load ptr, ptr %3, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.task_struct, ptr %17, i32 0, i32 2
  %19 = load i64, ptr %18, align 8, !tbaa !11
  call void (ptr, ...) @tfp_printf(ptr noundef @.str.1, i32 noundef %16, i64 noundef %19)
  %20 = load i32, ptr %4, align 4, !tbaa !9
  %21 = load ptr, ptr %3, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.task_struct, ptr %21, i32 0, i32 3
  %23 = load i64, ptr %22, align 8, !tbaa !15
  call void (ptr, ...) @tfp_printf(ptr noundef @.str.2, i32 noundef %20, i64 noundef %23)
  %24 = load i32, ptr %4, align 4, !tbaa !9
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.task_struct, ptr %25, i32 0, i32 4
  %27 = load i64, ptr %26, align 8, !tbaa !16
  call void (ptr, ...) @tfp_printf(ptr noundef @.str.3, i32 noundef %24, i64 noundef %27)
  %28 = load i32, ptr %4, align 4, !tbaa !9
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.task_struct, ptr %29, i32 0, i32 0
  %31 = getelementptr inbounds %struct.cpu_context, ptr %30, i32 0, i32 11
  %32 = load i64, ptr %31, align 8, !tbaa !17
  call void (ptr, ...) @tfp_printf(ptr noundef @.str.4, i32 noundef %28, i64 noundef %32)
  call void (ptr, ...) @tfp_printf(ptr noundef @.str.5)
  br label %33

33:                                               ; preds = %11
  %34 = load i32, ptr %4, align 4, !tbaa !9
  %35 = add nsw i32 %34, 1
  store i32 %35, ptr %4, align 4, !tbaa !9
  br label %7, !llvm.loop !18

36:                                               ; preds = %10
  call void (ptr, ...) @tfp_printf(ptr noundef @.str.6)
  %37 = load ptr, ptr @current, align 8, !tbaa !5
  %38 = load ptr, ptr %2, align 8, !tbaa !5
  %39 = icmp eq ptr %37, %38
  br i1 %39, label %40, label %41

40:                                               ; preds = %36
  store i32 1, ptr %5, align 4
  br label %46

41:                                               ; preds = %36
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %42 = load ptr, ptr @current, align 8, !tbaa !5
  store ptr %42, ptr %6, align 8, !tbaa !5
  %43 = load ptr, ptr %2, align 8, !tbaa !5
  store ptr %43, ptr @current, align 8, !tbaa !5
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  %45 = load ptr, ptr %2, align 8, !tbaa !5
  call void @cpu_switch_to(ptr noundef %44, ptr noundef %45)
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  store i32 0, ptr %5, align 4
  br label %46

46:                                               ; preds = %41, %40
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  %47 = load i32, ptr %5, align 4
  switch i32 %47, label %49 [
    i32 0, label %48
    i32 1, label %48
  ]

48:                                               ; preds = %46, %46
  ret void

49:                                               ; preds = %46
  unreachable
}

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

declare void @cpu_switch_to(ptr noundef, ptr noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { "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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !14, i64 112}
!12 = !{!"task_struct", !13, i64 0, !14, i64 104, !14, i64 112, !14, i64 120, !14, i64 128}
!13 = !{!"cpu_context", !14, i64 0, !14, i64 8, !14, i64 16, !14, i64 24, !14, i64 32, !14, i64 40, !14, i64 48, !14, i64 56, !14, i64 64, !14, i64 72, !14, i64 80, !14, i64 88, !14, i64 96}
!14 = !{!"long", !7, i64 0}
!15 = !{!12, !14, i64 120}
!16 = !{!12, !14, i64 128}
!17 = !{!12, !14, i64 88}
!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"

@current = external dso_local local_unnamed_addr global ptr, align 8
@task = external dso_local local_unnamed_addr global [3 x ptr], align 16
@.str = external hidden unnamed_addr constant [42 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [26 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [25 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [30 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [23 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [35 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [16 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local void @switch_to(ptr noundef %0) local_unnamed_addr #0 {
  tail call void (ptr, ...) @tfp_printf(ptr noundef nonnull @.str) #2
  %2 = load ptr, ptr @task, align 16, !tbaa !5
  %3 = getelementptr inbounds i8, ptr %2, i64 112
  %4 = load i64, ptr %3, align 8, !tbaa !9
  tail call void (ptr, ...) @tfp_printf(ptr noundef nonnull @.str.1, i32 noundef 0, i64 noundef %4) #2
  %5 = getelementptr inbounds i8, ptr %2, i64 120
  %6 = load i64, ptr %5, align 8, !tbaa !13
  tail call void (ptr, ...) @tfp_printf(ptr noundef nonnull @.str.2, i32 noundef 0, i64 noundef %6) #2
  %7 = getelementptr inbounds i8, ptr %2, i64 128
  %8 = load i64, ptr %7, align 8, !tbaa !14
  tail call void (ptr, ...) @tfp_printf(ptr noundef nonnull @.str.3, i32 noundef 0, i64 noundef %8) #2
  %9 = getelementptr inbounds i8, ptr %2, i64 88
  %10 = load i64, ptr %9, align 8, !tbaa !15
  tail call void (ptr, ...) @tfp_printf(ptr noundef nonnull @.str.4, i32 noundef 0, i64 noundef %10) #2
  tail call void (ptr, ...) @tfp_printf(ptr noundef nonnull @.str.5) #2
  %11 = load ptr, ptr getelementptr inbounds (i8, ptr @task, i64 8), align 8, !tbaa !5
  %12 = getelementptr inbounds i8, ptr %11, i64 112
  %13 = load i64, ptr %12, align 8, !tbaa !9
  tail call void (ptr, ...) @tfp_printf(ptr noundef nonnull @.str.1, i32 noundef 1, i64 noundef %13) #2
  %14 = getelementptr inbounds i8, ptr %11, i64 120
  %15 = load i64, ptr %14, align 8, !tbaa !13
  tail call void (ptr, ...) @tfp_printf(ptr noundef nonnull @.str.2, i32 noundef 1, i64 noundef %15) #2
  %16 = getelementptr inbounds i8, ptr %11, i64 128
  %17 = load i64, ptr %16, align 8, !tbaa !14
  tail call void (ptr, ...) @tfp_printf(ptr noundef nonnull @.str.3, i32 noundef 1, i64 noundef %17) #2
  %18 = getelementptr inbounds i8, ptr %11, i64 88
  %19 = load i64, ptr %18, align 8, !tbaa !15
  tail call void (ptr, ...) @tfp_printf(ptr noundef nonnull @.str.4, i32 noundef 1, i64 noundef %19) #2
  tail call void (ptr, ...) @tfp_printf(ptr noundef nonnull @.str.5) #2
  %20 = load ptr, ptr getelementptr inbounds (i8, ptr @task, i64 16), align 16, !tbaa !5
  %21 = getelementptr inbounds i8, ptr %20, i64 112
  %22 = load i64, ptr %21, align 8, !tbaa !9
  tail call void (ptr, ...) @tfp_printf(ptr noundef nonnull @.str.1, i32 noundef 2, i64 noundef %22) #2
  %23 = getelementptr inbounds i8, ptr %20, i64 120
  %24 = load i64, ptr %23, align 8, !tbaa !13
  tail call void (ptr, ...) @tfp_printf(ptr noundef nonnull @.str.2, i32 noundef 2, i64 noundef %24) #2
  %25 = getelementptr inbounds i8, ptr %20, i64 128
  %26 = load i64, ptr %25, align 8, !tbaa !14
  tail call void (ptr, ...) @tfp_printf(ptr noundef nonnull @.str.3, i32 noundef 2, i64 noundef %26) #2
  %27 = getelementptr inbounds i8, ptr %20, i64 88
  %28 = load i64, ptr %27, align 8, !tbaa !15
  tail call void (ptr, ...) @tfp_printf(ptr noundef nonnull @.str.4, i32 noundef 2, i64 noundef %28) #2
  tail call void (ptr, ...) @tfp_printf(ptr noundef nonnull @.str.5) #2
  tail call void (ptr, ...) @tfp_printf(ptr noundef nonnull @.str.6) #2
  %29 = load ptr, ptr @current, align 8, !tbaa !5
  %30 = icmp eq ptr %29, %0
  br i1 %30, label %32, label %31

31:                                               ; preds = %1
  store ptr %0, ptr @current, align 8, !tbaa !5
  tail call void @cpu_switch_to(ptr noundef %29, ptr noundef %0) #2
  br label %32

32:                                               ; preds = %1, %31
  ret void
}

declare void @tfp_printf(ptr noundef, ...) local_unnamed_addr #1

declare void @cpu_switch_to(ptr noundef, 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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !12, i64 112}
!10 = !{!"task_struct", !11, i64 0, !12, i64 104, !12, i64 112, !12, i64 120, !12, i64 128}
!11 = !{!"cpu_context", !12, i64 0, !12, i64 8, !12, i64 16, !12, i64 24, !12, i64 32, !12, i64 40, !12, i64 48, !12, i64 56, !12, i64 64, !12, i64 72, !12, i64 80, !12, i64 88, !12, i64 96}
!12 = !{!"long", !7, i64 0}
!13 = !{!10, !12, i64 120}
!14 = !{!10, !12, i64 128}
!15 = !{!10, !12, i64 88}


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