; LLVM IR from tomoyasuzuki/kvmm :: lapic (enq_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 }
%struct.vcpu = type { i32, ptr, %struct.kvm_sregs, %struct.kvm_regs }
%struct.kvm_sregs = type { %struct.kvm_segment, %struct.kvm_segment, %struct.kvm_segment, %struct.kvm_segment, %struct.kvm_segment, %struct.kvm_segment, %struct.kvm_segment, %struct.kvm_segment, %struct.kvm_dtable, %struct.kvm_dtable, i64, i64, i64, i64, i64, i64, i64, [4 x i64] }
%struct.kvm_segment = type { i64, i32, i16, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8 }
%struct.kvm_dtable = type { i64, i16, [3 x i16] }
%struct.kvm_regs = type { i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64 }
%struct.kvm_run = type { i8, i8, [6 x i8], i32, i8, i8, i16, i64, i64, %union.anon, i64, i64, %union.anon.27 }
%union.anon = type { %struct.anon.12 }
%struct.anon.12 = type { [32 x i64] }
%union.anon.27 = type { %struct.kvm_sync_regs, [1528 x i8] }
%struct.kvm_sync_regs = type { %struct.kvm_regs, %struct.kvm_sregs, %struct.kvm_vcpu_events }
%struct.kvm_vcpu_events = type { %struct.anon.28, %struct.anon.29, %struct.anon.30, i32, i32, %struct.anon.31, [27 x i8], i8, i64 }
%struct.anon.28 = type { i8, i8, i8, i8, i32 }
%struct.anon.29 = type { i8, i8, i8, i8 }
%struct.anon.30 = type { i8, i8, i8, i8 }
%struct.anon.31 = type { i8, i8, i8, i8 }

@lapic = external dso_local global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local void @enq_irr(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  %5 = load ptr, ptr @lapic, align 8, !tbaa !5
  %6 = getelementptr inbounds %struct.lapic, ptr %5, i32 0, i32 1
  %7 = load i32, ptr %6, align 8, !tbaa !11
  %8 = icmp ne i32 %7, 0
  br i1 %8, label %28, label %9

9:                                                ; preds = %2
  %10 = load i32, ptr %4, align 4, !tbaa !9
  %11 = load ptr, ptr @lapic, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.lapic, ptr %11, i32 0, i32 2
  %13 = load ptr, ptr %12, align 8, !tbaa !13
  %14 = getelementptr inbounds %struct.irr_queue, ptr %13, i32 0, i32 0
  %15 = load ptr, ptr @lapic, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.lapic, ptr %15, i32 0, i32 2
  %17 = load ptr, ptr %16, align 8, !tbaa !13
  %18 = getelementptr inbounds %struct.irr_queue, ptr %17, i32 0, i32 1
  %19 = load i32, ptr %18, align 4, !tbaa !14
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [10 x i32], ptr %14, i64 0, i64 %20
  store i32 %10, ptr %21, align 4, !tbaa !9
  %22 = load ptr, ptr @lapic, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.lapic, ptr %22, i32 0, i32 2
  %24 = load ptr, ptr %23, align 8, !tbaa !13
  %25 = getelementptr inbounds %struct.irr_queue, ptr %24, i32 0, i32 1
  %26 = load i32, ptr %25, align 4, !tbaa !14
  %27 = add nsw i32 %26, 1
  store i32 %27, ptr %25, align 4, !tbaa !14
  br label %28

28:                                               ; preds = %9, %2
  %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 !13
  %32 = getelementptr inbounds %struct.irr_queue, ptr %31, i32 0, i32 1
  %33 = load i32, ptr %32, align 4, !tbaa !14
  %34 = icmp sge i32 %33, 9
  br i1 %34, label %35, label %38

35:                                               ; preds = %28
  %36 = load ptr, ptr @lapic, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.lapic, ptr %36, i32 0, i32 1
  store i32 1, ptr %37, align 8, !tbaa !11
  br label %38

38:                                               ; preds = %35, %28
  %39 = load ptr, ptr %3, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.vcpu, ptr %39, i32 0, i32 1
  %41 = load ptr, ptr %40, align 8, !tbaa !16
  %42 = getelementptr inbounds %struct.kvm_run, ptr %41, i32 0, i32 0
  store i8 1, ptr %42, align 8, !tbaa !24
  ret void
}

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

!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, !10, i64 4096}
!12 = !{!"lapic", !7, i64 0, !10, i64 4096, !6, i64 4104}
!13 = !{!12, !6, i64 4104}
!14 = !{!15, !10, i64 40}
!15 = !{!"irr_queue", !7, i64 0, !10, i64 40}
!16 = !{!17, !6, i64 8}
!17 = !{!"vcpu", !10, i64 0, !6, i64 8, !18, i64 16, !23, i64 328}
!18 = !{!"kvm_sregs", !19, i64 0, !19, i64 24, !19, i64 48, !19, i64 72, !19, i64 96, !19, i64 120, !19, i64 144, !19, i64 168, !22, i64 192, !22, i64 208, !20, i64 224, !20, i64 232, !20, i64 240, !20, i64 248, !20, i64 256, !20, i64 264, !20, i64 272, !7, i64 280}
!19 = !{!"kvm_segment", !20, i64 0, !10, i64 8, !21, i64 12, !7, i64 14, !7, i64 15, !7, i64 16, !7, i64 17, !7, i64 18, !7, i64 19, !7, i64 20, !7, i64 21, !7, i64 22, !7, i64 23}
!20 = !{!"long long", !7, i64 0}
!21 = !{!"short", !7, i64 0}
!22 = !{!"kvm_dtable", !20, i64 0, !21, i64 8, !7, i64 10}
!23 = !{!"kvm_regs", !20, i64 0, !20, i64 8, !20, i64 16, !20, i64 24, !20, i64 32, !20, i64 40, !20, i64 48, !20, i64 56, !20, i64 64, !20, i64 72, !20, i64 80, !20, i64 88, !20, i64 96, !20, i64 104, !20, i64 112, !20, i64 120, !20, i64 128, !20, i64 136}
!24 = !{!25, !7, i64 0}
!25 = !{!"kvm_run", !7, i64 0, !7, i64 1, !7, i64 2, !10, i64 8, !7, i64 12, !7, i64 13, !21, i64 14, !20, i64 16, !20, i64 24, !7, i64 32, !20, i64 288, !20, i64 296, !7, i64 304}


; 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: mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @enq_irr(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = load ptr, ptr @lapic, align 8, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %3, i64 4096
  %5 = load i32, ptr %4, align 8, !tbaa !9
  %.not = icmp eq i32 %5, 0
  %6 = getelementptr inbounds i8, ptr %3, i64 4104
  %7 = load ptr, ptr %6, align 8, !tbaa !12
  %8 = getelementptr inbounds i8, ptr %7, i64 40
  %9 = load i32, ptr %8, align 4, !tbaa !13
  br i1 %.not, label %10, label %._crit_edge

10:                                               ; preds = %2
  %11 = sext i32 %9 to i64
  %12 = getelementptr inbounds [10 x i32], ptr %7, i64 0, i64 %11
  store i32 %1, ptr %12, align 4, !tbaa !15
  %13 = load i32, ptr %8, align 4, !tbaa !13
  %14 = add nsw i32 %13, 1
  store i32 %14, ptr %8, align 4, !tbaa !13
  br label %._crit_edge

._crit_edge:                                      ; preds = %2, %10
  %15 = phi i32 [ %14, %10 ], [ %9, %2 ]
  %16 = icmp sgt i32 %15, 8
  br i1 %16, label %17, label %18

17:                                               ; preds = %._crit_edge
  store i32 1, ptr %4, align 8, !tbaa !9
  br label %18

18:                                               ; preds = %17, %._crit_edge
  %19 = getelementptr inbounds i8, ptr %0, i64 8
  %20 = load ptr, ptr %19, align 8, !tbaa !16
  store i8 1, ptr %20, align 8, !tbaa !24
  ret void
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn 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, !11, i64 4096}
!10 = !{!"lapic", !7, i64 0, !11, i64 4096, !6, i64 4104}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !6, i64 4104}
!13 = !{!14, !11, i64 40}
!14 = !{!"irr_queue", !7, i64 0, !11, i64 40}
!15 = !{!11, !11, i64 0}
!16 = !{!17, !6, i64 8}
!17 = !{!"vcpu", !11, i64 0, !6, i64 8, !18, i64 16, !23, i64 328}
!18 = !{!"kvm_sregs", !19, i64 0, !19, i64 24, !19, i64 48, !19, i64 72, !19, i64 96, !19, i64 120, !19, i64 144, !19, i64 168, !22, i64 192, !22, i64 208, !20, i64 224, !20, i64 232, !20, i64 240, !20, i64 248, !20, i64 256, !20, i64 264, !20, i64 272, !7, i64 280}
!19 = !{!"kvm_segment", !20, i64 0, !11, i64 8, !21, i64 12, !7, i64 14, !7, i64 15, !7, i64 16, !7, i64 17, !7, i64 18, !7, i64 19, !7, i64 20, !7, i64 21, !7, i64 22, !7, i64 23}
!20 = !{!"long long", !7, i64 0}
!21 = !{!"short", !7, i64 0}
!22 = !{!"kvm_dtable", !20, i64 0, !21, i64 8, !7, i64 10}
!23 = !{!"kvm_regs", !20, i64 0, !20, i64 8, !20, i64 16, !20, i64 24, !20, i64 32, !20, i64 40, !20, i64 48, !20, i64 56, !20, i64 64, !20, i64 72, !20, i64 80, !20, i64 88, !20, i64 96, !20, i64 104, !20, i64 112, !20, i64 120, !20, i64 128, !20, i64 136}
!24 = !{!25, !7, i64 0}
!25 = !{!"kvm_run", !7, i64 0, !7, i64 1, !7, i64 2, !11, i64 8, !7, i64 12, !7, i64 13, !21, i64 14, !20, i64 16, !20, i64 24, !7, i64 32, !20, i64 288, !20, i64 296, !7, i64 304}


; Effective O3 passes: BDCEPass,EarlyCSEPass,GVNPass,GlobalOptPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass