; LLVM IR from tomoyasuzuki/kvmm :: lapic (deq_irr)

; 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.lapic = type { [1024 x i32], i32, ptr }
%struct.irr_queue = type { [10 x i32], i32 }

@lapic = external dso_local global ptr, align 8

; 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 i32 @deq_irr() #1 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #2
  %4 = load ptr, ptr @lapic, align 8, !tbaa !5
  %5 = getelementptr inbounds %struct.lapic, ptr %4, i32 0, i32 2
  %6 = load ptr, ptr %5, align 8, !tbaa !9
  %7 = getelementptr inbounds %struct.irr_queue, ptr %6, i32 0, i32 0
  %8 = getelementptr inbounds [10 x i32], ptr %7, i64 0, i64 0
  %9 = load i32, ptr %8, align 4, !tbaa !12
  store i32 %9, ptr %2, align 4, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  store i32 0, ptr %3, align 4, !tbaa !12
  br label %10

10:                                               ; preds = %36, %0
  %11 = load i32, ptr %3, align 4, !tbaa !12
  %12 = load ptr, ptr @lapic, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.lapic, ptr %12, i32 0, i32 2
  %14 = load ptr, ptr %13, align 8, !tbaa !9
  %15 = getelementptr inbounds %struct.irr_queue, ptr %14, i32 0, i32 1
  %16 = load i32, ptr %15, align 4, !tbaa !13
  %17 = icmp slt i32 %11, %16
  br i1 %17, label %19, label %18

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

19:                                               ; preds = %10
  %20 = load ptr, ptr @lapic, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.lapic, ptr %20, i32 0, i32 2
  %22 = load ptr, ptr %21, align 8, !tbaa !9
  %23 = getelementptr inbounds %struct.irr_queue, ptr %22, i32 0, i32 0
  %24 = load i32, ptr %3, align 4, !tbaa !12
  %25 = add nsw i32 %24, 1
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [10 x i32], ptr %23, i64 0, i64 %26
  %28 = load i32, ptr %27, align 4, !tbaa !12
  %29 = load ptr, ptr @lapic, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.lapic, ptr %29, i32 0, i32 2
  %31 = load ptr, ptr %30, align 8, !tbaa !9
  %32 = getelementptr inbounds %struct.irr_queue, ptr %31, i32 0, i32 0
  %33 = load i32, ptr %3, align 4, !tbaa !12
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [10 x i32], ptr %32, i64 0, i64 %34
  store i32 %28, ptr %35, align 4, !tbaa !12
  br label %36

36:                                               ; preds = %19
  %37 = load i32, ptr %3, align 4, !tbaa !12
  %38 = add nsw i32 %37, 1
  store i32 %38, ptr %3, align 4, !tbaa !12
  br label %10, !llvm.loop !15

39:                                               ; preds = %18
  %40 = load ptr, ptr @lapic, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.lapic, ptr %40, i32 0, i32 2
  %42 = load ptr, ptr %41, align 8, !tbaa !9
  %43 = getelementptr inbounds %struct.irr_queue, ptr %42, i32 0, i32 1
  %44 = load i32, ptr %43, align 4, !tbaa !13
  %45 = add nsw i32 %44, -1
  store i32 %45, ptr %43, align 4, !tbaa !13
  %46 = load ptr, ptr @lapic, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.lapic, ptr %46, i32 0, i32 2
  %48 = load ptr, ptr %47, align 8, !tbaa !9
  %49 = getelementptr inbounds %struct.irr_queue, ptr %48, i32 0, i32 1
  %50 = load i32, ptr %49, align 4, !tbaa !13
  %51 = icmp slt i32 %50, 9
  br i1 %51, label %52, label %55

52:                                               ; preds = %39
  %53 = load ptr, ptr @lapic, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.lapic, ptr %53, i32 0, i32 1
  store i32 0, ptr %54, align 8, !tbaa !17
  br label %55

55:                                               ; preds = %52, %39
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #2
  %56 = load i32, ptr %1, align 4
  ret i32 %56
}

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 = { 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 4104}
!10 = !{!"lapic", !7, i64 0, !11, i64 4096, !6, i64 4104}
!11 = !{!"int", !7, i64 0}
!12 = !{!11, !11, i64 0}
!13 = !{!14, !11, i64 40}
!14 = !{!"irr_queue", !7, i64 0, !11, i64 40}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!10, !11, i64 4096}


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

@lapic = external dso_local local_unnamed_addr global ptr, align 8

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local i32 @deq_irr() local_unnamed_addr #0 {
  %1 = load ptr, ptr @lapic, align 8, !tbaa !5
  %2 = getelementptr inbounds i8, ptr %1, i64 4104
  %3 = load ptr, ptr %2, align 8, !tbaa !9
  %4 = getelementptr inbounds i8, ptr %3, i64 40
  %5 = load i32, ptr %4, align 4, !tbaa !12
  %6 = icmp sgt i32 %5, 0
  br i1 %6, label %.lr.ph, label %._crit_edge.thread

._crit_edge.thread:                               ; preds = %0
  %7 = add nsw i32 %5, -1
  store i32 %7, ptr %4, align 4, !tbaa !12
  br label %16

._crit_edge:                                      ; preds = %.lr.ph
  %8 = add nsw i32 %13, -1
  store i32 %8, ptr %4, align 4, !tbaa !12
  %9 = icmp slt i32 %13, 10
  br i1 %9, label %16, label %18

.lr.ph:                                           ; preds = %0, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %0 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %10 = getelementptr inbounds [10 x i32], ptr %3, i64 0, i64 %indvars.iv.next
  %11 = load i32, ptr %10, align 4, !tbaa !14
  %12 = getelementptr inbounds [10 x i32], ptr %3, i64 0, i64 %indvars.iv
  store i32 %11, ptr %12, align 4, !tbaa !14
  %13 = load i32, ptr %4, align 4, !tbaa !12
  %14 = sext i32 %13 to i64
  %15 = icmp slt i64 %indvars.iv.next, %14
  br i1 %15, label %.lr.ph, label %._crit_edge, !llvm.loop !15

16:                                               ; preds = %._crit_edge.thread, %._crit_edge
  %17 = getelementptr inbounds i8, ptr %1, i64 4096
  store i32 0, ptr %17, align 8, !tbaa !17
  br label %18

18:                                               ; preds = %16, %._crit_edge
  ret i32 undef
}

attributes #0 = { nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 4104}
!10 = !{!"lapic", !7, i64 0, !11, i64 4096, !6, i64 4104}
!11 = !{!"int", !7, i64 0}
!12 = !{!13, !11, i64 40}
!13 = !{!"irr_queue", !7, i64 0, !11, i64 40}
!14 = !{!11, !11, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!10, !11, i64 4096}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass