; LLVM IR from simonaertssen/MIT-6.172-Performance-Engineering-of-Software-Systems :: Week 3 Vectorization/homework (loop)

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

%struct.timespec = type { i64, i64 }

@.str = external hidden unnamed_addr constant [72 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [9 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 [1024 x i32], align 16
  %7 = alloca [1024 x i32], align 16
  %8 = alloca [1024 x i32], align 16
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca %struct.timespec, align 8
  %14 = alloca %struct.timespec, align 8
  %15 = alloca double, align 8
  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 4096, ptr %6) #5
  call void @llvm.lifetime.start.p0(i64 4096, ptr %7) #5
  call void @llvm.lifetime.start.p0(i64 4096, ptr %8) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #5
  store i32 0, ptr %9, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #5
  store i32 0, ptr %12, align 4, !tbaa !5
  store i32 0, ptr %11, align 4, !tbaa !5
  br label %16

16:                                               ; preds = %29, %2
  %17 = load i32, ptr %11, align 4, !tbaa !5
  %18 = icmp slt i32 %17, 1024
  br i1 %18, label %19, label %32

19:                                               ; preds = %16
  %20 = load i32, ptr %11, align 4, !tbaa !5
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [1024 x i32], ptr %6, i64 0, i64 %21
  store i32 1, ptr %22, align 4, !tbaa !5
  %23 = load i32, ptr %11, align 4, !tbaa !5
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [1024 x i32], ptr %7, i64 0, i64 %24
  store i32 1, ptr %25, align 4, !tbaa !5
  %26 = load i32, ptr %11, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [1024 x i32], ptr %8, i64 0, i64 %27
  store i32 1, ptr %28, align 4, !tbaa !5
  br label %29

29:                                               ; preds = %19
  %30 = load i32, ptr %11, align 4, !tbaa !5
  %31 = add nsw i32 %30, 1
  store i32 %31, ptr %11, align 4, !tbaa !5
  br label %16, !llvm.loop !11

32:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 16, ptr %13) #5
  %33 = call { i64, i64 } @gettime()
  %34 = getelementptr inbounds { i64, i64 }, ptr %13, i32 0, i32 0
  %35 = extractvalue { i64, i64 } %33, 0
  store i64 %35, ptr %34, align 8
  %36 = getelementptr inbounds { i64, i64 }, ptr %13, i32 0, i32 1
  %37 = extractvalue { i64, i64 } %33, 1
  store i64 %37, ptr %36, align 8
  store i32 0, ptr %11, align 4, !tbaa !5
  br label %38

38:                                               ; preds = %48, %32
  %39 = load i32, ptr %11, align 4, !tbaa !5
  %40 = icmp slt i32 %39, 1024
  br i1 %40, label %41, label %51

41:                                               ; preds = %38
  %42 = load i32, ptr %11, align 4, !tbaa !5
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds [1024 x i32], ptr %6, i64 0, i64 %43
  %45 = load i32, ptr %44, align 4, !tbaa !5
  %46 = load i32, ptr %9, align 4, !tbaa !5
  %47 = add i32 %46, %45
  store i32 %47, ptr %9, align 4, !tbaa !5
  br label %48

48:                                               ; preds = %41
  %49 = load i32, ptr %11, align 4, !tbaa !5
  %50 = add nsw i32 %49, 1
  store i32 %50, ptr %11, align 4, !tbaa !5
  br label %38, !llvm.loop !13

51:                                               ; preds = %38
  call void @llvm.lifetime.start.p0(i64 16, ptr %14) #5
  %52 = call { i64, i64 } @gettime()
  %53 = getelementptr inbounds { i64, i64 }, ptr %14, i32 0, i32 0
  %54 = extractvalue { i64, i64 } %52, 0
  store i64 %54, ptr %53, align 8
  %55 = getelementptr inbounds { i64, i64 }, ptr %14, i32 0, i32 1
  %56 = extractvalue { i64, i64 } %52, 1
  store i64 %56, ptr %55, align 8
  %57 = call i32 @rand_r(ptr noundef %12) #5
  %58 = srem i32 %57, 1024
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds [1024 x i32], ptr %8, i64 0, i64 %59
  %61 = load i32, ptr %60, align 4, !tbaa !5
  %62 = load i32, ptr %9, align 4, !tbaa !5
  %63 = add i32 %62, %61
  store i32 %63, ptr %9, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #5
  %64 = getelementptr inbounds { i64, i64 }, ptr %13, i32 0, i32 0
  %65 = load i64, ptr %64, align 8
  %66 = getelementptr inbounds { i64, i64 }, ptr %13, i32 0, i32 1
  %67 = load i64, ptr %66, align 8
  %68 = getelementptr inbounds { i64, i64 }, ptr %14, i32 0, i32 0
  %69 = load i64, ptr %68, align 8
  %70 = getelementptr inbounds { i64, i64 }, ptr %14, i32 0, i32 1
  %71 = load i64, ptr %70, align 8
  %72 = call double @tdiff(i64 %65, i64 %67, i64 %69, i64 %71)
  store double %72, ptr %15, align 8, !tbaa !14
  %73 = load double, ptr %15, align 8, !tbaa !14
  %74 = call i32 (ptr, ...) @printf(ptr noundef @.str, double noundef %73, i32 noundef 1024, i32 noundef 10000000, ptr noundef @.str.1, ptr noundef @.str.2)
  %75 = load i32, ptr %9, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #5
  call void @llvm.lifetime.end.p0(i64 16, ptr %14) #5
  call void @llvm.lifetime.end.p0(i64 16, ptr %13) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 4096, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 4096, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 4096, ptr %6) #5
  ret i32 %75
}

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden { i64, i64 } @gettime() #2

; Function Attrs: nounwind
declare i32 @rand_r(ptr noundef) #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden double @tdiff(i64, i64, i64, i64) #2

declare i32 @printf(ptr noundef, ...) #4

; 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 = { inlinehint 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 #3 = { 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 #4 = { "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 #5 = { 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 = !{!15, !15, i64 0}
!15 = !{!"double", !7, i64 0}


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

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

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

middle.block:                                     ; preds = %vector.body
  %14 = tail call { i64, i64 } @gettime()
  br label %vector.body26

vector.body26:                                    ; preds = %vector.body26, %middle.block
  %index27 = phi i64 [ 0, %middle.block ], [ %index.next30.3, %vector.body26 ]
  %vec.phi = phi <4 x i32> [ zeroinitializer, %middle.block ], [ %29, %vector.body26 ]
  %vec.phi28 = phi <4 x i32> [ zeroinitializer, %middle.block ], [ %30, %vector.body26 ]
  %15 = getelementptr inbounds [1024 x i32], ptr %2, i64 0, i64 %index27
  %16 = getelementptr inbounds i8, ptr %15, i64 16
  %wide.load = load <4 x i32>, ptr %15, align 16, !tbaa !5
  %wide.load29 = load <4 x i32>, ptr %16, align 16, !tbaa !5
  %17 = add <4 x i32> %wide.load, %vec.phi
  %18 = add <4 x i32> %wide.load29, %vec.phi28
  %index.next30 = or disjoint i64 %index27, 8
  %19 = getelementptr inbounds [1024 x i32], ptr %2, i64 0, i64 %index.next30
  %20 = getelementptr inbounds i8, ptr %19, i64 16
  %wide.load.1 = load <4 x i32>, ptr %19, align 16, !tbaa !5
  %wide.load29.1 = load <4 x i32>, ptr %20, align 16, !tbaa !5
  %21 = add <4 x i32> %wide.load.1, %17
  %22 = add <4 x i32> %wide.load29.1, %18
  %index.next30.1 = or disjoint i64 %index27, 16
  %23 = getelementptr inbounds [1024 x i32], ptr %2, i64 0, i64 %index.next30.1
  %24 = getelementptr inbounds i8, ptr %23, i64 16
  %wide.load.2 = load <4 x i32>, ptr %23, align 16, !tbaa !5
  %wide.load29.2 = load <4 x i32>, ptr %24, align 16, !tbaa !5
  %25 = add <4 x i32> %wide.load.2, %21
  %26 = add <4 x i32> %wide.load29.2, %22
  %index.next30.2 = or disjoint i64 %index27, 24
  %27 = getelementptr inbounds [1024 x i32], ptr %2, i64 0, i64 %index.next30.2
  %28 = getelementptr inbounds i8, ptr %27, i64 16
  %wide.load.3 = load <4 x i32>, ptr %27, align 16, !tbaa !5
  %wide.load29.3 = load <4 x i32>, ptr %28, align 16, !tbaa !5
  %29 = add <4 x i32> %wide.load.3, %25
  %30 = add <4 x i32> %wide.load29.3, %26
  %index.next30.3 = add nuw nsw i64 %index27, 32
  %31 = icmp eq i64 %index.next30.3, 1024
  br i1 %31, label %middle.block22, label %vector.body26, !llvm.loop !13

middle.block22:                                   ; preds = %vector.body26
  %bin.rdx = add <4 x i32> %30, %29
  %32 = tail call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx)
  %33 = extractvalue { i64, i64 } %14, 1
  %34 = extractvalue { i64, i64 } %14, 0
  %35 = tail call { i64, i64 } @gettime()
  %36 = extractvalue { i64, i64 } %35, 0
  %37 = extractvalue { i64, i64 } %35, 1
  %38 = call i32 @rand_r(ptr noundef nonnull %4) #6
  %39 = srem i32 %38, 1024
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [1024 x i32], ptr %3, i64 0, i64 %40
  %42 = load i32, ptr %41, align 4, !tbaa !5
  %43 = add i32 %42, %32
  %44 = call double @tdiff(i64 %34, i64 %33, i64 %36, i64 %37)
  %45 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, double noundef %44, i32 noundef 1024, i32 noundef 10000000, ptr noundef nonnull @.str.1, ptr noundef nonnull @.str.2)
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %4) #6
  call void @llvm.lifetime.end.p0(i64 4096, ptr nonnull %3) #6
  call void @llvm.lifetime.end.p0(i64 4096, ptr nonnull %2) #6
  ret i32 %43
}

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden { i64, i64 } @gettime() local_unnamed_addr #2

; Function Attrs: nounwind
declare i32 @rand_r(ptr noundef) local_unnamed_addr #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden double @tdiff(i64, i64, i64, i64) local_unnamed_addr #2

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

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

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

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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint 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 #3 = { 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 #4 = { 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 #5 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #6 = { 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, !11, !12}


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