; LLVM IR from asllop/AppOS :: event (event_internal)

; 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.QueueStruct = type { ptr, i64, i32, i32, i32 }
%struct.EventStruct = type { ptr, i64 }

@queueArray = external hidden global [128 x %struct.QueueStruct], align 16

declare i32 @core_lock(i32 noundef) #0

declare i32 @core_unlock(i32 noundef) #0

; Function Attrs: nounwind uwtable
define dso_local zeroext i1 @dequeue_event(i8 noundef zeroext %0, ptr noundef %1) #1 {
  %3 = alloca i1, align 1
  %4 = alloca i8, align 1
  %5 = alloca ptr, align 8
  store i8 %0, ptr %4, align 1, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !8
  %6 = call i32 @core_lock(i32 noundef 2)
  %7 = load i8, ptr %4, align 1, !tbaa !5
  %8 = zext i8 %7 to i64
  %9 = getelementptr inbounds [128 x %struct.QueueStruct], ptr @queueArray, i64 0, i64 %8
  %10 = getelementptr inbounds %struct.QueueStruct, ptr %9, i32 0, i32 4
  %11 = load i32, ptr %10, align 8, !tbaa !10
  %12 = icmp ne i32 %11, 0
  br i1 %12, label %13, label %88

13:                                               ; preds = %2
  %14 = load i8, ptr %4, align 1, !tbaa !5
  %15 = zext i8 %14 to i64
  %16 = getelementptr inbounds [128 x %struct.QueueStruct], ptr @queueArray, i64 0, i64 %15
  %17 = getelementptr inbounds %struct.QueueStruct, ptr %16, i32 0, i32 4
  %18 = load i32, ptr %17, align 8, !tbaa !10
  %19 = add i32 %18, -1
  store i32 %19, ptr %17, align 8, !tbaa !10
  %20 = load i8, ptr %4, align 1, !tbaa !5
  %21 = zext i8 %20 to i64
  %22 = getelementptr inbounds [128 x %struct.QueueStruct], ptr @queueArray, i64 0, i64 %21
  %23 = getelementptr inbounds %struct.QueueStruct, ptr %22, i32 0, i32 0
  %24 = load ptr, ptr %23, align 16, !tbaa !14
  %25 = load i8, ptr %4, align 1, !tbaa !5
  %26 = zext i8 %25 to i64
  %27 = getelementptr inbounds [128 x %struct.QueueStruct], ptr @queueArray, i64 0, i64 %26
  %28 = getelementptr inbounds %struct.QueueStruct, ptr %27, i32 0, i32 2
  %29 = load i32, ptr %28, align 16, !tbaa !15
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds %struct.EventStruct, ptr %24, i64 %30
  %32 = getelementptr inbounds %struct.EventStruct, ptr %31, i32 0, i32 0
  %33 = load ptr, ptr %32, align 8, !tbaa !16
  %34 = load ptr, ptr %5, align 8, !tbaa !8
  %35 = getelementptr inbounds %struct.EventStruct, ptr %34, i32 0, i32 0
  store ptr %33, ptr %35, align 8, !tbaa !16
  %36 = load i8, ptr %4, align 1, !tbaa !5
  %37 = zext i8 %36 to i64
  %38 = getelementptr inbounds [128 x %struct.QueueStruct], ptr @queueArray, i64 0, i64 %37
  %39 = getelementptr inbounds %struct.QueueStruct, ptr %38, i32 0, i32 0
  %40 = load ptr, ptr %39, align 16, !tbaa !14
  %41 = load i8, ptr %4, align 1, !tbaa !5
  %42 = zext i8 %41 to i64
  %43 = getelementptr inbounds [128 x %struct.QueueStruct], ptr @queueArray, i64 0, i64 %42
  %44 = getelementptr inbounds %struct.QueueStruct, ptr %43, i32 0, i32 2
  %45 = load i32, ptr %44, align 16, !tbaa !15
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds %struct.EventStruct, ptr %40, i64 %46
  %48 = getelementptr inbounds %struct.EventStruct, ptr %47, i32 0, i32 1
  %49 = load i64, ptr %48, align 8, !tbaa !18
  %50 = load ptr, ptr %5, align 8, !tbaa !8
  %51 = getelementptr inbounds %struct.EventStruct, ptr %50, i32 0, i32 1
  store i64 %49, ptr %51, align 8, !tbaa !18
  %52 = load i8, ptr %4, align 1, !tbaa !5
  %53 = zext i8 %52 to i64
  %54 = getelementptr inbounds [128 x %struct.QueueStruct], ptr @queueArray, i64 0, i64 %53
  %55 = getelementptr inbounds %struct.QueueStruct, ptr %54, i32 0, i32 4
  %56 = load i32, ptr %55, align 8, !tbaa !10
  %57 = icmp ne i32 %56, 0
  br i1 %57, label %58, label %77

58:                                               ; preds = %13
  %59 = load i8, ptr %4, align 1, !tbaa !5
  %60 = zext i8 %59 to i64
  %61 = getelementptr inbounds [128 x %struct.QueueStruct], ptr @queueArray, i64 0, i64 %60
  %62 = getelementptr inbounds %struct.QueueStruct, ptr %61, i32 0, i32 2
  %63 = load i32, ptr %62, align 16, !tbaa !15
  %64 = add nsw i32 %63, 1
  store i32 %64, ptr %62, align 16, !tbaa !15
  %65 = load i8, ptr %4, align 1, !tbaa !5
  %66 = zext i8 %65 to i64
  %67 = getelementptr inbounds [128 x %struct.QueueStruct], ptr @queueArray, i64 0, i64 %66
  %68 = getelementptr inbounds %struct.QueueStruct, ptr %67, i32 0, i32 2
  %69 = load i32, ptr %68, align 16, !tbaa !15
  %70 = icmp sgt i32 %69, 511
  br i1 %70, label %71, label %76

71:                                               ; preds = %58
  %72 = load i8, ptr %4, align 1, !tbaa !5
  %73 = zext i8 %72 to i64
  %74 = getelementptr inbounds [128 x %struct.QueueStruct], ptr @queueArray, i64 0, i64 %73
  %75 = getelementptr inbounds %struct.QueueStruct, ptr %74, i32 0, i32 2
  store i32 0, ptr %75, align 16, !tbaa !15
  br label %76

76:                                               ; preds = %71, %58
  br label %86

77:                                               ; preds = %13
  %78 = load i8, ptr %4, align 1, !tbaa !5
  %79 = zext i8 %78 to i64
  %80 = getelementptr inbounds [128 x %struct.QueueStruct], ptr @queueArray, i64 0, i64 %79
  %81 = getelementptr inbounds %struct.QueueStruct, ptr %80, i32 0, i32 2
  store i32 0, ptr %81, align 16, !tbaa !15
  %82 = load i8, ptr %4, align 1, !tbaa !5
  %83 = zext i8 %82 to i64
  %84 = getelementptr inbounds [128 x %struct.QueueStruct], ptr @queueArray, i64 0, i64 %83
  %85 = getelementptr inbounds %struct.QueueStruct, ptr %84, i32 0, i32 3
  store i32 0, ptr %85, align 4, !tbaa !19
  br label %86

86:                                               ; preds = %77, %76
  %87 = call i32 @core_unlock(i32 noundef 2)
  store i1 true, ptr %3, align 1
  br label %90

88:                                               ; preds = %2
  %89 = call i32 @core_unlock(i32 noundef 2)
  store i1 false, ptr %3, align 1
  br label %90

90:                                               ; preds = %88, %86
  %91 = load i1, ptr %3, align 1
  ret i1 %91
}

attributes #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 = { 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"any pointer", !6, i64 0}
!10 = !{!11, !13, i64 24}
!11 = !{!"QueueStruct", !9, i64 0, !12, i64 8, !13, i64 16, !13, i64 20, !13, i64 24}
!12 = !{!"long", !6, i64 0}
!13 = !{!"int", !6, i64 0}
!14 = !{!11, !9, i64 0}
!15 = !{!11, !13, i64 16}
!16 = !{!17, !9, i64 0}
!17 = !{!"EventStruct", !9, i64 0, !12, i64 8}
!18 = !{!17, !12, i64 8}
!19 = !{!11, !13, i64 20}


; 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.QueueStruct = type { ptr, i64, i32, i32, i32 }
%struct.EventStruct = type { ptr, i64 }

@queueArray = external hidden local_unnamed_addr global [128 x %struct.QueueStruct], align 16

declare i32 @core_lock(i32 noundef) local_unnamed_addr #0

declare i32 @core_unlock(i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef zeroext i1 @dequeue_event(i8 noundef zeroext %0, ptr nocapture noundef writeonly %1) local_unnamed_addr #1 {
  %3 = tail call i32 @core_lock(i32 noundef 2) #2
  %4 = zext i8 %0 to i64
  %5 = getelementptr inbounds [128 x %struct.QueueStruct], ptr @queueArray, i64 0, i64 %4
  %6 = getelementptr inbounds i8, ptr %5, i64 24
  %7 = load i32, ptr %6, align 8, !tbaa !5
  %.not = icmp ne i32 %7, 0
  br i1 %.not, label %8, label %25

8:                                                ; preds = %2
  %9 = add i32 %7, -1
  store i32 %9, ptr %6, align 8, !tbaa !5
  %10 = load ptr, ptr %5, align 16, !tbaa !12
  %11 = getelementptr inbounds i8, ptr %5, i64 16
  %12 = load i32, ptr %11, align 16, !tbaa !13
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds %struct.EventStruct, ptr %10, i64 %13
  %15 = load ptr, ptr %14, align 8, !tbaa !14
  store ptr %15, ptr %1, align 8, !tbaa !14
  %16 = getelementptr inbounds %struct.EventStruct, ptr %10, i64 %13, i32 1
  %17 = load i64, ptr %16, align 8, !tbaa !16
  %18 = getelementptr inbounds i8, ptr %1, i64 8
  store i64 %17, ptr %18, align 8, !tbaa !16
  %.not15 = icmp eq i32 %9, 0
  br i1 %.not15, label %22, label %19

19:                                               ; preds = %8
  %20 = add nsw i32 %12, 1
  %21 = icmp sgt i32 %12, 510
  %spec.store.select = select i1 %21, i32 0, i32 %20
  br label %24

22:                                               ; preds = %8
  %23 = getelementptr inbounds i8, ptr %5, i64 20
  store i32 0, ptr %23, align 4, !tbaa !17
  br label %24

24:                                               ; preds = %19, %22
  %spec.store.select.sink = phi i32 [ 0, %22 ], [ %spec.store.select, %19 ]
  store i32 %spec.store.select.sink, ptr %11, align 16
  br label %25

25:                                               ; preds = %2, %24
  %26 = tail call i32 @core_unlock(i32 noundef 2) #2
  ret i1 %.not
}

attributes #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 = { 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, !11, i64 24}
!6 = !{!"QueueStruct", !7, i64 0, !10, i64 8, !11, i64 16, !11, i64 20, !11, i64 24}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"int", !8, i64 0}
!12 = !{!6, !7, i64 0}
!13 = !{!6, !11, i64 16}
!14 = !{!15, !7, i64 0}
!15 = !{!"EventStruct", !7, i64 0, !10, i64 8}
!16 = !{!15, !10, i64 8}
!17 = !{!6, !11, i64 20}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InstCombinePass,MergedLoadStoreMotionPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass