; LLVM IR from ponylang/ponyc :: src/libponyrt/asio (epoll)

; 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.epoll_event = type <{ i32, %union.epoll_data }>
%union.epoll_data = type { ptr }
%struct.sigaction = type { %union.anon, %struct.__sigset_t, i32, ptr }
%union.anon = type { ptr }
%struct.__sigset_t = type { [16 x i64] }
%struct.asio_backend_t = type { i32, i32, [64 x %struct.epoll_event], [128 x ptr], i8, %struct.messageq_t }
%struct.messageq_t = type { ptr, ptr }

; Function Attrs: nounwind uwtable
define dso_local ptr @ponyint_asio_backend_init() #0 {
  %1 = alloca ptr, align 8
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca %struct.epoll_event, align 1
  %5 = alloca %struct.sigaction, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #5
  %6 = call noalias ptr @ponyint_pool_alloc(i64 noundef 6)
  store ptr %6, ptr %2, align 8, !tbaa !5
  %7 = load ptr, ptr %2, align 8, !tbaa !5
  call void @llvm.memset.p0.i64(ptr align 8 %7, i8 0, i64 1824, i1 false)
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.asio_backend_t, ptr %8, i32 0, i32 5
  call void @ponyint_messageq_init(ptr noundef %9)
  %10 = call i32 @epoll_create1(i32 noundef 524288) #5
  %11 = load ptr, ptr %2, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.asio_backend_t, ptr %11, i32 0, i32 0
  store i32 %10, ptr %12, align 8, !tbaa !9
  %13 = call i32 @eventfd(i32 noundef 0, i32 noundef 2048) #5
  %14 = load ptr, ptr %2, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.asio_backend_t, ptr %14, i32 0, i32 1
  store i32 %13, ptr %15, align 4, !tbaa !13
  %16 = load ptr, ptr %2, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.asio_backend_t, ptr %16, i32 0, i32 0
  %18 = load i32, ptr %17, align 8, !tbaa !9
  %19 = icmp eq i32 %18, 0
  br i1 %19, label %25, label %20

20:                                               ; preds = %0
  %21 = load ptr, ptr %2, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.asio_backend_t, ptr %21, i32 0, i32 1
  %23 = load i32, ptr %22, align 4, !tbaa !13
  %24 = icmp eq i32 %23, 0
  br i1 %24, label %25, label %27

25:                                               ; preds = %20, %0
  %26 = load ptr, ptr %2, align 8, !tbaa !5
  call void @ponyint_pool_free(i64 noundef 6, ptr noundef %26)
  store ptr null, ptr %1, align 8
  store i32 1, ptr %3, align 4
  br label %44

27:                                               ; preds = %20
  call void @llvm.lifetime.start.p0(i64 12, ptr %4) #5
  %28 = load ptr, ptr %2, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.epoll_event, ptr %4, i32 0, i32 1
  store ptr %28, ptr %29, align 1, !tbaa !14
  %30 = getelementptr inbounds %struct.epoll_event, ptr %4, i32 0, i32 0
  store i32 -2147475455, ptr %30, align 1, !tbaa !15
  %31 = load ptr, ptr %2, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.asio_backend_t, ptr %31, i32 0, i32 0
  %33 = load i32, ptr %32, align 8, !tbaa !9
  %34 = load ptr, ptr %2, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.asio_backend_t, ptr %34, i32 0, i32 1
  %36 = load i32, ptr %35, align 4, !tbaa !13
  %37 = call i32 @epoll_ctl(i32 noundef %33, i32 noundef 1, i32 noundef %36, ptr noundef %4) #5
  call void @llvm.lifetime.start.p0(i64 152, ptr %5) #5
  %38 = getelementptr inbounds %struct.sigaction, ptr %5, i32 0, i32 0
  store ptr @empty_signal_handler, ptr %38, align 8, !tbaa !14
  %39 = getelementptr inbounds %struct.sigaction, ptr %5, i32 0, i32 1
  %40 = call i32 @sigemptyset(ptr noundef %39) #5
  %41 = getelementptr inbounds %struct.sigaction, ptr %5, i32 0, i32 2
  store i32 268435456, ptr %41, align 8, !tbaa !17
  %42 = call i32 @sigaction(i32 noundef 12, ptr noundef %5, ptr noundef null) #5
  %43 = load ptr, ptr %2, align 8, !tbaa !5
  store ptr %43, ptr %1, align 8
  store i32 1, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 152, ptr %5) #5
  call void @llvm.lifetime.end.p0(i64 12, ptr %4) #5
  br label %44

44:                                               ; preds = %27, %25
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #5
  %45 = load ptr, ptr %1, align 8
  ret ptr %45
}

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

declare noalias ptr @ponyint_pool_alloc(i64 noundef) #2

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

declare void @ponyint_messageq_init(ptr noundef) #2

; Function Attrs: nounwind
declare i32 @epoll_create1(i32 noundef) #4

; Function Attrs: nounwind
declare i32 @eventfd(i32 noundef, i32 noundef) #4

declare void @ponyint_pool_free(i64 noundef, ptr noundef) #2

; Function Attrs: nounwind
declare i32 @epoll_ctl(i32 noundef, i32 noundef, i32 noundef, ptr noundef) #4

; Function Attrs: nounwind uwtable
declare hidden void @empty_signal_handler(i32 noundef) #0

; Function Attrs: nounwind
declare i32 @sigemptyset(ptr noundef) #4

; Function Attrs: nounwind
declare i32 @sigaction(i32 noundef, ptr noundef, ptr noundef) #4

; 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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { 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 #5 = { 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, !11, i64 0}
!10 = !{!"asio_backend_t", !11, i64 0, !11, i64 4, !7, i64 8, !7, i64 776, !7, i64 1800, !12, i64 1808}
!11 = !{!"int", !7, i64 0}
!12 = !{!"messageq_t", !7, i64 0, !6, i64 8}
!13 = !{!10, !11, i64 4}
!14 = !{!7, !7, i64 0}
!15 = !{!16, !11, i64 0}
!16 = !{!"epoll_event", !11, i64 0, !7, i64 4}
!17 = !{!18, !11, i64 136}
!18 = !{!"sigaction", !7, i64 0, !19, i64 8, !11, i64 136, !6, i64 144}
!19 = !{!"", !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.epoll_event = type <{ i32, %union.epoll_data }>
%union.epoll_data = type { ptr }
%struct.sigaction = type { %union.anon, %struct.__sigset_t, i32, ptr }
%union.anon = type { ptr }
%struct.__sigset_t = type { [16 x i64] }

; Function Attrs: nounwind uwtable
define dso_local noundef ptr @ponyint_asio_backend_init() local_unnamed_addr #0 {
  %1 = alloca %struct.epoll_event, align 4
  %2 = alloca %struct.sigaction, align 8
  %3 = tail call noalias ptr @ponyint_pool_alloc(i64 noundef 6) #5
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(1824) %3, i8 0, i64 1824, i1 false)
  %4 = getelementptr inbounds i8, ptr %3, i64 1808
  tail call void @ponyint_messageq_init(ptr noundef nonnull %4) #5
  %5 = tail call i32 @epoll_create1(i32 noundef 524288) #5
  store i32 %5, ptr %3, align 8, !tbaa !5
  %6 = tail call i32 @eventfd(i32 noundef 0, i32 noundef 2048) #5
  %7 = getelementptr inbounds i8, ptr %3, i64 4
  store i32 %6, ptr %7, align 4, !tbaa !12
  %8 = load i32, ptr %3, align 8, !tbaa !5
  %9 = icmp eq i32 %8, 0
  %10 = icmp eq i32 %6, 0
  %or.cond = select i1 %9, i1 true, i1 %10
  br i1 %or.cond, label %11, label %12

11:                                               ; preds = %0
  tail call void @ponyint_pool_free(i64 noundef 6, ptr noundef nonnull %3) #5
  br label %19

12:                                               ; preds = %0
  call void @llvm.lifetime.start.p0(i64 12, ptr nonnull %1) #5
  %13 = getelementptr inbounds i8, ptr %1, i64 4
  store ptr %3, ptr %13, align 4, !tbaa !13
  store i32 -2147475455, ptr %1, align 4, !tbaa !14
  %14 = call i32 @epoll_ctl(i32 noundef %8, i32 noundef 1, i32 noundef %6, ptr noundef nonnull %1) #5
  call void @llvm.lifetime.start.p0(i64 152, ptr nonnull %2) #5
  store ptr @empty_signal_handler, ptr %2, align 8, !tbaa !13
  %15 = getelementptr inbounds i8, ptr %2, i64 8
  %16 = call i32 @sigemptyset(ptr noundef nonnull %15) #5
  %17 = getelementptr inbounds i8, ptr %2, i64 136
  store i32 268435456, ptr %17, align 8, !tbaa !16
  %18 = call i32 @sigaction(i32 noundef 12, ptr noundef nonnull %2, ptr noundef null) #5
  call void @llvm.lifetime.end.p0(i64 152, ptr nonnull %2) #5
  call void @llvm.lifetime.end.p0(i64 12, ptr nonnull %1) #5
  br label %19

19:                                               ; preds = %12, %11
  %.0 = phi ptr [ null, %11 ], [ %3, %12 ]
  ret ptr %.0
}

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

declare noalias ptr @ponyint_pool_alloc(i64 noundef) local_unnamed_addr #2

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

declare void @ponyint_messageq_init(ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind
declare i32 @epoll_create1(i32 noundef) local_unnamed_addr #4

; Function Attrs: nounwind
declare i32 @eventfd(i32 noundef, i32 noundef) local_unnamed_addr #4

declare void @ponyint_pool_free(i64 noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind
declare i32 @epoll_ctl(i32 noundef, i32 noundef, i32 noundef, ptr noundef) local_unnamed_addr #4

; Function Attrs: nounwind uwtable
declare hidden void @empty_signal_handler(i32 noundef) #0

; Function Attrs: nounwind
declare i32 @sigemptyset(ptr noundef) local_unnamed_addr #4

; Function Attrs: nounwind
declare i32 @sigaction(i32 noundef, ptr noundef, ptr noundef) local_unnamed_addr #4

; 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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { 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 #5 = { 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 = !{!"asio_backend_t", !7, i64 0, !7, i64 4, !8, i64 8, !8, i64 776, !8, i64 1800, !10, i64 1808}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"messageq_t", !8, i64 0, !11, i64 8}
!11 = !{!"any pointer", !8, i64 0}
!12 = !{!6, !7, i64 4}
!13 = !{!8, !8, i64 0}
!14 = !{!15, !7, i64 0}
!15 = !{!"epoll_event", !7, i64 0, !8, i64 4}
!16 = !{!17, !7, i64 136}
!17 = !{!"sigaction", !8, i64 0, !18, i64 8, !7, i64 136, !11, i64 144}
!18 = !{!"", !8, i64 0}


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