; LLVM IR from chirag-singhal/Coding-Interview-101 :: Miscellaneous/Labs/Lab7 (complete)

; 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.timeval = type { i64, i64 }
%struct.employee = type { [20 x i8], i32 }

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

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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

; Function Attrs: nounwind uwtable
declare dso_local void @insertionSort(ptr noundef, i32 noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local void @quickSort(ptr noundef, i32 noundef, i32 noundef, i32 noundef) #2

; 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

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

19:                                               ; preds = %39, %2
  %20 = load i32, ptr %8, align 4, !tbaa !9
  %21 = load i32, ptr %4, align 4, !tbaa !9
  %22 = icmp slt i32 %20, %21
  br i1 %22, label %24, label %23

23:                                               ; preds = %19
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #6
  br label %42

24:                                               ; preds = %19
  %25 = load i32, ptr %8, align 4, !tbaa !9
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds %struct.employee, ptr %15, i64 %26
  %28 = load ptr, ptr %3, align 8, !tbaa !5
  %29 = load i32, ptr %8, align 4, !tbaa !9
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds %struct.employee, ptr %28, i64 %30
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %27, ptr align 4 %31, i64 24, i1 false), !tbaa.struct !11
  %32 = load i32, ptr %8, align 4, !tbaa !9
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds %struct.employee, ptr %18, i64 %33
  %35 = load ptr, ptr %3, align 8, !tbaa !5
  %36 = load i32, ptr %8, align 4, !tbaa !9
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds %struct.employee, ptr %35, i64 %37
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %34, ptr align 4 %38, i64 24, i1 false), !tbaa.struct !11
  br label %39

39:                                               ; preds = %24
  %40 = load i32, ptr %8, align 4, !tbaa !9
  %41 = add nsw i32 %40, 1
  store i32 %41, ptr %8, align 4, !tbaa !9
  br label %19, !llvm.loop !13

42:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #6
  %43 = call noalias ptr @malloc(i64 noundef 16) #7
  store ptr %43, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 16, ptr %10) #6
  call void @llvm.lifetime.start.p0(i64 16, ptr %11) #6
  %44 = call i32 @gettimeofday(ptr noundef %10, ptr noundef null) #6
  %45 = load i32, ptr %4, align 4, !tbaa !9
  %46 = sub nsw i32 %45, 1
  call void @quickSort(ptr noundef %15, i32 noundef 0, i32 noundef %46, i32 noundef 0)
  %47 = call i32 @gettimeofday(ptr noundef %11, ptr noundef null) #6
  %48 = getelementptr inbounds %struct.timeval, ptr %11, i32 0, i32 0
  %49 = load i64, ptr %48, align 8, !tbaa !15
  %50 = getelementptr inbounds %struct.timeval, ptr %10, i32 0, i32 0
  %51 = load i64, ptr %50, align 8, !tbaa !15
  %52 = sub nsw i64 %49, %51
  %53 = mul nsw i64 %52, 1000
  %54 = sitofp i64 %53 to double
  %55 = load ptr, ptr %9, align 8, !tbaa !5
  %56 = getelementptr inbounds double, ptr %55, i64 1
  store double %54, ptr %56, align 8, !tbaa !18
  %57 = getelementptr inbounds %struct.timeval, ptr %11, i32 0, i32 1
  %58 = load i64, ptr %57, align 8, !tbaa !20
  %59 = sitofp i64 %58 to double
  %60 = getelementptr inbounds %struct.timeval, ptr %10, i32 0, i32 1
  %61 = load i64, ptr %60, align 8, !tbaa !20
  %62 = sitofp i64 %61 to double
  %63 = fsub double %59, %62
  %64 = fdiv double %63, 1.000000e+03
  %65 = load ptr, ptr %9, align 8, !tbaa !5
  %66 = getelementptr inbounds double, ptr %65, i64 1
  %67 = load double, ptr %66, align 8, !tbaa !18
  %68 = fadd double %67, %64
  store double %68, ptr %66, align 8, !tbaa !18
  %69 = call i32 @gettimeofday(ptr noundef %10, ptr noundef null) #6
  %70 = load i32, ptr %4, align 4, !tbaa !9
  call void @insertionSort(ptr noundef %18, i32 noundef %70)
  %71 = call i32 @gettimeofday(ptr noundef %11, ptr noundef null) #6
  %72 = getelementptr inbounds %struct.timeval, ptr %11, i32 0, i32 0
  %73 = load i64, ptr %72, align 8, !tbaa !15
  %74 = getelementptr inbounds %struct.timeval, ptr %10, i32 0, i32 0
  %75 = load i64, ptr %74, align 8, !tbaa !15
  %76 = sub nsw i64 %73, %75
  %77 = mul nsw i64 %76, 1000
  %78 = sitofp i64 %77 to double
  %79 = load ptr, ptr %9, align 8, !tbaa !5
  %80 = getelementptr inbounds double, ptr %79, i64 0
  store double %78, ptr %80, align 8, !tbaa !18
  %81 = getelementptr inbounds %struct.timeval, ptr %11, i32 0, i32 1
  %82 = load i64, ptr %81, align 8, !tbaa !20
  %83 = sitofp i64 %82 to double
  %84 = getelementptr inbounds %struct.timeval, ptr %10, i32 0, i32 1
  %85 = load i64, ptr %84, align 8, !tbaa !20
  %86 = sitofp i64 %85 to double
  %87 = fsub double %83, %86
  %88 = fdiv double %87, 1.000000e+03
  %89 = load ptr, ptr %9, align 8, !tbaa !5
  %90 = getelementptr inbounds double, ptr %89, i64 0
  %91 = load double, ptr %90, align 8, !tbaa !18
  %92 = fadd double %91, %88
  store double %92, ptr %90, align 8, !tbaa !18
  %93 = load ptr, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 16, ptr %11) #6
  call void @llvm.lifetime.end.p0(i64 16, ptr %10) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #6
  %94 = load ptr, ptr %5, align 8
  call void @llvm.stackrestore.p0(ptr %94)
  ret ptr %93
}

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

