; LLVM IR from tyoma/agge :: src/agge/parallel.ll (ZN4agge8parallel4callERNS0_15kernel_functionE)

; 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"

%"class.agge::parallel" = type { [8 x i8], %"class.agge::raw_memory_object", i32, [4 x i8] }
%"class.agge::raw_memory_object" = type <{ ptr, i32, [4 x i8] }>
%"struct.agge::parallel::thread" = type { [8 x i8], %"class.agge::hybrid_event", %"class.agge::hybrid_event", ptr, i32, %"class.agge::thread" }
%"class.agge::hybrid_event" = type { i64, ptr }
%"class.agge::thread" = type { ptr, ptr, ptr }

declare void @_ZN4agge12hybrid_event6signalEv(ptr noundef nonnull align 8 dereferenceable(16)) #0

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

declare void @_ZN4agge12hybrid_event4waitEv(ptr noundef nonnull align 8 dereferenceable(16)) #0

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

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef ptr @_ZN4agge17raw_memory_object3getINS_8parallel6threadEEEPT_j(ptr noundef nonnull align 8 dereferenceable(12), i32 noundef) #2 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN4agge8parallel4callERNS0_15kernel_functionE(ptr noundef nonnull align 8 dereferenceable(28) %0, ptr noundef nonnull align 8 dereferenceable(8) %1) #3 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  %8 = load ptr, ptr %3, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  %9 = getelementptr inbounds %"class.agge::parallel", ptr %8, i32 0, i32 1
  %10 = call noundef ptr @_ZN4agge17raw_memory_object3getINS_8parallel6threadEEEPT_j(ptr noundef nonnull align 8 dereferenceable(12) %9, i32 noundef 0)
  store ptr %10, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %11

11:                                               ; preds = %29, %2
  %12 = load i32, ptr %6, align 4, !tbaa !9
  %13 = getelementptr inbounds %"class.agge::parallel", ptr %8, i32 0, i32 2
  %14 = load i32, ptr %13, align 8, !tbaa !11
  %15 = icmp ne i32 %12, %14
  br i1 %15, label %17, label %16

16:                                               ; preds = %11
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  br label %32

17:                                               ; preds = %11
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  %20 = load i32, ptr %6, align 4, !tbaa !9
  %21 = zext i32 %20 to i64
  %22 = getelementptr inbounds %"struct.agge::parallel::thread", ptr %19, i64 %21
  %23 = getelementptr inbounds %"struct.agge::parallel::thread", ptr %22, i32 0, i32 3
  store ptr %18, ptr %23, align 8, !tbaa !14
  %24 = load ptr, ptr %5, align 8, !tbaa !5
  %25 = load i32, ptr %6, align 4, !tbaa !9
  %26 = zext i32 %25 to i64
  %27 = getelementptr inbounds %"struct.agge::parallel::thread", ptr %24, i64 %26
  %28 = getelementptr inbounds %"struct.agge::parallel::thread", ptr %27, i32 0, i32 1
  call void @_ZN4agge12hybrid_event6signalEv(ptr noundef nonnull align 8 dereferenceable(16) %28)
  br label %29

29:                                               ; preds = %17
  %30 = load i32, ptr %6, align 4, !tbaa !9
  %31 = add i32 %30, 1
  store i32 %31, ptr %6, align 4, !tbaa !9
  br label %11, !llvm.loop !19

32:                                               ; preds = %16
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = load ptr, ptr %33, align 8, !tbaa !21
  %35 = getelementptr inbounds ptr, ptr %34, i64 0
  %36 = load ptr, ptr %35, align 8
  call void %36(ptr noundef nonnull align 8 dereferenceable(8) %33, i32 noundef 0)
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %37

37:                                               ; preds = %49, %32
  %38 = load i32, ptr %7, align 4, !tbaa !9
  %39 = getelementptr inbounds %"class.agge::parallel", ptr %8, i32 0, i32 2
  %40 = load i32, ptr %39, align 8, !tbaa !11
  %41 = icmp ne i32 %38, %40
  br i1 %41, label %43, label %42

42:                                               ; preds = %37
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  br label %52

43:                                               ; preds = %37
  %44 = load ptr, ptr %5, align 8, !tbaa !5
  %45 = load i32, ptr %7, align 4, !tbaa !9
  %46 = zext i32 %45 to i64
  %47 = getelementptr inbounds %"struct.agge::parallel::thread", ptr %44, i64 %46
  %48 = getelementptr inbounds %"struct.agge::parallel::thread", ptr %47, i32 0, i32 2
  call void @_ZN4agge12hybrid_event4waitEv(ptr noundef nonnull align 8 dereferenceable(16) %48)
  br label %49

49:                                               ; preds = %43
  %50 = load i32, ptr %7, align 4, !tbaa !9
  %51 = add i32 %50, 1
  store i32 %51, ptr %7, align 4, !tbaa !9
  br label %37, !llvm.loop !23

52:                                               ; preds = %42
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  ret void
}

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint mustprogress 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 = { mustprogress 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 #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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 24}
!12 = !{!"_ZTSN4agge8parallelE", !13, i64 8, !10, i64 24}
!13 = !{!"_ZTSN4agge17raw_memory_objectE", !6, i64 0, !10, i64 8}
!14 = !{!15, !6, i64 40}
!15 = !{!"_ZTSN4agge8parallel6threadE", !16, i64 8, !16, i64 24, !6, i64 40, !10, i64 48, !18, i64 56}
!16 = !{!"_ZTSN4agge12hybrid_eventE", !17, i64 0, !6, i64 8}
!17 = !{!"long", !7, i64 0}
!18 = !{!"_ZTSN4agge6threadE", !6, i64 0, !6, i64 8, !6, i64 16}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = !{!22, !22, i64 0}
!22 = !{!"vtable pointer", !8, i64 0}
!23 = distinct !{!23, !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.agge::parallel::thread" = type { [8 x i8], %"class.agge::hybrid_event", %"class.agge::hybrid_event", ptr, i32, %"class.agge::thread" }
%"class.agge::hybrid_event" = type { i64, ptr }
%"class.agge::thread" = type { ptr, ptr, ptr }

declare void @_ZN4agge12hybrid_event6signalEv(ptr noundef nonnull align 8 dereferenceable(16)) local_unnamed_addr #0

declare void @_ZN4agge12hybrid_event4waitEv(ptr noundef nonnull align 8 dereferenceable(16)) local_unnamed_addr #0

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef ptr @_ZN4agge17raw_memory_object3getINS_8parallel6threadEEEPT_j(ptr noundef nonnull align 8 dereferenceable(12), i32 noundef) local_unnamed_addr #1 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN4agge8parallel4callERNS0_15kernel_functionE(ptr noundef nonnull align 8 dereferenceable(28) %0, ptr noundef nonnull align 8 dereferenceable(8) %1) local_unnamed_addr #2 align 2 {
  %3 = getelementptr inbounds i8, ptr %0, i64 8
  %4 = tail call noundef ptr @_ZN4agge17raw_memory_object3getINS_8parallel6threadEEEPT_j(ptr noundef nonnull align 8 dereferenceable(12) %3, i32 noundef 0)
  %5 = getelementptr inbounds i8, ptr %0, i64 24
  %6 = load i32, ptr %5, align 8, !tbaa !5
  %.not14 = icmp eq i32 %6, 0
  br i1 %.not14, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %2
  %7 = load ptr, ptr %1, align 8, !tbaa !12
  %8 = load ptr, ptr %7, align 8
  tail call void %8(ptr noundef nonnull align 8 dereferenceable(8) %1, i32 noundef 0)
  %9 = load i32, ptr %5, align 8, !tbaa !5
  %.not1316 = icmp eq i32 %9, 0
  br i1 %.not1316, label %._crit_edge20, label %.lr.ph19

.lr.ph:                                           ; preds = %2, %.lr.ph
  %.01215 = phi i32 [ %14, %.lr.ph ], [ 0, %2 ]
  %10 = zext i32 %.01215 to i64
  %11 = getelementptr inbounds %"struct.agge::parallel::thread", ptr %4, i64 %10
  %12 = getelementptr inbounds i8, ptr %11, i64 40
  store ptr %1, ptr %12, align 8, !tbaa !14
  %13 = getelementptr inbounds i8, ptr %11, i64 8
  tail call void @_ZN4agge12hybrid_event6signalEv(ptr noundef nonnull align 8 dereferenceable(16) %13)
  %14 = add i32 %.01215, 1
  %15 = load i32, ptr %5, align 8, !tbaa !5
  %.not = icmp eq i32 %14, %15
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !19

._crit_edge20:                                    ; preds = %.lr.ph19, %._crit_edge
  ret void

.lr.ph19:                                         ; preds = %._crit_edge, %.lr.ph19
  %.017 = phi i32 [ %18, %.lr.ph19 ], [ 0, %._crit_edge ]
  %16 = zext i32 %.017 to i64
  %17 = getelementptr inbounds %"struct.agge::parallel::thread", ptr %4, i64 %16, i32 2
  tail call void @_ZN4agge12hybrid_event4waitEv(ptr noundef nonnull align 8 dereferenceable(16) %17)
  %18 = add i32 %.017, 1
  %19 = load i32, ptr %5, align 8, !tbaa !5
  %.not13 = icmp eq i32 %18, %19
  br i1 %.not13, label %._crit_edge20, label %.lr.ph19, !llvm.loop !21
}

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 = { inlinehint mustprogress 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 = { mustprogress 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, !11, i64 24}
!6 = !{!"_ZTSN4agge8parallelE", !7, i64 8, !11, i64 24}
!7 = !{!"_ZTSN4agge17raw_memory_objectE", !8, i64 0, !11, i64 8}
!8 = !{!"any pointer", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C++ TBAA"}
!11 = !{!"int", !9, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"vtable pointer", !10, i64 0}
!14 = !{!15, !8, i64 40}
!15 = !{!"_ZTSN4agge8parallel6threadE", !16, i64 8, !16, i64 24, !8, i64 40, !11, i64 48, !18, i64 56}
!16 = !{!"_ZTSN4agge12hybrid_eventE", !17, i64 0, !8, i64 8}
!17 = !{!"long", !9, i64 0}
!18 = !{!"_ZTSN4agge6threadE", !8, i64 0, !8, i64 8, !8, i64 16}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = distinct !{!21, !20}


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