; LLVM IR from narke/Aragveli :: src/kernel/arch/x86 (acpi)

; 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.AcpiMadt = type { %struct.AcpiHeader, i64, i64 }
%struct.AcpiHeader = type { i64, i64, i8, i8, [6 x i8], [8 x i8], i64, i64, i64 }
%struct.ApicHeader = type { i8, i8 }
%struct.ApicInterruptOverride = type <{ %struct.ApicHeader, i8, i8, i64, i16 }>

@g_madt = external hidden 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 zeroext i16 @AcpiRemapIrq(i16 noundef zeroext %0) #1 {
  %2 = alloca i16, align 2
  %3 = alloca i16, align 2
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i8, align 1
  %9 = alloca i8, align 1
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  store i16 %0, ptr %3, align 2, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  %12 = load ptr, ptr @g_madt, align 8, !tbaa !9
  store ptr %12, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %13 = load ptr, ptr %4, align 8, !tbaa !9
  %14 = getelementptr inbounds %struct.AcpiMadt, ptr %13, i64 1
  store ptr %14, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %15 = load ptr, ptr %4, align 8, !tbaa !9
  %16 = load ptr, ptr %4, align 8, !tbaa !9
  %17 = getelementptr inbounds %struct.AcpiMadt, ptr %16, i32 0, i32 0
  %18 = getelementptr inbounds %struct.AcpiHeader, ptr %17, i32 0, i32 1
  %19 = load i64, ptr %18, align 1, !tbaa !11
  %20 = getelementptr inbounds i8, ptr %15, i64 %19
  store ptr %20, ptr %6, align 8, !tbaa !9
  br label %21

21:                                               ; preds = %62, %1
  %22 = load ptr, ptr %5, align 8, !tbaa !9
  %23 = load ptr, ptr %6, align 8, !tbaa !9
  %24 = icmp ult ptr %22, %23
  br i1 %24, label %25, label %63

25:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  %26 = load ptr, ptr %5, align 8, !tbaa !9
  store ptr %26, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 1, ptr %8) #2
  %27 = load ptr, ptr %7, align 8, !tbaa !9
  %28 = getelementptr inbounds %struct.ApicHeader, ptr %27, i32 0, i32 0
  %29 = load i8, ptr %28, align 1, !tbaa !15
  store i8 %29, ptr %8, align 1, !tbaa !17
  call void @llvm.lifetime.start.p0(i64 1, ptr %9) #2
  %30 = load ptr, ptr %7, align 8, !tbaa !9
  %31 = getelementptr inbounds %struct.ApicHeader, ptr %30, i32 0, i32 1
  %32 = load i8, ptr %31, align 1, !tbaa !18
  store i8 %32, ptr %9, align 1, !tbaa !17
  %33 = load i8, ptr %8, align 1, !tbaa !17
  %34 = zext i8 %33 to i32
  %35 = icmp eq i32 %34, 2
  br i1 %35, label %36, label %54

36:                                               ; preds = %25
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  %37 = load ptr, ptr %5, align 8, !tbaa !9
  store ptr %37, ptr %10, align 8, !tbaa !9
  %38 = load ptr, ptr %10, align 8, !tbaa !9
  %39 = getelementptr inbounds %struct.ApicInterruptOverride, ptr %38, i32 0, i32 2
  %40 = load i8, ptr %39, align 1, !tbaa !19
  %41 = zext i8 %40 to i32
  %42 = load i16, ptr %3, align 2, !tbaa !5
  %43 = zext i16 %42 to i32
  %44 = icmp eq i32 %41, %43
  br i1 %44, label %45, label %50

45:                                               ; preds = %36
  %46 = load ptr, ptr %10, align 8, !tbaa !9
  %47 = getelementptr inbounds %struct.ApicInterruptOverride, ptr %46, i32 0, i32 3
  %48 = load i64, ptr %47, align 1, !tbaa !21
  %49 = trunc i64 %48 to i16
  store i16 %49, ptr %2, align 2
  store i32 1, ptr %11, align 4
  br label %51

50:                                               ; preds = %36
  store i32 0, ptr %11, align 4
  br label %51

51:                                               ; preds = %50, %45
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  %52 = load i32, ptr %11, align 4
  switch i32 %52, label %60 [
    i32 0, label %53
  ]

53:                                               ; preds = %51
  br label %54

54:                                               ; preds = %53, %25
  %55 = load i8, ptr %9, align 1, !tbaa !17
  %56 = zext i8 %55 to i32
  %57 = load ptr, ptr %5, align 8, !tbaa !9
  %58 = sext i32 %56 to i64
  %59 = getelementptr inbounds i8, ptr %57, i64 %58
  store ptr %59, ptr %5, align 8, !tbaa !9
  store i32 0, ptr %11, align 4
  br label %60

60:                                               ; preds = %54, %51
  call void @llvm.lifetime.end.p0(i64 1, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  %61 = load i32, ptr %11, align 4
  switch i32 %61, label %65 [
    i32 0, label %62
  ]

62:                                               ; preds = %60
  br label %21, !llvm.loop !22

63:                                               ; preds = %21
  %64 = load i16, ptr %3, align 2, !tbaa !5
  store i16 %64, ptr %2, align 2
  store i32 1, ptr %11, align 4
  br label %65

65:                                               ; preds = %63, %60
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  %66 = load i16, ptr %2, align 2
  ret i16 %66
}

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 = !{!"short", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !14, i64 8}
!12 = !{!"AcpiMadt", !13, i64 0, !14, i64 56, !14, i64 64}
!13 = !{!"AcpiHeader", !14, i64 0, !14, i64 8, !7, i64 16, !7, i64 17, !7, i64 18, !7, i64 24, !14, i64 32, !14, i64 40, !14, i64 48}
!14 = !{!"long", !7, i64 0}
!15 = !{!16, !7, i64 0}
!16 = !{!"ApicHeader", !7, i64 0, !7, i64 1}
!17 = !{!7, !7, i64 0}
!18 = !{!16, !7, i64 1}
!19 = !{!20, !7, i64 3}
!20 = !{!"ApicInterruptOverride", !16, i64 0, !7, i64 2, !7, i64 3, !14, i64 4, !6, i64 12}
!21 = !{!20, !14, i64 4}
!22 = distinct !{!22, !23}
!23 = !{!"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"

@g_madt = external hidden local_unnamed_addr global ptr, align 8

; Function Attrs: nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) uwtable
define dso_local zeroext i16 @AcpiRemapIrq(i16 noundef zeroext %0) local_unnamed_addr #0 {
  %2 = load ptr, ptr @g_madt, align 8, !tbaa !5
  %3 = getelementptr inbounds i8, ptr %2, i64 8
  %4 = load i64, ptr %3, align 1, !tbaa !9
  %5 = getelementptr inbounds i8, ptr %2, i64 %4
  %6 = icmp sgt i64 %4, 72
  br i1 %6, label %.lr.ph.preheader, label %.loopexit

.lr.ph.preheader:                                 ; preds = %1
  %7 = getelementptr inbounds i8, ptr %2, i64 72
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %19
  %.02129 = phi ptr [ %21, %19 ], [ %7, %.lr.ph.preheader ]
  %8 = load i8, ptr %.02129, align 1, !tbaa !13
  %9 = getelementptr inbounds i8, ptr %.02129, i64 1
  %10 = load i8, ptr %9, align 1, !tbaa !15
  %11 = icmp eq i8 %8, 2
  br i1 %11, label %12, label %19

12:                                               ; preds = %.lr.ph
  %13 = getelementptr inbounds i8, ptr %.02129, i64 3
  %14 = load i8, ptr %13, align 1, !tbaa !16
  %15 = zext i8 %14 to i16
  %.not = icmp eq i16 %15, %0
  br i1 %.not, label %.thread24, label %19

.thread24:                                        ; preds = %12
  %16 = getelementptr inbounds i8, ptr %.02129, i64 4
  %17 = load i64, ptr %16, align 1, !tbaa !19
  %18 = trunc i64 %17 to i16
  br label %.loopexit

19:                                               ; preds = %.lr.ph, %12
  %20 = zext i8 %10 to i64
  %21 = getelementptr inbounds i8, ptr %.02129, i64 %20
  %22 = icmp ult ptr %21, %5
  br i1 %22, label %.lr.ph, label %.loopexit, !llvm.loop !20

.loopexit:                                        ; preds = %19, %1, %.thread24
  %.4 = phi i16 [ %18, %.thread24 ], [ %0, %1 ], [ %0, %19 ]
  ret i16 %.4
}

attributes #0 = { nofree norecurse nosync nounwind memory(read, 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, !12, i64 8}
!10 = !{!"AcpiMadt", !11, i64 0, !12, i64 56, !12, i64 64}
!11 = !{!"AcpiHeader", !12, i64 0, !12, i64 8, !7, i64 16, !7, i64 17, !7, i64 18, !7, i64 24, !12, i64 32, !12, i64 40, !12, i64 48}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !7, i64 0}
!14 = !{!"ApicHeader", !7, i64 0, !7, i64 1}
!15 = !{!14, !7, i64 1}
!16 = !{!17, !7, i64 3}
!17 = !{!"ApicInterruptOverride", !14, i64 0, !7, i64 2, !7, i64 3, !12, i64 4, !18, i64 12}
!18 = !{!"short", !7, i64 0}
!19 = !{!17, !12, i64 4}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}


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