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

@g_total_size = external hidden global i64, align 8

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

11:                                               ; preds = %1
  %12 = load i64, ptr %3, align 8, !tbaa !5
  store i64 %12, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %39

13:                                               ; preds = %1
  %14 = load i64, ptr %3, align 8, !tbaa !5
  store i64 %14, ptr @g_total_size, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %15 = load i64, ptr %3, align 8, !tbaa !5
  %16 = load ptr, ptr %4, align 8, !tbaa !9
  %17 = getelementptr inbounds %struct.WorkloadManager, ptr %16, i32 0, i32 2
  %18 = load i32, ptr %17, align 4, !tbaa !11
  %19 = sext i32 %18 to i64
  %20 = udiv i64 %15, %19
  store i64 %20, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  %21 = load i64, ptr %3, align 8, !tbaa !5
  %22 = load ptr, ptr %4, align 8, !tbaa !9
  %23 = getelementptr inbounds %struct.WorkloadManager, ptr %22, i32 0, i32 2
  %24 = load i32, ptr %23, align 4, !tbaa !11
  %25 = sext i32 %24 to i64
  %26 = urem i64 %21, %25
  store i64 %26, ptr %7, align 8, !tbaa !5
  %27 = load ptr, ptr %4, align 8, !tbaa !9
  %28 = getelementptr inbounds %struct.WorkloadManager, ptr %27, i32 0, i32 1
  %29 = load i32, ptr %28, align 8, !tbaa !17
  %30 = sext i32 %29 to i64
  %31 = load i64, ptr %7, align 8, !tbaa !5
  %32 = icmp ult i64 %30, %31
  br i1 %32, label %33, label %36

33:                                               ; preds = %13
  %34 = load i64, ptr %6, align 8, !tbaa !5
  %35 = add i64 %34, 1
  store i64 %35, ptr %6, align 8, !tbaa !5
  br label %36

36:                                               ; preds = %33, %13
  %37 = load ptr, ptr %4, align 8, !tbaa !9
  call void @cleanup_workload_manager(ptr noundef %37)
  %38 = load i64, ptr %6, align 8, !tbaa !5
  store i64 %38, ptr %2, align 8
  store i32 1, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  br label %39

39:                                               ; preds = %36, %11
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  %40 = load i64, ptr %2, align 8
  ret i64 %40
}

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

; Function Attrs: nounwind uwtable
declare dso_local ptr @init_workload_manager() #0

; Function Attrs: nounwind uwtable
declare dso_local void @cleanup_workload_manager(ptr noundef) #0

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

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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, !6, i64 0}
!6 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !13, i64 12}
!12 = !{!"", !10, i64 0, !13, i64 8, !13, i64 12, !14, i64 16, !6, i64 24, !14, i64 32, !10, i64 40, !6, i64 48, !15, i64 56}
!13 = !{!"int", !7, i64 0}
!14 = !{!"_Bool", !7, i64 0}
!15 = !{!"", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !16, i64 40}
!16 = !{!"double", !7, i64 0}
!17 = !{!12, !13, i64 8}


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

@g_total_size = external hidden local_unnamed_addr global i64, align 8

; Function Attrs: nounwind uwtable
define dso_local i64 @distribute_workload(i64 noundef %0) local_unnamed_addr #0 {
  %2 = tail call ptr @init_workload_manager()
  %.not = icmp eq ptr %2, null
  br i1 %.not, label %14, label %3

3:                                                ; preds = %1
  store i64 %0, ptr @g_total_size, align 8, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %2, i64 12
  %5 = load i32, ptr %4, align 4, !tbaa !9
  %6 = sext i32 %5 to i64
  %7 = udiv i64 %0, %6
  %8 = urem i64 %0, %6
  %9 = getelementptr inbounds i8, ptr %2, i64 8
  %10 = load i32, ptr %9, align 8, !tbaa !16
  %11 = sext i32 %10 to i64
  %12 = icmp ugt i64 %8, %11
  %13 = zext i1 %12 to i64
  %spec.select = add i64 %7, %13
  tail call void @cleanup_workload_manager(ptr noundef nonnull %2)
  br label %14

14:                                               ; preds = %1, %3
  %.0 = phi i64 [ %spec.select, %3 ], [ %0, %1 ]
  ret i64 %.0
}

; Function Attrs: nounwind uwtable
declare dso_local ptr @init_workload_manager() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @cleanup_workload_manager(ptr noundef) local_unnamed_addr #0

attributes #0 = { 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" }

!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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !12, i64 12}
!10 = !{!"", !11, i64 0, !12, i64 8, !12, i64 12, !13, i64 16, !6, i64 24, !13, i64 32, !11, i64 40, !6, i64 48, !14, i64 56}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!"_Bool", !7, i64 0}
!14 = !{!"", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !15, i64 40}
!15 = !{!"double", !7, i64 0}
!16 = !{!10, !12, i64 8}


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