; LLVM IR from xuxping/codelife :: foroffer (question14)

; 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: nounwind uwtable
define dso_local i32 @maxCutProduct(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  %11 = load i32, ptr %3, align 4, !tbaa !5
  %12 = icmp slt i32 %11, 2
  br i1 %12, label %13, label %14

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

14:                                               ; preds = %1
  %15 = load i32, ptr %3, align 4, !tbaa !5
  %16 = icmp eq i32 %15, 2
  br i1 %16, label %17, label %18

17:                                               ; preds = %14
  store i32 1, ptr %2, align 4
  br label %79

18:                                               ; preds = %14
  %19 = load i32, ptr %3, align 4, !tbaa !5
  %20 = icmp eq i32 %19, 3
  br i1 %20, label %21, label %22

21:                                               ; preds = %18
  store i32 2, ptr %2, align 4
  br label %79

22:                                               ; preds = %18
  %23 = load i32, ptr %3, align 4, !tbaa !5
  %24 = icmp eq i32 %23, 4
  br i1 %24, label %25, label %26

25:                                               ; preds = %22
  store i32 4, ptr %2, align 4
  br label %79

26:                                               ; preds = %22
  %27 = load i32, ptr %3, align 4, !tbaa !5
  %28 = add nsw i32 %27, 1
  %29 = zext i32 %28 to i64
  %30 = call ptr @llvm.stacksave.p0()
  store ptr %30, ptr %4, align 8
  %31 = alloca i32, i64 %29, align 16
  store i64 %29, ptr %5, align 8
  %32 = getelementptr inbounds i32, ptr %31, i64 0
  store i32 0, ptr %32, align 16, !tbaa !5
  %33 = getelementptr inbounds i32, ptr %31, i64 1
  store i32 1, ptr %33, align 4, !tbaa !5
  %34 = getelementptr inbounds i32, ptr %31, i64 2
  store i32 2, ptr %34, align 8, !tbaa !5
  %35 = getelementptr inbounds i32, ptr %31, i64 3
  store i32 3, ptr %35, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 0, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 4, ptr %7, align 4, !tbaa !5
  br label %36

36:                                               ; preds = %73, %26
  %37 = load i32, ptr %7, align 4, !tbaa !5
  %38 = load i32, ptr %3, align 4, !tbaa !5
  %39 = icmp sle i32 %37, %38
  br i1 %39, label %41, label %40

40:                                               ; preds = %36
  store i32 2, ptr %8, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  br label %76

41:                                               ; preds = %36
  store i32 0, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  store i32 1, ptr %9, align 4, !tbaa !5
  br label %42

42:                                               ; preds = %65, %41
  %43 = load i32, ptr %9, align 4, !tbaa !5
  %44 = load i32, ptr %7, align 4, !tbaa !5
  %45 = icmp slt i32 %43, %44
  br i1 %45, label %47, label %46

46:                                               ; preds = %42
  store i32 5, ptr %8, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  br label %68

47:                                               ; preds = %42
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  %48 = load i32, ptr %9, align 4, !tbaa !5
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds i32, ptr %31, i64 %49
  %51 = load i32, ptr %50, align 4, !tbaa !5
  %52 = load i32, ptr %7, align 4, !tbaa !5
  %53 = load i32, ptr %9, align 4, !tbaa !5
  %54 = sub nsw i32 %52, %53
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds i32, ptr %31, i64 %55
  %57 = load i32, ptr %56, align 4, !tbaa !5
  %58 = mul nsw i32 %51, %57
  store i32 %58, ptr %10, align 4, !tbaa !5
  %59 = load i32, ptr %6, align 4, !tbaa !5
  %60 = load i32, ptr %10, align 4, !tbaa !5
  %61 = icmp slt i32 %59, %60
  br i1 %61, label %62, label %64

62:                                               ; preds = %47
  %63 = load i32, ptr %10, align 4, !tbaa !5
  store i32 %63, ptr %6, align 4, !tbaa !5
  br label %64

64:                                               ; preds = %62, %47
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  br label %65

65:                                               ; preds = %64
  %66 = load i32, ptr %9, align 4, !tbaa !5
  %67 = add nsw i32 %66, 1
  store i32 %67, ptr %9, align 4, !tbaa !5
  br label %42, !llvm.loop !9

68:                                               ; preds = %46
  %69 = load i32, ptr %6, align 4, !tbaa !5
  %70 = load i32, ptr %7, align 4, !tbaa !5
  %71 = sext i32 %70 to i64
  %72 = getelementptr inbounds i32, ptr %31, i64 %71
  store i32 %69, ptr %72, align 4, !tbaa !5
  br label %73

73:                                               ; preds = %68
  %74 = load i32, ptr %7, align 4, !tbaa !5
  %75 = add nsw i32 %74, 1
  store i32 %75, ptr %7, align 4, !tbaa !5
  br label %36, !llvm.loop !11

76:                                               ; preds = %40
  %77 = load i32, ptr %6, align 4, !tbaa !5
  store i32 %77, ptr %2, align 4
  store i32 1, ptr %8, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  %78 = load ptr, ptr %4, align 8
  call void @llvm.stackrestore.p0(ptr %78)
  br label %79

79:                                               ; preds = %76, %25, %21, %17, %13
  %80 = load i32, ptr %2, align 4
  ret i32 %80
}

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #1

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

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

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #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 }
attributes #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


; 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.maxCutProduct = private unnamed_addr constant [3 x i32] [i32 1, i32 2, i32 4], align 4

; Function Attrs: nofree norecurse nosync nounwind uwtable
define dso_local range(i32 0, -2147483648) i32 @maxCutProduct(i32 noundef %0) local_unnamed_addr #0 {
  %2 = icmp slt i32 %0, 2
  br i1 %2, label %30, label %3

3:                                                ; preds = %1
  %switch.tableidx = add nsw i32 %0, -2
  %4 = icmp ult i32 %switch.tableidx, 3
  br i1 %4, label %switch.lookup, label %.preheader.preheader

.preheader.preheader:                             ; preds = %3
  %5 = add nuw i32 %0, 1
  %6 = zext i32 %5 to i64
  %7 = tail call ptr @llvm.stacksave.p0()
  %8 = alloca i32, i64 %6, align 16
  store <4 x i32> <i32 0, i32 1, i32 2, i32 3>, ptr %8, align 16, !tbaa !5
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %.loopexit
  %indvar = phi i64 [ 0, %.preheader.preheader ], [ %indvar.next, %.loopexit ]
  %indvars.iv31 = phi i64 [ 4, %.preheader.preheader ], [ %indvars.iv.next32, %.loopexit ]
  %9 = add i64 %indvar, 3
  %min.iters.check = icmp ult i64 %9, 8
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.ph

scalar.ph.preheader:                              ; preds = %middle.block, %.preheader
  %indvars.iv.ph = phi i64 [ 1, %.preheader ], [ %ind.end, %middle.block ]
  %.126.ph = phi i32 [ 0, %.preheader ], [ %21, %middle.block ]
  br label %scalar.ph

vector.ph:                                        ; preds = %.preheader
  %n.vec = and i64 %9, -8
  %ind.end = or disjoint i64 %n.vec, 1
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %18, %vector.body ]
  %vec.phi36 = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %19, %vector.body ]
  %offset.idx = or disjoint i64 %index, 1
  %10 = getelementptr inbounds i32, ptr %8, i64 %offset.idx
  %11 = getelementptr inbounds i8, ptr %10, i64 16
  %wide.load = load <4 x i32>, ptr %10, align 4, !tbaa !5
  %wide.load37 = load <4 x i32>, ptr %11, align 4, !tbaa !5
  %12 = sub nsw i64 %indvars.iv31, %offset.idx
  %13 = getelementptr inbounds i32, ptr %8, i64 %12
  %14 = getelementptr inbounds i8, ptr %13, i64 -12
  %15 = getelementptr inbounds i8, ptr %13, i64 -28
  %wide.load38 = load <4 x i32>, ptr %14, align 4, !tbaa !5
  %reverse = shufflevector <4 x i32> %wide.load38, <4 x i32> poison, <4 x i32> <i32 3, i32 2, i32 1, i32 0>
  %wide.load39 = load <4 x i32>, ptr %15, align 4, !tbaa !5
  %reverse40 = shufflevector <4 x i32> %wide.load39, <4 x i32> poison, <4 x i32> <i32 3, i32 2, i32 1, i32 0>
  %16 = mul nsw <4 x i32> %reverse, %wide.load
  %17 = mul nsw <4 x i32> %reverse40, %wide.load37
  %18 = tail call <4 x i32> @llvm.smax.v4i32(<4 x i32> %vec.phi, <4 x i32> %16)
  %19 = tail call <4 x i32> @llvm.smax.v4i32(<4 x i32> %vec.phi36, <4 x i32> %17)
  %index.next = add nuw i64 %index, 8
  %20 = icmp eq i64 %index.next, %n.vec
  br i1 %20, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %rdx.minmax = tail call <4 x i32> @llvm.smax.v4i32(<4 x i32> %18, <4 x i32> %19)
  %21 = tail call i32 @llvm.vector.reduce.smax.v4i32(<4 x i32> %rdx.minmax)
  %cmp.n = icmp eq i64 %9, %n.vec
  br i1 %cmp.n, label %.loopexit, label %scalar.ph.preheader

._crit_edge:                                      ; preds = %.loopexit
  tail call void @llvm.stackrestore.p0(ptr %7)
  br label %30

.loopexit:                                        ; preds = %scalar.ph, %middle.block
  %spec.select.lcssa = phi i32 [ %21, %middle.block ], [ %spec.select, %scalar.ph ]
  %22 = getelementptr inbounds i32, ptr %8, i64 %indvars.iv31
  store i32 %spec.select.lcssa, ptr %22, align 4, !tbaa !5
  %indvars.iv.next32 = add nuw nsw i64 %indvars.iv31, 1
  %exitcond35.not = icmp eq i64 %indvars.iv.next32, %6
  %indvar.next = add i64 %indvar, 1
  br i1 %exitcond35.not, label %._crit_edge, label %.preheader, !llvm.loop !13

scalar.ph:                                        ; preds = %scalar.ph.preheader, %scalar.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %scalar.ph ], [ %indvars.iv.ph, %scalar.ph.preheader ]
  %.126 = phi i32 [ %spec.select, %scalar.ph ], [ %.126.ph, %scalar.ph.preheader ]
  %23 = getelementptr inbounds i32, ptr %8, i64 %indvars.iv
  %24 = load i32, ptr %23, align 4, !tbaa !5
  %25 = sub nsw i64 %indvars.iv31, %indvars.iv
  %26 = getelementptr inbounds i32, ptr %8, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %28 = mul nsw i32 %27, %24
  %spec.select = tail call i32 @llvm.smax.i32(i32 %.126, i32 %28)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %indvars.iv31
  br i1 %exitcond.not, label %.loopexit, label %scalar.ph, !llvm.loop !14

switch.lookup:                                    ; preds = %3
  %29 = zext nneg i32 %switch.tableidx to i64
  %switch.gep = getelementptr inbounds [3 x i32], ptr @switch.table.maxCutProduct, i64 0, i64 %29
  %switch.load = load i32, ptr %switch.gep, align 4
  br label %30

30:                                               ; preds = %switch.lookup, %1, %._crit_edge
  %.0 = phi i32 [ %spec.select.lcssa, %._crit_edge ], [ 0, %1 ], [ %switch.load, %switch.lookup ]
  ret i32 %.0
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #1

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare <4 x i32> @llvm.smax.v4i32(<4 x i32>, <4 x i32>) #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.smax.v4i32(<4 x i32>) #2

attributes #0 = { nofree norecurse nosync 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 = { mustprogress nocallback nofree nosync nounwind willreturn }
attributes #2 = { 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, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10, !11, !12}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!"llvm.loop.isvectorized", i32 1}
!12 = !{!"llvm.loop.unroll.runtime.disable"}
!13 = distinct !{!13, !10}
!14 = distinct !{!14, !10, !12, !11}


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