; LLVM IR from ahampriyanshu/algo-ds-101 :: Algorithms/Sorting/Merge_Sort (non_recursive_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.Stack = type { i32, [10000 x %struct.Element] }
%struct.Element = type { i32, i32, i32 }

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @Push(ptr noundef, i64, i32) #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 { i64, i32 } @Pop(ptr noundef) #0

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

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

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

; Function Attrs: nounwind uwtable
define dso_local void @merge_sort(ptr noundef %0, ptr noundef %1, i32 noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca %struct.Stack, align 4
  %8 = alloca %struct.Element, align 4
  %9 = alloca { i64, i32 }, align 4
  %10 = alloca %struct.Element, align 4
  %11 = alloca { i64, i32 }, align 8
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca %struct.Element, align 4
  %15 = alloca %struct.Element, align 4
  %16 = alloca { i64, i32 }, align 4
  %17 = alloca { i64, i32 }, align 4
  %18 = alloca { i64, i32 }, align 4
  %19 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i32 %2, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 120004, ptr %7) #3
  %20 = getelementptr inbounds %struct.Stack, ptr %7, i32 0, i32 0
  store i32 -1, ptr %20, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 12, ptr %8) #3
  %21 = getelementptr inbounds %struct.Element, ptr %8, i32 0, i32 0
  store i32 0, ptr %21, align 4, !tbaa !13
  %22 = getelementptr inbounds %struct.Element, ptr %8, i32 0, i32 1
  %23 = load i32, ptr %6, align 4, !tbaa !9
  %24 = sub nsw i32 %23, 1
  store i32 %24, ptr %22, align 4, !tbaa !15
  %25 = getelementptr inbounds %struct.Element, ptr %8, i32 0, i32 2
  store i32 0, ptr %25, align 4, !tbaa !16
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %9, ptr align 4 %8, i64 12, i1 false)
  %26 = getelementptr inbounds { i64, i32 }, ptr %9, i32 0, i32 0
  %27 = load i64, ptr %26, align 4
  %28 = getelementptr inbounds { i64, i32 }, ptr %9, i32 0, i32 1
  %29 = load i32, ptr %28, align 4
  %30 = call i32 @Push(ptr noundef %7, i64 %27, i32 %29)
  br label %31

31:                                               ; preds = %104, %102, %3
  %32 = call i32 @IsEmpty(ptr noundef %7)
  %33 = icmp ne i32 %32, 0
  %34 = xor i1 %33, true
  br i1 %34, label %35, label %105

35:                                               ; preds = %31
  call void @llvm.lifetime.start.p0(i64 12, ptr %10) #3
  %36 = call { i64, i32 } @Pop(ptr noundef %7)
  store { i64, i32 } %36, ptr %11, align 8
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %10, ptr align 8 %11, i64 12, i1 false)
  %37 = getelementptr inbounds %struct.Element, ptr %10, i32 0, i32 2
  %38 = load i32, ptr %37, align 4, !tbaa !16
  %39 = icmp eq i32 %38, 0
  br i1 %39, label %40, label %86

40:                                               ; preds = %35
  %41 = getelementptr inbounds %struct.Element, ptr %10, i32 0, i32 2
  store i32 1, ptr %41, align 4, !tbaa !16
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  %42 = getelementptr inbounds %struct.Element, ptr %10, i32 0, i32 0
  %43 = load i32, ptr %42, align 4, !tbaa !13
  %44 = getelementptr inbounds %struct.Element, ptr %10, i32 0, i32 1
  %45 = load i32, ptr %44, align 4, !tbaa !15
  %46 = add nsw i32 %43, %45
  %47 = sdiv i32 %46, 2
  store i32 %47, ptr %12, align 4, !tbaa !9
  %48 = getelementptr inbounds %struct.Element, ptr %10, i32 0, i32 1
  %49 = load i32, ptr %48, align 4, !tbaa !15
  %50 = getelementptr inbounds %struct.Element, ptr %10, i32 0, i32 0
  %51 = load i32, ptr %50, align 4, !tbaa !13
  %52 = icmp sle i32 %49, %51
  br i1 %52, label %53, label %54

53:                                               ; preds = %40
  store i32 2, ptr %13, align 4
  br label %83, !llvm.loop !17

