; LLVM IR from VAR-solutions/Algorithms :: Sorting/Cocktail/C (Cocktail)

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

@__const.cocktailsort.inc = external hidden unnamed_addr constant [2 x i64], align 16

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

; Function Attrs: nounwind uwtable
define dso_local void @cocktailsort(ptr noundef %0, i64 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i8, align 1
  %6 = alloca [2 x i64], align 16
  %7 = alloca [2 x i64], align 16
  %8 = alloca [2 x i64], align 16
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i64 %1, ptr %4, align 8, !tbaa !9
  br label %12

12:                                               ; preds = %83, %2
  call void @llvm.lifetime.start.p0(i64 1, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 16, ptr %6) #3
  store i64 1, ptr %6, align 8, !tbaa !9
  %13 = getelementptr inbounds i64, ptr %6, i64 1
  %14 = load i64, ptr %4, align 8, !tbaa !9
  %15 = sub i64 %14, 1
  store i64 %15, ptr %13, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 16, ptr %7) #3
  %16 = load i64, ptr %4, align 8, !tbaa !9
  store i64 %16, ptr %7, align 8, !tbaa !9
  %17 = getelementptr inbounds i64, ptr %7, i64 1
  store i64 0, ptr %17, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 16, ptr %8) #3
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %8, ptr align 16 @__const.cocktailsort.inc, i64 16, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  store i32 0, ptr %9, align 4, !tbaa !11
  br label %18

18:                                               ; preds = %75, %12
  %19 = load i32, ptr %9, align 4, !tbaa !11
  %20 = icmp slt i32 %19, 2
  br i1 %20, label %22, label %21

21:                                               ; preds = %18
  store i32 4, ptr %10, align 4
  br label %78

22:                                               ; preds = %18
  store i8 1, ptr %5, align 1, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  %23 = load i32, ptr %9, align 4, !tbaa !11
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [2 x i64], ptr %6, i64 0, i64 %24
  %26 = load i64, ptr %25, align 8, !tbaa !9
  %27 = trunc i64 %26 to i32
  store i32 %27, ptr %11, align 4, !tbaa !11
  br label %28

28:                                               ; preds = %61, %22
  %29 = load i32, ptr %11, align 4, !tbaa !11
  %30 = sext i32 %29 to i64
  %31 = load i32, ptr %9, align 4, !tbaa !11
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [2 x i64], ptr %7, i64 0, i64 %32
  %34 = load i64, ptr %33, align 8, !tbaa !9
  %35 = icmp ne i64 %30, %34
  br i1 %35, label %37, label %36

36:                                               ; preds = %28
  store i32 7, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  br label %70

37:                                               ; preds = %28
  %38 = load ptr, ptr %3, align 8, !tbaa !5
  %39 = load i32, ptr %11, align 4, !tbaa !11
  %40 = sub nsw i32 %39, 1
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds i32, ptr %38, i64 %41
  %43 = load i32, ptr %42, align 4, !tbaa !11
  %44 = load ptr, ptr %3, align 8, !tbaa !5
  %45 = load i32, ptr %11, align 4, !tbaa !11
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds i32, ptr %44, i64 %46
  %48 = load i32, ptr %47, align 4, !tbaa !11
  %49 = icmp sgt i32 %43, %48
  br i1 %49, label %50, label %60

50:                                               ; preds = %37
  %51 = load ptr, ptr %3, align 8, !tbaa !5
  %52 = load i32, ptr %11, align 4, !tbaa !11
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds i32, ptr %51, i64 %53
  %55 = getelementptr inbounds i32, ptr %54, i64 -1
  %56 = load ptr, ptr %3, align 8, !tbaa !5
  %57 = load i32, ptr %11, align 4, !tbaa !11
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds i32, ptr %56, i64 %58
  call void @swap(ptr noundef %55, ptr noundef %59)
  store i8 0, ptr %5, align 1, !tbaa !13
  br label %60

60:                                               ; preds = %50, %37
  br label %61

61:                                               ; preds = %60
  %62 = load i32, ptr %9, align 4, !tbaa !11
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds [2 x i64], ptr %8, i64 0, i64 %63
  %65 = load i64, ptr %64, align 8, !tbaa !9
  %66 = load i32, ptr %11, align 4, !tbaa !11
  %67 = sext i32 %66 to i64
  %68 = add i64 %67, %65
  %69 = trunc i64 %68 to i32
  store i32 %69, ptr %11, align 4, !tbaa !11
  br label %28, !llvm.loop !14

70:                                               ; preds = %36
  %71 = load i8, ptr %5, align 1, !tbaa !13
  %72 = icmp ne i8 %71, 0
  br i1 %72, label %73, label %74

73:                                               ; preds = %70
  store i32 1, ptr %10, align 4
  br label %78

74:                                               ; preds = %70
  br label %75

75:                                               ; preds = %74
  %76 = load i32, ptr %9, align 4, !tbaa !11
  %77 = add nsw i32 %76, 1
  store i32 %77, ptr %9, align 4, !tbaa !11
  br label %18, !llvm.loop !16

78:                                               ; preds = %73, %21
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  %79 = load i32, ptr %10, align 4
  switch i32 %79, label %81 [
    i32 4, label %80
  ]

80:                                               ; preds = %78
  store i32 0, ptr %10, align 4
  br label %81

81:                                               ; preds = %80, %78
  call void @llvm.lifetime.end.p0(i64 16, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 16, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 16, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %5) #3
  %82 = load i32, ptr %10, align 4
  switch i32 %82, label %85 [
    i32 0, label %83
    i32 1, label %84
  ]

83:                                               ; preds = %81
  br label %12

84:                                               ; preds = %81
  ret void

85:                                               ; preds = %81
  unreachable
}

; 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 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: 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 = { 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 = !{!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 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}


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

@__const.cocktailsort.inc = external hidden unnamed_addr constant [2 x i64], align 16

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

; Function Attrs: nounwind uwtable
define dso_local void @cocktailsort(ptr noundef %0, i64 noundef %1) local_unnamed_addr #0 {
.critedge:
  %2 = shl nsw i64 %1, 32
  %sext25.1 = add i64 %2, -4294967296
  %.not26 = icmp eq i64 %1, 1
  %3 = load i64, ptr @__const.cocktailsort.inc, align 16
  %.not26.1 = icmp eq i64 %sext25.1, 0
  %4 = load i64, ptr getelementptr inbounds (i8, ptr @__const.cocktailsort.inc, i64 8), align 8
  br i1 %.not26, label %._crit_edge.thread, label %.lr.ph

.lr.ph.1:                                         ; preds = %._crit_edge, %14
  %sext28.1 = phi i64 [ %sext.1, %14 ], [ %sext25.1, %._crit_edge ]
  %.02127.1 = phi i8 [ %.1.1, %14 ], [ 1, %._crit_edge ]
  %5 = ashr exact i64 %sext28.1, 32
  %sext24.1 = add i64 %sext28.1, -4294967296
  %6 = ashr exact i64 %sext24.1, 30
  %7 = getelementptr inbounds i8, ptr %0, i64 %6
  %8 = load i32, ptr %7, align 4, !tbaa !5
  %9 = getelementptr inbounds i32, ptr %0, i64 %5
  %10 = load i32, ptr %9, align 4, !tbaa !5
  %11 = icmp sgt i32 %8, %10
  br i1 %11, label %12, label %14

12:                                               ; preds = %.lr.ph.1
  %13 = getelementptr inbounds i8, ptr %9, i64 -4
  tail call void @swap(ptr noundef nonnull %13, ptr noundef nonnull %9)
  br label %14

14:                                               ; preds = %12, %.lr.ph.1
  %.1.1 = phi i8 [ 0, %12 ], [ %.02127.1, %.lr.ph.1 ]
  %15 = add i64 %4, %5
  %sext.1 = shl i64 %15, 32
  %.not.1 = icmp eq i64 %sext.1, 0
  br i1 %.not.1, label %._crit_edge.1, label %.lr.ph.1, !llvm.loop !9

._crit_edge.1:                                    ; preds = %14
  %.not23.1 = icmp ne i8 %.1.1, 0
  %brmerge = or i1 %.not23.1, %.not26
  br i1 %brmerge, label %._crit_edge.thread, label %.lr.ph.backedge

._crit_edge:                                      ; preds = %25
  %.not23 = icmp ne i8 %.1, 0
  %or.cond = select i1 %.not23, i1 true, i1 %.not26.1
  br i1 %or.cond, label %._crit_edge.thread, label %.lr.ph.1

.lr.ph:                                           ; preds = %.critedge, %.lr.ph.backedge
  %16 = phi i64 [ %.be, %.lr.ph.backedge ], [ 1, %.critedge ]
  %sext28 = phi i64 [ %sext28.be, %.lr.ph.backedge ], [ 4294967296, %.critedge ]
  %.02127 = phi i8 [ %.02127.be, %.lr.ph.backedge ], [ 1, %.critedge ]
  %sext24 = add i64 %sext28, -4294967296
  %17 = ashr exact i64 %sext24, 30
  %18 = getelementptr inbounds i8, ptr %0, i64 %17
  %19 = load i32, ptr %18, align 4, !tbaa !5
  %20 = getelementptr inbounds i32, ptr %0, i64 %16
  %21 = load i32, ptr %20, align 4, !tbaa !5
  %22 = icmp sgt i32 %19, %21
  br i1 %22, label %23, label %25

23:                                               ; preds = %.lr.ph
  %24 = getelementptr inbounds i8, ptr %20, i64 -4
  tail call void @swap(ptr noundef nonnull %24, ptr noundef nonnull %20)
  br label %25

25:                                               ; preds = %.lr.ph, %23
  %.1 = phi i8 [ 0, %23 ], [ %.02127, %.lr.ph ]
  %26 = add i64 %3, %16
  %sext = shl i64 %26, 32
  %27 = ashr exact i64 %sext, 32
  %.not = icmp eq i64 %27, %1
  br i1 %.not, label %._crit_edge, label %.lr.ph.backedge

.lr.ph.backedge:                                  ; preds = %25, %._crit_edge.1
  %.be = phi i64 [ %27, %25 ], [ 1, %._crit_edge.1 ]
  %sext28.be = phi i64 [ %sext, %25 ], [ 4294967296, %._crit_edge.1 ]
  %.02127.be = phi i8 [ %.1, %25 ], [ 1, %._crit_edge.1 ]
  br label %.lr.ph, !llvm.loop !9

._crit_edge.thread:                               ; preds = %._crit_edge.1, %._crit_edge, %.critedge
  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" }

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


; Effective O3 passes: ConstraintEliminationPass,CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass