; LLVM IR from AlgoGenesis/C :: Miscellaneous Algorithms/Weighted Fair Queueing (WFQ) (program)

; 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.Process = type { i32, i32, i32, float }

@.str = external hidden unnamed_addr constant [54 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local void @weightedFairQueueing(ptr noundef %0, i32 noundef %1, i32 noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca float, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store i32 %2, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store float 0.000000e+00, ptr %7, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 0, ptr %8, align 4, !tbaa !9
  br label %11

11:                                               ; preds = %101, %3
  %12 = load i32, ptr %8, align 4, !tbaa !9
  %13 = load i32, ptr %5, align 4, !tbaa !9
  %14 = icmp slt i32 %12, %13
  br i1 %14, label %15, label %102

15:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  store i32 0, ptr %9, align 4, !tbaa !9
  br label %16

16:                                               ; preds = %98, %15
  %17 = load i32, ptr %9, align 4, !tbaa !9
  %18 = load i32, ptr %5, align 4, !tbaa !9
  %19 = icmp slt i32 %17, %18
  br i1 %19, label %21, label %20

20:                                               ; preds = %16
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  br label %101

21:                                               ; preds = %16
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  %23 = load i32, ptr %9, align 4, !tbaa !9
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds %struct.Process, ptr %22, i64 %24
  %26 = getelementptr inbounds %struct.Process, ptr %25, i32 0, i32 2
  %27 = load i32, ptr %26, align 4, !tbaa !13
  %28 = icmp sgt i32 %27, 0
  br i1 %28, label %29, label %97

29:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  %30 = load i32, ptr %6, align 4, !tbaa !9
  %31 = load ptr, ptr %4, align 8, !tbaa !5
  %32 = load i32, ptr %9, align 4, !tbaa !9
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds %struct.Process, ptr %31, i64 %33
  %35 = getelementptr inbounds %struct.Process, ptr %34, i32 0, i32 1
  %36 = load i32, ptr %35, align 4, !tbaa !15
  %37 = mul nsw i32 %30, %36
  store i32 %37, ptr %10, align 4, !tbaa !9
  %38 = load ptr, ptr %4, align 8, !tbaa !5
  %39 = load i32, ptr %9, align 4, !tbaa !9
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds %struct.Process, ptr %38, i64 %40
  %42 = getelementptr inbounds %struct.Process, ptr %41, i32 0, i32 2
  %43 = load i32, ptr %42, align 4, !tbaa !13
  %44 = load i32, ptr %10, align 4, !tbaa !9
  %45 = icmp sgt i32 %43, %44
  br i1 %45, label %46, label %59

46:                                               ; preds = %29
  %47 = load i32, ptr %10, align 4, !tbaa !9
  %48 = load ptr, ptr %4, align 8, !tbaa !5
  %49 = load i32, ptr %9, align 4, !tbaa !9
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds %struct.Process, ptr %48, i64 %50
  %52 = getelementptr inbounds %struct.Process, ptr %51, i32 0, i32 2
  %53 = load i32, ptr %52, align 4, !tbaa !13
  %54 = sub nsw i32 %53, %47
  store i32 %54, ptr %52, align 4, !tbaa !13
  %55 = load i32, ptr %10, align 4, !tbaa !9
  %56 = sitofp i32 %55 to float
  %57 = load float, ptr %7, align 4, !tbaa !11
  %58 = fadd float %57, %56
  store float %58, ptr %7, align 4, !tbaa !11
  br label %82

59:                                               ; preds = %29
  %60 = load ptr, ptr %4, align 8, !tbaa !5
  %61 = load i32, ptr %9, align 4, !tbaa !9
  %62 = sext i32 %61 to i64
  %63 = getelementptr inbounds %struct.Process, ptr %60, i64 %62
  %64 = getelementptr inbounds %struct.Process, ptr %63, i32 0, i32 2
  %65 = load i32, ptr %64, align 4, !tbaa !13
  %66 = sitofp i32 %65 to float
  %67 = load float, ptr %7, align 4, !tbaa !11
  %68 = fadd float %67, %66
  store float %68, ptr %7, align 4, !tbaa !11
  %69 = load float, ptr %7, align 4, !tbaa !11
  %70 = load ptr, ptr %4, align 8, !tbaa !5
  %71 = load i32, ptr %9, align 4, !tbaa !9
  %72 = sext i32 %71 to i64
  %73 = getelementptr inbounds %struct.Process, ptr %70, i64 %72
  %74 = getelementptr inbounds %struct.Process, ptr %73, i32 0, i32 3
  store float %69, ptr %74, align 4, !tbaa !16
  %75 = load ptr, ptr %4, align 8, !tbaa !5
  %76 = load i32, ptr %9, align 4, !tbaa !9
  %77 = sext i32 %76 to i64
  %78 = getelementptr inbounds %struct.Process, ptr %75, i64 %77
  %79 = getelementptr inbounds %struct.Process, ptr %78, i32 0, i32 2
  store i32 0, ptr %79, align 4, !tbaa !13
  %80 = load i32, ptr %8, align 4, !tbaa !9
  %81 = add nsw i32 %80, 1
  store i32 %81, ptr %8, align 4, !tbaa !9
  br label %82

82:                                               ; preds = %59, %46
  %83 = load ptr, ptr %4, align 8, !tbaa !5
  %84 = load i32, ptr %9, align 4, !tbaa !9
  %85 = sext i32 %84 to i64
  %86 = getelementptr inbounds %struct.Process, ptr %83, i64 %85
  %87 = getelementptr inbounds %struct.Process, ptr %86, i32 0, i32 0
  %88 = load i32, ptr %87, align 4, !tbaa !17
  %89 = load i32, ptr %10, align 4, !tbaa !9
  %90 = load ptr, ptr %4, align 8, !tbaa !5
  %91 = load i32, ptr %9, align 4, !tbaa !9
  %92 = sext i32 %91 to i64
  %93 = getelementptr inbounds %struct.Process, ptr %90, i64 %92
  %94 = getelementptr inbounds %struct.Process, ptr %93, i32 0, i32 2
  %95 = load i32, ptr %94, align 4, !tbaa !13
  %96 = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %88, i32 noundef %89, i32 noundef %95)
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  br label %97

97:                                               ; preds = %82, %21
  br label %98

98:                                               ; preds = %97
  %99 = load i32, ptr %9, align 4, !tbaa !9
  %100 = add nsw i32 %99, 1
  store i32 %100, ptr %9, align 4, !tbaa !9
  br label %16, !llvm.loop !18

101:                                              ; preds = %20
  br label %11, !llvm.loop !20

102:                                              ; preds = %11
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  ret void
}

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

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

; 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 = { "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 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"float", !7, i64 0}
!13 = !{!14, !10, i64 8}
!14 = !{!"", !10, i64 0, !10, i64 4, !10, i64 8, !12, i64 12}
!15 = !{!14, !10, i64 4}
!16 = !{!14, !12, i64 12}
!17 = !{!14, !10, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}
!20 = distinct !{!20, !19}


; 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.Process = type { i32, i32, i32, float }

@.str = external hidden unnamed_addr constant [54 x i8], align 1

; Function Attrs: nofree nounwind uwtable
define dso_local void @weightedFairQueueing(ptr nocapture noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = icmp sgt i32 %1, 0
  br i1 %4, label %.preheader.us.preheader, label %._crit_edge

.preheader.us.preheader:                          ; preds = %3
  %wide.trip.count = zext nneg i32 %1 to i64
  br label %.preheader.us

.preheader.us:                                    ; preds = %.preheader.us.backedge, %.preheader.us.preheader
  %indvars.iv = phi i64 [ 0, %.preheader.us.preheader ], [ %indvars.iv.be, %.preheader.us.backedge ]
  %.141.us = phi float [ 0.000000e+00, %.preheader.us.preheader ], [ %.3.us, %.preheader.us.backedge ]
  %.13339.us = phi i32 [ 0, %.preheader.us.preheader ], [ %.335.us, %.preheader.us.backedge ]
  %5 = getelementptr inbounds %struct.Process, ptr %0, i64 %indvars.iv
  %6 = getelementptr inbounds i8, ptr %5, i64 8
  %7 = load i32, ptr %6, align 4, !tbaa !5
  %8 = icmp sgt i32 %7, 0
  br i1 %8, label %9, label %26

9:                                                ; preds = %.preheader.us
  %10 = getelementptr inbounds i8, ptr %5, i64 4
  %11 = load i32, ptr %10, align 4, !tbaa !11
  %12 = mul nsw i32 %11, %2
  %13 = icmp sgt i32 %7, %12
  br i1 %13, label %19, label %14

14:                                               ; preds = %9
  %15 = uitofp nneg i32 %7 to float
  %16 = fadd float %.141.us, %15
  %17 = getelementptr inbounds i8, ptr %5, i64 12
  store float %16, ptr %17, align 4, !tbaa !12
  %18 = add nsw i32 %.13339.us, 1
  br label %23

19:                                               ; preds = %9
  %20 = sub nsw i32 %7, %12
  %21 = sitofp i32 %12 to float
  %22 = fadd float %.141.us, %21
  br label %23

23:                                               ; preds = %19, %14
  %.sink = phi i32 [ %20, %19 ], [ 0, %14 ]
  %.234.us = phi i32 [ %.13339.us, %19 ], [ %18, %14 ]
  %.2.us = phi float [ %22, %19 ], [ %16, %14 ]
  store i32 %.sink, ptr %6, align 4
  %24 = load i32, ptr %5, align 4, !tbaa !13
  %25 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %24, i32 noundef %12, i32 noundef %.sink)
  br label %26

26:                                               ; preds = %23, %.preheader.us
  %.335.us = phi i32 [ %.234.us, %23 ], [ %.13339.us, %.preheader.us ]
  %.3.us = phi float [ %.2.us, %23 ], [ %.141.us, %.preheader.us ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %..loopexit_crit_edge.us, label %.preheader.us.backedge

.preheader.us.backedge:                           ; preds = %26, %..loopexit_crit_edge.us
  %indvars.iv.be = phi i64 [ %indvars.iv.next, %26 ], [ 0, %..loopexit_crit_edge.us ]
  br label %.preheader.us, !llvm.loop !14

..loopexit_crit_edge.us:                          ; preds = %26
  %27 = icmp slt i32 %.335.us, %1
  br i1 %27, label %.preheader.us.backedge, label %._crit_edge

._crit_edge:                                      ; preds = %..loopexit_crit_edge.us, %3
  ret void
}

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

attributes #0 = { 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, !7, i64 8}
!6 = !{!"", !7, i64 0, !7, i64 4, !7, i64 8, !10, i64 12}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"float", !8, i64 0}
!11 = !{!6, !7, i64 4}
!12 = !{!6, !10, i64 12}
!13 = !{!6, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,MergedLoadStoreMotionPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass