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

@.str = external hidden unnamed_addr constant [2 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [75 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [10 x i8], align 1
@__func__.pony_asio_event_unsubscribe = external hidden unnamed_addr constant [28 x i8], align 1

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

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

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

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

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

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

declare void @ponyint_assert_fail(ptr noundef, ptr noundef, i64 noundef, ptr noundef) #3

declare ptr @ponyint_asio_get_backend(...) #3

declare i32 @close(i32 noundef) #3

; Function Attrs: nounwind uwtable
define dso_local void @pony_asio_event_unsubscribe(ptr noundef %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i8, align 1
  %8 = alloca %struct.sigaction, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  %9 = load ptr, ptr %2, align 8, !tbaa !5
  %10 = icmp eq ptr %9, null
  br i1 %10, label %21, label %11

11:                                               ; preds = %1
  %12 = load ptr, ptr %2, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.asio_event_t, ptr %12, i32 0, i32 4
  %14 = load i32, ptr %13, align 8, !tbaa !9
  %15 = icmp eq i32 %14, 0
  br i1 %15, label %21, label %16

16:                                               ; preds = %11
  %17 = load ptr, ptr %2, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.asio_event_t, ptr %17, i32 0, i32 4
  %19 = load i32, ptr %18, align 8, !tbaa !9
  %20 = icmp eq i32 %19, -1
  br i1 %20, label %21, label %22

21:                                               ; preds = %16, %11, %1
  call void @ponyint_assert_fail(ptr noundef @.str, ptr noundef @.str.1, i64 noundef 455, ptr noundef @__func__.pony_asio_event_unsubscribe)
  br label %108

22:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  %23 = call ptr (...) @ponyint_asio_get_backend()
  store ptr %23, ptr %3, align 8, !tbaa !5
  %24 = load ptr, ptr %3, align 8, !tbaa !5
  %25 = icmp ne ptr %24, null
  br i1 %25, label %26, label %27

26:                                               ; preds = %22
  br label %28

27:                                               ; preds = %22
  call void @ponyint_assert_fail(ptr noundef @.str.2, ptr noundef @.str.1, i64 noundef 460, ptr noundef @__func__.pony_asio_event_unsubscribe)
  br label %28

28:                                               ; preds = %27, %26
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.asio_backend_t, ptr %29, i32 0, i32 0
  %31 = load i32, ptr %30, align 8, !tbaa !14
  %32 = load ptr, ptr %2, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.asio_event_t, ptr %32, i32 0, i32 3
  %34 = load i32, ptr %33, align 4, !tbaa !17
  %35 = call i32 @epoll_ctl(i32 noundef %31, i32 noundef 2, i32 noundef %34, ptr noundef null) #4
  %36 = load ptr, ptr %2, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.asio_event_t, ptr %36, i32 0, i32 4
  %38 = load i32, ptr %37, align 8, !tbaa !9
  %39 = and i32 %38, 4
  %40 = icmp ne i32 %39, 0
  br i1 %40, label %41, label %54

41:                                               ; preds = %28
  %42 = load ptr, ptr %2, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.asio_event_t, ptr %42, i32 0, i32 3
  %44 = load i32, ptr %43, align 4, !tbaa !17
  %45 = icmp ne i32 %44, -1
  br i1 %45, label %46, label %53

46:                                               ; preds = %41
  %47 = load ptr, ptr %2, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.asio_event_t, ptr %47, i32 0, i32 3
  %49 = load i32, ptr %48, align 4, !tbaa !17
  %50 = call i32 @close(i32 noundef %49)
  %51 = load ptr, ptr %2, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.asio_event_t, ptr %51, i32 0, i32 3
  store i32 -1, ptr %52, align 4, !tbaa !17
  br label %53

53:                                               ; preds = %46, %41
  br label %54

54:                                               ; preds = %53, %28
  %55 = load ptr, ptr %2, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.asio_event_t, ptr %55, i32 0, i32 4
  %57 = load i32, ptr %56, align 8, !tbaa !9
  %58 = and i32 %57, 8
  %59 = icmp ne i32 %58, 0
  br i1 %59, label %60, label %104

60:                                               ; preds = %54
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  %61 = load ptr, ptr %2, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.asio_event_t, ptr %61, i32 0, i32 8
  %63 = load i64, ptr %62, align 8, !tbaa !18
  %64 = trunc i64 %63 to i32
  store i32 %64, ptr %4, align 4, !tbaa !19
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  %65 = load ptr, ptr %2, align 8, !tbaa !5
  store ptr %65, ptr %5, align 8, !tbaa !5
  %66 = load i32, ptr %4, align 4, !tbaa !19
  %67 = icmp slt i32 %66, 128
  br i1 %67, label %68, label %103

68:                                               ; preds = %60
  %69 = load ptr, ptr %3, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.asio_backend_t, ptr %69, i32 0, i32 3
  %71 = load i32, ptr %4, align 4, !tbaa !19
  %72 = sext i32 %71 to i64
  %73 = getelementptr inbounds [128 x ptr], ptr %70, i64 0, i64 %72
  store ptr null, ptr %6, align 8, !tbaa !5
  %74 = load i64, ptr %5, align 8
  %75 = load i64, ptr %6, align 8
  %76 = cmpxchg ptr %73, i64 %74, i64 %75 acq_rel acquire, align 8
  %77 = extractvalue { i64, i1 } %76, 0
  %78 = extractvalue { i64, i1 } %76, 1
  br i1 %78, label %80, label %79

79:                                               ; preds = %68
  store i64 %77, ptr %5, align 8
  br label %80

80:                                               ; preds = %79, %68
  %81 = zext i1 %78 to i8
  store i8 %81, ptr %7, align 1, !tbaa !20
  %82 = load i8, ptr %7, align 1, !tbaa !20, !range !21, !noundef !22
  %83 = trunc i8 %82 to i1
  br i1 %83, label %84, label %103

84:                                               ; preds = %80
  call void @llvm.lifetime.start.p0(i64 152, ptr %8) #4
  %85 = load i32, ptr %4, align 4, !tbaa !19
  %86 = icmp eq i32 %85, 12
  br i1 %86, label %87, label %89

87:                                               ; preds = %84
  %88 = getelementptr inbounds %struct.sigaction, ptr %8, i32 0, i32 0
  store ptr @empty_signal_handler, ptr %88, align 8, !tbaa !23
  br label %91

89:                                               ; preds = %84
  %90 = getelementptr inbounds %struct.sigaction, ptr %8, i32 0, i32 0
  store ptr null, ptr %90, align 8, !tbaa !23
  br label %91

91:                                               ; preds = %89, %87
  %92 = getelementptr inbounds %struct.sigaction, ptr %8, i32 0, i32 1
  %93 = call i32 @sigemptyset(ptr noundef %92) #4
  %94 = getelementptr inbounds %struct.sigaction, ptr %8, i32 0, i32 2
  store i32 268435456, ptr %94, align 8, !tbaa !24
  %95 = load i32, ptr %4, align 4, !tbaa !19
  %96 = call i32 @sigaction(i32 noundef %95, ptr noundef %8, ptr noundef null) #4
  %97 = load ptr, ptr %2, align 8, !tbaa !5
  %98 = getelementptr inbounds %struct.asio_event_t, ptr %97, i32 0, i32 3
  %99 = load i32, ptr %98, align 4, !tbaa !17
  %100 = call i32 @close(i32 noundef %99)
  %101 = load ptr, ptr %2, align 8, !tbaa !5
  %102 = getelementptr inbounds %struct.asio_event_t, ptr %101, i32 0, i32 3
  store i32 -1, ptr %102, align 4, !tbaa !17
  call void @llvm.lifetime.end.p0(i64 152, ptr %8) #4
  br label %103

103:                                              ; preds = %91, %80, %60
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  br label %104

104:                                              ; preds = %103, %54
  %105 = load ptr, ptr %2, align 8, !tbaa !5
  %106 = getelementptr inbounds %struct.asio_event_t, ptr %105, i32 0, i32 4
  store i32 0, ptr %106, align 8, !tbaa !9
  %107 = load ptr, ptr %2, align 8, !tbaa !5
  call void @send_request(ptr noundef %107, i32 noundef 0)
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  br label %108

108:                                              ; preds = %104, %21
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden void @send_request(ptr noundef, i32 noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { 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 #3 = { "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 #4 = { 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 24}
!10 = !{!"asio_event_t", !6, i64 0, !6, i64 8, !11, i64 16, !11, i64 20, !11, i64 24, !12, i64 28, !12, i64 29, !12, i64 30, !13, i64 32}
!11 = !{!"int", !7, i64 0}
!12 = !{!"_Bool", !7, i64 0}
!13 = !{!"long", !7, i64 0}
!14 = !{!15, !11, i64 0}
!15 = !{!"asio_backend_t", !11, i64 0, !11, i64 4, !7, i64 8, !7, i64 776, !7, i64 1800, !16, i64 1808}
!16 = !{!"messageq_t", !7, i64 0, !6, i64 8}
!17 = !{!10, !11, i64 20}
!18 = !{!10, !13, i64 32}
!19 = !{!11, !11, i64 0}
!20 = !{!12, !12, i64 0}
!21 = !{i8 0, i8 2}
!22 = !{}
!23 = !{!7, !7, i64 0}
!24 = !{!25, !11, i64 136}
!25 = !{!"sigaction", !7, i64 0, !26, i64 8, !11, i64 136, !6, i64 144}
!26 = !{!"", !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.sigaction = type { %union.anon, %struct.__sigset_t, i32, ptr }
%union.anon = type { ptr }
%struct.__sigset_t = type { [16 x i64] }

@.str = external hidden unnamed_addr constant [2 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [75 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [10 x i8], align 1
@__func__.pony_asio_event_unsubscribe = external hidden unnamed_addr constant [28 x i8], align 1

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

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

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

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

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

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

declare void @ponyint_assert_fail(ptr noundef, ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #3

declare ptr @ponyint_asio_get_backend(...) local_unnamed_addr #3

declare i32 @close(i32 noundef) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
define dso_local void @pony_asio_event_unsubscribe(ptr noundef %0) local_unnamed_addr #2 {
  %2 = alloca %struct.sigaction, align 8
  %3 = icmp eq ptr %0, null
  br i1 %3, label %7, label %4

4:                                                ; preds = %1
  %5 = getelementptr inbounds i8, ptr %0, i64 24
  %6 = load i32, ptr %5, align 8, !tbaa !5
  switch i32 %6, label %8 [
    i32 0, label %7
    i32 -1, label %7
  ]

7:                                                ; preds = %4, %4, %1
  tail call void @ponyint_assert_fail(ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i64 noundef 455, ptr noundef nonnull @__func__.pony_asio_event_unsubscribe) #4
  br label %46

8:                                                ; preds = %4
  %9 = tail call ptr (...) @ponyint_asio_get_backend() #4
  %.not = icmp eq ptr %9, null
  br i1 %.not, label %10, label %11

10:                                               ; preds = %8
  tail call void @ponyint_assert_fail(ptr noundef nonnull @.str.2, ptr noundef nonnull @.str.1, i64 noundef 460, ptr noundef nonnull @__func__.pony_asio_event_unsubscribe) #4
  br label %11

11:                                               ; preds = %8, %10
  %12 = load i32, ptr %9, align 8, !tbaa !13
  %13 = getelementptr inbounds i8, ptr %0, i64 20
  %14 = load i32, ptr %13, align 4, !tbaa !16
  %15 = tail call i32 @epoll_ctl(i32 noundef %12, i32 noundef 2, i32 noundef %14, ptr noundef null) #4
  %16 = load i32, ptr %5, align 8, !tbaa !5
  %17 = and i32 %16, 4
  %.not27 = icmp eq i32 %17, 0
  br i1 %.not27, label %22, label %18

18:                                               ; preds = %11
  %19 = load i32, ptr %13, align 4, !tbaa !16
  %.not28 = icmp eq i32 %19, -1
  br i1 %.not28, label %22, label %20

20:                                               ; preds = %18
  %21 = tail call i32 @close(i32 noundef %19) #4
  store i32 -1, ptr %13, align 4, !tbaa !16
  %.pre = load i32, ptr %5, align 8, !tbaa !5
  br label %22

22:                                               ; preds = %18, %20, %11
  %23 = phi i32 [ %16, %18 ], [ %.pre, %20 ], [ %16, %11 ]
  %24 = and i32 %23, 8
  %.not29 = icmp eq i32 %24, 0
  br i1 %.not29, label %45, label %25

25:                                               ; preds = %22
  %26 = getelementptr inbounds i8, ptr %0, i64 32
  %27 = load i64, ptr %26, align 8, !tbaa !17
  %28 = trunc i64 %27 to i32
  %29 = icmp slt i32 %28, 128
  br i1 %29, label %30, label %45

30:                                               ; preds = %25
  %31 = getelementptr inbounds i8, ptr %9, i64 776
  %sext = shl i64 %27, 32
  %32 = ashr exact i64 %sext, 32
  %33 = getelementptr inbounds [128 x ptr], ptr %31, i64 0, i64 %32
  %34 = ptrtoint ptr %0 to i64
  %35 = cmpxchg ptr %33, i64 %34, i64 0 acq_rel acquire, align 8
  %36 = extractvalue { i64, i1 } %35, 1
  br i1 %36, label %37, label %45

37:                                               ; preds = %30
  call void @llvm.lifetime.start.p0(i64 152, ptr nonnull %2) #4
  %38 = icmp eq i32 %28, 12
  %empty_signal_handler. = select i1 %38, ptr @empty_signal_handler, ptr null
  store ptr %empty_signal_handler., ptr %2, align 8, !tbaa !18
  %39 = getelementptr inbounds i8, ptr %2, i64 8
  %40 = call i32 @sigemptyset(ptr noundef nonnull %39) #4
  %41 = getelementptr inbounds i8, ptr %2, i64 136
  store i32 268435456, ptr %41, align 8, !tbaa !19
  %42 = call i32 @sigaction(i32 noundef %28, ptr noundef nonnull %2, ptr noundef null) #4
  %43 = load i32, ptr %13, align 4, !tbaa !16
  %44 = call i32 @close(i32 noundef %43) #4
  store i32 -1, ptr %13, align 4, !tbaa !16
  call void @llvm.lifetime.end.p0(i64 152, ptr nonnull %2) #4
  br label %45

45:                                               ; preds = %25, %30, %37, %22
  store i32 0, ptr %5, align 8, !tbaa !5
  call void @send_request(ptr noundef nonnull %0, i32 noundef 0)
  br label %46

46:                                               ; preds = %45, %7
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden void @send_request(ptr noundef, i32 noundef) local_unnamed_addr #2

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { 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 #3 = { "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 #4 = { 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, !10, i64 24}
!6 = !{!"asio_event_t", !7, i64 0, !7, i64 8, !10, i64 16, !10, i64 20, !10, i64 24, !11, i64 28, !11, i64 29, !11, i64 30, !12, i64 32}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"_Bool", !8, i64 0}
!12 = !{!"long", !8, i64 0}
!13 = !{!14, !10, i64 0}
!14 = !{!"asio_backend_t", !10, i64 0, !10, i64 4, !8, i64 8, !8, i64 776, !8, i64 1800, !15, i64 1808}
!15 = !{!"messageq_t", !8, i64 0, !7, i64 8}
!16 = !{!6, !10, i64 20}
!17 = !{!6, !12, i64 32}
!18 = !{!8, !8, i64 0}
!19 = !{!20, !10, i64 136}
!20 = !{!"sigaction", !8, i64 0, !21, i64 8, !10, i64 136, !7, i64 144}
!21 = !{!"", !8, i64 0}


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