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

; 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
declare dso_local i32 @irq_is_valid(i32 noundef) #0

; Function Attrs: nounwind uwtable
define dso_local void @emulate_interrupt(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 0, ptr %4, align 4, !tbaa !9
  br label %5

5:                                                ; preds = %42, %1
  %6 = load i32, ptr %4, align 4, !tbaa !9
  %7 = load ptr, ptr @lapic, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.lapic, ptr %7, i32 0, i32 2
  %9 = load ptr, ptr %8, align 8, !tbaa !11
  %10 = getelementptr inbounds %struct.irr_queue, ptr %9, i32 0, i32 1
  %11 = load i32, ptr %10, align 4, !tbaa !13
  %12 = icmp slt i32 %6, %11
  br i1 %12, label %14, label %13

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

14:                                               ; preds = %5
  %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 !11
  %18 = getelementptr inbounds %struct.irr_queue, ptr %17, i32 0, i32 0
  %19 = load i32, ptr %4, align 4, !tbaa !9
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [10 x i32], ptr %18, i64 0, i64 %20
  %22 = load i32, ptr %21, align 4, !tbaa !9
  store i32 %22, ptr %3, align 4, !tbaa !9
  %23 = load i32, ptr %3, align 4, !tbaa !9
  %24 = call i32 @irq_is_valid(i32 noundef %23)
  %25 = icmp ne i32 %24, 0
  br i1 %25, label %28, label %26

26:                                               ; preds = %14
  %27 = call i32 @deq_irr()
  br label %42

28:                                               ; preds = %14
  %29 = load i32, ptr %3, align 4, !tbaa !9
  switch i32 %29, label %40 [
    i32 36, label %30
    i32 46, label %35
  ]

30:                                               ; preds = %28
  %31 = load ptr, ptr %2, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.vcpu, ptr %31, i32 0, i32 0
  %33 = load i32, ptr %32, align 8, !tbaa !15
  %34 = load i32, ptr %3, align 4, !tbaa !9
  call void @inject_interrupt(i32 noundef %33, i32 noundef %34)
  br label %41

35:                                               ; preds = %28
  %36 = load ptr, ptr %2, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.vcpu, ptr %36, i32 0, i32 0
  %38 = load i32, ptr %37, align 8, !tbaa !15
  %39 = load i32, ptr %3, align 4, !tbaa !9
  call void @inject_interrupt(i32 noundef %38, i32 noundef %39)
  br label %41

40:                                               ; preds = %28
  br label %42

41:                                               ; preds = %35, %30
  br label %42

42:                                               ; preds = %41, %40, %26
  %43 = load i32, ptr %4, align 4, !tbaa !9
  %44 = add nsw i32 %43, 1
  store i32 %44, ptr %4, align 4, !tbaa !9
  br label %5, !llvm.loop !23

45:                                               ; preds = %13
  %46 = load ptr, ptr %2, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.vcpu, ptr %46, i32 0, i32 1
  %48 = load ptr, ptr %47, align 8, !tbaa !25
  %49 = getelementptr inbounds %struct.kvm_run, ptr %48, i32 0, i32 0
  store i8 0, ptr %49, align 8, !tbaa !26
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #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: nounwind uwtable
declare dso_local void @inject_interrupt(i32 noundef, i32 noundef) #0

; 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 i32 @deq_irr() #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 = !{!"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, !6, i64 4104}
!12 = !{!"lapic", !7, i64 0, !10, i64 4096, !6, i64 4104}
!13 = !{!14, !10, i64 40}
!14 = !{!"irr_queue", !7, i64 0, !10, i64 40}
!15 = !{!16, !10, i64 0}
!16 = !{!"vcpu", !10, i64 0, !6, i64 8, !17, i64 16, !22, i64 328}
!17 = !{!"kvm_sregs", !18, i64 0, !18, i64 24, !18, i64 48, !18, i64 72, !18, i64 96, !18, i64 120, !18, i64 144, !18, i64 168, !21, i64 192, !21, i64 208, !19, i64 224, !19, i64 232, !19, i64 240, !19, i64 248, !19, i64 256, !19, i64 264, !19, i64 272, !7, i64 280}
!18 = !{!"kvm_segment", !19, i64 0, !10, i64 8, !20, 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}
!19 = !{!"long long", !7, i64 0}
!20 = !{!"short", !7, i64 0}
!21 = !{!"kvm_dtable", !19, i64 0, !20, i64 8, !7, i64 10}
!22 = !{!"kvm_regs", !19, i64 0, !19, i64 8, !19, i64 16, !19, i64 24, !19, i64 32, !19, i64 40, !19, i64 48, !19, i64 56, !19, i64 64, !19, i64 72, !19, i64 80, !19, i64 88, !19, i64 96, !19, i64 104, !19, i64 112, !19, i64 120, !19, i64 128, !19, i64 136}
!23 = distinct !{!23, !24}
!24 = !{!"llvm.loop.mustprogress"}
!25 = !{!16, !6, i64 8}
!26 = !{!27, !7, i64 0}
!27 = !{!"kvm_run", !7, i64 0, !7, i64 1, !7, i64 2, !10, i64 8, !7, i64 12, !7, i64 13, !20, i64 14, !19, i64 16, !19, i64 24, !7, i64 32, !19, i64 288, !19, 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: nounwind uwtable
declare dso_local i32 @irq_is_valid(i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @emulate_interrupt(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
  %2 = load ptr, ptr @lapic, align 8, !tbaa !5
  %3 = getelementptr inbounds i8, ptr %2, i64 4104
  %4 = load ptr, ptr %3, align 8, !tbaa !9
  %5 = getelementptr inbounds i8, ptr %4, i64 40
  %6 = load i32, ptr %5, align 4, !tbaa !12
  %7 = icmp sgt i32 %6, 0
  br i1 %7, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %21, %1
  %8 = getelementptr inbounds i8, ptr %0, i64 8
  %9 = load ptr, ptr %8, align 8, !tbaa !14
  store i8 0, ptr %9, align 8, !tbaa !22
  ret void

.lr.ph:                                           ; preds = %1, %21
  %indvars.iv = phi i64 [ %indvars.iv.next, %21 ], [ 0, %1 ]
  %10 = phi ptr [ %24, %21 ], [ %4, %1 ]
  %11 = getelementptr inbounds [10 x i32], ptr %10, i64 0, i64 %indvars.iv
  %12 = load i32, ptr %11, align 4, !tbaa !24
  %13 = tail call i32 @irq_is_valid(i32 noundef %12)
  %.not = icmp eq i32 %13, 0
  br i1 %.not, label %14, label %16

14:                                               ; preds = %.lr.ph
  %15 = tail call i32 @deq_irr()
  br label %21

16:                                               ; preds = %.lr.ph
  switch i32 %12, label %21 [
    i32 36, label %17
    i32 46, label %19
  ]

17:                                               ; preds = %16
  %18 = load i32, ptr %0, align 8, !tbaa !25
  tail call void @inject_interrupt(i32 noundef %18, i32 noundef 36)
  br label %21

19:                                               ; preds = %16
  %20 = load i32, ptr %0, align 8, !tbaa !25
  tail call void @inject_interrupt(i32 noundef %20, i32 noundef 46)
  br label %21

21:                                               ; preds = %17, %19, %16, %14
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %22 = load ptr, ptr @lapic, align 8, !tbaa !5
  %23 = getelementptr inbounds i8, ptr %22, i64 4104
  %24 = load ptr, ptr %23, align 8, !tbaa !9
  %25 = getelementptr inbounds i8, ptr %24, i64 40
  %26 = load i32, ptr %25, align 4, !tbaa !12
  %27 = sext i32 %26 to i64
  %28 = icmp slt i64 %indvars.iv.next, %27
  br i1 %28, label %.lr.ph, label %._crit_edge, !llvm.loop !26
}

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @deq_irr() local_unnamed_addr #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" }

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


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass