; LLVM IR from kaynewbie/algorithm :: algorithm/CodingInterviews (cut-rope)

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

; 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: nounwind uwtable
define dso_local i32 @maxProducstInGreedy(i32 noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  %7 = load i32, ptr %3, align 4, !tbaa !5
  switch i32 %7, label %11 [
    i32 0, label %8
    i32 1, label %8
    i32 2, label %9
    i32 3, label %10
  ]

8:                                                ; preds = %1, %1
  store i32 0, ptr %2, align 4
  br label %39

9:                                                ; preds = %1
  store i32 1, ptr %2, align 4
  br label %39

10:                                               ; preds = %1
  store i32 2, ptr %2, align 4
  br label %39

11:                                               ; preds = %1
  br label %12

12:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  %13 = load i32, ptr %3, align 4, !tbaa !5
  %14 = sdiv i32 %13, 3
  store i32 %14, ptr %4, align 4, !tbaa !5
  %15 = load i32, ptr %3, align 4, !tbaa !5
  %16 = load i32, ptr %4, align 4, !tbaa !5
  %17 = mul nsw i32 %16, 3
  %18 = sub nsw i32 %15, %17
  %19 = icmp eq i32 %18, 1
  br i1 %19, label %20, label %23

20:                                               ; preds = %12
  %21 = load i32, ptr %4, align 4, !tbaa !5
  %22 = add nsw i32 %21, -1
  store i32 %22, ptr %4, align 4, !tbaa !5
  br label %23

23:                                               ; preds = %20, %12
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %24 = load i32, ptr %3, align 4, !tbaa !5
  %25 = load i32, ptr %4, align 4, !tbaa !5
  %26 = mul nsw i32 %25, 3
  %27 = sub nsw i32 %24, %26
  %28 = sdiv i32 %27, 2
  store i32 %28, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %29 = load i32, ptr %4, align 4, !tbaa !5
  %30 = sitofp i32 %29 to double
  %31 = call double @pow(double noundef 3.000000e+00, double noundef %30) #3
  %32 = fptosi double %31 to i32
  %33 = load i32, ptr %5, align 4, !tbaa !5
  %34 = sitofp i32 %33 to double
  %35 = call double @pow(double noundef 2.000000e+00, double noundef %34) #3
  %36 = fptosi double %35 to i32
  %37 = mul nsw i32 %32, %36
  store i32 %37, ptr %6, align 4, !tbaa !5
  %38 = load i32, ptr %6, align 4, !tbaa !5
  store i32 %38, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  br label %39

39:                                               ; preds = %23, %10, %9, %8
  %40 = load i32, ptr %2, align 4
  ret i32 %40
}

; Function Attrs: nounwind
declare double @pow(double noundef, double noundef) #2

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

@switch.table.maxProducstInGreedy = private unnamed_addr constant [4 x i32] [i32 0, i32 0, i32 1, i32 2], align 4

; Function Attrs: mustprogress nofree nounwind willreturn uwtable
define dso_local i32 @maxProducstInGreedy(i32 noundef %0) local_unnamed_addr #0 {
  %2 = icmp ult i32 %0, 4
  br i1 %2, label %switch.lookup, label %3

3:                                                ; preds = %1
  %4 = sdiv i32 %0, 3
  %.neg = mul nsw i32 %4, -3
  %5 = add i32 %.neg, %0
  %6 = icmp eq i32 %5, 1
  %7 = sext i1 %6 to i32
  %spec.select = add nsw i32 %4, %7
  %.neg12 = mul i32 %spec.select, -3
  %8 = add i32 %.neg12, %0
  %9 = sdiv i32 %8, 2
  %10 = sitofp i32 %spec.select to double
  %11 = tail call double @pow(double noundef 3.000000e+00, double noundef %10) #3
  %12 = fptosi double %11 to i32
  %ldexp = tail call double @ldexp(double 1.000000e+00, i32 %9) #3
  %13 = fptosi double %ldexp to i32
  %14 = mul nsw i32 %13, %12
  br label %16

switch.lookup:                                    ; preds = %1
  %15 = zext nneg i32 %0 to i64
  %switch.gep = getelementptr inbounds [4 x i32], ptr @switch.table.maxProducstInGreedy, i64 0, i64 %15
  %switch.load = load i32, ptr %switch.gep, align 4
  br label %16

16:                                               ; preds = %switch.lookup, %3
  %.0 = phi i32 [ %14, %3 ], [ %switch.load, %switch.lookup ]
  ret i32 %.0
}

; Function Attrs: mustprogress nofree nounwind willreturn memory(write)
declare double @pow(double noundef, double noundef) local_unnamed_addr #1

; Function Attrs: nofree willreturn
declare double @ldexp(double, i32) local_unnamed_addr #2

attributes #0 = { mustprogress nofree nounwind willreturn 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 = { mustprogress nofree nounwind willreturn memory(write) "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 = { nofree willreturn }
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)"}


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