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

; 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.x86_idt_entry = type { i16, i16, i16, i16 }

@idt_array = external hidden global [256 x %struct.x86_idt_entry], align 16

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

; Function Attrs: nounwind uwtable
define dso_local i32 @x86_idt_set_handler(i64 noundef %0, i64 noundef %1) #1 {
  %3 = alloca i32, align 4
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  store i64 %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !5
  %7 = load i64, ptr %4, align 8, !tbaa !5
  %8 = icmp uge i64 %7, 256
  br i1 %8, label %9, label %10

9:                                                ; preds = %2
  store i32 -1, ptr %3, align 4
  br label %53

10:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %11 = load i64, ptr %4, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.x86_idt_entry, ptr @idt_array, i64 %11
  store ptr %12, ptr %6, align 8, !tbaa !9
  %13 = load i64, ptr %5, align 8, !tbaa !5
  %14 = icmp ne i64 %13, 0
  br i1 %14, label %15, label %37

15:                                               ; preds = %10
  %16 = load i64, ptr %5, align 8, !tbaa !5
  %17 = and i64 %16, 65535
  %18 = trunc i64 %17 to i16
  %19 = load ptr, ptr %6, align 8, !tbaa !9
  %20 = getelementptr inbounds %struct.x86_idt_entry, ptr %19, i32 0, i32 0
  store i16 %18, ptr %20, align 1, !tbaa !11
  %21 = load i64, ptr %5, align 8, !tbaa !5
  %22 = lshr i64 %21, 16
  %23 = and i64 %22, 65535
  %24 = trunc i64 %23 to i16
  %25 = load ptr, ptr %6, align 8, !tbaa !9
  %26 = getelementptr inbounds %struct.x86_idt_entry, ptr %25, i32 0, i32 3
  store i16 %24, ptr %26, align 1, !tbaa !14
  %27 = load ptr, ptr %6, align 8, !tbaa !9
  %28 = getelementptr inbounds %struct.x86_idt_entry, ptr %27, i32 0, i32 2
  %29 = load i16, ptr %28, align 1
  %30 = and i16 %29, -24577
  %31 = or i16 %30, 0
  store i16 %31, ptr %28, align 1
  %32 = load ptr, ptr %6, align 8, !tbaa !9
  %33 = getelementptr inbounds %struct.x86_idt_entry, ptr %32, i32 0, i32 2
  %34 = load i16, ptr %33, align 1
  %35 = and i16 %34, 32767
  %36 = or i16 %35, -32768
  store i16 %36, ptr %33, align 1
  br label %52

37:                                               ; preds = %10
  %38 = load ptr, ptr %6, align 8, !tbaa !9
  %39 = getelementptr inbounds %struct.x86_idt_entry, ptr %38, i32 0, i32 0
  store i16 0, ptr %39, align 1, !tbaa !11
  %40 = load ptr, ptr %6, align 8, !tbaa !9
  %41 = getelementptr inbounds %struct.x86_idt_entry, ptr %40, i32 0, i32 3
  store i16 0, ptr %41, align 1, !tbaa !14
  %42 = load ptr, ptr %6, align 8, !tbaa !9
  %43 = getelementptr inbounds %struct.x86_idt_entry, ptr %42, i32 0, i32 2
  %44 = load i16, ptr %43, align 1
  %45 = and i16 %44, -24577
  %46 = or i16 %45, 0
  store i16 %46, ptr %43, align 1
  %47 = load ptr, ptr %6, align 8, !tbaa !9
  %48 = getelementptr inbounds %struct.x86_idt_entry, ptr %47, i32 0, i32 2
  %49 = load i16, ptr %48, align 1
  %50 = and i16 %49, 32767
  %51 = or i16 %50, 0
  store i16 %51, ptr %48, align 1
  br label %52

52:                                               ; preds = %37, %15
  store i32 0, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  br label %53

53:                                               ; preds = %52, %9
  %54 = load i32, ptr %3, align 4
  ret i32 %54
}

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

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 = !{!"long", !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, !13, i64 0}
!12 = !{!"x86_idt_entry", !13, i64 0, !13, i64 2, !7, i64 4, !7, i64 5, !7, i64 5, !7, i64 5, !7, i64 5, !13, i64 6}
!13 = !{!"short", !7, i64 0}
!14 = !{!12, !13, i64 6}


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

%struct.x86_idt_entry = type { i16, i16, i16, i16 }

@idt_array = external hidden local_unnamed_addr global [256 x %struct.x86_idt_entry], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local range(i32 -1, 1) i32 @x86_idt_set_handler(i64 noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = icmp ugt i64 %0, 255
  br i1 %3, label %20, label %4

4:                                                ; preds = %2
  %5 = getelementptr inbounds %struct.x86_idt_entry, ptr @idt_array, i64 %0
  %.not = icmp eq i64 %1, 0
  br i1 %.not, label %15, label %6

6:                                                ; preds = %4
  %7 = trunc i64 %1 to i16
  store i16 %7, ptr %5, align 8, !tbaa !5
  %8 = lshr i64 %1, 16
  %9 = trunc i64 %8 to i16
  %10 = getelementptr inbounds i8, ptr %5, i64 6
  store i16 %9, ptr %10, align 2, !tbaa !10
  %11 = getelementptr inbounds i8, ptr %5, i64 4
  %12 = load i16, ptr %11, align 4
  %13 = and i16 %12, 8191
  %14 = or disjoint i16 %13, -32768
  store i16 %14, ptr %11, align 4
  br label %20

15:                                               ; preds = %4
  store i16 0, ptr %5, align 8, !tbaa !5
  %16 = getelementptr inbounds i8, ptr %5, i64 6
  store i16 0, ptr %16, align 2, !tbaa !10
  %17 = getelementptr inbounds i8, ptr %5, i64 4
  %18 = load i16, ptr %17, align 4
  %19 = and i16 %18, 8191
  store i16 %19, ptr %17, align 4
  br label %20

20:                                               ; preds = %6, %15, %2
  %.0 = phi i32 [ -1, %2 ], [ 0, %15 ], [ 0, %6 ]
  ret i32 %.0
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, argmem: none, 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, !7, i64 0}
!6 = !{!"x86_idt_entry", !7, i64 0, !7, i64 2, !8, i64 4, !8, i64 5, !8, i64 5, !8, i64 5, !8, i64 5, !7, i64 6}
!7 = !{!"short", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!6, !7, i64 6}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass