; LLVM IR from zephyrproject-rtos/zephyr :: scripts/native_simulator/native/src (irq_ctrl)

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

@currently_running_prio = external hidden global i32, align 4
@irq_prio = external hidden global [32 x i8], align 16
@irqs_locked = external hidden global i8, align 1

; Function Attrs: nounwind uwtable
define dso_local i32 @hw_irq_ctrl_get_highest_prio_irq() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i64, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = load i8, ptr @irqs_locked, align 1, !tbaa !5, !range !9, !noundef !10
  %7 = trunc i8 %6 to i1
  br i1 %7, label %8, label %9

8:                                                ; preds = %0
  store i32 -1, ptr %1, align 4
  br label %49

9:                                                ; preds = %0
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #3
  %10 = call i64 @hw_irq_ctrl_get_irq_status()
  store i64 %10, ptr %2, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  store i32 -1, ptr %3, align 4, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  store i32 256, ptr %4, align 4, !tbaa !13
  br label %11

11:                                               ; preds = %46, %9
  %12 = load i64, ptr %2, align 8, !tbaa !11
  %13 = icmp ne i64 %12, 0
  br i1 %13, label %14, label %47

14:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %15 = load i64, ptr %2, align 8, !tbaa !11
  %16 = call i32 @nsi_find_lsb_set64(i64 noundef %15)
  %17 = sub i32 %16, 1
  store i32 %17, ptr %5, align 4, !tbaa !13
  %18 = load i32, ptr %5, align 4, !tbaa !13
  %19 = zext i32 %18 to i64
  %20 = shl i64 1, %19
  %21 = xor i64 %20, -1
  %22 = load i64, ptr %2, align 8, !tbaa !11
  %23 = and i64 %22, %21
  store i64 %23, ptr %2, align 8, !tbaa !11
  %24 = load i32, ptr %4, align 4, !tbaa !13
  %25 = load i32, ptr %5, align 4, !tbaa !13
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [32 x i8], ptr @irq_prio, i64 0, i64 %26
  %28 = load i8, ptr %27, align 1, !tbaa !15
  %29 = zext i8 %28 to i32
  %30 = icmp sgt i32 %24, %29
  br i1 %30, label %31, label %46

31:                                               ; preds = %14
  %32 = load i32, ptr @currently_running_prio, align 4, !tbaa !13
  %33 = load i32, ptr %5, align 4, !tbaa !13
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [32 x i8], ptr @irq_prio, i64 0, i64 %34
  %36 = load i8, ptr %35, align 1, !tbaa !15
  %37 = zext i8 %36 to i32
  %38 = icmp sgt i32 %32, %37
  br i1 %38, label %39, label %46

39:                                               ; preds = %31
  %40 = load i32, ptr %5, align 4, !tbaa !13
  store i32 %40, ptr %3, align 4, !tbaa !13
  %41 = load i32, ptr %5, align 4, !tbaa !13
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds [32 x i8], ptr @irq_prio, i64 0, i64 %42
  %44 = load i8, ptr %43, align 1, !tbaa !15
  %45 = zext i8 %44 to i32
  store i32 %45, ptr %4, align 4, !tbaa !13
  br label %46

46:                                               ; preds = %39, %31, %14
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  br label %11, !llvm.loop !16

47:                                               ; preds = %11
  %48 = load i32, ptr %3, align 4, !tbaa !13
  store i32 %48, ptr %1, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #3
  br label %49

49:                                               ; preds = %47, %8
  %50 = load i32, ptr %1, align 4
  ret i32 %50
}

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

; Function Attrs: nounwind uwtable
declare dso_local i64 @hw_irq_ctrl_get_irq_status() #0

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @nsi_find_lsb_set64(i64 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 = { inlinehint 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 = !{!"_Bool", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{i8 0, i8 2}
!10 = !{}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"int", !7, i64 0}
!15 = !{!7, !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"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"

@currently_running_prio = external hidden local_unnamed_addr global i32, align 4
@irq_prio = external hidden local_unnamed_addr global [32 x i8], align 16
@irqs_locked = external hidden local_unnamed_addr global i8, align 1

; Function Attrs: nounwind uwtable
define dso_local i32 @hw_irq_ctrl_get_highest_prio_irq() local_unnamed_addr #0 {
  %1 = load i8, ptr @irqs_locked, align 1, !tbaa !5, !range !9, !noundef !10
  %2 = trunc nuw i8 %1 to i1
  br i1 %2, label %.loopexit, label %3

3:                                                ; preds = %0
  %4 = tail call i64 @hw_irq_ctrl_get_irq_status()
  %.not17 = icmp eq i64 %4, 0
  br i1 %.not17, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %3, %.lr.ph
  %.01120 = phi i32 [ %.1, %.lr.ph ], [ 256, %3 ]
  %.01219 = phi i32 [ %.113, %.lr.ph ], [ -1, %3 ]
  %.01418 = phi i64 [ %10, %.lr.ph ], [ %4, %3 ]
  %5 = tail call i32 @nsi_find_lsb_set64(i64 noundef %.01418)
  %6 = add i32 %5, -1
  %7 = zext nneg i32 %6 to i64
  %8 = shl nuw i64 1, %7
  %9 = xor i64 %8, -1
  %10 = and i64 %.01418, %9
  %11 = sext i32 %6 to i64
  %12 = getelementptr inbounds [32 x i8], ptr @irq_prio, i64 0, i64 %11
  %13 = load i8, ptr %12, align 1, !tbaa !11
  %14 = zext i8 %13 to i32
  %15 = icmp sgt i32 %.01120, %14
  %16 = load i32, ptr @currently_running_prio, align 4
  %17 = icmp sgt i32 %16, %14
  %or.cond = select i1 %15, i1 %17, i1 false
  %.113 = select i1 %or.cond, i32 %6, i32 %.01219
  %.1 = select i1 %or.cond, i32 %14, i32 %.01120
  %.not = icmp eq i64 %10, 0
  br i1 %.not, label %.loopexit, label %.lr.ph, !llvm.loop !12

.loopexit:                                        ; preds = %.lr.ph, %3, %0
  %.0 = phi i32 [ -1, %0 ], [ -1, %3 ], [ %.113, %.lr.ph ]
  ret i32 %.0
}

; Function Attrs: nounwind uwtable
declare dso_local i64 @hw_irq_ctrl_get_irq_status() local_unnamed_addr #0

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @nsi_find_lsb_set64(i64 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 = { inlinehint 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" }

!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 = !{!"_Bool", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{i8 0, i8 2}
!10 = !{}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass