; LLVM IR from uNetworking/uSockets :: src/eventing (epoll_kqueue)

; 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.us_loop_t = type { %struct.us_internal_loop_data_t, i32, i32, i32, i32, [1024 x %struct.epoll_event], [8 x i8] }
%struct.us_internal_loop_data_t = type { ptr, ptr, i32, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, i32, i64 }
%struct.epoll_event = type <{ i32, %union.epoll_data }>
%union.epoll_data = type { ptr }

; Function Attrs: nounwind uwtable
declare dso_local i32 @us_poll_events(ptr noundef) #0

; 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 nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nounwind uwtable
define dso_local void @us_loop_run(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %6 = load ptr, ptr %2, align 8, !tbaa !5
  call void @us_loop_integrate(ptr noundef %6)
  br label %7

7:                                                ; preds = %85, %1
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.us_loop_t, ptr %8, i32 0, i32 1
  %10 = load i32, ptr %9, align 8, !tbaa !9
  %11 = icmp ne i32 %10, 0
  br i1 %11, label %12, label %87

12:                                               ; preds = %7
  %13 = load ptr, ptr %2, align 8, !tbaa !5
  call void @us_internal_loop_pre(ptr noundef %13)
  %14 = load ptr, ptr %2, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.us_loop_t, ptr %14, i32 0, i32 4
  %16 = load i32, ptr %15, align 4, !tbaa !14
  %17 = load ptr, ptr %2, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.us_loop_t, ptr %17, i32 0, i32 5
  %19 = getelementptr inbounds [1024 x %struct.epoll_event], ptr %18, i64 0, i64 0
  %20 = call i32 @epoll_wait(i32 noundef %16, ptr noundef %19, i32 noundef 1024, i32 noundef -1)
  %21 = load ptr, ptr %2, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.us_loop_t, ptr %21, i32 0, i32 2
  store i32 %20, ptr %22, align 4, !tbaa !15
  %23 = load ptr, ptr %2, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.us_loop_t, ptr %23, i32 0, i32 3
  store i32 0, ptr %24, align 16, !tbaa !16
  br label %25

25:                                               ; preds = %80, %12
  %26 = load ptr, ptr %2, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.us_loop_t, ptr %26, i32 0, i32 3
  %28 = load i32, ptr %27, align 16, !tbaa !16
  %29 = load ptr, ptr %2, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.us_loop_t, ptr %29, i32 0, i32 2
  %31 = load i32, ptr %30, align 4, !tbaa !15
  %32 = icmp slt i32 %28, %31
  br i1 %32, label %33, label %85

33:                                               ; preds = %25
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  %34 = load ptr, ptr %2, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.us_loop_t, ptr %34, i32 0, i32 5
  %36 = load ptr, ptr %2, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.us_loop_t, ptr %36, i32 0, i32 3
  %38 = load i32, ptr %37, align 16, !tbaa !16
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [1024 x %struct.epoll_event], ptr %35, i64 0, i64 %39
  %41 = getelementptr inbounds %struct.epoll_event, ptr %40, i32 0, i32 1
  %42 = load ptr, ptr %41, align 4, !tbaa !17
  store ptr %42, ptr %3, align 8, !tbaa !5
  %43 = load ptr, ptr %3, align 8, !tbaa !5
  %44 = icmp ne ptr %43, null
  br i1 %44, label %45, label %79

45:                                               ; preds = %33
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  %46 = load ptr, ptr %2, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.us_loop_t, ptr %46, i32 0, i32 5
  %48 = load ptr, ptr %2, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.us_loop_t, ptr %48, i32 0, i32 3
  %50 = load i32, ptr %49, align 16, !tbaa !16
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds [1024 x %struct.epoll_event], ptr %47, i64 0, i64 %51
  %53 = getelementptr inbounds %struct.epoll_event, ptr %52, i32 0, i32 0
  %54 = load i32, ptr %53, align 4, !tbaa !18
  store i32 %54, ptr %4, align 4, !tbaa !20
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %55 = load ptr, ptr %2, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.us_loop_t, ptr %55, i32 0, i32 5
  %57 = load ptr, ptr %2, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.us_loop_t, ptr %57, i32 0, i32 3
  %59 = load i32, ptr %58, align 16, !tbaa !16
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [1024 x %struct.epoll_event], ptr %56, i64 0, i64 %60
  %62 = getelementptr inbounds %struct.epoll_event, ptr %61, i32 0, i32 0
  %63 = load i32, ptr %62, align 4, !tbaa !18
  %64 = and i32 %63, 24
  store i32 %64, ptr %5, align 4, !tbaa !20
  %65 = load ptr, ptr %3, align 8, !tbaa !5
  %66 = call i32 @us_poll_events(ptr noundef %65)
  %67 = load i32, ptr %4, align 4, !tbaa !20
  %68 = and i32 %67, %66
  store i32 %68, ptr %4, align 4, !tbaa !20
  %69 = load i32, ptr %4, align 4, !tbaa !20
  %70 = icmp ne i32 %69, 0
  br i1 %70, label %74, label %71

71:                                               ; preds = %45
  %72 = load i32, ptr %5, align 4, !tbaa !20
  %73 = icmp ne i32 %72, 0
  br i1 %73, label %74, label %78

74:                                               ; preds = %71, %45
  %75 = load ptr, ptr %3, align 8, !tbaa !5
  %76 = load i32, ptr %5, align 4, !tbaa !20
  %77 = load i32, ptr %4, align 4, !tbaa !20
  call void @us_internal_dispatch_ready_poll(ptr noundef %75, i32 noundef %76, i32 noundef %77)
  br label %78

78:                                               ; preds = %74, %71
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  br label %79

79:                                               ; preds = %78, %33
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  br label %80

80:                                               ; preds = %79
  %81 = load ptr, ptr %2, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.us_loop_t, ptr %81, i32 0, i32 3
  %83 = load i32, ptr %82, align 16, !tbaa !16
  %84 = add nsw i32 %83, 1
  store i32 %84, ptr %82, align 16, !tbaa !16
  br label %25, !llvm.loop !21

85:                                               ; preds = %25
  %86 = load ptr, ptr %2, align 8, !tbaa !5
  call void @us_internal_loop_post(ptr noundef %86)
  br label %7, !llvm.loop !23

87:                                               ; preds = %7
  ret void
}

declare void @us_loop_integrate(ptr noundef) #2

declare void @us_internal_loop_pre(ptr noundef) #2

declare i32 @epoll_wait(i32 noundef, ptr noundef, i32 noundef, i32 noundef) #2

declare void @us_internal_dispatch_ready_poll(ptr noundef, i32 noundef, i32 noundef) #2

declare void @us_internal_loop_post(ptr noundef) #2

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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !12, i64 104}
!10 = !{!"us_loop_t", !11, i64 0, !12, i64 104, !12, i64 108, !12, i64 112, !12, i64 116, !7, i64 120}
!11 = !{!"us_internal_loop_data_t", !6, i64 0, !6, i64 8, !12, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !12, i64 88, !13, i64 96}
!12 = !{!"int", !7, i64 0}
!13 = !{!"long long", !7, i64 0}
!14 = !{!10, !12, i64 116}
!15 = !{!10, !12, i64 108}
!16 = !{!10, !12, i64 112}
!17 = !{!7, !7, i64 0}
!18 = !{!19, !12, i64 0}
!19 = !{!"epoll_event", !12, i64 0, !7, i64 4}
!20 = !{!12, !12, i64 0}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}
!23 = distinct !{!23, !22}


; 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.epoll_event = type <{ i32, %union.epoll_data }>
%union.epoll_data = type { ptr }

; Function Attrs: nounwind uwtable
declare dso_local i32 @us_poll_events(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @us_loop_run(ptr noundef %0) local_unnamed_addr #0 {
  tail call void @us_loop_integrate(ptr noundef %0) #2
  %2 = getelementptr inbounds i8, ptr %0, i64 104
  %3 = load i32, ptr %2, align 8, !tbaa !5
  %.not28 = icmp eq i32 %3, 0
  br i1 %.not28, label %._crit_edge31, label %.lr.ph30

.lr.ph30:                                         ; preds = %1
  %4 = getelementptr inbounds i8, ptr %0, i64 116
  %5 = getelementptr inbounds i8, ptr %0, i64 120
  %6 = getelementptr inbounds i8, ptr %0, i64 108
  %7 = getelementptr inbounds i8, ptr %0, i64 112
  br label %8

8:                                                ; preds = %.lr.ph30, %._crit_edge
  tail call void @us_internal_loop_pre(ptr noundef nonnull %0) #2
  %9 = load i32, ptr %4, align 4, !tbaa !13
  %10 = tail call i32 @epoll_wait(i32 noundef %9, ptr noundef nonnull %5, i32 noundef 1024, i32 noundef -1) #2
  store i32 %10, ptr %6, align 4, !tbaa !14
  store i32 0, ptr %7, align 16, !tbaa !15
  %11 = icmp sgt i32 %10, 0
  br i1 %11, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %8, %24
  %storemerge27 = phi i32 [ %26, %24 ], [ 0, %8 ]
  %12 = sext i32 %storemerge27 to i64
  %13 = getelementptr inbounds [1024 x %struct.epoll_event], ptr %5, i64 0, i64 %12
  %14 = getelementptr inbounds i8, ptr %13, i64 4
  %15 = load ptr, ptr %14, align 4, !tbaa !16
  %.not26 = icmp eq ptr %15, null
  br i1 %.not26, label %24, label %16

16:                                               ; preds = %.lr.ph
  %17 = load i32, ptr %13, align 4, !tbaa !17
  %18 = tail call i32 @us_poll_events(ptr noundef nonnull %15)
  %19 = or i32 %18, 24
  %20 = and i32 %19, %17
  %or.cond.not = icmp eq i32 %20, 0
  br i1 %or.cond.not, label %24, label %21

21:                                               ; preds = %16
  %22 = and i32 %17, 24
  %23 = and i32 %18, %17
  tail call void @us_internal_dispatch_ready_poll(ptr noundef nonnull %15, i32 noundef %22, i32 noundef %23) #2
  br label %24

24:                                               ; preds = %21, %16, %.lr.ph
  %25 = load i32, ptr %7, align 16, !tbaa !15
  %26 = add nsw i32 %25, 1
  store i32 %26, ptr %7, align 16, !tbaa !15
  %27 = load i32, ptr %6, align 4, !tbaa !14
  %28 = icmp slt i32 %26, %27
  br i1 %28, label %.lr.ph, label %._crit_edge, !llvm.loop !19

._crit_edge:                                      ; preds = %24, %8
  tail call void @us_internal_loop_post(ptr noundef nonnull %0) #2
  %29 = load i32, ptr %2, align 8, !tbaa !5
  %.not = icmp eq i32 %29, 0
  br i1 %.not, label %._crit_edge31, label %8, !llvm.loop !21

._crit_edge31:                                    ; preds = %._crit_edge, %1
  ret void
}

declare void @us_loop_integrate(ptr noundef) local_unnamed_addr #1

declare void @us_internal_loop_pre(ptr noundef) local_unnamed_addr #1

declare i32 @epoll_wait(i32 noundef, ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #1

declare void @us_internal_dispatch_ready_poll(ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #1

declare void @us_internal_loop_post(ptr noundef) local_unnamed_addr #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 = { "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 104}
!6 = !{!"us_loop_t", !7, i64 0, !11, i64 104, !11, i64 108, !11, i64 112, !11, i64 116, !9, i64 120}
!7 = !{!"us_internal_loop_data_t", !8, i64 0, !8, i64 8, !11, i64 16, !8, i64 24, !8, i64 32, !8, i64 40, !8, i64 48, !8, i64 56, !8, i64 64, !8, i64 72, !8, i64 80, !11, i64 88, !12, i64 96}
!8 = !{!"any pointer", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"int", !9, i64 0}
!12 = !{!"long long", !9, i64 0}
!13 = !{!6, !11, i64 116}
!14 = !{!6, !11, i64 108}
!15 = !{!6, !11, i64 112}
!16 = !{!9, !9, i64 0}
!17 = !{!18, !11, i64 0}
!18 = !{!"epoll_event", !11, i64 0, !9, i64 4}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = distinct !{!21, !20}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass