; LLVM IR from Dev-XYS/Algorithms :: Graham-Scan.ll (Z5qsortii)

; 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.point = type { i32, i32 }
%struct.segment = type { %struct.point, %struct.point }

@P = external dso_local global [100000 x %struct.point], align 16
@D = external dso_local global [100000 x %struct.segment], align 16

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z9directionR7segmentS0_(ptr noundef nonnull align 4 dereferenceable(16), ptr noundef nonnull align 4 dereferenceable(16)) #0

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5qsortii(i32 noundef %0, i32 noundef %1) #1 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca %struct.segment, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca %struct.segment, align 4
  %9 = alloca %struct.point, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !5
  %10 = load i32, ptr %3, align 4, !tbaa !5
  %11 = load i32, ptr %4, align 4, !tbaa !5
  %12 = icmp slt i32 %10, %11
  br i1 %12, label %13, label %69

13:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 16, ptr %5) #4
  %14 = load i32, ptr %4, align 4, !tbaa !5
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds [100000 x %struct.segment], ptr @D, i64 0, i64 %15
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %5, ptr align 16 %16, i64 16, i1 false), !tbaa.struct !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  %17 = load i32, ptr %3, align 4, !tbaa !5
  %18 = sub nsw i32 %17, 1
  store i32 %18, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  %19 = load i32, ptr %3, align 4, !tbaa !5
  store i32 %19, ptr %7, align 4, !tbaa !5
  br label %20

20:                                               ; preds = %59, %13
  %21 = load i32, ptr %7, align 4, !tbaa !5
  %22 = load i32, ptr %4, align 4, !tbaa !5
  %23 = icmp sle i32 %21, %22
  br i1 %23, label %25, label %24

24:                                               ; preds = %20
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  br label %62

25:                                               ; preds = %20
  %26 = load i32, ptr %7, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [100000 x %struct.segment], ptr @D, i64 0, i64 %27
  %29 = call noundef i32 @_Z9directionR7segmentS0_(ptr noundef nonnull align 4 dereferenceable(16) %5, ptr noundef nonnull align 4 dereferenceable(16) %28)
  %30 = icmp sge i32 %29, 0
  br i1 %30, label %31, label %58

31:                                               ; preds = %25
  %32 = load i32, ptr %6, align 4, !tbaa !5
  %33 = add nsw i32 %32, 1
  store i32 %33, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 16, ptr %8) #4
  %34 = load i32, ptr %7, align 4, !tbaa !5
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [100000 x %struct.segment], ptr @D, i64 0, i64 %35
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %8, ptr align 16 %36, i64 16, i1 false), !tbaa.struct !9
  %37 = load i32, ptr %6, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [100000 x %struct.segment], ptr @D, i64 0, i64 %38
  %40 = load i32, ptr %7, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [100000 x %struct.segment], ptr @D, i64 0, i64 %41
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %42, ptr align 16 %39, i64 16, i1 false), !tbaa.struct !9
  %43 = load i32, ptr %6, align 4, !tbaa !5
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds [100000 x %struct.segment], ptr @D, i64 0, i64 %44
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %45, ptr align 4 %8, i64 16, i1 false), !tbaa.struct !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %46 = load i32, ptr %7, align 4, !tbaa !5
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds [100000 x %struct.point], ptr @P, i64 0, i64 %47
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %9, ptr align 8 %48, i64 8, i1 false), !tbaa.struct !10
  %49 = load i32, ptr %6, align 4, !tbaa !5
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds [100000 x %struct.point], ptr @P, i64 0, i64 %50
  %52 = load i32, ptr %7, align 4, !tbaa !5
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds [100000 x %struct.point], ptr @P, i64 0, i64 %53
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %54, ptr align 8 %51, i64 8, i1 false), !tbaa.struct !10
  %55 = load i32, ptr %6, align 4, !tbaa !5
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [100000 x %struct.point], ptr @P, i64 0, i64 %56
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %57, ptr align 4 %9, i64 8, i1 false), !tbaa.struct !10
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 16, ptr %8) #4
  br label %58

58:                                               ; preds = %31, %25
  br label %59

59:                                               ; preds = %58
  %60 = load i32, ptr %7, align 4, !tbaa !5
  %61 = add nsw i32 %60, 1
  store i32 %61, ptr %7, align 4, !tbaa !5
  br label %20, !llvm.loop !11

