; LLVM IR from HarshCasper/NeoAlgo :: C/sort (Wiggle_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"

@.str = external hidden unnamed_addr constant [4 x i8], align 1

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

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

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

; Function Attrs: nounwind uwtable
define dso_local void @wiggle_sort(i32 noundef %0, ptr noundef %1) #1 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %12 = load i32, ptr %3, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = mul i64 4, %13
  %15 = call noalias ptr @malloc(i64 noundef %14) #5
  store ptr %15, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  store i32 0, ptr %8, align 4, !tbaa !5
  br label %16

16:                                               ; preds = %31, %2
  %17 = load i32, ptr %8, align 4, !tbaa !5
  %18 = load i32, ptr %3, align 4, !tbaa !5
  %19 = icmp slt i32 %17, %18
  br i1 %19, label %21, label %20

20:                                               ; preds = %16
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  br label %34

21:                                               ; preds = %16
  %22 = load ptr, ptr %4, align 8, !tbaa !9
  %23 = load i32, ptr %8, align 4, !tbaa !5
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds i32, ptr %22, i64 %24
  %26 = load i32, ptr %25, align 4, !tbaa !5
  %27 = load ptr, ptr %7, align 8, !tbaa !9
  %28 = load i32, ptr %8, align 4, !tbaa !5
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds i32, ptr %27, i64 %29
  store i32 %26, ptr %30, align 4, !tbaa !5
  br label %31

31:                                               ; preds = %21
  %32 = load i32, ptr %8, align 4, !tbaa !5
  %33 = add nsw i32 %32, 1
  store i32 %33, ptr %8, align 4, !tbaa !5
  br label %16, !llvm.loop !11

34:                                               ; preds = %20
  %35 = load ptr, ptr %7, align 8, !tbaa !9
  %36 = load i32, ptr %3, align 4, !tbaa !5
  %37 = sub nsw i32 %36, 1
  call void @merge_sort(ptr noundef %35, i32 noundef 0, i32 noundef %37)
  %38 = load i32, ptr %3, align 4, !tbaa !5
  %39 = sub nsw i32 %38, 1
  %40 = sdiv i32 %39, 2
  store i32 %40, ptr %5, align 4, !tbaa !5
  %41 = load i32, ptr %3, align 4, !tbaa !5
  %42 = sub nsw i32 %41, 1
  store i32 %42, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  store i32 0, ptr %9, align 4, !tbaa !5
  br label %43

43:                                               ; preds = %59, %34
  %44 = load i32, ptr %9, align 4, !tbaa !5
  %45 = load i32, ptr %3, align 4, !tbaa !5
  %46 = icmp slt i32 %44, %45
  br i1 %46, label %48, label %47

47:                                               ; preds = %43
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  br label %62

48:                                               ; preds = %43
  %49 = load ptr, ptr %7, align 8, !tbaa !9
  %50 = load i32, ptr %5, align 4, !tbaa !5
  %51 = add nsw i32 %50, -1
  store i32 %51, ptr %5, align 4, !tbaa !5
  %52 = sext i32 %50 to i64
  %53 = getelementptr inbounds i32, ptr %49, i64 %52
  %54 = load i32, ptr %53, align 4, !tbaa !5
  %55 = load ptr, ptr %4, align 8, !tbaa !9
  %56 = load i32, ptr %9, align 4, !tbaa !5
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds i32, ptr %55, i64 %57
  store i32 %54, ptr %58, align 4, !tbaa !5
  br label %59

59:                                               ; preds = %48
  %60 = load i32, ptr %9, align 4, !tbaa !5
  %61 = add nsw i32 %60, 2
  store i32 %61, ptr %9, align 4, !tbaa !5
  br label %43, !llvm.loop !13

62:                                               ; preds = %47
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  store i32 1, ptr %10, align 4, !tbaa !5
  br label %63

63:                                               ; preds = %79, %62
  %64 = load i32, ptr %10, align 4, !tbaa !5
  %65 = load i32, ptr %3, align 4, !tbaa !5
  %66 = icmp slt i32 %64, %65
  br i1 %66, label %68, label %67

67:                                               ; preds = %63
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  br label %82

68:                                               ; preds = %63
  %69 = load ptr, ptr %7, align 8, !tbaa !9
  %70 = load i32, ptr %6, align 4, !tbaa !5
  %71 = add nsw i32 %70, -1
  store i32 %71, ptr %6, align 4, !tbaa !5
  %72 = sext i32 %70 to i64
  %73 = getelementptr inbounds i32, ptr %69, i64 %72
  %74 = load i32, ptr %73, align 4, !tbaa !5
  %75 = load ptr, ptr %4, align 8, !tbaa !9
  %76 = load i32, ptr %10, align 4, !tbaa !5
  %77 = sext i32 %76 to i64
  %78 = getelementptr inbounds i32, ptr %75, i64 %77
  store i32 %74, ptr %78, align 4, !tbaa !5
  br label %79

79:                                               ; preds = %68
  %80 = load i32, ptr %10, align 4, !tbaa !5
  %81 = add nsw i32 %80, 2
  store i32 %81, ptr %10, align 4, !tbaa !5
  br label %63, !llvm.loop !14

82:                                               ; preds = %67
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  store i32 0, ptr %11, align 4, !tbaa !5
  br label %83

83:                                               ; preds = %95, %82
  %84 = load i32, ptr %11, align 4, !tbaa !5
  %85 = load i32, ptr %3, align 4, !tbaa !5
  %86 = icmp slt i32 %84, %85
  br i1 %86, label %88, label %87

87:                                               ; preds = %83
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  br label %98

88:                                               ; preds = %83
  %89 = load ptr, ptr %4, align 8, !tbaa !9
  %90 = load i32, ptr %11, align 4, !tbaa !5
  %91 = sext i32 %90 to i64
  %92 = getelementptr inbounds i32, ptr %89, i64 %91
  %93 = load i32, ptr %92, align 4, !tbaa !5
  %94 = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %93)
  br label %95

95:                                               ; preds = %88
  %96 = load i32, ptr %11, align 4, !tbaa !5
  %97 = add nsw i32 %96, 1
  store i32 %97, ptr %11, align 4, !tbaa !5
  br label %83, !llvm.loop !15

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

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

declare i32 @printf(ptr noundef, ...) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { nounwind allocsize(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 #3 = { "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 #4 = { nounwind }
attributes #5 = { nounwind allocsize(0) }

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

@.str = external hidden unnamed_addr constant [4 x i8], align 1

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

; Function Attrs: nounwind uwtable
define dso_local void @wiggle_sort(i32 noundef %0, ptr nocapture noundef %1) local_unnamed_addr #0 {
  %3 = sext i32 %0 to i64
  %4 = shl nsw i64 %3, 2
  %5 = tail call noalias ptr @malloc(i64 noundef %4) #4
  %6 = icmp sgt i32 %0, 0
  br i1 %6, label %.lr.ph41.preheader, label %.preheader.thread

.preheader.thread:                                ; preds = %2
  %7 = add nsw i32 %0, -1
  tail call void @merge_sort(ptr noundef %5, i32 noundef 0, i32 noundef %7)
  br label %._crit_edge47

.lr.ph41.preheader:                               ; preds = %2
  %8 = zext nneg i32 %0 to i64
  %9 = shl nuw nsw i64 %8, 2
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 4 %5, ptr align 4 %1, i64 %9, i1 false), !tbaa !5
  %10 = add nsw i32 %0, -1
  tail call void @merge_sort(ptr noundef %5, i32 noundef 0, i32 noundef %10)
  %11 = lshr i32 %10, 1
  %12 = zext nneg i32 %11 to i64
  %13 = add nsw i64 %3, -1
  %14 = lshr i64 %13, 1
  %15 = add nuw i64 %14, 1
  %xtraiter = and i64 %15, 3
  %16 = icmp ult i32 %0, 7
  br i1 %16, label %.preheader36.unr-lcssa, label %.lr.ph41.preheader.new

.lr.ph41.preheader.new:                           ; preds = %.lr.ph41.preheader
  %unroll_iter = and i64 %15, -4
  %invariant.gep = getelementptr i8, ptr %5, i64 -4
  %invariant.gep64 = getelementptr i8, ptr %5, i64 -8
  %invariant.gep66 = getelementptr i8, ptr %5, i64 -12
  br label %.lr.ph41

.preheader36.unr-lcssa:                           ; preds = %.lr.ph41, %.lr.ph41.preheader
  %indvars.iv49.unr = phi i64 [ %12, %.lr.ph41.preheader ], [ %indvars.iv.next50.3, %.lr.ph41 ]
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph41.preheader ], [ %indvars.iv.next.3, %.lr.ph41 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.preheader36, label %.lr.ph41.epil

.lr.ph41.epil:                                    ; preds = %.preheader36.unr-lcssa, %.lr.ph41.epil
  %indvars.iv49.epil = phi i64 [ %indvars.iv.next50.epil, %.lr.ph41.epil ], [ %indvars.iv49.unr, %.preheader36.unr-lcssa ]
  %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %.lr.ph41.epil ], [ %indvars.iv.unr, %.preheader36.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph41.epil ], [ 0, %.preheader36.unr-lcssa ]
  %indvars.iv.next50.epil = add nsw i64 %indvars.iv49.epil, -1
  %17 = getelementptr inbounds i32, ptr %5, i64 %indvars.iv49.epil
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = getelementptr inbounds i32, ptr %1, i64 %indvars.iv.epil
  store i32 %18, ptr %19, align 4, !tbaa !5
  %indvars.iv.next.epil = add nuw nsw i64 %indvars.iv.epil, 2
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %.preheader36, label %.lr.ph41.epil, !llvm.loop !9

.preheader36:                                     ; preds = %.lr.ph41.epil, %.preheader36.unr-lcssa
  %.not = icmp eq i32 %0, 1
  br i1 %.not, label %.preheader, label %.lr.ph44

.lr.ph41:                                         ; preds = %.lr.ph41, %.lr.ph41.preheader.new
  %indvars.iv49 = phi i64 [ %12, %.lr.ph41.preheader.new ], [ %indvars.iv.next50.3, %.lr.ph41 ]
  %indvars.iv = phi i64 [ 0, %.lr.ph41.preheader.new ], [ %indvars.iv.next.3, %.lr.ph41 ]
  %niter = phi i64 [ 0, %.lr.ph41.preheader.new ], [ %niter.next.3, %.lr.ph41 ]
  %20 = getelementptr inbounds i32, ptr %5, i64 %indvars.iv49
  %21 = load i32, ptr %20, align 4, !tbaa !5
  %22 = getelementptr inbounds i32, ptr %1, i64 %indvars.iv
  store i32 %21, ptr %22, align 4, !tbaa !5
  %indvars.iv.next = or disjoint i64 %indvars.iv, 2
  %gep = getelementptr i32, ptr %invariant.gep, i64 %indvars.iv49
  %23 = load i32, ptr %gep, align 4, !tbaa !5
  %24 = getelementptr inbounds i32, ptr %1, i64 %indvars.iv.next
  store i32 %23, ptr %24, align 4, !tbaa !5
  %indvars.iv.next.1 = or disjoint i64 %indvars.iv, 4
  %gep65 = getelementptr i32, ptr %invariant.gep64, i64 %indvars.iv49
  %25 = load i32, ptr %gep65, align 4, !tbaa !5
  %26 = getelementptr inbounds i32, ptr %1, i64 %indvars.iv.next.1
  store i32 %25, ptr %26, align 4, !tbaa !5
  %indvars.iv.next.2 = or disjoint i64 %indvars.iv, 6
  %indvars.iv.next50.3 = add nsw i64 %indvars.iv49, -4
  %gep67 = getelementptr i32, ptr %invariant.gep66, i64 %indvars.iv49
  %27 = load i32, ptr %gep67, align 4, !tbaa !5
  %28 = getelementptr inbounds i32, ptr %1, i64 %indvars.iv.next.2
  store i32 %27, ptr %28, align 4, !tbaa !5
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 8
  %niter.next.3 = add i64 %niter, 4
  %niter.ncmp.3.not = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3.not, label %.preheader36.unr-lcssa, label %.lr.ph41, !llvm.loop !11

.preheader:                                       ; preds = %.lr.ph44, %.preheader36
  br i1 %6, label %.lr.ph46.preheader, label %._crit_edge47

.lr.ph46.preheader:                               ; preds = %.preheader
  %wide.trip.count = zext nneg i32 %0 to i64
  br label %.lr.ph46

.lr.ph44:                                         ; preds = %.preheader36, %.lr.ph44
  %indvars.iv56.in = phi i64 [ %indvars.iv56, %.lr.ph44 ], [ %3, %.preheader36 ]
  %indvars.iv54 = phi i64 [ %indvars.iv.next55, %.lr.ph44 ], [ 1, %.preheader36 ]
  %indvars.iv56 = add nsw i64 %indvars.iv56.in, -1
  %29 = getelementptr inbounds i32, ptr %5, i64 %indvars.iv56
  %30 = load i32, ptr %29, align 4, !tbaa !5
  %31 = getelementptr inbounds i32, ptr %1, i64 %indvars.iv54
  store i32 %30, ptr %31, align 4, !tbaa !5
  %indvars.iv.next55 = add nuw nsw i64 %indvars.iv54, 2
  %32 = icmp slt i64 %indvars.iv.next55, %3
  br i1 %32, label %.lr.ph44, label %.preheader, !llvm.loop !13

._crit_edge47:                                    ; preds = %.lr.ph46, %.preheader.thread, %.preheader
  ret void

.lr.ph46:                                         ; preds = %.lr.ph46.preheader, %.lr.ph46
  %indvars.iv61 = phi i64 [ 0, %.lr.ph46.preheader ], [ %indvars.iv.next62, %.lr.ph46 ]
  %33 = getelementptr inbounds i32, ptr %1, i64 %indvars.iv61
  %34 = load i32, ptr %33, align 4, !tbaa !5
  %35 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %34)
  %indvars.iv.next62 = add nuw nsw i64 %indvars.iv61, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next62, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge47, label %.lr.ph46, !llvm.loop !14
}

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #1

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #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

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 nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { nofree nounwind "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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { nounwind allocsize(0) }

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


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