; LLVM IR from ahampriyanshu/algo-ds-101 :: Algorithms/DP/LSCS (LSCS)

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @largest_subarray_sum(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %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
  %11 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  %12 = load i32, ptr %5, align 4, !tbaa !9
  %13 = icmp sle i32 %12, 0
  br i1 %13, label %14, label %16

14:                                               ; preds = %2
  %15 = call i32 (ptr, ...) @printf(ptr noundef @.str)
  store i32 1, ptr %3, align 4
  br label %57

16:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 0, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 0, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 0, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  store i32 0, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  store i32 -2147483648, ptr %10, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  store i32 0, ptr %11, align 4, !tbaa !9
  br label %17

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

21:                                               ; preds = %17
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  br label %52

22:                                               ; preds = %17
  %23 = load i32, ptr %9, align 4, !tbaa !9
  %24 = icmp sle i32 %23, 0
  br i1 %24, label %25, label %32

25:                                               ; preds = %22
  %26 = load i32, ptr %11, align 4, !tbaa !9
  store i32 %26, ptr %6, align 4, !tbaa !9
  %27 = load ptr, ptr %4, align 8, !tbaa !5
  %28 = load i32, ptr %11, align 4, !tbaa !9
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds i32, ptr %27, i64 %29
  %31 = load i32, ptr %30, align 4, !tbaa !9
  store i32 %31, ptr %9, align 4, !tbaa !9
  br label %40

32:                                               ; preds = %22
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = load i32, ptr %11, align 4, !tbaa !9
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds i32, ptr %33, i64 %35
  %37 = load i32, ptr %36, align 4, !tbaa !9
  %38 = load i32, ptr %9, align 4, !tbaa !9
  %39 = add nsw i32 %38, %37
  store i32 %39, ptr %9, align 4, !tbaa !9
  br label %40

40:                                               ; preds = %32, %25
  %41 = load i32, ptr %9, align 4, !tbaa !9
  %42 = load i32, ptr %10, align 4, !tbaa !9
  %43 = icmp sgt i32 %41, %42
  br i1 %43, label %44, label %48

44:                                               ; preds = %40
  %45 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %45, ptr %10, align 4, !tbaa !9
  %46 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %46, ptr %7, align 4, !tbaa !9
  %47 = load i32, ptr %11, align 4, !tbaa !9
  store i32 %47, ptr %8, align 4, !tbaa !9
  br label %48

48:                                               ; preds = %44, %40
  br label %49

49:                                               ; preds = %48
  %50 = load i32, ptr %11, align 4, !tbaa !9
  %51 = add nsw i32 %50, 1
  store i32 %51, ptr %11, align 4, !tbaa !9
  br label %17, !llvm.loop !11

52:                                               ; preds = %21
  %53 = load i32, ptr %10, align 4, !tbaa !9
  %54 = load i32, ptr %7, align 4, !tbaa !9
  %55 = load i32, ptr %8, align 4, !tbaa !9
  %56 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %53, i32 noundef %54, i32 noundef %55)
  store i32 0, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  br label %57

57:                                               ; preds = %52, %14
  %58 = load i32, ptr %3, align 4
  ret i32 %58
}

declare i32 @printf(ptr noundef, ...) #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

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 = { "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 = { 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 = !{!"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 = distinct !{!11, !12}
!12 = !{!"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"

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

; Function Attrs: nofree nounwind uwtable
define dso_local range(i32 0, 2) i32 @largest_subarray_sum(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp slt i32 %1, 1
  br i1 %3, label %5, label %.preheader.preheader

.preheader.preheader:                             ; preds = %2
  %wide.trip.count = zext nneg i32 %1 to i64
  %xtraiter = and i64 %wide.trip.count, 1
  %4 = icmp eq i32 %1, 1
  br i1 %4, label %.unr-lcssa, label %.preheader.preheader.new

.preheader.preheader.new:                         ; preds = %.preheader.preheader
  %unroll_iter = and i64 %wide.trip.count, 2147483646
  br label %.preheader

5:                                                ; preds = %2
  %6 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str)
  br label %27

.unr-lcssa:                                       ; preds = %.preheader, %.preheader.preheader
  %.125.lcssa.ph = phi i32 [ poison, %.preheader.preheader ], [ %.125.1, %.preheader ]
  %.123.lcssa.ph = phi i32 [ poison, %.preheader.preheader ], [ %.123.1, %.preheader ]
  %.1.lcssa.ph = phi i32 [ poison, %.preheader.preheader ], [ %.1.1, %.preheader ]
  %indvars.iv.unr = phi i64 [ 0, %.preheader.preheader ], [ %indvars.iv.next.1, %.preheader ]
  %.01934.unr = phi i32 [ -2147483648, %.preheader.preheader ], [ %.1.1, %.preheader ]
  %.02033.unr = phi i32 [ 0, %.preheader.preheader ], [ %.121.1, %.preheader ]
  %.02232.unr = phi i32 [ 0, %.preheader.preheader ], [ %.123.1, %.preheader ]
  %.02431.unr = phi i32 [ 0, %.preheader.preheader ], [ %.125.1, %.preheader ]
  %.02630.unr = phi i32 [ 0, %.preheader.preheader ], [ %.127.1, %.preheader ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %13, label %.preheader.epil

.preheader.epil:                                  ; preds = %.unr-lcssa
  %7 = icmp slt i32 %.02033.unr, 1
  %8 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.unr
  %9 = load i32, ptr %8, align 4, !tbaa !5
  %.pre.epil = trunc nuw nsw i64 %indvars.iv.unr to i32
  %10 = trunc nuw nsw i64 %indvars.iv.unr to i32
  %.pre-phi.epil = select i1 %7, i32 %10, i32 %.pre.epil
  %.127.epil = select i1 %7, i32 %10, i32 %.02630.unr
  %11 = select i1 %7, i32 0, i32 %.02033.unr
  %.121.epil = add nsw i32 %9, %11
  %12 = icmp sgt i32 %.121.epil, %.01934.unr
  %.125.epil = select i1 %12, i32 %.127.epil, i32 %.02431.unr
  %.123.epil = select i1 %12, i32 %.pre-phi.epil, i32 %.02232.unr
  %.1.epil = tail call i32 @llvm.smax.i32(i32 %.121.epil, i32 %.01934.unr)
  br label %13

13:                                               ; preds = %.unr-lcssa, %.preheader.epil
  %.125.lcssa = phi i32 [ %.125.lcssa.ph, %.unr-lcssa ], [ %.125.epil, %.preheader.epil ]
  %.123.lcssa = phi i32 [ %.123.lcssa.ph, %.unr-lcssa ], [ %.123.epil, %.preheader.epil ]
  %.1.lcssa = phi i32 [ %.1.lcssa.ph, %.unr-lcssa ], [ %.1.epil, %.preheader.epil ]
  %14 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %.1.lcssa, i32 noundef %.125.lcssa, i32 noundef %.123.lcssa)
  br label %27

.preheader:                                       ; preds = %.preheader, %.preheader.preheader.new
  %indvars.iv = phi i64 [ 0, %.preheader.preheader.new ], [ %indvars.iv.next.1, %.preheader ]
  %.01934 = phi i32 [ -2147483648, %.preheader.preheader.new ], [ %.1.1, %.preheader ]
  %.02033 = phi i32 [ 0, %.preheader.preheader.new ], [ %.121.1, %.preheader ]
  %.02232 = phi i32 [ 0, %.preheader.preheader.new ], [ %.123.1, %.preheader ]
  %.02431 = phi i32 [ 0, %.preheader.preheader.new ], [ %.125.1, %.preheader ]
  %.02630 = phi i32 [ 0, %.preheader.preheader.new ], [ %.127.1, %.preheader ]
  %niter = phi i64 [ 0, %.preheader.preheader.new ], [ %niter.next.1, %.preheader ]
  %15 = icmp slt i32 %.02033, 1
  %16 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  %17 = load i32, ptr %16, align 4, !tbaa !5
  %.pre = trunc nuw nsw i64 %indvars.iv to i32
  %18 = trunc nuw nsw i64 %indvars.iv to i32
  %.pre-phi = select i1 %15, i32 %18, i32 %.pre
  %.127 = select i1 %15, i32 %18, i32 %.02630
  %19 = select i1 %15, i32 0, i32 %.02033
  %.121 = add nsw i32 %17, %19
  %20 = icmp sgt i32 %.121, %.01934
  %.125 = select i1 %20, i32 %.127, i32 %.02431
  %.123 = select i1 %20, i32 %.pre-phi, i32 %.02232
  %.1 = tail call i32 @llvm.smax.i32(i32 %.121, i32 %.01934)
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %21 = icmp slt i32 %.121, 1
  %22 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.next
  %23 = load i32, ptr %22, align 4, !tbaa !5
  %.pre.1 = trunc nuw nsw i64 %indvars.iv.next to i32
  %24 = trunc nuw nsw i64 %indvars.iv.next to i32
  %.pre-phi.1 = select i1 %21, i32 %24, i32 %.pre.1
  %.127.1 = select i1 %21, i32 %24, i32 %.127
  %25 = select i1 %21, i32 0, i32 %.121
  %.121.1 = add nsw i32 %23, %25
  %26 = icmp sgt i32 %.121.1, %.1
  %.125.1 = select i1 %26, i32 %.127.1, i32 %.125
  %.123.1 = select i1 %26, i32 %.pre-phi.1, i32 %.123
  %.1.1 = tail call i32 @llvm.smax.i32(i32 %.121.1, i32 %.1)
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %.unr-lcssa, label %.preheader, !llvm.loop !9

27:                                               ; preds = %13, %5
  %.028 = phi i32 [ 1, %5 ], [ 0, %13 ]
  ret i32 %.028
}

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

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

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" }
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}
!10 = !{!"llvm.loop.mustprogress"}


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