; LLVM IR from tsotchke/quantum_geometric_tensor :: src/quantum_geometric/distributed (workload_distribution)

; 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.WorkItem = type { ptr, i64, i32, i32, double }
%struct.WorkloadManager = type { ptr, i32, i32, i8, i64, i8, ptr, i64, %struct.WorkMetrics }
%struct.WorkMetrics = type { i64, i64, i64, i64, i64, double }
%struct.WorkQueue = type { ptr, i64, i64, i64, i64, %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 }

; 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: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

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

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

; Function Attrs: nounwind
declare i32 @pthread_cond_signal(ptr noundef) #2

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

; Function Attrs: nounwind uwtable
define dso_local i32 @get_work(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3) #4 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca %struct.WorkItem, align 8
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store ptr %2, ptr %8, align 8, !tbaa !5
  store ptr %3, ptr %9, align 8, !tbaa !5
  %11 = load ptr, ptr %6, align 8, !tbaa !5
  %12 = icmp ne ptr %11, null
  br i1 %12, label %13, label %22

13:                                               ; preds = %4
  %14 = load ptr, ptr %7, align 8, !tbaa !5
  %15 = icmp ne ptr %14, null
  br i1 %15, label %16, label %22

16:                                               ; preds = %13
  %17 = load ptr, ptr %8, align 8, !tbaa !5
  %18 = icmp ne ptr %17, null
  br i1 %18, label %19, label %22

19:                                               ; preds = %16
  %20 = load ptr, ptr %9, align 8, !tbaa !5
  %21 = icmp ne ptr %20, null
  br i1 %21, label %23, label %22

22:                                               ; preds = %19, %16, %13, %4
  store i32 -1, ptr %5, align 4
  br label %100

23:                                               ; preds = %19
  %24 = load ptr, ptr %6, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.WorkloadManager, ptr %24, i32 0, i32 0
  %26 = load ptr, ptr %25, align 8, !tbaa !9
  %27 = getelementptr inbounds %struct.WorkQueue, ptr %26, i32 0, i32 5
  %28 = call i32 @pthread_mutex_lock(ptr noundef %27) #5
  br label %29

29:                                               ; preds = %36, %23
  %30 = load ptr, ptr %6, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.WorkloadManager, ptr %30, i32 0, i32 0
  %32 = load ptr, ptr %31, align 8, !tbaa !9
  %33 = getelementptr inbounds %struct.WorkQueue, ptr %32, i32 0, i32 2
  %34 = load i64, ptr %33, align 8, !tbaa !16
  %35 = icmp eq i64 %34, 0
  br i1 %35, label %36, label %46

36:                                               ; preds = %29
  %37 = load ptr, ptr %6, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.WorkloadManager, ptr %37, i32 0, i32 0
  %39 = load ptr, ptr %38, align 8, !tbaa !9
  %40 = getelementptr inbounds %struct.WorkQueue, ptr %39, i32 0, i32 6
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.WorkloadManager, ptr %41, i32 0, i32 0
  %43 = load ptr, ptr %42, align 8, !tbaa !9
  %44 = getelementptr inbounds %struct.WorkQueue, ptr %43, i32 0, i32 5
  %45 = call i32 @pthread_cond_wait(ptr noundef %40, ptr noundef %44)
  br label %29, !llvm.loop !18

46:                                               ; preds = %29
  call void @llvm.lifetime.start.p0(i64 32, ptr %10) #5
  %47 = load ptr, ptr %6, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.WorkloadManager, ptr %47, i32 0, i32 0
  %49 = load ptr, ptr %48, align 8, !tbaa !9
  %50 = getelementptr inbounds %struct.WorkQueue, ptr %49, i32 0, i32 0
  %51 = load ptr, ptr %50, align 8, !tbaa !20
  %52 = load ptr, ptr %6, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.WorkloadManager, ptr %52, i32 0, i32 0
  %54 = load ptr, ptr %53, align 8, !tbaa !9
  %55 = getelementptr inbounds %struct.WorkQueue, ptr %54, i32 0, i32 3
  %56 = load i64, ptr %55, align 8, !tbaa !21
  %57 = getelementptr inbounds %struct.WorkItem, ptr %51, i64 %56
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %10, ptr align 8 %57, i64 32, i1 false), !tbaa.struct !22
  %58 = load ptr, ptr %6, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.WorkloadManager, ptr %58, i32 0, i32 0
  %60 = load ptr, ptr %59, align 8, !tbaa !9
  %61 = getelementptr inbounds %struct.WorkQueue, ptr %60, i32 0, i32 3
  %62 = load i64, ptr %61, align 8, !tbaa !21
  %63 = add i64 %62, 1
  %64 = load ptr, ptr %6, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.WorkloadManager, ptr %64, i32 0, i32 0
  %66 = load ptr, ptr %65, align 8, !tbaa !9
  %67 = getelementptr inbounds %struct.WorkQueue, ptr %66, i32 0, i32 1
  %68 = load i64, ptr %67, align 8, !tbaa !26
  %69 = urem i64 %63, %68
  %70 = load ptr, ptr %6, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.WorkloadManager, ptr %70, i32 0, i32 0
  %72 = load ptr, ptr %71, align 8, !tbaa !9
  %73 = getelementptr inbounds %struct.WorkQueue, ptr %72, i32 0, i32 3
  store i64 %69, ptr %73, align 8, !tbaa !21
  %74 = load ptr, ptr %6, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.WorkloadManager, ptr %74, i32 0, i32 0
  %76 = load ptr, ptr %75, align 8, !tbaa !9
  %77 = getelementptr inbounds %struct.WorkQueue, ptr %76, i32 0, i32 2
  %78 = load i64, ptr %77, align 8, !tbaa !16
  %79 = add i64 %78, -1
  store i64 %79, ptr %77, align 8, !tbaa !16
  %80 = load ptr, ptr %6, align 8, !tbaa !5
  %81 = getelementptr inbounds %struct.WorkloadManager, ptr %80, i32 0, i32 0
  %82 = load ptr, ptr %81, align 8, !tbaa !9
  %83 = getelementptr inbounds %struct.WorkQueue, ptr %82, i32 0, i32 7
  %84 = call i32 @pthread_cond_signal(ptr noundef %83) #5
  %85 = load ptr, ptr %6, align 8, !tbaa !5
  %86 = getelementptr inbounds %struct.WorkloadManager, ptr %85, i32 0, i32 0
  %87 = load ptr, ptr %86, align 8, !tbaa !9
  %88 = getelementptr inbounds %struct.WorkQueue, ptr %87, i32 0, i32 5
  %89 = call i32 @pthread_mutex_unlock(ptr noundef %88) #5
  %90 = getelementptr inbounds %struct.WorkItem, ptr %10, i32 0, i32 0
  %91 = load ptr, ptr %90, align 8, !tbaa !27
  %92 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %91, ptr %92, align 8, !tbaa !5
  %93 = getelementptr inbounds %struct.WorkItem, ptr %10, i32 0, i32 1
  %94 = load i64, ptr %93, align 8, !tbaa !29
  %95 = load ptr, ptr %8, align 8, !tbaa !5
  store i64 %94, ptr %95, align 8, !tbaa !23
  %96 = getelementptr inbounds %struct.WorkItem, ptr %10, i32 0, i32 2
  %97 = load i32, ptr %96, align 8, !tbaa !30
  %98 = load ptr, ptr %9, align 8, !tbaa !5
  store i32 %97, ptr %98, align 4, !tbaa !24
  %99 = load ptr, ptr %6, align 8, !tbaa !5
  call void @update_metrics(ptr noundef %99, ptr noundef %10)
  store i32 0, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 32, ptr %10) #5
  br label %100

100:                                              ; preds = %46, %22
  %101 = load i32, ptr %5, align 4
  ret i32 %101
}

; Function Attrs: nounwind uwtable
declare hidden void @update_metrics(ptr noundef, ptr noundef) #4

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #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 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 #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, !6, i64 0}
!10 = !{!"", !6, i64 0, !11, i64 8, !11, i64 12, !12, i64 16, !13, i64 24, !12, i64 32, !6, i64 40, !13, i64 48, !14, i64 56}
!11 = !{!"int", !7, i64 0}
!12 = !{!"_Bool", !7, i64 0}
!13 = !{!"long", !7, i64 0}
!14 = !{!"", !13, i64 0, !13, i64 8, !13, i64 16, !13, i64 24, !13, i64 32, !15, i64 40}
!15 = !{!"double", !7, i64 0}
!16 = !{!17, !13, i64 16}
!17 = !{!"", !6, i64 0, !13, i64 8, !13, i64 16, !13, i64 24, !13, i64 32, !7, i64 40, !7, i64 80, !7, i64 128}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}
!20 = !{!17, !6, i64 0}
!21 = !{!17, !13, i64 24}
!22 = !{i64 0, i64 8, !5, i64 8, i64 8, !23, i64 16, i64 4, !24, i64 20, i64 4, !24, i64 24, i64 8, !25}
!23 = !{!13, !13, i64 0}
!24 = !{!11, !11, i64 0}
!25 = !{!15, !15, i64 0}
!26 = !{!17, !13, i64 8}
!27 = !{!28, !6, i64 0}
!28 = !{!"", !6, i64 0, !13, i64 8, !11, i64 16, !11, i64 20, !15, i64 24}
!29 = !{!28, !13, i64 8}
!30 = !{!28, !11, i64 16}


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

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

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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

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

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

; Function Attrs: nounwind
declare i32 @pthread_cond_signal(ptr noundef) local_unnamed_addr #2

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @get_work(ptr noundef %0, ptr noundef writeonly %1, ptr noundef writeonly %2, ptr noundef writeonly %3) local_unnamed_addr #4 {
  %5 = alloca %struct.WorkItem, align 8
  %6 = insertelement <4 x ptr> poison, ptr %0, i64 0
  %7 = insertelement <4 x ptr> %6, ptr %1, i64 1
  %8 = insertelement <4 x ptr> %7, ptr %2, i64 2
  %9 = insertelement <4 x ptr> %8, ptr %3, i64 3
  %10 = icmp eq <4 x ptr> %9, zeroinitializer
  %11 = bitcast <4 x i1> %10 to i4
  %12 = icmp eq i4 %11, 0
  br i1 %12, label %13, label %50

13:                                               ; preds = %4
  %14 = load ptr, ptr %0, align 8, !tbaa !5
  %15 = getelementptr inbounds i8, ptr %14, i64 40
  %16 = tail call i32 @pthread_mutex_lock(ptr noundef nonnull %15) #5
  %17 = load ptr, ptr %0, align 8, !tbaa !5
  %18 = getelementptr inbounds i8, ptr %17, i64 16
  %19 = load i64, ptr %18, align 8, !tbaa !15
  %20 = icmp eq i64 %19, 0
  br i1 %20, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %13, %.lr.ph
  %21 = phi ptr [ %25, %.lr.ph ], [ %17, %13 ]
  %22 = getelementptr inbounds i8, ptr %21, i64 80
  %23 = getelementptr inbounds i8, ptr %21, i64 40
  %24 = tail call i32 @pthread_cond_wait(ptr noundef nonnull %22, ptr noundef nonnull %23) #5
  %25 = load ptr, ptr %0, align 8, !tbaa !5
  %26 = getelementptr inbounds i8, ptr %25, i64 16
  %27 = load i64, ptr %26, align 8, !tbaa !15
  %28 = icmp eq i64 %27, 0
  br i1 %28, label %.lr.ph, label %._crit_edge, !llvm.loop !17

._crit_edge:                                      ; preds = %.lr.ph, %13
  %29 = phi i64 [ %19, %13 ], [ %27, %.lr.ph ]
  %.lcssa30 = phi ptr [ %17, %13 ], [ %25, %.lr.ph ]
  %30 = getelementptr inbounds i8, ptr %.lcssa30, i64 16
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %5) #5
  %31 = load ptr, ptr %.lcssa30, align 8, !tbaa !19
  %32 = getelementptr inbounds i8, ptr %.lcssa30, i64 24
  %33 = load i64, ptr %32, align 8, !tbaa !20
  %34 = getelementptr inbounds %struct.WorkItem, ptr %31, i64 %33
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(32) %5, ptr noundef nonnull align 8 dereferenceable(32) %34, i64 32, i1 false), !tbaa.struct !21
  %35 = add i64 %33, 1
  %36 = getelementptr inbounds i8, ptr %.lcssa30, i64 8
  %37 = load i64, ptr %36, align 8, !tbaa !26
  %38 = urem i64 %35, %37
  store i64 %38, ptr %32, align 8, !tbaa !20
  %39 = add i64 %29, -1
  store i64 %39, ptr %30, align 8, !tbaa !15
  %40 = getelementptr inbounds i8, ptr %.lcssa30, i64 128
  %41 = tail call i32 @pthread_cond_signal(ptr noundef nonnull %40) #5
  %42 = load ptr, ptr %0, align 8, !tbaa !5
  %43 = getelementptr inbounds i8, ptr %42, i64 40
  %44 = tail call i32 @pthread_mutex_unlock(ptr noundef nonnull %43) #5
  %45 = load ptr, ptr %5, align 8, !tbaa !27
  store ptr %45, ptr %1, align 8, !tbaa !22
  %46 = getelementptr inbounds i8, ptr %5, i64 8
  %47 = load i64, ptr %46, align 8, !tbaa !29
  store i64 %47, ptr %2, align 8, !tbaa !23
  %48 = getelementptr inbounds i8, ptr %5, i64 16
  %49 = load i32, ptr %48, align 8, !tbaa !30
  store i32 %49, ptr %3, align 4, !tbaa !24
  call void @update_metrics(ptr noundef nonnull %0, ptr noundef nonnull %5)
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %5) #5
  br label %50

50:                                               ; preds = %4, %._crit_edge
  %.0 = phi i32 [ 0, %._crit_edge ], [ -1, %4 ]
  ret i32 %.0
}

; Function Attrs: nounwind uwtable
declare hidden void @update_metrics(ptr noundef, ptr noundef) local_unnamed_addr #4

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #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 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 #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 = !{!"", !7, i64 0, !10, i64 8, !10, i64 12, !11, i64 16, !12, i64 24, !11, i64 32, !7, i64 40, !12, i64 48, !13, i64 56}
!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 = !{!"", !12, i64 0, !12, i64 8, !12, i64 16, !12, i64 24, !12, i64 32, !14, i64 40}
!14 = !{!"double", !8, i64 0}
!15 = !{!16, !12, i64 16}
!16 = !{!"", !7, i64 0, !12, i64 8, !12, i64 16, !12, i64 24, !12, i64 32, !8, i64 40, !8, i64 80, !8, i64 128}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = !{!16, !7, i64 0}
!20 = !{!16, !12, i64 24}
!21 = !{i64 0, i64 8, !22, i64 8, i64 8, !23, i64 16, i64 4, !24, i64 20, i64 4, !24, i64 24, i64 8, !25}
!22 = !{!7, !7, i64 0}
!23 = !{!12, !12, i64 0}
!24 = !{!10, !10, i64 0}
!25 = !{!14, !14, i64 0}
!26 = !{!16, !12, i64 8}
!27 = !{!28, !7, i64 0}
!28 = !{!"", !7, i64 0, !12, i64 8, !10, i64 16, !10, i64 20, !14, i64 24}
!29 = !{!28, !12, i64 8}
!30 = !{!28, !10, i64 16}


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