54:                                               ; preds = %40
  call void @llvm.lifetime.start.p0(i64 12, ptr %14) #3
  %55 = getelementptr inbounds %struct.Element, ptr %14, i32 0, i32 0
  %56 = getelementptr inbounds %struct.Element, ptr %10, i32 0, i32 0
  %57 = load i32, ptr %56, align 4, !tbaa !13
  store i32 %57, ptr %55, align 4, !tbaa !13
  %58 = getelementptr inbounds %struct.Element, ptr %14, i32 0, i32 1
  %59 = load i32, ptr %12, align 4, !tbaa !9
  store i32 %59, ptr %58, align 4, !tbaa !15
  %60 = getelementptr inbounds %struct.Element, ptr %14, i32 0, i32 2
  store i32 0, ptr %60, align 4, !tbaa !16
  call void @llvm.lifetime.start.p0(i64 12, ptr %15) #3
  %61 = getelementptr inbounds %struct.Element, ptr %15, i32 0, i32 0
  %62 = load i32, ptr %12, align 4, !tbaa !9
  %63 = add nsw i32 %62, 1
  store i32 %63, ptr %61, align 4, !tbaa !13
  %64 = getelementptr inbounds %struct.Element, ptr %15, i32 0, i32 1
  %65 = getelementptr inbounds %struct.Element, ptr %10, i32 0, i32 1
  %66 = load i32, ptr %65, align 4, !tbaa !15
  store i32 %66, ptr %64, align 4, !tbaa !15
  %67 = getelementptr inbounds %struct.Element, ptr %15, i32 0, i32 2
  store i32 0, ptr %67, align 4, !tbaa !16
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %16, ptr align 4 %10, i64 12, i1 false)
  %68 = getelementptr inbounds { i64, i32 }, ptr %16, i32 0, i32 0
  %69 = load i64, ptr %68, align 4
  %70 = getelementptr inbounds { i64, i32 }, ptr %16, i32 0, i32 1
  %71 = load i32, ptr %70, align 4
  %72 = call i32 @Push(ptr noundef %7, i64 %69, i32 %71)
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %17, ptr align 4 %15, i64 12, i1 false)
  %73 = getelementptr inbounds { i64, i32 }, ptr %17, i32 0, i32 0
  %74 = load i64, ptr %73, align 4
  %75 = getelementptr inbounds { i64, i32 }, ptr %17, i32 0, i32 1
  %76 = load i32, ptr %75, align 4
  %77 = call i32 @Push(ptr noundef %7, i64 %74, i32 %76)
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %18, ptr align 4 %14, i64 12, i1 false)
  %78 = getelementptr inbounds { i64, i32 }, ptr %18, i32 0, i32 0
  %79 = load i64, ptr %78, align 4
  %80 = getelementptr inbounds { i64, i32 }, ptr %18, i32 0, i32 1
  %81 = load i32, ptr %80, align 4
  %82 = call i32 @Push(ptr noundef %7, i64 %79, i32 %81)
  call void @llvm.lifetime.end.p0(i64 12, ptr %15) #3
  call void @llvm.lifetime.end.p0(i64 12, ptr %14) #3
  store i32 0, ptr %13, align 4
  br label %83

83:                                               ; preds = %54, %53
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  %84 = load i32, ptr %13, align 4
  switch i32 %84, label %102 [
    i32 0, label %85
  ]

85:                                               ; preds = %83
  br label %101

86:                                               ; preds = %35
  call void @llvm.lifetime.start.p0(i64 4, ptr %19) #3
  %87 = getelementptr inbounds %struct.Element, ptr %10, i32 0, i32 0
  %88 = load i32, ptr %87, align 4, !tbaa !13
  %89 = getelementptr inbounds %struct.Element, ptr %10, i32 0, i32 1
  %90 = load i32, ptr %89, align 4, !tbaa !15
  %91 = add nsw i32 %88, %90
  %92 = sdiv i32 %91, 2
  store i32 %92, ptr %19, align 4, !tbaa !9
  %93 = load ptr, ptr %4, align 8, !tbaa !5
  %94 = load ptr, ptr %5, align 8, !tbaa !5
  %95 = load i32, ptr %6, align 4, !tbaa !9
  %96 = getelementptr inbounds %struct.Element, ptr %10, i32 0, i32 0
  %97 = load i32, ptr %96, align 4, !tbaa !13
  %98 = load i32, ptr %19, align 4, !tbaa !9
  %99 = getelementptr inbounds %struct.Element, ptr %10, i32 0, i32 1
  %100 = load i32, ptr %99, align 4, !tbaa !15
  call void @merge(ptr noundef %93, ptr noundef %94, i32 noundef %95, i32 noundef %97, i32 noundef %98, i32 noundef %100)
  call void @llvm.lifetime.end.p0(i64 4, ptr %19) #3
  br label %101

101:                                              ; preds = %86, %85
  store i32 0, ptr %13, align 4
  br label %102

102:                                              ; preds = %101, %83
  call void @llvm.lifetime.end.p0(i64 12, ptr %10) #3
  %103 = load i32, ptr %13, align 4
  switch i32 %103, label %106 [
    i32 0, label %104
    i32 2, label %31
  ]

104:                                              ; preds = %102
  br label %31, !llvm.loop !17

105:                                              ; preds = %31
  call void @llvm.lifetime.end.p0(i64 12, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 120004, ptr %7) #3
  ret void

106:                                              ; preds = %102
  unreachable
}

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 nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"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 = !{!12, !10, i64 0}
!12 = !{!"Stack", !10, i64 0, !7, i64 4}
!13 = !{!14, !10, i64 0}
!14 = !{!"Element", !10, i64 0, !10, i64 4, !10, i64 8}
!15 = !{!14, !10, i64 4}
!16 = !{!14, !10, 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.Stack = type { i32, [10000 x %struct.Element] }
%struct.Element = type { i32, i32, i32 }

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @Push(ptr noundef, i64, i32) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local { i64, i32 } @Pop(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @merge(ptr noundef, ptr noundef, i32 noundef, i32 noundef, i32 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 void @merge_sort(ptr noundef %0, ptr noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = alloca %struct.Stack, align 4
  call void @llvm.lifetime.start.p0(i64 120004, ptr nonnull %4) #2
  store i32 -1, ptr %4, align 4, !tbaa !5
  %5 = add nsw i32 %2, -1
  %.sroa.038.4.insert.ext = zext i32 %5 to i64
  %.sroa.038.4.insert.shift = shl nuw i64 %.sroa.038.4.insert.ext, 32
  %6 = call i32 @Push(ptr noundef nonnull %4, i64 %.sroa.038.4.insert.shift, i32 0)
  %7 = call i32 @IsEmpty(ptr noundef nonnull %4)
  %.not44 = icmp eq i32 %7, 0
  br i1 %.not44, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %3, %21
  %8 = call { i64, i32 } @Pop(ptr noundef nonnull %4)
  %.fca.0.extract = extractvalue { i64, i32 } %8, 0
  %.fca.1.extract = extractvalue { i64, i32 } %8, 1
  %9 = icmp eq i32 %.fca.1.extract, 0
  %.sroa.013.0.extract.trunc = trunc i64 %.fca.0.extract to i32
  %.sroa.013.4.extract.shift = lshr i64 %.fca.0.extract, 32
  %.sroa.013.4.extract.trunc = trunc nuw i64 %.sroa.013.4.extract.shift to i32
  br i1 %9, label %10, label %18

10:                                               ; preds = %.lr.ph
  %.not43 = icmp sgt i32 %.sroa.013.4.extract.trunc, %.sroa.013.0.extract.trunc
  br i1 %.not43, label %11, label %21, !llvm.loop !10

11:                                               ; preds = %10
  %12 = add nsw i32 %.sroa.013.4.extract.trunc, %.sroa.013.0.extract.trunc
  %13 = sdiv i32 %12, 2
  %.sroa.06.0.insert.ext = and i64 %.fca.0.extract, 4294967295
  %.sroa.06.4.insert.ext = zext i32 %13 to i64
  %.sroa.06.4.insert.shift = shl nuw i64 %.sroa.06.4.insert.ext, 32
  %.sroa.06.4.insert.insert = or disjoint i64 %.sroa.06.4.insert.shift, %.sroa.06.0.insert.ext
  %14 = add nsw i32 %13, 1
  %.sroa.05.0.insert.ext = zext i32 %14 to i64
  %.sroa.05.4.insert.shift = and i64 %.fca.0.extract, -4294967296
  %.sroa.05.4.insert.insert = or disjoint i64 %.sroa.05.4.insert.shift, %.sroa.05.0.insert.ext
  %15 = call i32 @Push(ptr noundef nonnull %4, i64 %.fca.0.extract, i32 1)
  %16 = call i32 @Push(ptr noundef nonnull %4, i64 %.sroa.05.4.insert.insert, i32 0)
  %17 = call i32 @Push(ptr noundef nonnull %4, i64 %.sroa.06.4.insert.insert, i32 0)
  br label %21

18:                                               ; preds = %.lr.ph
  %19 = add nsw i32 %.sroa.013.4.extract.trunc, %.sroa.013.0.extract.trunc
  %20 = sdiv i32 %19, 2
  call void @merge(ptr noundef %0, ptr noundef %1, i32 noundef %2, i32 noundef %.sroa.013.0.extract.trunc, i32 noundef %20, i32 noundef %.sroa.013.4.extract.trunc)
  br label %21

21:                                               ; preds = %11, %10, %18
  %22 = call i32 @IsEmpty(ptr noundef nonnull %4)
  %.not = icmp eq i32 %22, 0
  br i1 %.not, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %21, %3
  call void @llvm.lifetime.end.p0(i64 120004, ptr nonnull %4) #2
  ret void
}

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 = { 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 = !{!"Stack", !7, i64 0, !8, i64 4}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass