; LLVM IR from narke/Aragveli :: src/kernel/arch/x86 (cpu-context)

; 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.cpu_kstate = type { %struct.cpu_state }
%struct.cpu_state = type <{ i16, i16, i16, i16, i16, i16, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64 }>

; Function Attrs: nounwind uwtable
define dso_local void @cpu_kstate_init(ptr noundef %0, ptr noundef %1, i64 noundef %2, i64 noundef %3, i64 noundef %4, ptr noundef %5, i64 noundef %6) #0 {
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  %12 = alloca i64, align 8
  %13 = alloca ptr, align 8
  %14 = alloca i64, align 8
  %15 = alloca ptr, align 8
  %16 = alloca i64, align 8
  %17 = alloca ptr, align 8
  store ptr %0, ptr %8, align 8, !tbaa !5
  store ptr %1, ptr %9, align 8, !tbaa !5
  store i64 %2, ptr %10, align 8, !tbaa !9
  store i64 %3, ptr %11, align 8, !tbaa !9
  store i64 %4, ptr %12, align 8, !tbaa !9
  store ptr %5, ptr %13, align 8, !tbaa !5
  store i64 %6, ptr %14, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #4
  %18 = load i64, ptr %11, align 8, !tbaa !9
  %19 = load i64, ptr %12, align 8, !tbaa !9
  %20 = add i64 %18, %19
  store i64 %20, ptr %16, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %17) #4
  %21 = load i64, ptr %16, align 8, !tbaa !9
  %22 = inttoptr i64 %21 to ptr
  store ptr %22, ptr %17, align 8, !tbaa !5
  %23 = load i64, ptr %14, align 8, !tbaa !9
  %24 = load ptr, ptr %17, align 8, !tbaa !5
  %25 = getelementptr inbounds i64, ptr %24, i32 -1
  store ptr %25, ptr %17, align 8, !tbaa !5
  store i64 %23, ptr %25, align 8, !tbaa !9
  %26 = load ptr, ptr %13, align 8, !tbaa !5
  %27 = ptrtoint ptr %26 to i64
  %28 = load ptr, ptr %17, align 8, !tbaa !5
  %29 = getelementptr inbounds i64, ptr %28, i32 -1
  store ptr %29, ptr %17, align 8, !tbaa !5
  store i64 %27, ptr %29, align 8, !tbaa !9
  %30 = load i64, ptr %10, align 8, !tbaa !9
  %31 = load ptr, ptr %17, align 8, !tbaa !5
  %32 = getelementptr inbounds i64, ptr %31, i32 -1
  store ptr %32, ptr %17, align 8, !tbaa !5
  store i64 %30, ptr %32, align 8, !tbaa !9
  %33 = load ptr, ptr %9, align 8, !tbaa !5
  %34 = ptrtoint ptr %33 to i64
  %35 = load ptr, ptr %17, align 8, !tbaa !5
  %36 = getelementptr inbounds i64, ptr %35, i32 -1
  store ptr %36, ptr %17, align 8, !tbaa !5
  store i64 %34, ptr %36, align 8, !tbaa !9
  %37 = load ptr, ptr %17, align 8, !tbaa !5
  %38 = getelementptr inbounds i64, ptr %37, i32 -1
  store ptr %38, ptr %17, align 8, !tbaa !5
  store i64 0, ptr %38, align 8, !tbaa !9
  %39 = load ptr, ptr %17, align 8, !tbaa !5
  %40 = ptrtoint ptr %39 to i64
  %41 = sub i64 %40, 100
  store i64 %41, ptr %16, align 8, !tbaa !9
  %42 = load i64, ptr %16, align 8, !tbaa !9
  %43 = inttoptr i64 %42 to ptr
  store ptr %43, ptr %15, align 8, !tbaa !5
  %44 = load ptr, ptr %15, align 8, !tbaa !5
  call void @llvm.memset.p0.i64(ptr align 1 %44, i8 0, i64 100, i1 false)
  %45 = load ptr, ptr %15, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.cpu_kstate, ptr %45, i32 0, i32 0
  %47 = getelementptr inbounds %struct.cpu_state, ptr %46, i32 0, i32 14
  store i64 ptrtoint (ptr @core_routine to i64), ptr %47, align 1, !tbaa !11
  %48 = load ptr, ptr %15, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.cpu_kstate, ptr %48, i32 0, i32 0
  %50 = getelementptr inbounds %struct.cpu_state, ptr %49, i32 0, i32 15
  store i64 8, ptr %50, align 1, !tbaa !15
  %51 = load ptr, ptr %15, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.cpu_kstate, ptr %51, i32 0, i32 0
  %53 = getelementptr inbounds %struct.cpu_state, ptr %52, i32 0, i32 3
  store i16 16, ptr %53, align 1, !tbaa !16
  %54 = load ptr, ptr %15, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.cpu_kstate, ptr %54, i32 0, i32 0
  %56 = getelementptr inbounds %struct.cpu_state, ptr %55, i32 0, i32 2
  store i16 16, ptr %56, align 1, !tbaa !17
  %57 = load ptr, ptr %15, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.cpu_kstate, ptr %57, i32 0, i32 0
  %59 = getelementptr inbounds %struct.cpu_state, ptr %58, i32 0, i32 4
  store i16 16, ptr %59, align 1, !tbaa !18
  %60 = load ptr, ptr %15, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.cpu_kstate, ptr %60, i32 0, i32 0
  %62 = getelementptr inbounds %struct.cpu_state, ptr %61, i32 0, i32 16
  store i64 512, ptr %62, align 1, !tbaa !19
  %63 = load ptr, ptr %15, align 8, !tbaa !5
  %64 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %63, ptr %64, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %17) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #4
  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: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

