; LLVM IR from h-ssiqueira/Sort_Algorithms :: src/esotericFunMiscellaneous (pancakeSort)

; 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 @flip(ptr noundef %0, ptr noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %6 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %6, ptr %5, align 8, !tbaa !5
  br label %7

7:                                                ; preds = %11, %2
  %8 = load ptr, ptr %5, align 8, !tbaa !5
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = icmp ult ptr %8, %9
  br i1 %10, label %11, label %34

11:                                               ; preds = %7
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  %13 = load i64, ptr %12, align 8, !tbaa !9
  %14 = load ptr, ptr %5, align 8, !tbaa !5
  %15 = load i64, ptr %14, align 8, !tbaa !9
  %16 = xor i64 %13, %15
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  store i64 %16, ptr %17, align 8, !tbaa !9
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = load i64, ptr %18, align 8, !tbaa !9
  %20 = load ptr, ptr %5, align 8, !tbaa !5
  %21 = load i64, ptr %20, align 8, !tbaa !9
  %22 = xor i64 %19, %21
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  store i64 %22, ptr %23, align 8, !tbaa !9
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = load i64, ptr %24, align 8, !tbaa !9
  %26 = load ptr, ptr %5, align 8, !tbaa !5
  %27 = load i64, ptr %26, align 8, !tbaa !9
  %28 = xor i64 %25, %27
  %29 = load ptr, ptr %5, align 8, !tbaa !5
  store i64 %28, ptr %29, align 8, !tbaa !9
  %30 = load ptr, ptr %5, align 8, !tbaa !5
  %31 = getelementptr inbounds i64, ptr %30, i32 1
  store ptr %31, ptr %5, align 8, !tbaa !5
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = getelementptr inbounds i64, ptr %32, i32 -1
  store ptr %33, ptr %4, align 8, !tbaa !5
  br label %7, !llvm.loop !11

34:                                               ; preds = %7
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  ret void
}

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


; 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: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @flip(ptr noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = ptrtoint ptr %0 to i64
  %4 = ptrtoint ptr %1 to i64
  %5 = icmp ult ptr %0, %1
  br i1 %5, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %2
  %6 = ptrtoint ptr %1 to i64
  %7 = ptrtoint ptr %0 to i64
  %8 = add i64 %6, -8
  %9 = add i64 %7, 8
  %umax19 = tail call i64 @llvm.umax.i64(i64 %8, i64 %9)
  %10 = add i64 %umax19, -8
  %11 = icmp ne i64 %10, %7
  %umin20 = zext i1 %11 to i64
  %12 = add i64 %7, %umin20
  %13 = sub i64 %10, %12
  %14 = lshr i64 %13, 4
  %15 = add nuw nsw i64 %14, %umin20
  %16 = add nuw nsw i64 %15, 1
  %min.iters.check = icmp ult i64 %15, 15
  br i1 %min.iters.check, label %.lr.ph.preheader30, label %vector.memcheck

vector.memcheck:                                  ; preds = %.lr.ph.preheader
  %17 = add i64 %4, -8
  %18 = add i64 %3, 8
  %umax = tail call i64 @llvm.umax.i64(i64 %17, i64 %18)
  %19 = add i64 %umax, -8
  %20 = icmp ne i64 %19, %3
  %umin = zext i1 %20 to i64
  %21 = add i64 %3, %umin
  %22 = sub i64 %19, %21
  %23 = lshr i64 %22, 4
  %24 = add nuw nsw i64 %23, %umin
  %25 = shl nuw i64 %24, 3
  %26 = getelementptr i8, ptr %0, i64 %25
  %scevgep = getelementptr i8, ptr %26, i64 8
  %27 = mul nsw i64 %24, -8
  %scevgep17 = getelementptr i8, ptr %1, i64 %27
  %scevgep18 = getelementptr i8, ptr %1, i64 8
  %bound0 = icmp ugt ptr %scevgep18, %0
  %bound1 = icmp ult ptr %scevgep17, %scevgep
  %found.conflict = and i1 %bound0, %bound1
  br i1 %found.conflict, label %.lr.ph.preheader30, label %vector.ph

vector.ph:                                        ; preds = %vector.memcheck
  %n.vec = and i64 %16, 4611686018427387902
  %28 = shl i64 %n.vec, 3
  %ind.end = getelementptr i8, ptr %0, i64 %28
  %29 = mul i64 %n.vec, -8
  %ind.end21 = getelementptr i8, ptr %1, i64 %29
  %invariant.gep = getelementptr i8, ptr %1, i64 -8
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %offset.idx = shl i64 %index, 3
  %next.gep = getelementptr i8, ptr %0, i64 %offset.idx
  %offset.idx23 = mul i64 %index, -8
  %gep = getelementptr i8, ptr %invariant.gep, i64 %offset.idx23
  %wide.load = load <2 x i64>, ptr %gep, align 8, !tbaa !5, !alias.scope !9
  %reverse = shufflevector <2 x i64> %wide.load, <2 x i64> poison, <2 x i32> <i32 1, i32 0>
  %wide.load25 = load <2 x i64>, ptr %next.gep, align 8, !tbaa !5, !alias.scope !12, !noalias !9
  %30 = xor <2 x i64> %wide.load25, %reverse
  store <2 x i64> %30, ptr %next.gep, align 8, !tbaa !5, !alias.scope !12, !noalias !9
  %reverse27 = shufflevector <2 x i64> %wide.load, <2 x i64> poison, <2 x i32> <i32 1, i32 0>
  %31 = xor <2 x i64> %reverse27, %30
  %reverse28 = shufflevector <2 x i64> %31, <2 x i64> poison, <2 x i32> <i32 1, i32 0>
  store <2 x i64> %reverse28, ptr %gep, align 8, !tbaa !5, !alias.scope !9
  store <2 x i64> %reverse27, ptr %next.gep, align 8, !tbaa !5, !alias.scope !12, !noalias !9
  %index.next = add nuw i64 %index, 2
  %32 = icmp eq i64 %index.next, %n.vec
  br i1 %32, label %middle.block, label %vector.body, !llvm.loop !14

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %16, %n.vec
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph.preheader30

.lr.ph.preheader30:                               ; preds = %middle.block, %vector.memcheck, %.lr.ph.preheader
  %.016.ph = phi ptr [ %0, %vector.memcheck ], [ %0, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  %.01415.ph = phi ptr [ %1, %vector.memcheck ], [ %1, %.lr.ph.preheader ], [ %ind.end21, %middle.block ]
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader30, %.lr.ph
  %.016 = phi ptr [ %40, %.lr.ph ], [ %.016.ph, %.lr.ph.preheader30 ]
  %.01415 = phi ptr [ %41, %.lr.ph ], [ %.01415.ph, %.lr.ph.preheader30 ]
  %33 = load i64, ptr %.01415, align 8, !tbaa !5
  %34 = load i64, ptr %.016, align 8, !tbaa !5
  %35 = xor i64 %34, %33
  store i64 %35, ptr %.016, align 8, !tbaa !5
  %36 = load i64, ptr %.01415, align 8, !tbaa !5
  %37 = xor i64 %36, %35
  store i64 %37, ptr %.01415, align 8, !tbaa !5
  %38 = load i64, ptr %.016, align 8, !tbaa !5
  %39 = xor i64 %38, %37
  store i64 %39, ptr %.016, align 8, !tbaa !5
  %40 = getelementptr inbounds i8, ptr %.016, i64 8
  %41 = getelementptr inbounds i8, ptr %.01415, i64 -8
  %42 = icmp ult ptr %40, %41
  br i1 %42, label %.lr.ph, label %._crit_edge, !llvm.loop !18

._crit_edge:                                      ; preds = %.lr.ph, %middle.block, %2
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umax.i64(i64, i64) #1

attributes #0 = { nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) 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 speculatable willreturn memory(none) }

!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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10}
!10 = distinct !{!10, !11}
!11 = distinct !{!11, !"LVerDomain"}
!12 = !{!13}
!13 = distinct !{!13, !11}
!14 = distinct !{!14, !15, !16, !17}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!"llvm.loop.isvectorized", i32 1}
!17 = !{!"llvm.loop.unroll.runtime.disable"}
!18 = distinct !{!18, !15, !16}


; Effective O3 passes: EarlyCSEPass,GlobalDCEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass