; LLVM IR from kala13x/libxutils :: src/sys (cpu)

; 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.XByteBuffer = type { ptr, i64, i64, i16, i16, i32 }

@g_nCPUCount = external hidden global i32, align 4
@.str = external hidden unnamed_addr constant [14 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [10 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local i32 @XCPU_GetCount() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca %struct.XByteBuffer, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  %7 = atomicrmw add ptr @g_nCPUCount, i32 0 seq_cst, align 4
  %8 = add i32 %7, 0
  store i32 %8, ptr %2, align 4, !tbaa !5
  %9 = load i32, ptr %2, align 4, !tbaa !5
  %10 = icmp ugt i32 %9, 0
  br i1 %10, label %11, label %13

11:                                               ; preds = %0
  %12 = load i32, ptr %2, align 4, !tbaa !5
  store i32 %12, ptr %1, align 4
  store i32 1, ptr %3, align 4
  br label %44

13:                                               ; preds = %0
  call void @llvm.lifetime.start.p0(i64 32, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  store i32 0, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 0, ptr %6, align 4, !tbaa !5
  %14 = call i64 @XPath_LoadBuffer(ptr noundef @.str, ptr noundef %4)
  %15 = icmp ule i64 %14, 0
  br i1 %15, label %16, label %17

16:                                               ; preds = %13
  store i32 -1, ptr %1, align 4
  store i32 1, ptr %3, align 4
  br label %43

17:                                               ; preds = %13
  br label %18

18:                                               ; preds = %36, %17
  %19 = getelementptr inbounds %struct.XByteBuffer, ptr %4, i32 0, i32 0
  %20 = load ptr, ptr %19, align 8, !tbaa !9
  %21 = getelementptr inbounds %struct.XByteBuffer, ptr %4, i32 0, i32 2
  %22 = load i64, ptr %21, align 8, !tbaa !14
  %23 = load i32, ptr %6, align 4, !tbaa !5
  %24 = sext i32 %23 to i64
  %25 = call i32 @xstrnsrc(ptr noundef %20, i64 noundef %22, ptr noundef @.str.1, i64 noundef %24)
  store i32 %25, ptr %5, align 4, !tbaa !5
  %26 = load i32, ptr %5, align 4, !tbaa !5
  %27 = icmp slt i32 %26, 0
  br i1 %27, label %28, label %29

28:                                               ; preds = %18
  br label %39

29:                                               ; preds = %18
  %30 = load i32, ptr %5, align 4, !tbaa !5
  %31 = add nsw i32 %30, 1
  %32 = load i32, ptr %6, align 4, !tbaa !5
  %33 = add nsw i32 %32, %31
  store i32 %33, ptr %6, align 4, !tbaa !5
  %34 = load i32, ptr %2, align 4, !tbaa !5
  %35 = add i32 %34, 1
  store i32 %35, ptr %2, align 4, !tbaa !5
  br label %36

36:                                               ; preds = %29
  %37 = load i32, ptr %5, align 4, !tbaa !5
  %38 = icmp sge i32 %37, 0
  br i1 %38, label %18, label %39, !llvm.loop !15

39:                                               ; preds = %36, %28
  %40 = load i32, ptr %2, align 4, !tbaa !5
  %41 = atomicrmw xchg ptr @g_nCPUCount, i32 %40 seq_cst, align 4
  call void @XByteBuffer_Clear(ptr noundef %4)
  %42 = load i32, ptr %2, align 4, !tbaa !5
  store i32 %42, ptr %1, align 4
  store i32 1, ptr %3, align 4
  br label %43

43:                                               ; preds = %39, %16
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 32, ptr %4) #3
  br label %44

44:                                               ; preds = %43, %11
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  %45 = load i32, ptr %1, align 4
  ret i32 %45
}

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

declare i64 @XPath_LoadBuffer(ptr noundef, ptr noundef) #2

declare i32 @xstrnsrc(ptr noundef, i64 noundef, ptr noundef, i64 noundef) #2

declare void @XByteBuffer_Clear(ptr noundef) #2

; 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 = { "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 #3 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 0}
!10 = !{!"XByteBuffer", !11, i64 0, !12, i64 8, !12, i64 16, !13, i64 24, !13, i64 26, !6, i64 28}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!"short", !7, i64 0}
!14 = !{!10, !12, i64 16}
!15 = distinct !{!15, !16}
!16 = !{!"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"

%struct.XByteBuffer = type { ptr, i64, i64, i16, i16, i32 }

@g_nCPUCount = external hidden global i32, align 4
@.str = external hidden unnamed_addr constant [14 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [10 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local i32 @XCPU_GetCount() local_unnamed_addr #0 {
  %1 = alloca %struct.XByteBuffer, align 8
  %2 = atomicrmw or ptr @g_nCPUCount, i32 0 seq_cst, align 4
  %.not = icmp eq i32 %2, 0
  br i1 %.not, label %3, label %22

3:                                                ; preds = %0
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %1) #3
  %4 = call i64 @XPath_LoadBuffer(ptr noundef nonnull @.str, ptr noundef nonnull %1) #3
  %5 = icmp eq i64 %4, 0
  br i1 %5, label %21, label %.preheader

.preheader:                                       ; preds = %3
  %6 = getelementptr inbounds i8, ptr %1, i64 16
  %7 = load ptr, ptr %1, align 8, !tbaa !5
  %8 = load i64, ptr %6, align 8, !tbaa !13
  %9 = call i32 @xstrnsrc(ptr noundef %7, i64 noundef %8, ptr noundef nonnull @.str.1, i64 noundef 0) #3
  %10 = icmp slt i32 %9, 0
  br i1 %10, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %11 = phi i32 [ %18, %.lr.ph ], [ %9, %.preheader ]
  %.018 = phi i32 [ %13, %.lr.ph ], [ 0, %.preheader ]
  %.01117 = phi i32 [ %14, %.lr.ph ], [ 0, %.preheader ]
  %12 = add nuw nsw i32 %.018, 1
  %13 = add nuw nsw i32 %12, %11
  %14 = add i32 %.01117, 1
  %15 = load ptr, ptr %1, align 8, !tbaa !5
  %16 = load i64, ptr %6, align 8, !tbaa !13
  %17 = zext nneg i32 %13 to i64
  %18 = call i32 @xstrnsrc(ptr noundef %15, i64 noundef %16, ptr noundef nonnull @.str.1, i64 noundef %17) #3
  %19 = icmp slt i32 %18, 0
  br i1 %19, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %.preheader
  %.011.lcssa = phi i32 [ 0, %.preheader ], [ %14, %.lr.ph ]
  %20 = atomicrmw xchg ptr @g_nCPUCount, i32 %.011.lcssa seq_cst, align 4
  call void @XByteBuffer_Clear(ptr noundef nonnull %1) #3
  br label %21

21:                                               ; preds = %3, %._crit_edge
  %.012 = phi i32 [ %.011.lcssa, %._crit_edge ], [ -1, %3 ]
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %1) #3
  br label %22

22:                                               ; preds = %0, %21
  %.113 = phi i32 [ %.012, %21 ], [ %2, %0 ]
  ret i32 %.113
}

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

declare i64 @XPath_LoadBuffer(ptr noundef, ptr noundef) local_unnamed_addr #2

declare i32 @xstrnsrc(ptr noundef, i64 noundef, ptr noundef, i64 noundef) local_unnamed_addr #2

declare void @XByteBuffer_Clear(ptr noundef) local_unnamed_addr #2

; 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 = { "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 #3 = { 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, !7, i64 0}
!6 = !{!"XByteBuffer", !7, i64 0, !10, i64 8, !10, i64 16, !11, i64 24, !11, i64 26, !12, i64 28}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"short", !8, i64 0}
!12 = !{!"int", !8, i64 0}
!13 = !{!6, !10, i64 16}


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