; Function Attrs: nounwind
declare i32 @gettimeofday(ptr noundef, ptr noundef) #5

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 = { nocallback nofree nosync nounwind willreturn }
attributes #4 = { 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 #5 = { 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 #6 = { nounwind }
attributes #7 = { nounwind allocsize(0) }

!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 = !{i64 0, i64 20, !12, i64 20, i64 4, !9}
!12 = !{!7, !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!16, !17, i64 0}
!16 = !{!"timeval", !17, i64 0, !17, i64 8}
!17 = !{!"long", !7, i64 0}
!18 = !{!19, !19, i64 0}
!19 = !{!"double", !7, i64 0}
!20 = !{!16, !17, i64 8}


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

%struct.timeval = type { i64, i64 }
%struct.employee = type { [20 x i8], i32 }

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

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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

; Function Attrs: nounwind uwtable
declare dso_local void @insertionSort(ptr noundef, i32 noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare dso_local void @quickSort(ptr noundef, i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local noalias noundef ptr @testRun(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #2 {
  %3 = alloca %struct.timeval, align 8
  %4 = alloca %struct.timeval, align 8
  %5 = zext i32 %1 to i64
  %6 = alloca %struct.employee, i64 %5, align 16
  %7 = alloca %struct.employee, i64 %5, align 16
  %8 = icmp sgt i32 %1, 0
  br i1 %8, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %2
  %9 = mul nuw nsw i64 %5, 24
  call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 16 %6, ptr align 4 %0, i64 %9, i1 false)
  call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 16 %7, ptr align 4 %0, i64 %9, i1 false)
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph.preheader, %2
  %10 = tail call noalias dereferenceable_or_null(16) ptr @malloc(i64 noundef 16) #5
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %3) #6
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %4) #6
  %11 = call i32 @gettimeofday(ptr noundef nonnull %3, ptr noundef null) #6
  %12 = add nsw i32 %1, -1
  call void @quickSort(ptr noundef nonnull %6, i32 noundef 0, i32 noundef %12, i32 noundef 0)
  %13 = call i32 @gettimeofday(ptr noundef nonnull %4, ptr noundef null) #6
  %14 = load i64, ptr %4, align 8, !tbaa !5
  %15 = load i64, ptr %3, align 8, !tbaa !5
  %16 = sub nsw i64 %14, %15
  %17 = mul nsw i64 %16, 1000
  %18 = sitofp i64 %17 to double
  %19 = getelementptr inbounds i8, ptr %10, i64 8
  %20 = getelementptr inbounds i8, ptr %4, i64 8
  %21 = load i64, ptr %20, align 8, !tbaa !10
  %22 = sitofp i64 %21 to double
  %23 = getelementptr inbounds i8, ptr %3, i64 8
  %24 = load i64, ptr %23, align 8, !tbaa !10
  %25 = sitofp i64 %24 to double
  %26 = fsub double %22, %25
  %27 = fdiv double %26, 1.000000e+03
  %28 = fadd double %27, %18
  store double %28, ptr %19, align 8, !tbaa !11
  %29 = call i32 @gettimeofday(ptr noundef nonnull %3, ptr noundef null) #6
  call void @insertionSort(ptr noundef nonnull %7, i32 noundef %1)
  %30 = call i32 @gettimeofday(ptr noundef nonnull %4, ptr noundef null) #6
  %31 = load i64, ptr %4, align 8, !tbaa !5
  %32 = load i64, ptr %3, align 8, !tbaa !5
  %33 = sub nsw i64 %31, %32
  %34 = mul nsw i64 %33, 1000
  %35 = sitofp i64 %34 to double
  %36 = load i64, ptr %20, align 8, !tbaa !10
  %37 = sitofp i64 %36 to double
  %38 = load i64, ptr %23, align 8, !tbaa !10
  %39 = sitofp i64 %38 to double
  %40 = fsub double %37, %39
  %41 = fdiv double %40, 1.000000e+03
  %42 = fadd double %41, %35
  store double %42, ptr %10, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %4) #6
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %3) #6
  ret ptr %10
}

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

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

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 = { 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 #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 = { nounwind allocsize(0) }
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, !7, i64 0}
!6 = !{!"timeval", !7, i64 0, !7, i64 8}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!6, !7, i64 8}
!11 = !{!12, !12, i64 0}
!12 = !{!"double", !8, i64 0}


; Effective O3 passes: DSEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LoopIdiomRecognizePass,LoopRotatePass,LoopSimplifyPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass