; 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

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

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

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

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

12:                                               ; preds = %0
  store i64 0, ptr %1, align 8
  store i32 1, ptr %3, align 4
  br label %48

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

36:                                               ; preds = %13
  %37 = load i32, ptr %5, align 4, !tbaa !16
  %38 = sext i32 %37 to i64
  %39 = load i64, ptr %8, align 8, !tbaa !18
  %40 = add i64 %39, %38
  store i64 %40, ptr %8, align 8, !tbaa !18
  br label %45

41:                                               ; preds = %13
  %42 = load i64, ptr %7, align 8, !tbaa !18
  %43 = load i64, ptr %8, align 8, !tbaa !18
  %44 = add i64 %43, %42
  store i64 %44, ptr %8, align 8, !tbaa !18
  br label %45

45:                                               ; preds = %41, %36
  %46 = load ptr, ptr %2, align 8, !tbaa !5
  call void @cleanup_workload_manager(ptr noundef %46)
  %47 = load i64, ptr %8, align 8, !tbaa !18
  store i64 %47, ptr %1, align 8
  store i32 1, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  br label %48

48:                                               ; preds = %45, %12
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #2
  %49 = load i64, ptr %1, align 8
  ret i64 %49
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 12}
!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 = !{!11, !11, i64 0}
!17 = !{!10, !11, i64 8}
!18 = !{!13, !13, i64 0}


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

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

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

13:                                               ; preds = %0, %2
  %.015 = phi i64 [ %.0, %2 ], [ 0, %0 ]
  ret i64 %.015
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umin.i64(i64, i64) #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 speculatable willreturn memory(none) }

!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, !10, i64 12}
!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 = !{!6, !10, i64 8}
!16 = !{!12, !12, i64 0}


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