; LLVM IR from HarshCasper/NeoAlgo :: C/cp (Stock_Span)

; 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.2 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [2 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local i32 @isempty() #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @peek() #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @pop() #0

; 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

; Function Attrs: nounwind uwtable
declare dso_local void @push(i32 noundef) #0

; Function Attrs: nounwind uwtable
define dso_local void @calcSpan(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  %9 = load i32, ptr %4, align 4, !tbaa !9
  %10 = zext i32 %9 to i64
  %11 = call ptr @llvm.stacksave.p0()
  store ptr %11, ptr %6, align 8
  %12 = alloca i32, i64 %10, align 16
  store i64 %10, ptr %7, align 8
  %13 = load i32, ptr %4, align 4, !tbaa !9
  %14 = icmp eq i32 %13, 0
  br i1 %14, label %15, label %16

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

16:                                               ; preds = %2
  %17 = getelementptr inbounds i32, ptr %12, i64 0
  store i32 1, ptr %17, align 16, !tbaa !9
  call void @push(i32 noundef 0)
  store i32 1, ptr %5, align 4, !tbaa !9
  br label %18

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

22:                                               ; preds = %18
  br label %23

23:                                               ; preds = %40, %22
  %24 = call i32 @isempty()
  %25 = icmp ne i32 %24, 0
  br i1 %25, label %38, label %26

26:                                               ; preds = %23
  %27 = load ptr, ptr %3, align 8, !tbaa !5
  %28 = load i32, ptr %5, 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
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = call i32 @peek()
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds i32, ptr %32, i64 %34
  %36 = load i32, ptr %35, align 4, !tbaa !9
  %37 = icmp sgt i32 %31, %36
  br label %38

38:                                               ; preds = %26, %23
  %39 = phi i1 [ false, %23 ], [ %37, %26 ]
  br i1 %39, label %40, label %42

40:                                               ; preds = %38
  %41 = call i32 @pop()
  br label %23, !llvm.loop !11

42:                                               ; preds = %38
  %43 = call i32 @isempty()
  %44 = icmp ne i32 %43, 0
  br i1 %44, label %45, label %51

45:                                               ; preds = %42
  %46 = load i32, ptr %5, align 4, !tbaa !9
  %47 = add nsw i32 %46, 1
  %48 = load i32, ptr %5, align 4, !tbaa !9
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds i32, ptr %12, i64 %49
  store i32 %47, ptr %50, align 4, !tbaa !9
  br label %58

51:                                               ; preds = %42
  %52 = load i32, ptr %5, align 4, !tbaa !9
  %53 = call i32 @peek()
  %54 = sub nsw i32 %52, %53
  %55 = load i32, ptr %5, align 4, !tbaa !9
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds i32, ptr %12, i64 %56
  store i32 %54, ptr %57, align 4, !tbaa !9
  br label %58

58:                                               ; preds = %51, %45
  %59 = load i32, ptr %5, align 4, !tbaa !9
  call void @push(i32 noundef %59)
  br label %60

60:                                               ; preds = %58
  %61 = load i32, ptr %5, align 4, !tbaa !9
  %62 = add nsw i32 %61, 1
  store i32 %62, ptr %5, align 4, !tbaa !9
  br label %18, !llvm.loop !13

63:                                               ; preds = %18
  store i32 0, ptr %5, align 4, !tbaa !9
  br label %64

64:                                               ; preds = %74, %63
  %65 = load i32, ptr %5, align 4, !tbaa !9
  %66 = load i32, ptr %4, align 4, !tbaa !9
  %67 = icmp slt i32 %65, %66
  br i1 %67, label %68, label %77

68:                                               ; preds = %64
  %69 = load i32, ptr %5, align 4, !tbaa !9
  %70 = sext i32 %69 to i64
  %71 = getelementptr inbounds i32, ptr %12, i64 %70
  %72 = load i32, ptr %71, align 4, !tbaa !9
  %73 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i32 noundef %72)
  br label %74

74:                                               ; preds = %68
  %75 = load i32, ptr %5, align 4, !tbaa !9
  %76 = add nsw i32 %75, 1
  store i32 %76, ptr %5, align 4, !tbaa !9
  br label %64, !llvm.loop !14

77:                                               ; preds = %64
  %78 = call i32 (ptr, ...) @printf(ptr noundef @.str.3)
  store i32 0, ptr %8, align 4
  br label %79

79:                                               ; preds = %77, %15
  %80 = load ptr, ptr %6, align 8
  call void @llvm.stackrestore.p0(ptr %80)
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  %81 = load i32, ptr %8, align 4
  switch i32 %81, label %83 [
    i32 0, label %82
    i32 1, label %82
  ]

82:                                               ; preds = %79, %79
  ret void

83:                                               ; preds = %79
  unreachable
}

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

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

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 = { nocallback nofree nosync nounwind willreturn }
attributes #4 = { 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"}
!13 = distinct !{!13, !12}
!14 = distinct !{!14, !12}


; 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.2 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [2 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local i32 @isempty() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @peek() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @pop() local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
declare dso_local void @push(i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @calcSpan(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = zext i32 %1 to i64
  %4 = alloca i32, i64 %3, align 16
  %5 = icmp eq i32 %1, 0
  br i1 %5, label %34, label %6

6:                                                ; preds = %2
  store i32 1, ptr %4, align 16, !tbaa !5
  tail call void @push(i32 noundef 0)
  %7 = icmp sgt i32 %1, 1
  br i1 %7, label %.preheader19, label %.preheader

.preheader19:                                     ; preds = %6, %28
  %indvars.iv = phi i64 [ %indvars.iv.next.pre-phi, %28 ], [ 1, %6 ]
  %8 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  %9 = tail call i32 @isempty()
  %.not20 = icmp eq i32 %9, 0
  br i1 %.not20, label %.lr.ph, label %.critedge

.preheader:                                       ; preds = %28, %6
  %10 = icmp sgt i32 %1, 0
  br i1 %10, label %.lr.ph24, label %._crit_edge

.lr.ph:                                           ; preds = %.preheader19, %17
  %11 = load i32, ptr %8, align 4, !tbaa !5
  %12 = tail call i32 @peek()
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds i32, ptr %0, i64 %13
  %15 = load i32, ptr %14, align 4, !tbaa !5
  %16 = icmp sgt i32 %11, %15
  br i1 %16, label %17, label %.critedge

17:                                               ; preds = %.lr.ph
  %18 = tail call i32 @pop()
  %19 = tail call i32 @isempty()
  %.not = icmp eq i32 %19, 0
  br i1 %.not, label %.lr.ph, label %.critedge, !llvm.loop !9

.critedge:                                        ; preds = %.lr.ph, %17, %.preheader19
  %20 = tail call i32 @isempty()
  %.not18 = icmp eq i32 %20, 0
  br i1 %.not18, label %24, label %21

21:                                               ; preds = %.critedge
  %22 = add nuw nsw i64 %indvars.iv, 1
  %23 = trunc nuw nsw i64 %22 to i32
  %.pre = trunc nuw nsw i64 %indvars.iv to i32
  br label %28

24:                                               ; preds = %.critedge
  %25 = tail call i32 @peek()
  %26 = trunc nuw nsw i64 %indvars.iv to i32
  %27 = sub nsw i32 %26, %25
  %.pre31 = add nuw nsw i64 %indvars.iv, 1
  br label %28

28:                                               ; preds = %24, %21
  %indvars.iv.next.pre-phi = phi i64 [ %.pre31, %24 ], [ %22, %21 ]
  %.pre-phi = phi i32 [ %26, %24 ], [ %.pre, %21 ]
  %.sink = phi i32 [ %27, %24 ], [ %23, %21 ]
  %29 = getelementptr inbounds i32, ptr %4, i64 %indvars.iv
  store i32 %.sink, ptr %29, align 4
  tail call void @push(i32 noundef %.pre-phi)
  %exitcond.not = icmp eq i64 %indvars.iv.next.pre-phi, %3
  br i1 %exitcond.not, label %.preheader, label %.preheader19, !llvm.loop !11

.lr.ph24:                                         ; preds = %.preheader, %.lr.ph24
  %indvars.iv26 = phi i64 [ %indvars.iv.next27, %.lr.ph24 ], [ 0, %.preheader ]
  %30 = getelementptr inbounds i32, ptr %4, i64 %indvars.iv26
  %31 = load i32, ptr %30, align 4, !tbaa !5
  %32 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %31)
  %indvars.iv.next27 = add nuw nsw i64 %indvars.iv26, 1
  %exitcond30.not = icmp eq i64 %indvars.iv.next27, %3
  br i1 %exitcond30.not, label %._crit_edge, label %.lr.ph24, !llvm.loop !12

._crit_edge:                                      ; preds = %.lr.ph24, %.preheader
  %33 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3)
  br label %34

34:                                               ; preds = %2, %._crit_edge
  ret void
}

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


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