; LLVM IR from dnbaker/sketch :: kthread.ll (Z10kt_forpoolPvPFvS_liES_l)

; 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.kt_forpool_t = type { i32, i32, i64, ptr, ptr, ptr, ptr, %union.pthread_mutex_t, %union.pthread_cond_t, %union.pthread_cond_t }
%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }
%union.pthread_cond_t = type { %struct.__pthread_cond_s }
%struct.__pthread_cond_s = type { %union.__atomic_wide_counter, %union.__atomic_wide_counter, [2 x i32], [2 x i32], i32, i32, [2 x i32] }
%union.__atomic_wide_counter = type { i64 }
%struct.kto_worker_t = type { ptr, i64, i32 }

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

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

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

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

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

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

; Function Attrs: mustprogress uwtable
define dso_local void @_Z10kt_forpoolPvPFvS_liES_l(ptr noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3) #3 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  store i64 %3, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %11, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %12 = load ptr, ptr %9, align 8, !tbaa !5
  %13 = icmp ne ptr %12, null
  br i1 %13, label %14, label %80

14:                                               ; preds = %4
  %15 = load ptr, ptr %9, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.kt_forpool_t, ptr %15, i32 0, i32 0
  %17 = load i32, ptr %16, align 8, !tbaa !11
  %18 = icmp sgt i32 %17, 1
  br i1 %18, label %19, label %80

19:                                               ; preds = %14
  %20 = load i64, ptr %8, align 8, !tbaa !9
  %21 = load ptr, ptr %9, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.kt_forpool_t, ptr %21, i32 0, i32 2
  store i64 %20, ptr %22, align 8, !tbaa !14
  %23 = load ptr, ptr %6, align 8, !tbaa !5
  %24 = load ptr, ptr %9, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.kt_forpool_t, ptr %24, i32 0, i32 5
  store ptr %23, ptr %25, align 8, !tbaa !15
  %26 = load ptr, ptr %7, align 8, !tbaa !5
  %27 = load ptr, ptr %9, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.kt_forpool_t, ptr %27, i32 0, i32 6
  store ptr %26, ptr %28, align 8, !tbaa !16
  %29 = load ptr, ptr %9, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.kt_forpool_t, ptr %29, i32 0, i32 0
  %31 = load i32, ptr %30, align 8, !tbaa !11
  %32 = load ptr, ptr %9, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.kt_forpool_t, ptr %32, i32 0, i32 1
  store i32 %31, ptr %33, align 4, !tbaa !17
  store i64 0, ptr %10, align 8, !tbaa !9
  br label %34

34:                                               ; preds = %55, %19
  %35 = load i64, ptr %10, align 8, !tbaa !9
  %36 = load ptr, ptr %9, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.kt_forpool_t, ptr %36, i32 0, i32 0
  %38 = load i32, ptr %37, align 8, !tbaa !11
  %39 = sext i32 %38 to i64
  %40 = icmp slt i64 %35, %39
  br i1 %40, label %41, label %58

41:                                               ; preds = %34
  %42 = load i64, ptr %10, align 8, !tbaa !9
  %43 = load ptr, ptr %9, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.kt_forpool_t, ptr %43, i32 0, i32 4
  %45 = load ptr, ptr %44, align 8, !tbaa !18
  %46 = load i64, ptr %10, align 8, !tbaa !9
  %47 = getelementptr inbounds %struct.kto_worker_t, ptr %45, i64 %46
  %48 = getelementptr inbounds %struct.kto_worker_t, ptr %47, i32 0, i32 1
  store i64 %42, ptr %48, align 8, !tbaa !19
  %49 = load ptr, ptr %9, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.kt_forpool_t, ptr %49, i32 0, i32 4
  %51 = load ptr, ptr %50, align 8, !tbaa !18
  %52 = load i64, ptr %10, align 8, !tbaa !9
  %53 = getelementptr inbounds %struct.kto_worker_t, ptr %51, i64 %52
  %54 = getelementptr inbounds %struct.kto_worker_t, ptr %53, i32 0, i32 2
  store i32 1, ptr %54, align 8, !tbaa !21
  br label %55

55:                                               ; preds = %41
  %56 = load i64, ptr %10, align 8, !tbaa !9
  %57 = add nsw i64 %56, 1
  store i64 %57, ptr %10, align 8, !tbaa !9
  br label %34, !llvm.loop !22

58:                                               ; preds = %34
  %59 = load ptr, ptr %9, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.kt_forpool_t, ptr %59, i32 0, i32 7
  %61 = call i32 @pthread_mutex_lock(ptr noundef %60) #4
  %62 = load ptr, ptr %9, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.kt_forpool_t, ptr %62, i32 0, i32 9
  %64 = call i32 @pthread_cond_broadcast(ptr noundef %63) #4
  br label %65

65:                                               ; preds = %70, %58
  %66 = load ptr, ptr %9, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.kt_forpool_t, ptr %66, i32 0, i32 1
  %68 = load i32, ptr %67, align 4, !tbaa !17
  %69 = icmp ne i32 %68, 0
  br i1 %69, label %70, label %76

70:                                               ; preds = %65
  %71 = load ptr, ptr %9, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.kt_forpool_t, ptr %71, i32 0, i32 8
  %73 = load ptr, ptr %9, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.kt_forpool_t, ptr %73, i32 0, i32 7
  %75 = call i32 @pthread_cond_wait(ptr noundef %72, ptr noundef %74)
  br label %65, !llvm.loop !24

76:                                               ; preds = %65
  %77 = load ptr, ptr %9, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.kt_forpool_t, ptr %77, i32 0, i32 7
  %79 = call i32 @pthread_mutex_unlock(ptr noundef %78) #4
  br label %93

80:                                               ; preds = %14, %4
  store i64 0, ptr %10, align 8, !tbaa !9
  br label %81

81:                                               ; preds = %89, %80
  %82 = load i64, ptr %10, align 8, !tbaa !9
  %83 = load i64, ptr %8, align 8, !tbaa !9
  %84 = icmp slt i64 %82, %83
  br i1 %84, label %85, label %92

85:                                               ; preds = %81
  %86 = load ptr, ptr %6, align 8, !tbaa !5
  %87 = load ptr, ptr %7, align 8, !tbaa !5
  %88 = load i64, ptr %10, align 8, !tbaa !9
  call void %86(ptr noundef %87, i64 noundef %88, i32 noundef 0)
  br label %89

89:                                               ; preds = %85
  %90 = load i64, ptr %10, align 8, !tbaa !9
  %91 = add nsw i64 %90, 1
  store i64 %91, ptr %10, align 8, !tbaa !9
  br label %81, !llvm.loop !25

92:                                               ; preds = %81
  br label %93

93:                                               ; preds = %92, %76
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  ret void
}

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 = { "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 = !{!"long", !7, i64 0}
!11 = !{!12, !13, i64 0}
!12 = !{!"_ZTS12kt_forpool_t", !13, i64 0, !13, i64 4, !10, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !7, i64 48, !7, i64 88, !7, i64 136}
!13 = !{!"int", !7, i64 0}
!14 = !{!12, !10, i64 8}
!15 = !{!12, !6, i64 32}
!16 = !{!12, !6, i64 40}
!17 = !{!12, !13, i64 4}
!18 = !{!12, !6, i64 24}
!19 = !{!20, !10, i64 8}
!20 = !{!"_ZTS12kto_worker_t", !6, i64 0, !10, i64 8, !13, i64 16}
!21 = !{!20, !13, i64 16}
!22 = distinct !{!22, !23}
!23 = !{!"llvm.loop.mustprogress"}
!24 = distinct !{!24, !23}
!25 = distinct !{!25, !23}


; 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.kto_worker_t = type { ptr, i64, i32 }

; Function Attrs: nounwind
declare i32 @pthread_mutex_lock(ptr noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind
declare i32 @pthread_mutex_unlock(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind
declare i32 @pthread_cond_broadcast(ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local void @_Z10kt_forpoolPvPFvS_liES_l(ptr noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3) local_unnamed_addr #2 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %35, label %5

5:                                                ; preds = %4
  %6 = load i32, ptr %0, align 8, !tbaa !5
  %7 = icmp sgt i32 %6, 1
  br i1 %7, label %.new, label %35

.new:                                             ; preds = %5
  %8 = getelementptr inbounds i8, ptr %0, i64 8
  store i64 %3, ptr %8, align 8, !tbaa !12
  %9 = getelementptr inbounds i8, ptr %0, i64 32
  store ptr %1, ptr %9, align 8, !tbaa !13
  %10 = getelementptr inbounds i8, ptr %0, i64 40
  store ptr %2, ptr %10, align 8, !tbaa !14
  %11 = getelementptr inbounds i8, ptr %0, i64 4
  store i32 %6, ptr %11, align 4, !tbaa !15
  %12 = zext nneg i32 %6 to i64
  %13 = getelementptr inbounds i8, ptr %0, i64 24
  %14 = load ptr, ptr %13, align 8, !tbaa !16
  %xtraiter = and i64 %12, 1
  %unroll_iter = and i64 %12, 2147483646
  br label %15

15:                                               ; preds = %15, %.new
  %.032 = phi i64 [ 0, %.new ], [ %21, %15 ]
  %niter = phi i64 [ 0, %.new ], [ %niter.next.1, %15 ]
  %16 = getelementptr inbounds %struct.kto_worker_t, ptr %14, i64 %.032, i32 1
  store i64 %.032, ptr %16, align 8, !tbaa !17
  %17 = getelementptr inbounds %struct.kto_worker_t, ptr %14, i64 %.032, i32 2
  store i32 1, ptr %17, align 8, !tbaa !19
  %18 = or disjoint i64 %.032, 1
  %19 = getelementptr inbounds %struct.kto_worker_t, ptr %14, i64 %18, i32 1
  store i64 %18, ptr %19, align 8, !tbaa !17
  %20 = getelementptr inbounds %struct.kto_worker_t, ptr %14, i64 %18, i32 2
  store i32 1, ptr %20, align 8, !tbaa !19
  %21 = add nuw nsw i64 %.032, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %.unr-lcssa, label %15, !llvm.loop !20

.unr-lcssa:                                       ; preds = %15
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %24, label %.epilog-lcssa

.epilog-lcssa:                                    ; preds = %.unr-lcssa
  %22 = getelementptr inbounds %struct.kto_worker_t, ptr %14, i64 %21, i32 1
  store i64 %21, ptr %22, align 8, !tbaa !17
  %23 = getelementptr inbounds %struct.kto_worker_t, ptr %14, i64 %21, i32 2
  store i32 1, ptr %23, align 8, !tbaa !19
  br label %24

24:                                               ; preds = %.unr-lcssa, %.epilog-lcssa
  %25 = getelementptr inbounds i8, ptr %0, i64 48
  %26 = tail call i32 @pthread_mutex_lock(ptr noundef nonnull %25) #3
  %27 = getelementptr inbounds i8, ptr %0, i64 136
  %28 = tail call i32 @pthread_cond_broadcast(ptr noundef nonnull %27) #3
  %29 = load i32, ptr %11, align 4, !tbaa !15
  %.not3133 = icmp eq i32 %29, 0
  br i1 %.not3133, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %24
  %30 = getelementptr inbounds i8, ptr %0, i64 88
  br label %31

31:                                               ; preds = %.lr.ph, %31
  %32 = tail call i32 @pthread_cond_wait(ptr noundef nonnull %30, ptr noundef nonnull %25)
  %33 = load i32, ptr %11, align 4, !tbaa !15
  %.not31 = icmp eq i32 %33, 0
  br i1 %.not31, label %._crit_edge, label %31, !llvm.loop !22

._crit_edge:                                      ; preds = %31, %24
  %34 = tail call i32 @pthread_mutex_unlock(ptr noundef nonnull %25) #3
  br label %.loopexit

35:                                               ; preds = %5, %4
  %36 = icmp sgt i64 %3, 0
  br i1 %36, label %.lr.ph36, label %.loopexit

.lr.ph36:                                         ; preds = %35, %.lr.ph36
  %.134 = phi i64 [ %37, %.lr.ph36 ], [ 0, %35 ]
  tail call void %1(ptr noundef %2, i64 noundef %.134, i32 noundef 0)
  %37 = add nuw nsw i64 %.134, 1
  %exitcond37.not = icmp eq i64 %37, %3
  br i1 %exitcond37.not, label %.loopexit, label %.lr.ph36, !llvm.loop !23

.loopexit:                                        ; preds = %.lr.ph36, %35, %._crit_edge
  ret void
}

attributes #0 = { 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 #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 = { 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 = { 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 = !{!"_ZTS12kt_forpool_t", !7, i64 0, !7, i64 4, !10, i64 8, !11, i64 16, !11, i64 24, !11, i64 32, !11, i64 40, !8, i64 48, !8, i64 88, !8, i64 136}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"any pointer", !8, i64 0}
!12 = !{!6, !10, i64 8}
!13 = !{!6, !11, i64 32}
!14 = !{!6, !11, i64 40}
!15 = !{!6, !7, i64 4}
!16 = !{!6, !11, i64 24}
!17 = !{!18, !10, i64 8}
!18 = !{!"_ZTS12kto_worker_t", !11, i64 0, !10, i64 8, !7, i64 16}
!19 = !{!18, !7, i64 16}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}
!22 = distinct !{!22, !21}
!23 = distinct !{!23, !21}


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