; Function Attrs: noreturn nounwind uwtable
declare hidden void @core_routine(ptr noundef, i64 noundef, ptr noundef, i64 noundef) #3

; 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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { noreturn 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 #4 = { 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 = !{!"long", !7, i64 0}
!11 = !{!12, !10, i64 76}
!12 = !{!"cpu_kstate", !13, i64 0}
!13 = !{!"cpu_state", !14, i64 0, !14, i64 2, !14, i64 4, !14, i64 6, !14, i64 8, !14, i64 10, !10, i64 12, !10, i64 20, !10, i64 28, !10, i64 36, !10, i64 44, !10, i64 52, !10, i64 60, !10, i64 68, !10, i64 76, !10, i64 84, !10, i64 92}
!14 = !{!"short", !7, i64 0}
!15 = !{!12, !10, i64 84}
!16 = !{!12, !14, i64 6}
!17 = !{!12, !14, i64 4}
!18 = !{!12, !14, i64 8}
!19 = !{!12, !10, i64 92}


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

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(write, inaccessiblemem: none) uwtable
define dso_local void @cpu_kstate_init(ptr nocapture noundef writeonly %0, ptr noundef %1, i64 noundef %2, i64 noundef %3, i64 noundef %4, ptr noundef %5, i64 noundef %6) local_unnamed_addr #0 {
  %8 = add i64 %4, %3
  %9 = inttoptr i64 %8 to ptr
  %10 = getelementptr inbounds i8, ptr %9, i64 -8
  store i64 %6, ptr %10, align 8, !tbaa !5
  %11 = ptrtoint ptr %5 to i64
  %12 = getelementptr inbounds i8, ptr %9, i64 -16
  store i64 %11, ptr %12, align 8, !tbaa !5
  %13 = getelementptr inbounds i8, ptr %9, i64 -24
  store i64 %2, ptr %13, align 8, !tbaa !5
  %14 = ptrtoint ptr %1 to i64
  %15 = getelementptr inbounds i8, ptr %9, i64 -32
  store i64 %14, ptr %15, align 8, !tbaa !5
  %16 = getelementptr inbounds i8, ptr %9, i64 -40
  store i64 0, ptr %16, align 8, !tbaa !5
  %17 = ptrtoint ptr %16 to i64
  %18 = add i64 %17, -100
  %19 = inttoptr i64 %18 to ptr
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 1 dereferenceable(100) %19, i8 0, i64 76, i1 false)
  %20 = getelementptr inbounds i8, ptr %19, i64 76
  store i64 ptrtoint (ptr @core_routine to i64), ptr %20, align 1, !tbaa !9
  %21 = getelementptr inbounds i8, ptr %19, i64 84
  store i64 8, ptr %21, align 1, !tbaa !13
  %22 = getelementptr inbounds i8, ptr %19, i64 6
  store i16 16, ptr %22, align 1, !tbaa !14
  %23 = getelementptr inbounds i8, ptr %19, i64 4
  store i16 16, ptr %23, align 1, !tbaa !15
  %24 = getelementptr inbounds i8, ptr %19, i64 8
  store i16 16, ptr %24, align 1, !tbaa !16
  %25 = getelementptr inbounds i8, ptr %19, i64 92
  store i64 512, ptr %25, align 1, !tbaa !17
  store ptr %19, ptr %0, align 8, !tbaa !18
  ret void
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

; Function Attrs: noreturn nounwind uwtable
declare hidden void @core_routine(ptr noundef, i64 noundef, ptr noundef, i64 noundef) #2

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(write, 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" }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { noreturn 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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 76}
!10 = !{!"cpu_kstate", !11, i64 0}
!11 = !{!"cpu_state", !12, i64 0, !12, i64 2, !12, i64 4, !12, i64 6, !12, i64 8, !12, i64 10, !6, i64 12, !6, i64 20, !6, i64 28, !6, i64 36, !6, i64 44, !6, i64 52, !6, i64 60, !6, i64 68, !6, i64 76, !6, i64 84, !6, i64 92}
!12 = !{!"short", !7, i64 0}
!13 = !{!10, !6, i64 84}
!14 = !{!10, !12, i64 6}
!15 = !{!10, !12, i64 4}
!16 = !{!10, !12, i64 8}
!17 = !{!10, !6, i64 92}
!18 = !{!19, !19, i64 0}
!19 = !{!"any pointer", !7, i64 0}


; Effective O3 passes: DSEPass,EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,TailCallElimPass