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

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

; Function Attrs: nounwind uwtable
define dso_local void @x86_idt_setup() #0 {
  %1 = alloca %struct.x86_idtr, align 8
  %2 = alloca i16, align 2
  %3 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 16, ptr %1) #2
  call void @llvm.lifetime.start.p0(i64 2, ptr %2) #2
  store i16 0, ptr %2, align 2, !tbaa !5
  br label %4

4:                                                ; preds = %34, %0
  %5 = load i16, ptr %2, align 2, !tbaa !5
  %6 = zext i16 %5 to i32
  %7 = icmp slt i32 %6, 256
  br i1 %7, label %9, label %8

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

9:                                                ; preds = %4
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  %10 = load i16, ptr %2, align 2, !tbaa !5
  %11 = zext i16 %10 to i32
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds %struct.x86_idt_entry, ptr @idt_array, i64 %12
  store ptr %13, ptr %3, align 8, !tbaa !9
  %14 = load ptr, ptr %3, align 8, !tbaa !9
  %15 = getelementptr inbounds %struct.x86_idt_entry, ptr %14, i32 0, i32 1
  store i16 8, ptr %15, align 1, !tbaa !11
  %16 = load ptr, ptr %3, align 8, !tbaa !9
  %17 = getelementptr inbounds %struct.x86_idt_entry, ptr %16, i32 0, i32 2
  %18 = load i16, ptr %17, align 1
  %19 = and i16 %18, -256
  %20 = or i16 %19, 0
  store i16 %20, ptr %17, align 1
  %21 = load ptr, ptr %3, align 8, !tbaa !9
  %22 = getelementptr inbounds %struct.x86_idt_entry, ptr %21, i32 0, i32 2
  %23 = load i16, ptr %22, align 1
  %24 = and i16 %23, -3841
  %25 = or i16 %24, 3584
  store i16 %25, ptr %22, align 1
  %26 = load ptr, ptr %3, align 8, !tbaa !9
  %27 = getelementptr inbounds %struct.x86_idt_entry, ptr %26, i32 0, i32 2
  %28 = load i16, ptr %27, align 1
  %29 = and i16 %28, -4097
  %30 = or i16 %29, 0
  store i16 %30, ptr %27, align 1
  %31 = load i16, ptr %2, align 2, !tbaa !5
  %32 = zext i16 %31 to i64
  %33 = call i32 @x86_idt_set_handler(i64 noundef %32, i64 noundef 0)
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  br label %34

34:                                               ; preds = %9
  %35 = load i16, ptr %2, align 2, !tbaa !5
  %36 = add i16 %35, 1
  store i16 %36, ptr %2, align 2, !tbaa !5
  br label %4, !llvm.loop !13

37:                                               ; preds = %8
  %38 = getelementptr inbounds %struct.x86_idtr, ptr %1, i32 0, i32 1
  store i64 ptrtoint (ptr @idt_array to i64), ptr %38, align 2, !tbaa !15
  %39 = getelementptr inbounds %struct.x86_idtr, ptr %1, i32 0, i32 0
  store i16 2047, ptr %39, align 8, !tbaa !18
  call void asm sideeffect "lidt $0\0A", "*m,~{memory},~{dirflag},~{fpsr},~{flags}"(ptr elementtype(%struct.x86_idtr) %1) #2, !srcloc !19
  call void @llvm.lifetime.end.p0(i64 16, ptr %1) #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 i32 @x86_idt_set_handler(i64 noundef, i64 noundef) #0

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

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 = !{!"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, !6, i64 2}
!12 = !{!"x86_idt_entry", !6, i64 0, !6, i64 2, !7, i64 4, !7, i64 5, !7, i64 5, !7, i64 5, !7, i64 5, !6, i64 6}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!16, !17, i64 2}
!16 = !{!"x86_idtr", !6, i64 0, !17, i64 2}
!17 = !{!"long", !7, i64 0}
!18 = !{!16, !6, i64 0}
!19 = !{i64 1952}


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

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

; Function Attrs: nounwind uwtable
define dso_local void @x86_idt_setup() local_unnamed_addr #0 {
  %1 = alloca %struct.x86_idtr, align 8
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %1) #2
  br label %4

2:                                                ; preds = %4
  %3 = getelementptr inbounds i8, ptr %1, i64 2
  store i64 ptrtoint (ptr @idt_array to i64), ptr %3, align 2, !tbaa !5
  store i16 2047, ptr %1, align 8, !tbaa !11
  call void asm sideeffect "lidt $0\0A", "*m,~{memory},~{dirflag},~{fpsr},~{flags}"(ptr nonnull elementtype(%struct.x86_idtr) %1) #2, !srcloc !12
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %1) #2
  ret void

4:                                                ; preds = %0, %4
  %indvars.iv = phi i64 [ 0, %0 ], [ %indvars.iv.next, %4 ]
  %5 = getelementptr inbounds %struct.x86_idt_entry, ptr @idt_array, i64 %indvars.iv
  %6 = getelementptr inbounds i8, ptr %5, i64 2
  store i16 8, ptr %6, align 2, !tbaa !13
  %7 = getelementptr inbounds i8, ptr %5, i64 4
  %8 = load i16, ptr %7, align 4
  %9 = and i16 %8, -8192
  %10 = or disjoint i16 %9, 3584
  store i16 %10, ptr %7, align 4
  %11 = tail call i32 @x86_idt_set_handler(i64 noundef %indvars.iv, i64 noundef 0)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, 256
  br i1 %exitcond.not, label %2, label %4, !llvm.loop !15
}

; Function Attrs: mustprogress 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 i32 @x86_idt_set_handler(i64 noundef, i64 noundef) local_unnamed_addr #0

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

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 = { mustprogress 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, !10, i64 2}
!6 = !{!"x86_idtr", !7, i64 0, !10, i64 2}
!7 = !{!"short", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!6, !7, i64 0}
!12 = !{i64 1952}
!13 = !{!14, !7, i64 2}
!14 = !{!"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}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,SROAPass,SimplifyCFGPass,TailCallElimPass