62:                                               ; preds = %24
  %63 = load i32, ptr %3, align 4, !tbaa !5
  %64 = load i32, ptr %6, align 4, !tbaa !5
  %65 = sub nsw i32 %64, 1
  call void @_Z5qsortii(i32 noundef %63, i32 noundef %65)
  %66 = load i32, ptr %6, align 4, !tbaa !5
  %67 = add nsw i32 %66, 1
  %68 = load i32, ptr %4, align 4, !tbaa !5
  call void @_Z5qsortii(i32 noundef %67, i32 noundef %68)
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 16, ptr %5) #4
  br label %69

69:                                               ; preds = %62, %2
  ret void
}

; 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 nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

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

attributes #0 = { mustprogress 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 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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++ TBAA"}
!9 = !{i64 0, i64 4, !5, i64 4, i64 4, !5, i64 8, i64 4, !5, i64 12, i64 4, !5}
!10 = !{i64 0, i64 4, !5, i64 4, i64 4, !5}
!11 = distinct !{!11, !12}
!12 = !{!"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.point = type { i32, i32 }
%struct.segment = type { %struct.point, %struct.point }

@P = external dso_local local_unnamed_addr global [100000 x %struct.point], align 16
@D = external dso_local global [100000 x %struct.segment], align 16

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z9directionR7segmentS0_(ptr noundef nonnull align 4 dereferenceable(16), ptr noundef nonnull align 4 dereferenceable(16)) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z5qsortii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = alloca %struct.segment, align 4
  %4 = alloca %struct.segment, align 4
  %5 = icmp slt i32 %0, %1
  br i1 %5, label %6, label %28

6:                                                ; preds = %2
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %3) #3
  %7 = sext i32 %1 to i64
  %8 = getelementptr inbounds [100000 x %struct.segment], ptr @D, i64 0, i64 %7
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(16) %3, ptr noundef nonnull align 16 dereferenceable(16) %8, i64 16, i1 false), !tbaa.struct !5
  %9 = add nsw i32 %0, -1
  %10 = sext i32 %0 to i64
  %11 = add i32 %1, 1
  br label %15

12:                                               ; preds = %27
  %13 = add nsw i32 %.1, -1
  call void @_Z5qsortii(i32 noundef %0, i32 noundef %13)
  %14 = add nsw i32 %.1, 1
  call void @_Z5qsortii(i32 noundef %14, i32 noundef %1)
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %3) #3
  br label %28

15:                                               ; preds = %6, %27
  %indvars.iv = phi i64 [ %10, %6 ], [ %indvars.iv.next, %27 ]
  %.02225 = phi i32 [ %9, %6 ], [ %.1, %27 ]
  %16 = getelementptr inbounds [100000 x %struct.segment], ptr @D, i64 0, i64 %indvars.iv
  %17 = call noundef i32 @_Z9directionR7segmentS0_(ptr noundef nonnull align 4 dereferenceable(16) %3, ptr noundef nonnull align 4 dereferenceable(16) %16)
  %18 = icmp sgt i32 %17, -1
  br i1 %18, label %19, label %27

19:                                               ; preds = %15
  %20 = add nsw i32 %.02225, 1
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %4)
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(16) %4, ptr noundef nonnull align 16 dereferenceable(16) %16, i64 16, i1 false), !tbaa.struct !5
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [100000 x %struct.segment], ptr @D, i64 0, i64 %21
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(16) %16, ptr noundef nonnull align 16 dereferenceable(16) %22, i64 16, i1 false), !tbaa.struct !5
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(16) %22, ptr noundef nonnull align 4 dereferenceable(16) %4, i64 16, i1 false), !tbaa.struct !5
  %23 = getelementptr inbounds [100000 x %struct.point], ptr @P, i64 0, i64 %indvars.iv
  %24 = load i64, ptr %23, align 8
  %25 = getelementptr inbounds [100000 x %struct.point], ptr @P, i64 0, i64 %21
  %26 = load i64, ptr %25, align 8
  store i64 %26, ptr %23, align 8
  store i64 %24, ptr %25, align 8
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %4)
  br label %27

27:                                               ; preds = %15, %19
  %.1 = phi i32 [ %20, %19 ], [ %.02225, %15 ]
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %lftr.wideiv = trunc i64 %indvars.iv.next to i32
  %exitcond.not = icmp eq i32 %11, %lftr.wideiv
  br i1 %exitcond.not, label %12, label %15, !llvm.loop !10

28:                                               ; preds = %12, %2
  ret void
}

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

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


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopRotatePass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass