; LLVM IR from t4th/cortex-m3-rtos :: examples/critical_section/main.ll (Z11worker_taskPv)

; 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.SharedData = type { %"struct.kernel::critical_section::Context", [100 x i8], i64 }
%"struct.kernel::critical_section::Context" = type { i32, i32, i32, i32 }

@__const._Z11worker_taskPv.text_id = external hidden unnamed_addr constant [3 x ptr], align 16
@_ZZ11worker_taskPvE7task_id = external hidden global i32, align 4

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_Z14changeCaseSizeR10SharedData(ptr noundef nonnull align 8 dereferenceable(128)) #0

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

declare void @_ZN6kernel8hardware5debug5printEPKc(ptr noundef) #2

; Function Attrs: mustprogress uwtable
define dso_local void @_Z11worker_taskPv(ptr noundef %0) #3 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca [3 x ptr], align 16
  %5 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #5
  %6 = load ptr, ptr %2, align 8, !tbaa !5
  store ptr %6, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 24, ptr %4) #5
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %4, ptr align 16 @__const._Z11worker_taskPv.text_id, i64 24, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #5
  %7 = load i32, ptr @_ZZ11worker_taskPvE7task_id, align 4, !tbaa !9
  store i32 %7, ptr %5, align 4, !tbaa !9
  %8 = load i32, ptr @_ZZ11worker_taskPvE7task_id, align 4, !tbaa !9
  %9 = add nsw i32 %8, 1
  store i32 %9, ptr @_ZZ11worker_taskPvE7task_id, align 4, !tbaa !9
  br label %10

10:                                               ; preds = %33, %1
  br label %11

11:                                               ; preds = %10
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.SharedData, ptr %12, i32 0, i32 0
  call void @_ZN6kernel16critical_section5enterERNS0_7ContextE(ptr noundef nonnull align 4 dereferenceable(16) %13)
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  call void @_Z14changeCaseSizeR10SharedData(ptr noundef nonnull align 8 dereferenceable(128) %14)
  %15 = load i32, ptr %5, align 4, !tbaa !9
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [3 x ptr], ptr %4, i64 0, i64 %16
  %18 = load ptr, ptr %17, align 8, !tbaa !5
  call void @_ZN6kernel8hardware5debug5printEPKc(ptr noundef %18)
  %19 = load ptr, ptr %3, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.SharedData, ptr %19, i32 0, i32 1
  %21 = getelementptr inbounds [100 x i8], ptr %20, i64 0, i64 0
  call void @_ZN6kernel8hardware5debug5printEPKc(ptr noundef %21)
  %22 = load ptr, ptr %3, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.SharedData, ptr %22, i32 0, i32 0
  call void @_ZN6kernel16critical_section5leaveERNS0_7ContextE(ptr noundef nonnull align 4 dereferenceable(16) %23)
  %24 = load i32, ptr %5, align 4, !tbaa !9
  %25 = icmp eq i32 0, %24
  br i1 %25, label %26, label %27

26:                                               ; preds = %11
  call void @_Z5delayj(i32 noundef 230)
  br label %33

27:                                               ; preds = %11
  %28 = load i32, ptr %5, align 4, !tbaa !9
  %29 = icmp eq i32 1, %28
  br i1 %29, label %30, label %31

30:                                               ; preds = %27
  call void @_Z5delayj(i32 noundef 130)
  br label %32

31:                                               ; preds = %27
  call void @_Z5delayj(i32 noundef 900)
  br label %32

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

33:                                               ; preds = %32, %26
  br label %10, !llvm.loop !11
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_Z5delayj(i32 noundef) #0

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #4

declare void @_ZN6kernel16critical_section5enterERNS0_7ContextE(ptr noundef nonnull align 4 dereferenceable(16)) #2

declare void @_ZN6kernel16critical_section5leaveERNS0_7ContextE(ptr noundef nonnull align 4 dereferenceable(16)) #2

attributes #0 = { mustprogress 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 = { mustprogress 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 #4 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"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"

@__const._Z11worker_taskPv.text_id = external hidden unnamed_addr constant [3 x ptr], align 16
@_ZZ11worker_taskPvE7task_id = external hidden local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_Z14changeCaseSizeR10SharedData(ptr noundef nonnull align 8 dereferenceable(128)) local_unnamed_addr #0

declare void @_ZN6kernel8hardware5debug5printEPKc(ptr noundef) local_unnamed_addr #1

; Function Attrs: mustprogress noreturn uwtable
define dso_local void @_Z11worker_taskPv(ptr noundef %0) local_unnamed_addr #2 {
  %2 = load i32, ptr @_ZZ11worker_taskPvE7task_id, align 4, !tbaa !5
  %.fr = freeze i32 %2
  %3 = add nsw i32 %.fr, 1
  store i32 %3, ptr @_ZZ11worker_taskPvE7task_id, align 4, !tbaa !5
  %4 = sext i32 %.fr to i64
  %5 = getelementptr inbounds [3 x ptr], ptr @__const._Z11worker_taskPv.text_id, i64 0, i64 %4
  %6 = getelementptr inbounds i8, ptr %0, i64 16
  switch i32 %.fr, label %.split [
    i32 0, label %.split.us
    i32 1, label %.split.us7
  ]

.split.us:                                        ; preds = %1, %.split.us
  tail call void @_ZN6kernel16critical_section5enterERNS0_7ContextE(ptr noundef nonnull align 4 dereferenceable(16) %0)
  tail call void @_Z14changeCaseSizeR10SharedData(ptr noundef nonnull align 8 dereferenceable(128) %0)
  %7 = load ptr, ptr %5, align 8, !tbaa !9
  tail call void @_ZN6kernel8hardware5debug5printEPKc(ptr noundef %7)
  tail call void @_ZN6kernel8hardware5debug5printEPKc(ptr noundef nonnull %6)
  tail call void @_ZN6kernel16critical_section5leaveERNS0_7ContextE(ptr noundef nonnull align 4 dereferenceable(16) %0)
  tail call void @_Z5delayj(i32 noundef 230)
  br label %.split.us, !llvm.loop !11

.split.us7:                                       ; preds = %1, %.split.us7
  tail call void @_ZN6kernel16critical_section5enterERNS0_7ContextE(ptr noundef nonnull align 4 dereferenceable(16) %0)
  tail call void @_Z14changeCaseSizeR10SharedData(ptr noundef nonnull align 8 dereferenceable(128) %0)
  %8 = load ptr, ptr %5, align 8, !tbaa !9
  tail call void @_ZN6kernel8hardware5debug5printEPKc(ptr noundef %8)
  tail call void @_ZN6kernel8hardware5debug5printEPKc(ptr noundef nonnull %6)
  tail call void @_ZN6kernel16critical_section5leaveERNS0_7ContextE(ptr noundef nonnull align 4 dereferenceable(16) %0)
  tail call void @_Z5delayj(i32 noundef 130)
  br label %.split.us7, !llvm.loop !11

.split:                                           ; preds = %1, %.split
  tail call void @_ZN6kernel16critical_section5enterERNS0_7ContextE(ptr noundef nonnull align 4 dereferenceable(16) %0)
  tail call void @_Z14changeCaseSizeR10SharedData(ptr noundef nonnull align 8 dereferenceable(128) %0)
  %9 = load ptr, ptr %5, align 8, !tbaa !9
  tail call void @_ZN6kernel8hardware5debug5printEPKc(ptr noundef %9)
  tail call void @_ZN6kernel8hardware5debug5printEPKc(ptr noundef nonnull %6)
  tail call void @_ZN6kernel16critical_section5leaveERNS0_7ContextE(ptr noundef nonnull align 4 dereferenceable(16) %0)
  tail call void @_Z5delayj(i32 noundef 900)
  br label %.split, !llvm.loop !11
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_Z5delayj(i32 noundef) local_unnamed_addr #0

declare void @_ZN6kernel16critical_section5enterERNS0_7ContextE(ptr noundef nonnull align 4 dereferenceable(16)) local_unnamed_addr #1

declare void @_ZN6kernel16critical_section5leaveERNS0_7ContextE(ptr noundef nonnull align 4 dereferenceable(16)) local_unnamed_addr #1

attributes #0 = { mustprogress 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 = { mustprogress noreturn 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass