; LLVM IR from microsoft/LightAIMD :: src/einsum.ll_einsum_mn_mp (np_parallel)

; 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.task_func_einsum_mn_mp__np_arg = type { ptr, ptr, ptr, i64, i64, i64, i64 }
%struct.threadpool_context = type { i64, ptr, i64 }

; 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

declare ptr @x_malloc(i64 noundef) #1

declare void @threadpool_add_task(ptr noundef, i64 noundef, ptr noundef, ptr noundef) #1

declare void @threadpool_wait_job_done(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @task_func_einsum_mn_mp__np(ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local void @einsum_mn_mp__np_parallel(ptr noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3, i64 noundef %4, i64 noundef %5, ptr noundef %6) #2 {
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i64, align 8
  %12 = alloca i64, align 8
  %13 = alloca i64, align 8
  %14 = alloca ptr, align 8
  %15 = alloca i64, align 8
  %16 = alloca ptr, align 8
  store ptr %0, ptr %8, align 8, !tbaa !5
  store ptr %1, ptr %9, align 8, !tbaa !5
  store ptr %2, ptr %10, align 8, !tbaa !5
  store i64 %3, ptr %11, align 8, !tbaa !9
  store i64 %4, ptr %12, align 8, !tbaa !9
  store i64 %5, ptr %13, align 8, !tbaa !9
  store ptr %6, ptr %14, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #3
  store i64 0, ptr %15, align 8, !tbaa !9
  br label %17

17:                                               ; preds = %52, %7
  %18 = load i64, ptr %15, align 8, !tbaa !9
  %19 = load i64, ptr %12, align 8, !tbaa !9
  %20 = icmp ult i64 %18, %19
  br i1 %20, label %22, label %21

21:                                               ; preds = %17
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #3
  br label %55

22:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #3
  %23 = call ptr @x_malloc(i64 noundef 56)
  store ptr %23, ptr %16, align 8, !tbaa !5
  %24 = load ptr, ptr %8, align 8, !tbaa !5
  %25 = load ptr, ptr %16, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.task_func_einsum_mn_mp__np_arg, ptr %25, i32 0, i32 0
  store ptr %24, ptr %26, align 8, !tbaa !11
  %27 = load ptr, ptr %9, align 8, !tbaa !5
  %28 = load ptr, ptr %16, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.task_func_einsum_mn_mp__np_arg, ptr %28, i32 0, i32 1
  store ptr %27, ptr %29, align 8, !tbaa !13
  %30 = load ptr, ptr %10, align 8, !tbaa !5
  %31 = load ptr, ptr %16, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.task_func_einsum_mn_mp__np_arg, ptr %31, i32 0, i32 2
  store ptr %30, ptr %32, align 8, !tbaa !14
  %33 = load i64, ptr %11, align 8, !tbaa !9
  %34 = load ptr, ptr %16, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.task_func_einsum_mn_mp__np_arg, ptr %34, i32 0, i32 3
  store i64 %33, ptr %35, align 8, !tbaa !15
  %36 = load i64, ptr %12, align 8, !tbaa !9
  %37 = load ptr, ptr %16, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.task_func_einsum_mn_mp__np_arg, ptr %37, i32 0, i32 4
  store i64 %36, ptr %38, align 8, !tbaa !16
  %39 = load i64, ptr %13, align 8, !tbaa !9
  %40 = load ptr, ptr %16, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.task_func_einsum_mn_mp__np_arg, ptr %40, i32 0, i32 5
  store i64 %39, ptr %41, align 8, !tbaa !17
  %42 = load i64, ptr %15, align 8, !tbaa !9
  %43 = load ptr, ptr %16, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.task_func_einsum_mn_mp__np_arg, ptr %43, i32 0, i32 6
  store i64 %42, ptr %44, align 8, !tbaa !18
  %45 = load ptr, ptr %14, align 8, !tbaa !5
  %46 = load i64, ptr %15, align 8, !tbaa !9
  %47 = load ptr, ptr %14, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.threadpool_context, ptr %47, i32 0, i32 0
  %49 = load i64, ptr %48, align 8, !tbaa !19
  %50 = urem i64 %46, %49
  %51 = load ptr, ptr %16, align 8, !tbaa !5
  call void @threadpool_add_task(ptr noundef %45, i64 noundef %50, ptr noundef @task_func_einsum_mn_mp__np, ptr noundef %51)
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #3
  br label %52

52:                                               ; preds = %22
  %53 = load i64, ptr %15, align 8, !tbaa !9
  %54 = add i64 %53, 1
  store i64 %54, ptr %15, align 8, !tbaa !9
  br label %17, !llvm.loop !21

55:                                               ; preds = %21
  %56 = load ptr, ptr %14, align 8, !tbaa !5
  call void @threadpool_wait_job_done(ptr noundef %56)
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"task_func_einsum_mn_mp__np_arg", !6, i64 0, !6, i64 8, !6, i64 16, !10, i64 24, !10, i64 32, !10, i64 40, !10, i64 48}
!13 = !{!12, !6, i64 8}
!14 = !{!12, !6, i64 16}
!15 = !{!12, !10, i64 24}
!16 = !{!12, !10, i64 32}
!17 = !{!12, !10, i64 40}
!18 = !{!12, !10, i64 48}
!19 = !{!20, !10, i64 0}
!20 = !{!"threadpool_context", !10, i64 0, !6, i64 8, !7, i64 16}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}


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

declare ptr @x_malloc(i64 noundef) local_unnamed_addr #0

declare void @threadpool_add_task(ptr noundef, i64 noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

declare void @threadpool_wait_job_done(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @task_func_einsum_mn_mp__np(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @einsum_mn_mp__np_parallel(ptr noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3, i64 noundef %4, i64 noundef %5, ptr noundef %6) local_unnamed_addr #1 {
  %.not = icmp eq i64 %4, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %7
  tail call void @threadpool_wait_job_done(ptr noundef %6) #2
  ret void

.lr.ph:                                           ; preds = %7, %.lr.ph
  %.022 = phi i64 [ %17, %.lr.ph ], [ 0, %7 ]
  %8 = tail call ptr @x_malloc(i64 noundef 56) #2
  store ptr %0, ptr %8, align 8, !tbaa !5
  %9 = getelementptr inbounds i8, ptr %8, i64 8
  store ptr %1, ptr %9, align 8, !tbaa !11
  %10 = getelementptr inbounds i8, ptr %8, i64 16
  store ptr %2, ptr %10, align 8, !tbaa !12
  %11 = getelementptr inbounds i8, ptr %8, i64 24
  store i64 %3, ptr %11, align 8, !tbaa !13
  %12 = getelementptr inbounds i8, ptr %8, i64 32
  store i64 %4, ptr %12, align 8, !tbaa !14
  %13 = getelementptr inbounds i8, ptr %8, i64 40
  store i64 %5, ptr %13, align 8, !tbaa !15
  %14 = getelementptr inbounds i8, ptr %8, i64 48
  store i64 %.022, ptr %14, align 8, !tbaa !16
  %15 = load i64, ptr %6, align 8, !tbaa !17
  %16 = urem i64 %.022, %15
  tail call void @threadpool_add_task(ptr noundef nonnull %6, i64 noundef %16, ptr noundef nonnull @task_func_einsum_mn_mp__np, ptr noundef nonnull %8) #2
  %17 = add nuw i64 %.022, 1
  %exitcond.not = icmp eq i64 %17, %4
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !19
}

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 = { 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 #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, !7, i64 0}
!6 = !{!"task_func_einsum_mn_mp__np_arg", !7, i64 0, !7, i64 8, !7, i64 16, !10, i64 24, !10, i64 32, !10, i64 40, !10, i64 48}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!6, !7, i64 8}
!12 = !{!6, !7, i64 16}
!13 = !{!6, !10, i64 24}
!14 = !{!6, !10, i64 32}
!15 = !{!6, !10, i64 40}
!16 = !{!6, !10, i64 48}
!17 = !{!18, !10, i64 0}
!18 = !{!"threadpool_context", !10, i64 0, !7, i64 8, !8, i64 16}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}


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