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

; 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.student = type { [20 x i8], float }

@__const.main.num = external hidden unnamed_addr constant [2 x i32], align 4
@__const.main.file = external hidden unnamed_addr constant [2 x [12 x i8]], align 16
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [30 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [44 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [42 x i8], align 1
@endmem = external dso_local global ptr, align 8

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

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

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

; 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) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca [2 x i32], align 4
  %3 = alloca [2 x [12 x i8]], align 16
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca %struct.timeval, align 8
  %9 = alloca %struct.timeval, align 8
  %10 = alloca i32, align 4
  %11 = alloca double, align 8
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #6
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %2, ptr align 4 @__const.main.num, i64 8, i1 false)
  call void @llvm.lifetime.start.p0(i64 24, ptr %3) #6
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %3, ptr align 16 @__const.main.file, i64 24, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #6
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %12

12:                                               ; preds = %79, %0
  %13 = load i32, ptr %4, align 4, !tbaa !5
  %14 = icmp slt i32 %13, 2
  br i1 %14, label %16, label %15

15:                                               ; preds = %12
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #6
  br label %82

16:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #6
  %17 = load i32, ptr %4, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [2 x [12 x i8]], ptr %3, i64 0, i64 %18
  %20 = getelementptr inbounds [12 x i8], ptr %19, i64 0, i64 0
  %21 = call noalias ptr @fopen(ptr noundef %20, ptr noundef @.str.1)
  store ptr %21, ptr %5, align 8, !tbaa !9
  %22 = load ptr, ptr %5, align 8, !tbaa !9
  %23 = icmp eq ptr %22, null
  br i1 %23, label %24, label %26

24:                                               ; preds = %16
  %25 = call i32 (ptr, ...) @printf(ptr noundef @.str.2)
  br label %78

26:                                               ; preds = %16
  %27 = load i32, ptr %4, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [2 x i32], ptr %2, i64 0, i64 %28
  %30 = load i32, ptr %29, align 4, !tbaa !5
  %31 = zext i32 %30 to i64
  %32 = call ptr @llvm.stacksave.p0()
  store ptr %32, ptr %6, align 8
  %33 = alloca %struct.student, i64 %31, align 16
  store i64 %31, ptr %7, align 8
  %34 = load ptr, ptr %5, align 8, !tbaa !9
  %35 = load i32, ptr %4, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [2 x i32], ptr %2, i64 0, i64 %36
  %38 = load i32, ptr %37, align 4, !tbaa !5
  call void @readData(ptr noundef %34, ptr noundef %33, i32 noundef %38)
  call void @llvm.lifetime.start.p0(i64 16, ptr %8) #6
  call void @llvm.lifetime.start.p0(i64 16, ptr %9) #6
  %39 = call i32 @gettimeofday(ptr noundef %8, ptr noundef null) #6
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #6
  %40 = load i32, ptr %4, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [2 x i32], ptr %2, i64 0, i64 %41
  %43 = load i32, ptr %42, align 4, !tbaa !5
  call void @mergeSort(ptr noundef %33, i32 noundef %43)
  %44 = call i32 @gettimeofday(ptr noundef %9, ptr noundef null) #6
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #6
  %45 = getelementptr inbounds %struct.timeval, ptr %9, i32 0, i32 0
  %46 = load i64, ptr %45, align 8, !tbaa !11
  %47 = getelementptr inbounds %struct.timeval, ptr %8, i32 0, i32 0
  %48 = load i64, ptr %47, align 8, !tbaa !11
  %49 = sub nsw i64 %46, %48
  %50 = mul nsw i64 %49, 1000
  %51 = sitofp i64 %50 to double
  store double %51, ptr %11, align 8, !tbaa !14
  %52 = getelementptr inbounds %struct.timeval, ptr %9, i32 0, i32 1
  %53 = load i64, ptr %52, align 8, !tbaa !16
  %54 = sitofp i64 %53 to double
  %55 = getelementptr inbounds %struct.timeval, ptr %8, i32 0, i32 1
  %56 = load i64, ptr %55, align 8, !tbaa !16
  %57 = sitofp i64 %56 to double
  %58 = fsub double %54, %57
  %59 = fdiv double %58, 1.000000e+03
  %60 = load double, ptr %11, align 8, !tbaa !14
  %61 = fadd double %60, %59
  store double %61, ptr %11, align 8, !tbaa !14
  %62 = load i32, ptr %4, align 4, !tbaa !5
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds [2 x i32], ptr %2, i64 0, i64 %63
  %65 = load i32, ptr %64, align 4, !tbaa !5
  %66 = load double, ptr %11, align 8, !tbaa !14
  %67 = call i32 (ptr, ...) @printf(ptr noundef @.str.3, i32 noundef %65, double noundef %66)
  %68 = load i32, ptr %4, align 4, !tbaa !5
  %69 = sext i32 %68 to i64
  %70 = getelementptr inbounds [2 x i32], ptr %2, i64 0, i64 %69
  %71 = load i32, ptr %70, align 4, !tbaa !5
  %72 = ptrtoint ptr %10 to i64
  %73 = load ptr, ptr @endmem, align 8, !tbaa !9
  %74 = ptrtoint ptr %73 to i64
  %75 = sub nsw i64 %72, %74
  %76 = call i32 (ptr, ...) @printf(ptr noundef @.str.4, i32 noundef %71, i64 noundef %75)
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #6
  call void @llvm.lifetime.end.p0(i64 16, ptr %9) #6
  call void @llvm.lifetime.end.p0(i64 16, ptr %8) #6
  %77 = load ptr, ptr %6, align 8
  call void @llvm.stackrestore.p0(ptr %77)
  br label %78

78:                                               ; preds = %26, %24
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #6
  br label %79

79:                                               ; preds = %78
  %80 = load i32, ptr %4, align 4, !tbaa !5
  %81 = add nsw i32 %80, 1
  store i32 %81, ptr %4, align 4, !tbaa !5
  br label %12, !llvm.loop !17

82:                                               ; preds = %15
  call void @llvm.lifetime.end.p0(i64 24, ptr %3) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #6
  ret i32 0
}

declare noalias ptr @fopen(ptr noundef, ptr noundef) #3

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

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

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

declare void @mergeSort(ptr noundef, i32 noundef) #3

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

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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { "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 = { nocallback nofree nosync nounwind willreturn }
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 }

!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 = !{!12, !13, i64 0}
!12 = !{!"timeval", !13, i64 0, !13, i64 8}
!13 = !{!"long", !7, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"double", !7, i64 0}
!16 = !{!12, !13, i64 8}
!17 = distinct !{!17, !18}
!18 = !{!"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"

%struct.timeval = type { i64, i64 }
%struct.student = type { [20 x i8], float }

@__const.main.num = external hidden unnamed_addr constant [2 x i32], align 4
@__const.main.file = external hidden unnamed_addr constant [2 x [12 x i8]], align 16
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [30 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [44 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [42 x i8], align 1
@endmem = external dso_local local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
declare dso_local void @readData(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #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: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = alloca %struct.timeval, align 8
  %2 = alloca %struct.timeval, align 8
  %3 = alloca i32, align 4
  %4 = getelementptr inbounds i8, ptr %2, i64 8
  %5 = getelementptr inbounds i8, ptr %1, i64 8
  %6 = ptrtoint ptr %3 to i64
  %7 = call noalias ptr @fopen(ptr noundef nonnull @__const.main.file, ptr noundef nonnull @.str.1)
  %8 = icmp eq ptr %7, null
  br i1 %8, label %9, label %11

9:                                                ; preds = %0
  %10 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2)
  br label %35

11:                                               ; preds = %0
  %12 = load i32, ptr @__const.main.num, align 4, !tbaa !5
  %13 = zext i32 %12 to i64
  %14 = call ptr @llvm.stacksave.p0()
  %15 = alloca %struct.student, i64 %13, align 16
  call void @readData(ptr noundef nonnull %7, ptr noundef nonnull %15, i32 noundef %12)
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %1) #5
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %2) #5
  %16 = call i32 @gettimeofday(ptr noundef nonnull %1, ptr noundef null) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %3) #5
  call void @mergeSort(ptr noundef nonnull %15, i32 noundef %12) #5
  %17 = call i32 @gettimeofday(ptr noundef nonnull %2, ptr noundef null) #5
  %18 = load i64, ptr %2, align 8, !tbaa !9
  %19 = load i64, ptr %1, align 8, !tbaa !9
  %20 = sub nsw i64 %18, %19
  %21 = mul nsw i64 %20, 1000
  %22 = sitofp i64 %21 to double
  %23 = load i64, ptr %4, align 8, !tbaa !12
  %24 = sitofp i64 %23 to double
  %25 = load i64, ptr %5, align 8, !tbaa !12
  %26 = sitofp i64 %25 to double
  %27 = fsub double %24, %26
  %28 = fdiv double %27, 1.000000e+03
  %29 = fadd double %28, %22
  %30 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3, i32 noundef %12, double noundef %29)
  %31 = load ptr, ptr @endmem, align 8, !tbaa !13
  %32 = ptrtoint ptr %31 to i64
  %33 = sub nsw i64 %6, %32
  %34 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.4, i32 noundef %12, i64 noundef %33)
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %3) #5
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %2) #5
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %1) #5
  call void @llvm.stackrestore.p0(ptr %14)
  br label %35

35:                                               ; preds = %11, %9
  %36 = call noalias ptr @fopen(ptr noundef nonnull getelementptr inbounds (i8, ptr @__const.main.file, i64 12), ptr noundef nonnull @.str.1)
  %37 = icmp eq ptr %36, null
  br i1 %37, label %62, label %38

38:                                               ; preds = %35
  %39 = load i32, ptr getelementptr inbounds (i8, ptr @__const.main.num, i64 4), align 4, !tbaa !5
  %40 = zext i32 %39 to i64
  %41 = call ptr @llvm.stacksave.p0()
  %42 = alloca %struct.student, i64 %40, align 16
  call void @readData(ptr noundef nonnull %36, ptr noundef nonnull %42, i32 noundef %39)
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %1) #5
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %2) #5
  %43 = call i32 @gettimeofday(ptr noundef nonnull %1, ptr noundef null) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %3) #5
  call void @mergeSort(ptr noundef nonnull %42, i32 noundef %39) #5
  %44 = call i32 @gettimeofday(ptr noundef nonnull %2, ptr noundef null) #5
  %45 = load i64, ptr %2, align 8, !tbaa !9
  %46 = load i64, ptr %1, align 8, !tbaa !9
  %47 = sub nsw i64 %45, %46
  %48 = mul nsw i64 %47, 1000
  %49 = sitofp i64 %48 to double
  %50 = load i64, ptr %4, align 8, !tbaa !12
  %51 = sitofp i64 %50 to double
  %52 = load i64, ptr %5, align 8, !tbaa !12
  %53 = sitofp i64 %52 to double
  %54 = fsub double %51, %53
  %55 = fdiv double %54, 1.000000e+03
  %56 = fadd double %55, %49
  %57 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3, i32 noundef %39, double noundef %56)
  %58 = load ptr, ptr @endmem, align 8, !tbaa !13
  %59 = ptrtoint ptr %58 to i64
  %60 = sub nsw i64 %6, %59
  %61 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.4, i32 noundef %39, i64 noundef %60)
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %3) #5
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %2) #5
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %1) #5
  call void @llvm.stackrestore.p0(ptr %41)
  br label %64

62:                                               ; preds = %35
  %63 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2)
  br label %64

64:                                               ; preds = %62, %38
  ret i32 0
}

; Function Attrs: nofree nounwind
declare noalias noundef ptr @fopen(ptr nocapture noundef readonly, ptr nocapture noundef readonly) local_unnamed_addr #2

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

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

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

declare void @mergeSort(ptr noundef, i32 noundef) local_unnamed_addr #4

; Function Attrs: mustprogress 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 = { 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 = { mustprogress nocallback nofree nosync nounwind willreturn }
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, !11, i64 0}
!10 = !{!"timeval", !11, i64 0, !11, i64 8}
!11 = !{!"long", !7, i64 0}
!12 = !{!10, !11, i64 8}
!13 = !{!14, !14, i64 0}
!14 = !{!"any pointer", !7, i64 0}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,LoopRotatePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass