; LLVM IR from IOsonata/IOsonata :: src (sysstatus)

; 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.__Sys_Status = type { i32, [128 x i8] }

@g_SysState = external dso_local global i32, align 4
@g_StatusQCurrIdx = external dso_local global i32, align 4
@g_StatusQue = external dso_local global [2 x %struct.__Sys_Status], 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: 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 i32 @SysStatusSet(i32 noundef %0, ptr noundef %1) #1 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  store i32 %0, ptr %3, align 4, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  store i32 0, ptr %5, align 4, !tbaa !5
  %8 = load i32, ptr %3, align 4, !tbaa !5
  %9 = and i32 %8, -268435456
  %10 = icmp eq i32 %9, 0
  br i1 %10, label %11, label %15

11:                                               ; preds = %2
  %12 = load i32, ptr %3, align 4, !tbaa !5
  %13 = zext i32 %12 to i64
  store i64 %13, ptr %6, align 8, !tbaa !11
  %14 = load i64, ptr %6, align 8
  store atomic i64 %14, ptr @g_SysState seq_cst, align 4
  br label %15

15:                                               ; preds = %11, %2
  %16 = load i32, ptr @g_StatusQCurrIdx, align 4, !tbaa !5
  %17 = add nsw i32 %16, 1
  %18 = srem i32 %17, 2
  store i32 %18, ptr @g_StatusQCurrIdx, align 4, !tbaa !5
  %19 = load i32, ptr @g_StatusQCurrIdx, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [2 x %struct.__Sys_Status], ptr @g_StatusQue, i64 0, i64 %20
  %22 = getelementptr inbounds %struct.__Sys_Status, ptr %21, i32 0, i32 0
  %23 = load i32, ptr %22, align 4, !tbaa !13
  %24 = zext i32 %23 to i64
  %25 = inttoptr i64 %24 to ptr
  %26 = load i32, ptr %3, align 4, !tbaa !5
  %27 = zext i32 %26 to i64
  store i64 %27, ptr %7, align 8, !tbaa !11
  %28 = load i64, ptr %7, align 8
  store atomic i64 %28, ptr %25 seq_cst, align 8
  %29 = load ptr, ptr %4, align 8, !tbaa !9
  %30 = icmp ne ptr %29, null
  br i1 %30, label %31, label %39

31:                                               ; preds = %15
  %32 = load i32, ptr @g_StatusQCurrIdx, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [2 x %struct.__Sys_Status], ptr @g_StatusQue, i64 0, i64 %33
  %35 = getelementptr inbounds %struct.__Sys_Status, ptr %34, i32 0, i32 1
  %36 = getelementptr inbounds [128 x i8], ptr %35, i64 0, i64 0
  %37 = load ptr, ptr %4, align 8, !tbaa !9
  %38 = call ptr @strncpy(ptr noundef %36, ptr noundef %37, i64 noundef 128) #3
  store i32 127, ptr %5, align 4, !tbaa !5
  br label %39

39:                                               ; preds = %31, %15
  %40 = load i32, ptr @g_StatusQCurrIdx, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [2 x %struct.__Sys_Status], ptr @g_StatusQue, i64 0, i64 %41
  %43 = getelementptr inbounds %struct.__Sys_Status, ptr %42, i32 0, i32 1
  %44 = load i32, ptr %5, align 4, !tbaa !5
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [128 x i8], ptr %43, i64 0, i64 %45
  store i8 0, ptr %46, align 1, !tbaa !15
  %47 = load i32, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  ret i32 %47
}

; Function Attrs: nounwind
declare ptr @strncpy(ptr noundef, ptr noundef, i64 noundef) #2

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 "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, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !6, i64 0}
!14 = !{!"__Sys_Status", !6, i64 0, !7, i64 4}
!15 = !{!7, !7, i64 0}


; 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.__Sys_Status = type { i32, [128 x i8] }

@g_SysState = external dso_local local_unnamed_addr global i32, align 4
@g_StatusQCurrIdx = external dso_local local_unnamed_addr global i32, align 4
@g_StatusQue = external dso_local global [2 x %struct.__Sys_Status], align 16

; Function Attrs: mustprogress nofree nounwind willreturn memory(readwrite, inaccessiblemem: none) uwtable
define dso_local noundef i32 @SysStatusSet(i32 noundef returned %0, ptr noundef readonly %1) local_unnamed_addr #0 {
  %3 = icmp ult i32 %0, 268435456
  %4 = zext i32 %0 to i64
  br i1 %3, label %5, label %._crit_edge9

5:                                                ; preds = %2
  store atomic i64 %4, ptr @g_SysState seq_cst, align 4
  br label %._crit_edge9

._crit_edge9:                                     ; preds = %2, %5
  %6 = load i32, ptr @g_StatusQCurrIdx, align 4, !tbaa !5
  %7 = add nsw i32 %6, 1
  %8 = srem i32 %7, 2
  store i32 %8, ptr @g_StatusQCurrIdx, align 4, !tbaa !5
  %9 = sext i32 %8 to i64
  %10 = getelementptr inbounds [2 x %struct.__Sys_Status], ptr @g_StatusQue, i64 0, i64 %9
  %11 = load i32, ptr %10, align 4, !tbaa !9
  %12 = zext i32 %11 to i64
  %13 = inttoptr i64 %12 to ptr
  store atomic i64 %4, ptr %13 seq_cst, align 8
  %.not = icmp eq ptr %1, null
  %.pre = load i32, ptr @g_StatusQCurrIdx, align 4, !tbaa !5
  %.pre11 = sext i32 %.pre to i64
  br i1 %.not, label %._crit_edge, label %14

14:                                               ; preds = %._crit_edge9
  %15 = getelementptr inbounds [2 x %struct.__Sys_Status], ptr @g_StatusQue, i64 0, i64 %.pre11, i32 1
  %16 = tail call ptr @strncpy(ptr noundef nonnull dereferenceable(1) %15, ptr noundef nonnull dereferenceable(1) %1, i64 noundef 128) #2
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge9, %14
  %.0 = phi i64 [ 127, %14 ], [ 0, %._crit_edge9 ]
  %17 = getelementptr inbounds [2 x %struct.__Sys_Status], ptr @g_StatusQue, i64 0, i64 %.pre11, i32 1, i64 %.0
  store i8 0, ptr %17, align 1, !tbaa !11
  ret i32 %0
}

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: readwrite)
declare ptr @strncpy(ptr noalias noundef returned writeonly, ptr noalias nocapture noundef readonly, i64 noundef) local_unnamed_addr #1

attributes #0 = { mustprogress nofree nounwind willreturn memory(readwrite, 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 nofree nounwind willreturn memory(argmem: readwrite) "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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 0}
!10 = !{!"__Sys_Status", !6, i64 0, !7, i64 4}
!11 = !{!7, !7, i64 0}


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