; LLVM IR from mitdbg/deneva :: system/helper.ll (Z19init_client_globalsv)

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

@g_node_cnt = external global i32, align 4
@g_client_node_cnt = external global i32, align 4
@g_servers_per_client = external global i32, align 4
@g_clients_per_server = external global i32, align 4
@g_node_id = external global i32, align 4
@g_server_start_node = external global i32, align 4
@.str.6 = external hidden unnamed_addr constant [57 x i8], align 1

; 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: mustprogress uwtable
define dso_local void @_Z19init_client_globalsv() #1 {
  %1 = alloca i32, align 4
  %2 = load i32, ptr @g_node_cnt, align 4, !tbaa !5
  %3 = load i32, ptr @g_client_node_cnt, align 4, !tbaa !5
  %4 = icmp ugt i32 %2, %3
  br i1 %4, label %5, label %9

5:                                                ; preds = %0
  %6 = load i32, ptr @g_node_cnt, align 4, !tbaa !5
  %7 = load i32, ptr @g_client_node_cnt, align 4, !tbaa !5
  %8 = udiv i32 %6, %7
  store i32 %8, ptr @g_servers_per_client, align 4, !tbaa !5
  store i32 1, ptr @g_clients_per_server, align 4, !tbaa !5
  br label %13

9:                                                ; preds = %0
  store i32 1, ptr @g_servers_per_client, align 4, !tbaa !5
  %10 = load i32, ptr @g_client_node_cnt, align 4, !tbaa !5
  %11 = load i32, ptr @g_node_cnt, align 4, !tbaa !5
  %12 = udiv i32 %10, %11
  store i32 %12, ptr @g_clients_per_server, align 4, !tbaa !5
  br label %13

13:                                               ; preds = %9, %5
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  %14 = load i32, ptr @g_node_id, align 4, !tbaa !5
  %15 = load i32, ptr @g_node_cnt, align 4, !tbaa !5
  %16 = sub i32 %14, %15
  store i32 %16, ptr %1, align 4, !tbaa !5
  %17 = load i32, ptr %1, align 4, !tbaa !5
  %18 = load i32, ptr @g_servers_per_client, align 4, !tbaa !5
  %19 = mul i32 %17, %18
  %20 = load i32, ptr @g_node_cnt, align 4, !tbaa !5
  %21 = urem i32 %19, %20
  store i32 %21, ptr @g_server_start_node, align 4, !tbaa !5
  %22 = load i32, ptr @g_node_cnt, align 4, !tbaa !5
  %23 = load i32, ptr @g_client_node_cnt, align 4, !tbaa !5
  %24 = icmp uge i32 %22, %23
  br i1 %24, label %25, label %43

25:                                               ; preds = %13
  %26 = load i32, ptr @g_node_cnt, align 4, !tbaa !5
  %27 = load i32, ptr @g_client_node_cnt, align 4, !tbaa !5
  %28 = urem i32 %26, %27
  %29 = icmp ne i32 %28, 0
  br i1 %29, label %30, label %43

30:                                               ; preds = %25
  %31 = load i32, ptr @g_node_id, align 4, !tbaa !5
  %32 = load i32, ptr @g_node_cnt, align 4, !tbaa !5
  %33 = load i32, ptr @g_client_node_cnt, align 4, !tbaa !5
  %34 = add i32 %32, %33
  %35 = sub i32 %34, 1
  %36 = icmp eq i32 %31, %35
  br i1 %36, label %37, label %43

37:                                               ; preds = %30
  %38 = load i32, ptr @g_node_cnt, align 4, !tbaa !5
  %39 = load i32, ptr @g_client_node_cnt, align 4, !tbaa !5
  %40 = urem i32 %38, %39
  %41 = load i32, ptr @g_servers_per_client, align 4, !tbaa !5
  %42 = add i32 %41, %40
  store i32 %42, ptr @g_servers_per_client, align 4, !tbaa !5
  br label %43

43:                                               ; preds = %37, %30, %25, %13
  %44 = load i32, ptr @g_node_id, align 4, !tbaa !5
  %45 = load i32, ptr @g_servers_per_client, align 4, !tbaa !5
  %46 = load i32, ptr @g_server_start_node, align 4, !tbaa !5
  %47 = call i32 (ptr, ...) @printf(ptr noundef @.str.6, i32 noundef %44, i32 noundef %45, i32 noundef %46)
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  ret void
}

declare i32 @printf(ptr noundef, ...) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}


; 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_node_cnt = external local_unnamed_addr global i32, align 4
@g_client_node_cnt = external local_unnamed_addr global i32, align 4
@g_servers_per_client = external local_unnamed_addr global i32, align 4
@g_clients_per_server = external local_unnamed_addr global i32, align 4
@g_node_id = external local_unnamed_addr global i32, align 4
@g_server_start_node = external local_unnamed_addr global i32, align 4
@.str.6 = external hidden unnamed_addr constant [57 x i8], align 1

; Function Attrs: mustprogress nofree nounwind uwtable
define dso_local void @_Z19init_client_globalsv() local_unnamed_addr #0 {
  %1 = load i32, ptr @g_node_cnt, align 4, !tbaa !5
  %2 = load i32, ptr @g_client_node_cnt, align 4, !tbaa !5
  %3 = icmp ugt i32 %1, %2
  br i1 %3, label %4, label %6

4:                                                ; preds = %0
  %5 = udiv i32 %1, %2
  br label %8

6:                                                ; preds = %0
  %7 = udiv i32 %2, %1
  br label %8

8:                                                ; preds = %6, %4
  %.sink = phi i32 [ %5, %4 ], [ 1, %6 ]
  %storemerge = phi i32 [ 1, %4 ], [ %7, %6 ]
  store i32 %.sink, ptr @g_servers_per_client, align 4
  store i32 %storemerge, ptr @g_clients_per_server, align 4, !tbaa !5
  %9 = load i32, ptr @g_node_id, align 4, !tbaa !5
  %10 = sub i32 %9, %1
  %11 = mul i32 %10, %.sink
  %12 = urem i32 %11, %1
  store i32 %12, ptr @g_server_start_node, align 4, !tbaa !5
  %.not = icmp ult i32 %1, %2
  br i1 %.not, label %21, label %13

13:                                               ; preds = %8
  %14 = urem i32 %1, %2
  %.not5 = icmp eq i32 %14, 0
  br i1 %.not5, label %21, label %15

15:                                               ; preds = %13
  %16 = add i32 %1, -1
  %17 = add i32 %16, %2
  %18 = icmp eq i32 %9, %17
  br i1 %18, label %19, label %21

19:                                               ; preds = %15
  %20 = add i32 %14, %.sink
  store i32 %20, ptr @g_servers_per_client, align 4, !tbaa !5
  br label %21

21:                                               ; preds = %19, %15, %13, %8
  %22 = phi i32 [ %20, %19 ], [ %.sink, %15 ], [ %.sink, %13 ], [ %.sink, %8 ]
  %23 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.6, i32 noundef %9, i32 noundef %22, i32 noundef %12)
  ret void
}

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

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

!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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,MergedLoadStoreMotionPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,TailCallElimPass