; LLVM IR from ritikbanger/Hacktoberfest2022-DSA :: 06. Sorting (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"

@.str.1 = external hidden unnamed_addr constant [4 x i8], align 1

; 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: nounwind uwtable
declare dso_local void @mergesort(ptr noundef, ptr noundef, i32 noundef, i32 noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @isSorted(ptr noundef) #1

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

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

8:                                                ; preds = %22, %0
  %9 = load i32, ptr %4, align 4, !tbaa !5
  %10 = icmp slt i32 %9, 15
  br i1 %10, label %12, label %11

11:                                               ; preds = %8
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  br label %25

12:                                               ; preds = %8
  %13 = call i32 @rand() #4
  %14 = srem i32 %13, 100
  %15 = sub nsw i32 %14, 50
  %16 = load i32, ptr %4, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [15 x i32], ptr %2, i64 0, i64 %17
  store i32 %15, ptr %18, align 4, !tbaa !5
  %19 = load i32, ptr %4, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [15 x i32], ptr %3, i64 0, i64 %20
  store i32 %15, ptr %21, align 4, !tbaa !5
  br label %22

22:                                               ; preds = %12
  %23 = load i32, ptr %4, align 4, !tbaa !5
  %24 = add nsw i32 %23, 1
  store i32 %24, ptr %4, align 4, !tbaa !5
  br label %8, !llvm.loop !9

25:                                               ; preds = %11
  %26 = getelementptr inbounds [15 x i32], ptr %2, i64 0, i64 0
  %27 = getelementptr inbounds [15 x i32], ptr %3, i64 0, i64 0
  call void @mergesort(ptr noundef %26, ptr noundef %27, i32 noundef 0, i32 noundef 14)
  %28 = getelementptr inbounds [15 x i32], ptr %2, i64 0, i64 0
  %29 = call i32 @isSorted(ptr noundef %28)
  %30 = icmp ne i32 %29, 0
  br i1 %30, label %31, label %46

31:                                               ; preds = %25
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  store i32 0, ptr %5, align 4, !tbaa !5
  br label %32

32:                                               ; preds = %42, %31
  %33 = load i32, ptr %5, align 4, !tbaa !5
  %34 = icmp slt i32 %33, 15
  br i1 %34, label %36, label %35

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

36:                                               ; preds = %32
  %37 = load i32, ptr %5, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [15 x i32], ptr %2, i64 0, i64 %38
  %40 = load i32, ptr %39, align 4, !tbaa !5
  %41 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %40)
  br label %42

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

45:                                               ; preds = %35
  br label %46

46:                                               ; preds = %45, %25
  call void @llvm.lifetime.end.p0(i64 60, ptr %3) #4
  call void @llvm.lifetime.end.p0(i64 60, ptr %2) #4
  ret i32 0
}

; Function Attrs: nounwind
declare void @srand(i32 noundef) #3

; Function Attrs: nounwind
declare i64 @time(ptr noundef) #3

; Function Attrs: nounwind
declare i32 @rand() #3

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

!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"}
!11 = distinct !{!11, !10}


; 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.1 = external hidden unnamed_addr constant [4 x i8], align 1

; 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: nounwind uwtable
declare dso_local void @mergesort(ptr noundef, ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @isSorted(ptr noundef) local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #1 {
  %1 = alloca [15 x i32], align 16
  %2 = alloca [15 x i32], align 16
  call void @llvm.lifetime.start.p0(i64 60, ptr nonnull %1) #4
  call void @llvm.lifetime.start.p0(i64 60, ptr nonnull %2) #4
  %3 = tail call i64 @time(ptr noundef null) #4
  %4 = trunc i64 %3 to i32
  tail call void @srand(i32 noundef %4) #4
  %5 = tail call i32 @rand() #4
  %6 = srem i32 %5, 100
  %7 = add nsw i32 %6, -50
  store i32 %7, ptr %1, align 16, !tbaa !5
  store i32 %7, ptr %2, align 16, !tbaa !5
  %8 = tail call i32 @rand() #4
  %9 = srem i32 %8, 100
  %10 = add nsw i32 %9, -50
  %11 = getelementptr inbounds i8, ptr %1, i64 4
  store i32 %10, ptr %11, align 4, !tbaa !5
  %12 = getelementptr inbounds i8, ptr %2, i64 4
  store i32 %10, ptr %12, align 4, !tbaa !5
  %13 = tail call i32 @rand() #4
  %14 = srem i32 %13, 100
  %15 = add nsw i32 %14, -50
  %16 = getelementptr inbounds i8, ptr %1, i64 8
  store i32 %15, ptr %16, align 8, !tbaa !5
  %17 = getelementptr inbounds i8, ptr %2, i64 8
  store i32 %15, ptr %17, align 8, !tbaa !5
  %18 = tail call i32 @rand() #4
  %19 = srem i32 %18, 100
  %20 = add nsw i32 %19, -50
  %21 = getelementptr inbounds i8, ptr %1, i64 12
  store i32 %20, ptr %21, align 4, !tbaa !5
  %22 = getelementptr inbounds i8, ptr %2, i64 12
  store i32 %20, ptr %22, align 4, !tbaa !5
  %23 = tail call i32 @rand() #4
  %24 = srem i32 %23, 100
  %25 = add nsw i32 %24, -50
  %26 = getelementptr inbounds i8, ptr %1, i64 16
  store i32 %25, ptr %26, align 16, !tbaa !5
  %27 = getelementptr inbounds i8, ptr %2, i64 16
  store i32 %25, ptr %27, align 16, !tbaa !5
  %28 = tail call i32 @rand() #4
  %29 = srem i32 %28, 100
  %30 = add nsw i32 %29, -50
  %31 = getelementptr inbounds i8, ptr %1, i64 20
  store i32 %30, ptr %31, align 4, !tbaa !5
  %32 = getelementptr inbounds i8, ptr %2, i64 20
  store i32 %30, ptr %32, align 4, !tbaa !5
  %33 = tail call i32 @rand() #4
  %34 = srem i32 %33, 100
  %35 = add nsw i32 %34, -50
  %36 = getelementptr inbounds i8, ptr %1, i64 24
  store i32 %35, ptr %36, align 8, !tbaa !5
  %37 = getelementptr inbounds i8, ptr %2, i64 24
  store i32 %35, ptr %37, align 8, !tbaa !5
  %38 = tail call i32 @rand() #4
  %39 = srem i32 %38, 100
  %40 = add nsw i32 %39, -50
  %41 = getelementptr inbounds i8, ptr %1, i64 28
  store i32 %40, ptr %41, align 4, !tbaa !5
  %42 = getelementptr inbounds i8, ptr %2, i64 28
  store i32 %40, ptr %42, align 4, !tbaa !5
  %43 = tail call i32 @rand() #4
  %44 = srem i32 %43, 100
  %45 = add nsw i32 %44, -50
  %46 = getelementptr inbounds i8, ptr %1, i64 32
  store i32 %45, ptr %46, align 16, !tbaa !5
  %47 = getelementptr inbounds i8, ptr %2, i64 32
  store i32 %45, ptr %47, align 16, !tbaa !5
  %48 = tail call i32 @rand() #4
  %49 = srem i32 %48, 100
  %50 = add nsw i32 %49, -50
  %51 = getelementptr inbounds i8, ptr %1, i64 36
  store i32 %50, ptr %51, align 4, !tbaa !5
  %52 = getelementptr inbounds i8, ptr %2, i64 36
  store i32 %50, ptr %52, align 4, !tbaa !5
  %53 = tail call i32 @rand() #4
  %54 = srem i32 %53, 100
  %55 = add nsw i32 %54, -50
  %56 = getelementptr inbounds i8, ptr %1, i64 40
  store i32 %55, ptr %56, align 8, !tbaa !5
  %57 = getelementptr inbounds i8, ptr %2, i64 40
  store i32 %55, ptr %57, align 8, !tbaa !5
  %58 = tail call i32 @rand() #4
  %59 = srem i32 %58, 100
  %60 = add nsw i32 %59, -50
  %61 = getelementptr inbounds i8, ptr %1, i64 44
  store i32 %60, ptr %61, align 4, !tbaa !5
  %62 = getelementptr inbounds i8, ptr %2, i64 44
  store i32 %60, ptr %62, align 4, !tbaa !5
  %63 = tail call i32 @rand() #4
  %64 = srem i32 %63, 100
  %65 = add nsw i32 %64, -50
  %66 = getelementptr inbounds i8, ptr %1, i64 48
  store i32 %65, ptr %66, align 16, !tbaa !5
  %67 = getelementptr inbounds i8, ptr %2, i64 48
  store i32 %65, ptr %67, align 16, !tbaa !5
  %68 = tail call i32 @rand() #4
  %69 = srem i32 %68, 100
  %70 = add nsw i32 %69, -50
  %71 = getelementptr inbounds i8, ptr %1, i64 52
  store i32 %70, ptr %71, align 4, !tbaa !5
  %72 = getelementptr inbounds i8, ptr %2, i64 52
  store i32 %70, ptr %72, align 4, !tbaa !5
  %73 = tail call i32 @rand() #4
  %74 = srem i32 %73, 100
  %75 = add nsw i32 %74, -50
  %76 = getelementptr inbounds i8, ptr %1, i64 56
  store i32 %75, ptr %76, align 8, !tbaa !5
  %77 = getelementptr inbounds i8, ptr %2, i64 56
  store i32 %75, ptr %77, align 8, !tbaa !5
  call void @mergesort(ptr noundef nonnull %1, ptr noundef nonnull %2, i32 noundef 0, i32 noundef 14)
  %78 = call i32 @isSorted(ptr noundef nonnull %1)
  %.not = icmp eq i32 %78, 0
  br i1 %.not, label %.loopexit, label %.preheader.preheader

.preheader.preheader:                             ; preds = %0
  %79 = load i32, ptr %1, align 16, !tbaa !5
  %80 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %79)
  %81 = load i32, ptr %11, align 4, !tbaa !5
  %82 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %81)
  %83 = load i32, ptr %16, align 8, !tbaa !5
  %84 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %83)
  %85 = load i32, ptr %21, align 4, !tbaa !5
  %86 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %85)
  %87 = load i32, ptr %26, align 16, !tbaa !5
  %88 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %87)
  %89 = load i32, ptr %31, align 4, !tbaa !5
  %90 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %89)
  %91 = load i32, ptr %36, align 8, !tbaa !5
  %92 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %91)
  %93 = load i32, ptr %41, align 4, !tbaa !5
  %94 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %93)
  %95 = load i32, ptr %46, align 16, !tbaa !5
  %96 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %95)
  %97 = load i32, ptr %51, align 4, !tbaa !5
  %98 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %97)
  %99 = load i32, ptr %56, align 8, !tbaa !5
  %100 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %99)
  %101 = load i32, ptr %61, align 4, !tbaa !5
  %102 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %101)
  %103 = load i32, ptr %66, align 16, !tbaa !5
  %104 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %103)
  %105 = load i32, ptr %71, align 4, !tbaa !5
  %106 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %105)
  %107 = load i32, ptr %76, align 8, !tbaa !5
  %108 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %107)
  br label %.loopexit

.loopexit:                                        ; preds = %.preheader.preheader, %0
  call void @llvm.lifetime.end.p0(i64 60, ptr nonnull %2) #4
  call void @llvm.lifetime.end.p0(i64 60, ptr nonnull %1) #4
  ret i32 0
}

; Function Attrs: nounwind
declare void @srand(i32 noundef) local_unnamed_addr #3

; Function Attrs: nounwind
declare i64 @time(ptr noundef) local_unnamed_addr #3

; Function Attrs: nounwind
declare i32 @rand() local_unnamed_addr #3

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

!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"}


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