; LLVM IR from ismdeep/sort-algos-c :: include/sort-algos (cocktail-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"

; Function Attrs: nounwind uwtable
define dso_local void @cocktail_sort(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3) #0 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !9
  store ptr %3, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %12 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %12, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  %13 = load ptr, ptr %6, align 8, !tbaa !5
  %14 = load i64, ptr %7, align 8, !tbaa !9
  %15 = sub i64 0, %14
  %16 = getelementptr inbounds i8, ptr %13, i64 %15
  store ptr %16, ptr %11, align 8, !tbaa !5
  br label %17

17:                                               ; preds = %76, %4
  %18 = load ptr, ptr %10, align 8, !tbaa !5
  %19 = load ptr, ptr %11, align 8, !tbaa !5
  %20 = icmp ult ptr %18, %19
  br i1 %20, label %21, label %80

21:                                               ; preds = %17
  %22 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %22, ptr %9, align 8, !tbaa !5
  br label %23

23:                                               ; preds = %41, %21
  %24 = load ptr, ptr %9, align 8, !tbaa !5
  %25 = load ptr, ptr %11, align 8, !tbaa !5
  %26 = icmp ult ptr %24, %25
  br i1 %26, label %27, label %45

27:                                               ; preds = %23
  %28 = load ptr, ptr %8, align 8, !tbaa !5
  %29 = load ptr, ptr %9, align 8, !tbaa !5
  %30 = load ptr, ptr %9, align 8, !tbaa !5
  %31 = load i64, ptr %7, align 8, !tbaa !9
  %32 = getelementptr inbounds i8, ptr %30, i64 %31
  %33 = call zeroext i1 %28(ptr noundef %29, ptr noundef %32)
  br i1 %33, label %40, label %34

34:                                               ; preds = %27
  %35 = load ptr, ptr %9, align 8, !tbaa !5
  %36 = load ptr, ptr %9, align 8, !tbaa !5
  %37 = load i64, ptr %7, align 8, !tbaa !9
  %38 = getelementptr inbounds i8, ptr %36, i64 %37
  %39 = load i64, ptr %7, align 8, !tbaa !9
  call void @data_swap(ptr noundef %35, ptr noundef %38, i64 noundef %39)
  br label %40

40:                                               ; preds = %34, %27
  br label %41

41:                                               ; preds = %40
  %42 = load i64, ptr %7, align 8, !tbaa !9
  %43 = load ptr, ptr %9, align 8, !tbaa !5
  %44 = getelementptr inbounds i8, ptr %43, i64 %42
  store ptr %44, ptr %9, align 8, !tbaa !5
  br label %23, !llvm.loop !11

45:                                               ; preds = %23
  %46 = load i64, ptr %7, align 8, !tbaa !9
  %47 = load ptr, ptr %11, align 8, !tbaa !5
  %48 = sub i64 0, %46
  %49 = getelementptr inbounds i8, ptr %47, i64 %48
  store ptr %49, ptr %11, align 8, !tbaa !5
  %50 = load ptr, ptr %11, align 8, !tbaa !5
  store ptr %50, ptr %9, align 8, !tbaa !5
  br label %51

51:                                               ; preds = %71, %45
  %52 = load ptr, ptr %9, align 8, !tbaa !5
  %53 = load ptr, ptr %10, align 8, !tbaa !5
  %54 = icmp ugt ptr %52, %53
  br i1 %54, label %55, label %76

55:                                               ; preds = %51
  %56 = load ptr, ptr %8, align 8, !tbaa !5
  %57 = load ptr, ptr %9, align 8, !tbaa !5
  %58 = load i64, ptr %7, align 8, !tbaa !9
  %59 = sub i64 0, %58
  %60 = getelementptr inbounds i8, ptr %57, i64 %59
  %61 = load ptr, ptr %9, align 8, !tbaa !5
  %62 = call zeroext i1 %56(ptr noundef %60, ptr noundef %61)
  br i1 %62, label %70, label %63

63:                                               ; preds = %55
  %64 = load ptr, ptr %9, align 8, !tbaa !5
  %65 = load ptr, ptr %9, align 8, !tbaa !5
  %66 = load i64, ptr %7, align 8, !tbaa !9
  %67 = sub i64 0, %66
  %68 = getelementptr inbounds i8, ptr %65, i64 %67
  %69 = load i64, ptr %7, align 8, !tbaa !9
  call void @data_swap(ptr noundef %64, ptr noundef %68, i64 noundef %69)
  br label %70

70:                                               ; preds = %63, %55
  br label %71

71:                                               ; preds = %70
  %72 = load i64, ptr %7, align 8, !tbaa !9
  %73 = load ptr, ptr %9, align 8, !tbaa !5
  %74 = sub i64 0, %72
  %75 = getelementptr inbounds i8, ptr %73, i64 %74
  store ptr %75, ptr %9, align 8, !tbaa !5
  br label %51, !llvm.loop !13

76:                                               ; preds = %51
  %77 = load i64, ptr %7, align 8, !tbaa !9
  %78 = load ptr, ptr %10, align 8, !tbaa !5
  %79 = getelementptr inbounds i8, ptr %78, i64 %77
  store ptr %79, ptr %10, align 8, !tbaa !5
  br label %17, !llvm.loop !14

80:                                               ; preds = %17
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  ret void
}

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

declare void @data_swap(ptr noundef, ptr noundef, i64 noundef) #2

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

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 = { "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 }

!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 = !{!"long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}
!14 = distinct !{!14, !12}


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

; Function Attrs: nounwind uwtable
define dso_local void @cocktail_sort(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr nocapture noundef readonly %3) local_unnamed_addr #0 {
  %5 = sub i64 0, %2
  %6 = getelementptr inbounds i8, ptr %1, i64 %5
  %7 = icmp ugt ptr %6, %0
  br i1 %7, label %.preheader, label %._crit_edge40

.preheader:                                       ; preds = %4, %._crit_edge
  %.039 = phi ptr [ %15, %._crit_edge ], [ %6, %4 ]
  %.03438 = phi ptr [ %22, %._crit_edge ], [ %0, %4 ]
  br label %8

8:                                                ; preds = %.preheader, %12
  %.03536 = phi ptr [ %.03438, %.preheader ], [ %9, %12 ]
  %9 = getelementptr inbounds i8, ptr %.03536, i64 %2
  %10 = tail call zeroext i1 %3(ptr noundef %.03536, ptr noundef %9) #2
  br i1 %10, label %12, label %11

11:                                               ; preds = %8
  tail call void @data_swap(ptr noundef %.03536, ptr noundef %9, i64 noundef %2) #2
  br label %12

12:                                               ; preds = %8, %11
  %13 = icmp ult ptr %9, %.039
  br i1 %13, label %8, label %14, !llvm.loop !5

14:                                               ; preds = %12
  %15 = getelementptr inbounds i8, ptr %.039, i64 %5
  %16 = icmp ugt ptr %15, %.03438
  br i1 %16, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %14, %20
  %.137 = phi ptr [ %17, %20 ], [ %15, %14 ]
  %17 = getelementptr inbounds i8, ptr %.137, i64 %5
  %18 = tail call zeroext i1 %3(ptr noundef nonnull %17, ptr noundef nonnull %.137) #2
  br i1 %18, label %20, label %19

19:                                               ; preds = %.lr.ph
  tail call void @data_swap(ptr noundef nonnull %.137, ptr noundef nonnull %17, i64 noundef %2) #2
  br label %20

20:                                               ; preds = %.lr.ph, %19
  %21 = icmp ugt ptr %17, %.03438
  br i1 %21, label %.lr.ph, label %._crit_edge, !llvm.loop !7

._crit_edge:                                      ; preds = %20, %14
  %22 = getelementptr inbounds i8, ptr %.03438, i64 %2
  %23 = icmp ult ptr %22, %15
  br i1 %23, label %.preheader, label %._crit_edge40, !llvm.loop !8

._crit_edge40:                                    ; preds = %._crit_edge, %4
  ret void
}

declare void @data_swap(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #1

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 = { "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 = { 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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = distinct !{!7, !6}
!8 = distinct !{!8, !6}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass