; LLVM IR from Lakhankumawat/LearnCPP :: G-GreedyAlgorithms/FractionalKnapsack.ll (Z18KnapSackFractionalP6Objectii)

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

%"class.std::basic_ostream" = type { ptr, %"class.std::basic_ios" }
%"class.std::basic_ios" = type { %"class.std::ios_base", ptr, i8, i8, ptr, ptr, ptr, ptr }
%"class.std::ios_base" = type { ptr, i64, i64, i32, i32, i32, ptr, %"struct.std::ios_base::_Words", [8 x %"struct.std::ios_base::_Words"], i32, ptr, %"class.std::locale" }
%"struct.std::ios_base::_Words" = type { ptr, i64 }
%"class.std::locale" = type { ptr }
%struct.Object = type { float, float, float }

@_ZSt4cout = external global %"class.std::basic_ostream", align 8
@.str = external hidden unnamed_addr constant [9 x i8], align 1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_Z3cmp6ObjectS_(<2 x float>, float, <2 x float>, float) #0

; Function Attrs: mustprogress uwtable
define dso_local void @_Z18KnapSackFractionalP6Objectii(ptr noundef %0, i32 noundef %1, i32 noundef %2) #1 {
  %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
  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
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  %11 = load i32, ptr %5, align 4, !tbaa !9
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds %struct.Object, ptr %10, i64 %12
  call void @_ZSt4sortIP6ObjectPFbS0_S0_EEvT_S4_T0_(ptr noundef %9, ptr noundef %13, ptr noundef @_Z3cmp6ObjectS_)
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #5
  store float 0.000000e+00, ptr %7, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #5
  store i32 0, ptr %8, align 4, !tbaa !9
  br label %14

14:                                               ; preds = %99, %3
  %15 = load i32, ptr %8, align 4, !tbaa !9
  %16 = load i32, ptr %5, align 4, !tbaa !9
  %17 = icmp slt i32 %15, %16
  br i1 %17, label %19, label %18

18:                                               ; preds = %14
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  br label %102

19:                                               ; preds = %14
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = load i32, ptr %8, align 4, !tbaa !9
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds %struct.Object, ptr %20, i64 %22
  %24 = getelementptr inbounds %struct.Object, ptr %23, i32 0, i32 0
  %25 = load float, ptr %24, align 4, !tbaa !13
  %26 = load i32, ptr %6, align 4, !tbaa !9
  %27 = sitofp i32 %26 to float
  %28 = fcmp ole float %25, %27
  br i1 %28, label %29, label %48

29:                                               ; preds = %19
  %30 = load ptr, ptr %4, align 8, !tbaa !5
  %31 = load i32, ptr %8, align 4, !tbaa !9
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds %struct.Object, ptr %30, i64 %32
  %34 = getelementptr inbounds %struct.Object, ptr %33, i32 0, i32 1
  %35 = load float, ptr %34, align 4, !tbaa !15
  %36 = load float, ptr %7, align 4, !tbaa !11
  %37 = fadd float %36, %35
  store float %37, ptr %7, align 4, !tbaa !11
  %38 = load ptr, ptr %4, align 8, !tbaa !5
  %39 = load i32, ptr %8, align 4, !tbaa !9
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds %struct.Object, ptr %38, i64 %40
  %42 = getelementptr inbounds %struct.Object, ptr %41, i32 0, i32 0
  %43 = load float, ptr %42, align 4, !tbaa !13
  %44 = load i32, ptr %6, align 4, !tbaa !9
  %45 = sitofp i32 %44 to float
  %46 = fsub float %45, %43
  %47 = fptosi float %46 to i32
  store i32 %47, ptr %6, align 4, !tbaa !9
  br label %98

48:                                               ; preds = %19
  %49 = load i32, ptr %6, align 4, !tbaa !9
  %50 = icmp ne i32 %49, 0
  br i1 %50, label %51, label %97

51:                                               ; preds = %48
  %52 = load i32, ptr %6, align 4, !tbaa !9
  %53 = sitofp i32 %52 to float
  %54 = load ptr, ptr %4, align 8, !tbaa !5
  %55 = load i32, ptr %8, align 4, !tbaa !9
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds %struct.Object, ptr %54, i64 %56
  %58 = getelementptr inbounds %struct.Object, ptr %57, i32 0, i32 0
  %59 = load float, ptr %58, align 4, !tbaa !13
  %60 = fdiv float %53, %59
  %61 = load ptr, ptr %4, align 8, !tbaa !5
  %62 = load i32, ptr %8, align 4, !tbaa !9
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds %struct.Object, ptr %61, i64 %63
  %65 = getelementptr inbounds %struct.Object, ptr %64, i32 0, i32 2
  store float %60, ptr %65, align 4, !tbaa !16
  %66 = load ptr, ptr %4, align 8, !tbaa !5
  %67 = load i32, ptr %8, align 4, !tbaa !9
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds %struct.Object, ptr %66, i64 %68
  %70 = getelementptr inbounds %struct.Object, ptr %69, i32 0, i32 2
  %71 = load float, ptr %70, align 4, !tbaa !16
  %72 = load ptr, ptr %4, align 8, !tbaa !5
  %73 = load i32, ptr %8, align 4, !tbaa !9
  %74 = sext i32 %73 to i64
  %75 = getelementptr inbounds %struct.Object, ptr %72, i64 %74
  %76 = getelementptr inbounds %struct.Object, ptr %75, i32 0, i32 1
  %77 = load float, ptr %76, align 4, !tbaa !15
  %78 = load float, ptr %7, align 4, !tbaa !11
  %79 = call float @llvm.fmuladd.f32(float %71, float %77, float %78)
  store float %79, ptr %7, align 4, !tbaa !11
  %80 = load ptr, ptr %4, align 8, !tbaa !5
  %81 = load i32, ptr %8, align 4, !tbaa !9
  %82 = sext i32 %81 to i64
  %83 = getelementptr inbounds %struct.Object, ptr %80, i64 %82
  %84 = getelementptr inbounds %struct.Object, ptr %83, i32 0, i32 2
  %85 = load float, ptr %84, align 4, !tbaa !16
  %86 = load ptr, ptr %4, align 8, !tbaa !5
  %87 = load i32, ptr %8, align 4, !tbaa !9
  %88 = sext i32 %87 to i64
  %89 = getelementptr inbounds %struct.Object, ptr %86, i64 %88
  %90 = getelementptr inbounds %struct.Object, ptr %89, i32 0, i32 0
  %91 = load float, ptr %90, align 4, !tbaa !13
  %92 = load i32, ptr %6, align 4, !tbaa !9
  %93 = sitofp i32 %92 to float
  %94 = fneg float %85
  %95 = call float @llvm.fmuladd.f32(float %94, float %91, float %93)
  %96 = fptosi float %95 to i32
  store i32 %96, ptr %6, align 4, !tbaa !9
  br label %97

97:                                               ; preds = %51, %48
  br label %98

98:                                               ; preds = %97, %29
  br label %99

99:                                               ; preds = %98
  %100 = load i32, ptr %8, align 4, !tbaa !9
  %101 = add nsw i32 %100, 1
  store i32 %101, ptr %8, align 4, !tbaa !9
  br label %14, !llvm.loop !17

102:                                              ; preds = %18
  %103 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, ptr noundef @.str)
  %104 = load float, ptr %7, align 4, !tbaa !11
  %105 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEf(ptr noundef nonnull align 8 dereferenceable(8) %103, float noundef %104)
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  ret void
}

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZSt4sortIP6ObjectPFbS0_S0_EEvT_S4_T0_(ptr noundef, ptr noundef, ptr noundef) #2

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare float @llvm.fmuladd.f32(float, float, float) #4

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

; Function Attrs: inlinehint mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef) #2

; Function Attrs: mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEf(ptr noundef nonnull align 8 dereferenceable(8), float noundef) #1 align 2

attributes #0 = { mustprogress nounwind uwtable "min-legal-vector-width"="64" "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 = { 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 = { inlinehint 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 #3 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"float", !7, i64 0}
!13 = !{!14, !12, i64 0}
!14 = !{!"_ZTS6Object", !12, i64 0, !12, i64 4, !12, i64 8}
!15 = !{!14, !12, i64 4}
!16 = !{!14, !12, i64 8}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


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

%"class.std::basic_ostream" = type { ptr, %"class.std::basic_ios" }
%"class.std::basic_ios" = type { %"class.std::ios_base", ptr, i8, i8, ptr, ptr, ptr, ptr }
%"class.std::ios_base" = type { ptr, i64, i64, i32, i32, i32, ptr, %"struct.std::ios_base::_Words", [8 x %"struct.std::ios_base::_Words"], i32, ptr, %"class.std::locale" }
%"struct.std::ios_base::_Words" = type { ptr, i64 }
%"class.std::locale" = type { ptr }
%struct.Object = type { float, float, float }

@_ZSt4cout = external global %"class.std::basic_ostream", align 8
@.str = external hidden unnamed_addr constant [9 x i8], align 1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_Z3cmp6ObjectS_(<2 x float>, float, <2 x float>, float) #0

; Function Attrs: mustprogress uwtable
define dso_local void @_Z18KnapSackFractionalP6Objectii(ptr noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #1 {
  %4 = sext i32 %1 to i64
  %5 = getelementptr inbounds %struct.Object, ptr %0, i64 %4
  tail call void @_ZSt4sortIP6ObjectPFbS0_S0_EEvT_S4_T0_(ptr noundef %0, ptr noundef %5, ptr noundef nonnull @_Z3cmp6ObjectS_)
  %6 = icmp sgt i32 %1, 0
  br i1 %6, label %.lr.ph.preheader, label %._crit_edge

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

._crit_edge:                                      ; preds = %29, %3
  %.031.lcssa = phi float [ 0.000000e+00, %3 ], [ %.1, %29 ]
  %7 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, ptr noundef nonnull @.str)
  %8 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEf(ptr noundef nonnull align 8 dereferenceable(8) %7, float noundef %.031.lcssa)
  ret void

.lr.ph:                                           ; preds = %.lr.ph.preheader, %29
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next, %29 ]
  %.03139 = phi float [ 0.000000e+00, %.lr.ph.preheader ], [ %.1, %29 ]
  %.03238 = phi i32 [ %2, %.lr.ph.preheader ], [ %.133, %29 ]
  %9 = getelementptr inbounds %struct.Object, ptr %0, i64 %indvars.iv
  %10 = load float, ptr %9, align 4, !tbaa !5
  %11 = sitofp i32 %.03238 to float
  %12 = fcmp ugt float %10, %11
  br i1 %12, label %19, label %13

13:                                               ; preds = %.lr.ph
  %14 = getelementptr inbounds i8, ptr %9, i64 4
  %15 = load float, ptr %14, align 4, !tbaa !10
  %16 = fadd float %.03139, %15
  %17 = fsub float %11, %10
  %18 = fptosi float %17 to i32
  br label %29

19:                                               ; preds = %.lr.ph
  %.not = icmp eq i32 %.03238, 0
  br i1 %.not, label %29, label %20

20:                                               ; preds = %19
  %21 = fdiv float %11, %10
  %22 = getelementptr inbounds i8, ptr %9, i64 8
  store float %21, ptr %22, align 4, !tbaa !11
  %23 = getelementptr inbounds i8, ptr %9, i64 4
  %24 = load float, ptr %23, align 4, !tbaa !10
  %25 = tail call float @llvm.fmuladd.f32(float %21, float %24, float %.03139)
  %26 = fneg float %21
  %27 = tail call float @llvm.fmuladd.f32(float %26, float %10, float %11)
  %28 = fptosi float %27 to i32
  br label %29

29:                                               ; preds = %13, %20, %19
  %.133 = phi i32 [ %18, %13 ], [ %28, %20 ], [ 0, %19 ]
  %.1 = phi float [ %16, %13 ], [ %25, %20 ], [ %.03139, %19 ]
  %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 %._crit_edge, label %.lr.ph, !llvm.loop !12
}

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZSt4sortIP6ObjectPFbS0_S0_EEvT_S4_T0_(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare float @llvm.fmuladd.f32(float, float, float) #3

; Function Attrs: inlinehint mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef) local_unnamed_addr #2

; Function Attrs: mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEf(ptr noundef nonnull align 8 dereferenceable(8), float noundef) local_unnamed_addr #1 align 2

attributes #0 = { mustprogress nounwind uwtable "min-legal-vector-width"="64" "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 = { 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 = { inlinehint 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 #3 = { mustprogress 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, !7, i64 0}
!6 = !{!"_ZTS6Object", !7, i64 0, !7, i64 4, !7, i64 8}
!7 = !{!"float", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!6, !7, i64 4}
!11 = !{!6, !7, i64 8}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


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