; LLVM IR from freddiev4/dailyprogrammerchallenges :: Intermediate Challenges/Challenge 0014 Intermediate/solutions (solution)

; 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 [4 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local i32 @main(i32 noundef %0, ptr noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca [10001 x i32], align 16
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store i32 0, ptr %3, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 40004, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 1, ptr %7, align 4, !tbaa !5
  br label %9

9:                                                ; preds = %16, %2
  %10 = load i32, ptr %7, align 4, !tbaa !5
  %11 = icmp sle i32 %10, 10000
  br i1 %11, label %12, label %19

12:                                               ; preds = %9
  %13 = load i32, ptr %7, align 4, !tbaa !5
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [10001 x i32], ptr %6, i64 0, i64 %14
  store i32 1, ptr %15, align 4, !tbaa !5
  br label %16

16:                                               ; preds = %12
  %17 = load i32, ptr %7, align 4, !tbaa !5
  %18 = add nsw i32 %17, 1
  store i32 %18, ptr %7, align 4, !tbaa !5
  br label %9, !llvm.loop !11

19:                                               ; preds = %9
  store i32 1, ptr %8, align 4, !tbaa !5
  br label %20

20:                                               ; preds = %54, %19
  %21 = load i32, ptr %8, align 4, !tbaa !5
  %22 = icmp sle i32 %21, 10000
  br i1 %22, label %23, label %57

23:                                               ; preds = %20
  store i32 1, ptr %7, align 4, !tbaa !5
  br label %24

24:                                               ; preds = %50, %23
  %25 = load i32, ptr %7, align 4, !tbaa !5
  %26 = load i32, ptr %8, align 4, !tbaa !5
  %27 = icmp sle i32 %25, %26
  br i1 %27, label %28, label %53

28:                                               ; preds = %24
  %29 = load i32, ptr %7, align 4, !tbaa !5
  %30 = load i32, ptr %8, align 4, !tbaa !5
  %31 = add nsw i32 %29, %30
  %32 = load i32, ptr %7, align 4, !tbaa !5
  %33 = mul nsw i32 2, %32
  %34 = load i32, ptr %8, align 4, !tbaa !5
  %35 = mul nsw i32 %33, %34
  %36 = add nsw i32 %31, %35
  %37 = icmp sgt i32 %36, 10000
  br i1 %37, label %38, label %39

38:                                               ; preds = %28
  br label %53

39:                                               ; preds = %28
  %40 = load i32, ptr %7, align 4, !tbaa !5
  %41 = load i32, ptr %8, align 4, !tbaa !5
  %42 = add nsw i32 %40, %41
  %43 = load i32, ptr %7, align 4, !tbaa !5
  %44 = mul nsw i32 2, %43
  %45 = load i32, ptr %8, align 4, !tbaa !5
  %46 = mul nsw i32 %44, %45
  %47 = add nsw i32 %42, %46
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [10001 x i32], ptr %6, i64 0, i64 %48
  store i32 0, ptr %49, align 4, !tbaa !5
  br label %50

50:                                               ; preds = %39
  %51 = load i32, ptr %7, align 4, !tbaa !5
  %52 = add nsw i32 %51, 1
  store i32 %52, ptr %7, align 4, !tbaa !5
  br label %24, !llvm.loop !13

53:                                               ; preds = %38, %24
  br label %54

54:                                               ; preds = %53
  %55 = load i32, ptr %8, align 4, !tbaa !5
  %56 = add nsw i32 %55, 1
  store i32 %56, ptr %8, align 4, !tbaa !5
  br label %20, !llvm.loop !14

57:                                               ; preds = %20
  store i32 1, ptr %7, align 4, !tbaa !5
  br label %58

58:                                               ; preds = %73, %57
  %59 = load i32, ptr %7, align 4, !tbaa !5
  %60 = icmp sle i32 %59, 10000
  br i1 %60, label %61, label %76

61:                                               ; preds = %58
  %62 = load i32, ptr %7, align 4, !tbaa !5
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds [10001 x i32], ptr %6, i64 0, i64 %63
  %65 = load i32, ptr %64, align 4, !tbaa !5
  %66 = icmp ne i32 %65, 0
  br i1 %66, label %67, label %72

67:                                               ; preds = %61
  %68 = load i32, ptr %7, align 4, !tbaa !5
  %69 = mul nsw i32 2, %68
  %70 = add nsw i32 %69, 1
  %71 = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %70)
  br label %72

72:                                               ; preds = %67, %61
  br label %73

73:                                               ; preds = %72
  %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 %58, !llvm.loop !15

76:                                               ; preds = %58
  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 40004, ptr %6) #3
  ret i32 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

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 = { 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 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}
!14 = distinct !{!14, !12}
!15 = distinct !{!15, !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 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: nofree nounwind uwtable
define dso_local noundef i32 @main(i32 noundef %0, ptr nocapture noundef readnone %1) local_unnamed_addr #0 {
vector.ph:
  %2 = alloca [10001 x i32], align 16
  call void @llvm.lifetime.start.p0(i64 40004, ptr nonnull %2) #3
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next.4, %vector.body ]
  %offset.idx = or disjoint i64 %index, 1
  %3 = getelementptr inbounds [10001 x i32], ptr %2, i64 0, i64 %offset.idx
  %4 = getelementptr inbounds i8, ptr %3, i64 16
  store <4 x i32> <i32 1, i32 1, i32 1, i32 1>, ptr %3, align 4, !tbaa !5
  store <4 x i32> <i32 1, i32 1, i32 1, i32 1>, ptr %4, align 4, !tbaa !5
  %offset.idx.1 = add nuw nsw i64 %index, 9
  %5 = getelementptr inbounds [10001 x i32], ptr %2, i64 0, i64 %offset.idx.1
  %6 = getelementptr inbounds i8, ptr %5, i64 16
  store <4 x i32> <i32 1, i32 1, i32 1, i32 1>, ptr %5, align 4, !tbaa !5
  store <4 x i32> <i32 1, i32 1, i32 1, i32 1>, ptr %6, align 4, !tbaa !5
  %offset.idx.2 = add nuw nsw i64 %index, 17
  %7 = getelementptr inbounds [10001 x i32], ptr %2, i64 0, i64 %offset.idx.2
  %8 = getelementptr inbounds i8, ptr %7, i64 16
  store <4 x i32> <i32 1, i32 1, i32 1, i32 1>, ptr %7, align 4, !tbaa !5
  store <4 x i32> <i32 1, i32 1, i32 1, i32 1>, ptr %8, align 4, !tbaa !5
  %offset.idx.3 = add nuw nsw i64 %index, 25
  %9 = getelementptr inbounds [10001 x i32], ptr %2, i64 0, i64 %offset.idx.3
  %10 = getelementptr inbounds i8, ptr %9, i64 16
  store <4 x i32> <i32 1, i32 1, i32 1, i32 1>, ptr %9, align 4, !tbaa !5
  store <4 x i32> <i32 1, i32 1, i32 1, i32 1>, ptr %10, align 4, !tbaa !5
  %offset.idx.4 = add nuw nsw i64 %index, 33
  %11 = getelementptr inbounds [10001 x i32], ptr %2, i64 0, i64 %offset.idx.4
  %12 = getelementptr inbounds i8, ptr %11, i64 16
  store <4 x i32> <i32 1, i32 1, i32 1, i32 1>, ptr %11, align 4, !tbaa !5
  store <4 x i32> <i32 1, i32 1, i32 1, i32 1>, ptr %12, align 4, !tbaa !5
  %index.next.4 = add nuw nsw i64 %index, 40
  %13 = icmp eq i64 %index.next.4, 10000
  br i1 %13, label %.preheader23, label %vector.body, !llvm.loop !9

.preheader23:                                     ; preds = %vector.body, %22
  %indvars.iv38 = phi i64 [ %indvars.iv.next39, %22 ], [ 1, %vector.body ]
  %indvars.iv36 = phi i64 [ %indvars.iv.next37, %22 ], [ 2, %vector.body ]
  %14 = shl nuw nsw i64 %indvars.iv38, 1
  br label %15

15:                                               ; preds = %.preheader23, %20
  %indvars.iv30 = phi i64 [ 1, %.preheader23 ], [ %indvars.iv.next31, %20 ]
  %16 = add nuw nsw i64 %indvars.iv30, %indvars.iv38
  %17 = mul nuw nsw i64 %14, %indvars.iv30
  %18 = add nuw nsw i64 %16, %17
  %19 = icmp ugt i64 %18, 10000
  br i1 %19, label %22, label %20

20:                                               ; preds = %15
  %21 = getelementptr inbounds [10001 x i32], ptr %2, i64 0, i64 %18
  store i32 0, ptr %21, align 4, !tbaa !5
  %indvars.iv.next31 = add nuw nsw i64 %indvars.iv30, 1
  %exitcond35.not = icmp eq i64 %indvars.iv.next31, %indvars.iv36
  br i1 %exitcond35.not, label %22, label %15, !llvm.loop !13

22:                                               ; preds = %20, %15
  %indvars.iv.next39 = add nuw nsw i64 %indvars.iv38, 1
  %indvars.iv.next37 = add nuw nsw i64 %indvars.iv36, 1
  %exitcond43.not = icmp eq i64 %indvars.iv.next39, 10001
  br i1 %exitcond43.not, label %.preheader, label %.preheader23, !llvm.loop !14

.preheader:                                       ; preds = %22, %29
  %indvars.iv44 = phi i64 [ %indvars.iv.next45, %29 ], [ 1, %22 ]
  %23 = getelementptr inbounds [10001 x i32], ptr %2, i64 0, i64 %indvars.iv44
  %24 = load i32, ptr %23, align 4, !tbaa !5
  %.not = icmp eq i32 %24, 0
  br i1 %.not, label %29, label %25

25:                                               ; preds = %.preheader
  %indvars.iv44.tr = trunc i64 %indvars.iv44 to i32
  %26 = shl i32 %indvars.iv44.tr, 1
  %27 = or disjoint i32 %26, 1
  %28 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %27)
  br label %29

29:                                               ; preds = %.preheader, %25
  %indvars.iv.next45 = add nuw nsw i64 %indvars.iv44, 1
  %exitcond47.not = icmp eq i64 %indvars.iv.next45, 10001
  br i1 %exitcond47.not, label %30, label %.preheader, !llvm.loop !15

30:                                               ; preds = %29
  call void @llvm.lifetime.end.p0(i64 40004, ptr nonnull %2) #3
  ret i32 0
}

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

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

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

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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #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, !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}
!15 = distinct !{!15, !10}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass