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

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

@task = external dso_local global [3 x ptr], align 16

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

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

; Function Attrs: nounwind uwtable
define dso_local void @_schedule() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  call void @preempt_disable()
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  br label %6

6:                                                ; preds = %72, %0
  br label %7

7:                                                ; preds = %6
  store i32 -1, ptr %2, align 4, !tbaa !5
  store i32 0, ptr %1, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %8

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

11:                                               ; preds = %8
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  br label %41

12:                                               ; preds = %8
  %13 = load i32, ptr %4, align 4, !tbaa !5
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [3 x ptr], ptr @task, i64 0, i64 %14
  %16 = load ptr, ptr %15, align 8, !tbaa !9
  store ptr %16, ptr %3, align 8, !tbaa !9
  %17 = load ptr, ptr %3, align 8, !tbaa !9
  %18 = icmp ne ptr %17, null
  br i1 %18, label %19, label %37

19:                                               ; preds = %12
  %20 = load ptr, ptr %3, align 8, !tbaa !9
  %21 = getelementptr inbounds %struct.task_struct, ptr %20, i32 0, i32 1
  %22 = load i64, ptr %21, align 8, !tbaa !11
  %23 = icmp eq i64 %22, 0
  br i1 %23, label %24, label %37

24:                                               ; preds = %19
  %25 = load ptr, ptr %3, align 8, !tbaa !9
  %26 = getelementptr inbounds %struct.task_struct, ptr %25, i32 0, i32 2
  %27 = load i64, ptr %26, align 8, !tbaa !15
  %28 = load i32, ptr %2, align 4, !tbaa !5
  %29 = sext i32 %28 to i64
  %30 = icmp sgt i64 %27, %29
  br i1 %30, label %31, label %37

31:                                               ; preds = %24
  %32 = load ptr, ptr %3, align 8, !tbaa !9
  %33 = getelementptr inbounds %struct.task_struct, ptr %32, i32 0, i32 2
  %34 = load i64, ptr %33, align 8, !tbaa !15
  %35 = trunc i64 %34 to i32
  store i32 %35, ptr %2, align 4, !tbaa !5
  %36 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %36, ptr %1, align 4, !tbaa !5
  br label %37

37:                                               ; preds = %31, %24, %19, %12
  br label %38

38:                                               ; preds = %37
  %39 = load i32, ptr %4, align 4, !tbaa !5
  %40 = add nsw i32 %39, 1
  store i32 %40, ptr %4, align 4, !tbaa !5
  br label %8, !llvm.loop !16

41:                                               ; preds = %11
  %42 = load i32, ptr %2, align 4, !tbaa !5
  %43 = icmp ne i32 %42, 0
  br i1 %43, label %44, label %45

44:                                               ; preds = %41
  br label %73

45:                                               ; preds = %41
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  store i32 0, ptr %5, align 4, !tbaa !5
  br label %46

46:                                               ; preds = %69, %45
  %47 = load i32, ptr %5, align 4, !tbaa !5
  %48 = icmp slt i32 %47, 3
  br i1 %48, label %50, label %49

49:                                               ; preds = %46
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  br label %72

50:                                               ; preds = %46
  %51 = load i32, ptr %5, align 4, !tbaa !5
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds [3 x ptr], ptr @task, i64 0, i64 %52
  %54 = load ptr, ptr %53, align 8, !tbaa !9
  store ptr %54, ptr %3, align 8, !tbaa !9
  %55 = load ptr, ptr %3, align 8, !tbaa !9
  %56 = icmp ne ptr %55, null
  br i1 %56, label %57, label %68

57:                                               ; preds = %50
  %58 = load ptr, ptr %3, align 8, !tbaa !9
  %59 = getelementptr inbounds %struct.task_struct, ptr %58, i32 0, i32 2
  %60 = load i64, ptr %59, align 8, !tbaa !15
  %61 = ashr i64 %60, 1
  %62 = load ptr, ptr %3, align 8, !tbaa !9
  %63 = getelementptr inbounds %struct.task_struct, ptr %62, i32 0, i32 3
  %64 = load i64, ptr %63, align 8, !tbaa !18
  %65 = add nsw i64 %61, %64
  %66 = load ptr, ptr %3, align 8, !tbaa !9
  %67 = getelementptr inbounds %struct.task_struct, ptr %66, i32 0, i32 2
  store i64 %65, ptr %67, align 8, !tbaa !15
  br label %68

68:                                               ; preds = %57, %50
  br label %69

69:                                               ; preds = %68
  %70 = load i32, ptr %5, align 4, !tbaa !5
  %71 = add nsw i32 %70, 1
  store i32 %71, ptr %5, align 4, !tbaa !5
  br label %46, !llvm.loop !19

72:                                               ; preds = %49
  br label %6

73:                                               ; preds = %44
  %74 = load i32, ptr %1, align 4, !tbaa !5
  %75 = sext i32 %74 to i64
  %76 = getelementptr inbounds [3 x ptr], ptr @task, i64 0, i64 %75
  %77 = load ptr, ptr %76, align 8, !tbaa !9
  call void @switch_to(ptr noundef %77)
  call void @preempt_enable()
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #2
  ret void
}

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @switch_to(ptr noundef) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !14, i64 104}
!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 112}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!12, !14, i64 120}
!19 = distinct !{!19, !17}


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

@task = external dso_local local_unnamed_addr global [3 x ptr], align 16

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

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

; Function Attrs: nounwind uwtable
define dso_local void @_schedule() local_unnamed_addr #0 {
  tail call void @preempt_disable()
  %.pre = load ptr, ptr @task, align 16, !tbaa !5
  %.not25 = icmp eq ptr %.pre, null
  %1 = getelementptr inbounds i8, ptr %.pre, i64 104
  %2 = getelementptr inbounds i8, ptr %.pre, i64 112
  %3 = load ptr, ptr getelementptr inbounds (i8, ptr @task, i64 8), align 8, !tbaa !5
  %.not25.1 = icmp eq ptr %3, null
  %4 = getelementptr inbounds i8, ptr %3, i64 104
  %5 = getelementptr inbounds i8, ptr %3, i64 112
  %6 = load ptr, ptr getelementptr inbounds (i8, ptr @task, i64 16), align 16, !tbaa !5
  %.not25.2 = icmp eq ptr %6, null
  %7 = getelementptr inbounds i8, ptr %6, i64 104
  %8 = getelementptr inbounds i8, ptr %6, i64 112
  %9 = getelementptr inbounds i8, ptr %.pre, i64 112
  %10 = getelementptr inbounds i8, ptr %.pre, i64 120
  %11 = getelementptr inbounds i8, ptr %3, i64 112
  %12 = getelementptr inbounds i8, ptr %3, i64 120
  %13 = getelementptr inbounds i8, ptr %6, i64 112
  %14 = getelementptr inbounds i8, ptr %6, i64 120
  br label %.loopexit

.loopexit:                                        ; preds = %.loopexit.backedge, %0
  br i1 %.not25, label %20, label %15

.preheader.preheader:                             ; preds = %38
  br i1 %.not25, label %.preheader.1, label %39

15:                                               ; preds = %.loopexit
  %16 = load i64, ptr %1, align 8, !tbaa !9
  %17 = icmp eq i64 %16, 0
  br i1 %17, label %18, label %20

18:                                               ; preds = %15
  %19 = load i64, ptr %2, align 8, !tbaa !13
  %spec.select2633 = tail call i64 @llvm.smax.i64(i64 %19, i64 -1)
  %spec.select26 = trunc i64 %spec.select2633 to i32
  br label %20

20:                                               ; preds = %18, %.loopexit, %15
  %.1 = phi i32 [ -1, %15 ], [ -1, %.loopexit ], [ %spec.select26, %18 ]
  br i1 %.not25.1, label %29, label %21

21:                                               ; preds = %20
  %22 = load i64, ptr %4, align 8, !tbaa !9
  %23 = icmp eq i64 %22, 0
  br i1 %23, label %24, label %29

24:                                               ; preds = %21
  %25 = load i64, ptr %5, align 8, !tbaa !13
  %26 = sext i32 %.1 to i64
  %27 = icmp sgt i64 %25, %26
  %28 = trunc i64 %25 to i32
  %spec.select.1 = zext i1 %27 to i32
  %spec.select26.1 = select i1 %27, i32 %28, i32 %.1
  br label %29

29:                                               ; preds = %24, %21, %20
  %.120.1 = phi i32 [ 0, %21 ], [ 0, %20 ], [ %spec.select.1, %24 ]
  %.1.1 = phi i32 [ %.1, %21 ], [ %.1, %20 ], [ %spec.select26.1, %24 ]
  br i1 %.not25.2, label %38, label %30

30:                                               ; preds = %29
  %31 = load i64, ptr %7, align 8, !tbaa !9
  %32 = icmp eq i64 %31, 0
  br i1 %32, label %33, label %38

33:                                               ; preds = %30
  %34 = load i64, ptr %8, align 8, !tbaa !13
  %35 = sext i32 %.1.1 to i64
  %36 = icmp sgt i64 %34, %35
  %37 = trunc i64 %34 to i32
  %spec.select.2 = select i1 %36, i32 2, i32 %.120.1
  %spec.select26.2 = select i1 %36, i32 %37, i32 %.1.1
  br label %38

38:                                               ; preds = %33, %30, %29
  %.120.2 = phi i32 [ %.120.1, %30 ], [ %.120.1, %29 ], [ %spec.select.2, %33 ]
  %.1.2 = phi i32 [ %.1.1, %30 ], [ %.1.1, %29 ], [ %spec.select26.2, %33 ]
  %.not = icmp eq i32 %.1.2, 0
  br i1 %.not, label %.preheader.preheader, label %54

39:                                               ; preds = %.preheader.preheader
  %40 = load i64, ptr %9, align 8, !tbaa !13
  %41 = ashr i64 %40, 1
  %42 = load i64, ptr %10, align 8, !tbaa !14
  %43 = add nsw i64 %41, %42
  store i64 %43, ptr %9, align 8, !tbaa !13
  br label %.preheader.1

.preheader.1:                                     ; preds = %.preheader.preheader, %39
  br i1 %.not25.1, label %.preheader.2, label %44

44:                                               ; preds = %.preheader.1
  %45 = load i64, ptr %11, align 8, !tbaa !13
  %46 = ashr i64 %45, 1
  %47 = load i64, ptr %12, align 8, !tbaa !14
  %48 = add nsw i64 %46, %47
  store i64 %48, ptr %11, align 8, !tbaa !13
  br label %.preheader.2

.preheader.2:                                     ; preds = %44, %.preheader.1
  br i1 %.not25.2, label %.loopexit.backedge, label %49

49:                                               ; preds = %.preheader.2
  %50 = load i64, ptr %13, align 8, !tbaa !13
  %51 = ashr i64 %50, 1
  %52 = load i64, ptr %14, align 8, !tbaa !14
  %53 = add nsw i64 %51, %52
  store i64 %53, ptr %13, align 8, !tbaa !13
  br label %.loopexit.backedge

.loopexit.backedge:                               ; preds = %49, %.preheader.2
  br label %.loopexit, !llvm.loop !15

54:                                               ; preds = %38
  %55 = zext nneg i32 %.120.2 to i64
  %56 = getelementptr inbounds [3 x ptr], ptr @task, i64 0, i64 %55
  %57 = load ptr, ptr %56, align 8, !tbaa !5
  tail call void @switch_to(ptr noundef %57)
  tail call void @preempt_enable()
  ret void
}

; Function Attrs: nounwind uwtable
declare dso_local void @switch_to(ptr noundef) local_unnamed_addr #0

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.smax.i64(i64, i64) #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 speculatable willreturn memory(none) }

!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 104}
!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 112}
!14 = !{!10, !12, i64 120}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass