; LLVM IR from Beliavsky/FortranTip :: squares (squares_p)

; 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 ptr @squares_p(i32 noundef %0, ptr noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  %9 = call noalias ptr @malloc(i64 noundef 4000000) #5
  store ptr %9, ptr %5, align 8, !tbaa !9
  %10 = load ptr, ptr %4, align 8, !tbaa !9
  store i32 1000000, ptr %10, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  store i32 0, ptr %7, align 4, !tbaa !5
  br label %11

11:                                               ; preds = %41, %2
  %12 = load i32, ptr %7, align 4, !tbaa !5
  %13 = icmp slt i32 %12, 1000000
  br i1 %13, label %15, label %14

14:                                               ; preds = %11
  store i32 2, ptr %8, align 4
  br label %44

15:                                               ; preds = %11
  %16 = load i32, ptr %7, align 4, !tbaa !5
  %17 = add nsw i32 %16, 1
  store i32 %17, ptr %6, align 4, !tbaa !5
  %18 = load i32, ptr %6, align 4, !tbaa !5
  %19 = load i32, ptr %6, align 4, !tbaa !5
  %20 = mul nsw i32 %18, %19
  %21 = load ptr, ptr %5, align 8, !tbaa !9
  %22 = load i32, ptr %7, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds i32, ptr %21, i64 %23
  store i32 %20, ptr %24, align 4, !tbaa !5
  %25 = load ptr, ptr %5, align 8, !tbaa !9
  %26 = load i32, ptr %7, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds i32, ptr %25, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !5
  %30 = load i32, ptr %3, align 4, !tbaa !5
  %31 = icmp sgt i32 %29, %30
  br i1 %31, label %32, label %40

32:                                               ; preds = %15
  %33 = load i32, ptr %7, align 4, !tbaa !5
  %34 = load ptr, ptr %4, align 8, !tbaa !9
  store i32 %33, ptr %34, align 4, !tbaa !5
  %35 = load ptr, ptr %5, align 8, !tbaa !9
  %36 = load ptr, ptr %4, align 8, !tbaa !9
  %37 = load i32, ptr %36, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = call ptr @realloc(ptr noundef %35, i64 noundef %38) #6
  store ptr %39, ptr %5, align 8, !tbaa !9
  store i32 2, ptr %8, align 4
  br label %44

40:                                               ; preds = %15
  br label %41

41:                                               ; preds = %40
  %42 = load i32, ptr %7, align 4, !tbaa !5
  %43 = add nsw i32 %42, 1
  store i32 %43, ptr %7, align 4, !tbaa !5
  br label %11, !llvm.loop !11

44:                                               ; preds = %32, %14
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  br label %45

45:                                               ; preds = %44
  %46 = load ptr, ptr %5, align 8, !tbaa !9
  store i32 1, ptr %8, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  ret ptr %46
}

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

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

; Function Attrs: nounwind allocsize(1)
declare ptr @realloc(ptr noundef, i64 noundef) #3

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 memory(argmem: readwrite) }
attributes #2 = { nounwind allocsize(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 = { nounwind allocsize(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 #4 = { nounwind }
attributes #5 = { nounwind allocsize(0) }
attributes #6 = { nounwind allocsize(1) }

!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 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !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"

; Function Attrs: nounwind memory(readwrite, argmem: write) uwtable
define dso_local noalias noundef ptr @squares_p(i32 noundef %0, ptr nocapture noundef writeonly %1) local_unnamed_addr #0 {
  %3 = tail call noalias dereferenceable_or_null(4000000) ptr @malloc(i64 noundef 4000000) #3
  store i32 1000000, ptr %1, align 4, !tbaa !5
  br label %4

4:                                                ; preds = %20, %2
  %indvars.iv = phi i64 [ 0, %2 ], [ %indvars.iv.next.3, %20 ]
  %exitcond.not = icmp eq i64 %indvars.iv, 1000000
  br i1 %exitcond.not, label %.loopexit, label %5

5:                                                ; preds = %4
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %6 = mul nuw nsw i64 %indvars.iv.next, %indvars.iv.next
  %7 = getelementptr inbounds i32, ptr %3, i64 %indvars.iv
  %8 = trunc nuw i64 %6 to i32
  store i32 %8, ptr %7, align 4, !tbaa !5
  %9 = icmp sgt i32 %8, %0
  br i1 %9, label %25, label %10, !llvm.loop !9

10:                                               ; preds = %5
  %indvars.iv.next.1 = or disjoint i64 %indvars.iv, 2
  %11 = mul nuw nsw i64 %indvars.iv.next.1, %indvars.iv.next.1
  %12 = getelementptr inbounds i32, ptr %3, i64 %indvars.iv.next
  %13 = trunc nuw i64 %11 to i32
  store i32 %13, ptr %12, align 4, !tbaa !5
  %14 = icmp sgt i32 %13, %0
  br i1 %14, label %25, label %15, !llvm.loop !9

15:                                               ; preds = %10
  %indvars.iv.next.2 = or disjoint i64 %indvars.iv, 3
  %16 = mul nuw nsw i64 %indvars.iv.next.2, %indvars.iv.next.2
  %17 = getelementptr inbounds i32, ptr %3, i64 %indvars.iv.next.1
  %18 = trunc nuw i64 %16 to i32
  store i32 %18, ptr %17, align 4, !tbaa !5
  %19 = icmp sgt i32 %18, %0
  br i1 %19, label %25, label %20, !llvm.loop !9

20:                                               ; preds = %15
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %21 = mul nuw nsw i64 %indvars.iv.next.3, %indvars.iv.next.3
  %22 = getelementptr inbounds i32, ptr %3, i64 %indvars.iv.next.2
  %23 = trunc nuw i64 %21 to i32
  store i32 %23, ptr %22, align 4, !tbaa !5
  %24 = icmp sgt i32 %23, %0
  br i1 %24, label %25, label %4, !llvm.loop !9

25:                                               ; preds = %20, %15, %10, %5
  %indvars.iv.lcssa21 = phi i64 [ %indvars.iv, %5 ], [ %indvars.iv.next, %10 ], [ %indvars.iv.next.1, %15 ], [ %indvars.iv.next.2, %20 ]
  %26 = trunc nuw nsw i64 %indvars.iv.lcssa21 to i32
  store i32 %26, ptr %1, align 4, !tbaa !5
  %27 = tail call ptr @realloc(ptr noundef nonnull %3, i64 noundef %indvars.iv.lcssa21) #4
  br label %.loopexit

.loopexit:                                        ; preds = %4, %25
  %.015 = phi ptr [ %27, %25 ], [ %3, %4 ]
  ret ptr %.015
}

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nounwind willreturn allockind("realloc") allocsize(1) memory(argmem: readwrite, inaccessiblemem: readwrite)
declare noalias noundef ptr @realloc(ptr allocptr nocapture noundef, i64 noundef) local_unnamed_addr #2

attributes #0 = { nounwind memory(readwrite, argmem: write) 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 allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { mustprogress nounwind willreturn allockind("realloc") allocsize(1) memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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 allocsize(0) }
attributes #4 = { nounwind allocsize(1) }

!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: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass