; LLVM IR from HarshCasper/NeoAlgo :: C/ds (SegregateOddEven)

; 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.node = type { i32, ptr }

@header = external dso_local global ptr, align 8
@last = external dso_local global ptr, align 8

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

7:                                                ; preds = %45, %0
  %8 = load ptr, ptr %1, align 8, !tbaa !5
  %9 = load ptr, ptr %2, align 8, !tbaa !5
  %10 = icmp ne ptr %8, %9
  br i1 %10, label %11, label %46

11:                                               ; preds = %7
  %12 = load ptr, ptr %1, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.node, ptr %12, i32 0, i32 0
  %14 = load i32, ptr %13, align 8, !tbaa !9
  %15 = srem i32 %14, 2
  %16 = icmp ne i32 %15, 0
  br i1 %16, label %17, label %40

17:                                               ; preds = %11
  %18 = load ptr, ptr %1, align 8, !tbaa !5
  %19 = load ptr, ptr @header, align 8, !tbaa !5
  %20 = icmp eq ptr %18, %19
  br i1 %20, label %21, label %26

21:                                               ; preds = %17
  %22 = load ptr, ptr @header, align 8, !tbaa !5
  store ptr %22, ptr %4, align 8, !tbaa !5
  %23 = load ptr, ptr @header, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.node, ptr %23, i32 0, i32 1
  %25 = load ptr, ptr %24, align 8, !tbaa !12
  store ptr %25, ptr @header, align 8, !tbaa !5
  br label %26

26:                                               ; preds = %21, %17
  %27 = load ptr, ptr %1, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.node, ptr %27, i32 0, i32 1
  %29 = load ptr, ptr %28, align 8, !tbaa !12
  store ptr %29, ptr %3, align 8, !tbaa !5
  %30 = load ptr, ptr %3, align 8, !tbaa !5
  %31 = load ptr, ptr %4, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.node, ptr %31, i32 0, i32 1
  store ptr %30, ptr %32, align 8, !tbaa !12
  %33 = load ptr, ptr %1, align 8, !tbaa !5
  %34 = load ptr, ptr @last, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.node, ptr %34, i32 0, i32 1
  store ptr %33, ptr %35, align 8, !tbaa !12
  %36 = load ptr, ptr %1, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.node, ptr %36, i32 0, i32 1
  store ptr null, ptr %37, align 8, !tbaa !12
  %38 = load ptr, ptr %1, align 8, !tbaa !5
  store ptr %38, ptr @last, align 8, !tbaa !5
  %39 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %39, ptr %1, align 8, !tbaa !5
  br label %45

40:                                               ; preds = %11
  %41 = load ptr, ptr %1, align 8, !tbaa !5
  store ptr %41, ptr %4, align 8, !tbaa !5
  %42 = load ptr, ptr %1, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.node, ptr %42, i32 0, i32 1
  %44 = load ptr, ptr %43, align 8, !tbaa !12
  store ptr %44, ptr %1, align 8, !tbaa !5
  br label %45

45:                                               ; preds = %40, %26
  br label %7, !llvm.loop !13

46:                                               ; preds = %7
  %47 = load ptr, ptr %2, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.node, ptr %47, i32 0, i32 0
  %49 = load i32, ptr %48, align 8, !tbaa !9
  %50 = srem i32 %49, 2
  %51 = icmp ne i32 %50, 0
  br i1 %51, label %52, label %65

52:                                               ; preds = %46
  %53 = load ptr, ptr %1, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.node, ptr %53, i32 0, i32 1
  %55 = load ptr, ptr %54, align 8, !tbaa !12
  store ptr %55, ptr %3, align 8, !tbaa !5
  %56 = load ptr, ptr %3, align 8, !tbaa !5
  %57 = load ptr, ptr %4, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.node, ptr %57, i32 0, i32 1
  store ptr %56, ptr %58, align 8, !tbaa !12
  %59 = load ptr, ptr %1, align 8, !tbaa !5
  %60 = load ptr, ptr @last, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.node, ptr %60, i32 0, i32 1
  store ptr %59, ptr %61, align 8, !tbaa !12
  %62 = load ptr, ptr %1, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.node, ptr %62, i32 0, i32 1
  store ptr null, ptr %63, align 8, !tbaa !12
  %64 = load ptr, ptr %1, align 8, !tbaa !5
  store ptr %64, ptr @last, align 8, !tbaa !5
  br label %65

65:                                               ; preds = %52, %46
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #2
  ret void
}

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 }

!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, !11, i64 0}
!10 = !{!"node", !11, i64 0, !6, i64 8}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !6, i64 8}
!13 = distinct !{!13, !14}
!14 = !{!"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"

@header = external dso_local local_unnamed_addr global ptr, align 8
@last = external dso_local local_unnamed_addr global ptr, align 8

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @SegregateOddEven() local_unnamed_addr #0 {
  %1 = load ptr, ptr @header, align 8, !tbaa !5
  %2 = load ptr, ptr @last, align 8, !tbaa !5
  %.not28 = icmp eq ptr %1, %2
  br i1 %.not28, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %0, %20
  %.031 = phi ptr [ %.2, %20 ], [ undef, %0 ]
  %.02030 = phi ptr [ %.121, %20 ], [ %1, %0 ]
  %3 = phi ptr [ %21, %20 ], [ %1, %0 ]
  %.0202729 = phi ptr [ %.02026, %20 ], [ %2, %0 ]
  %4 = load i32, ptr %.02030, align 8, !tbaa !9
  %5 = and i32 %4, 1
  %.not25 = icmp eq i32 %5, 0
  br i1 %.not25, label %17, label %6

6:                                                ; preds = %.lr.ph
  %7 = icmp eq ptr %.02030, %3
  br i1 %7, label %8, label %11

8:                                                ; preds = %6
  %9 = getelementptr inbounds i8, ptr %3, i64 8
  %10 = load ptr, ptr %9, align 8, !tbaa !12
  store ptr %10, ptr @header, align 8, !tbaa !5
  br label %11

11:                                               ; preds = %8, %6
  %12 = phi ptr [ %10, %8 ], [ %3, %6 ]
  %.1 = phi ptr [ %3, %8 ], [ %.031, %6 ]
  %13 = getelementptr inbounds i8, ptr %.02030, i64 8
  %14 = load ptr, ptr %13, align 8, !tbaa !12
  %15 = getelementptr inbounds i8, ptr %.1, i64 8
  store ptr %14, ptr %15, align 8, !tbaa !12
  %16 = getelementptr inbounds i8, ptr %.0202729, i64 8
  store ptr %.02030, ptr %16, align 8, !tbaa !12
  store ptr null, ptr %13, align 8, !tbaa !12
  store ptr %.02030, ptr @last, align 8, !tbaa !5
  br label %20

17:                                               ; preds = %.lr.ph
  %18 = getelementptr inbounds i8, ptr %.02030, i64 8
  %19 = load ptr, ptr %18, align 8, !tbaa !12
  br label %20

20:                                               ; preds = %17, %11
  %.02026 = phi ptr [ %.02030, %11 ], [ %.0202729, %17 ]
  %21 = phi ptr [ %12, %11 ], [ %3, %17 ]
  %.121 = phi ptr [ %14, %11 ], [ %19, %17 ]
  %.2 = phi ptr [ %.1, %11 ], [ %.02030, %17 ]
  %.not = icmp eq ptr %.121, %2
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !13

._crit_edge:                                      ; preds = %20, %0
  %22 = phi ptr [ %2, %0 ], [ %.02026, %20 ]
  %.020.lcssa = phi ptr [ %1, %0 ], [ %.121, %20 ]
  %.0.lcssa = phi ptr [ undef, %0 ], [ %.2, %20 ]
  %23 = load i32, ptr %2, align 8, !tbaa !9
  %24 = and i32 %23, 1
  %.not24 = icmp eq i32 %24, 0
  br i1 %.not24, label %30, label %25

25:                                               ; preds = %._crit_edge
  %26 = getelementptr inbounds i8, ptr %.020.lcssa, i64 8
  %27 = load ptr, ptr %26, align 8, !tbaa !12
  %28 = getelementptr inbounds i8, ptr %.0.lcssa, i64 8
  store ptr %27, ptr %28, align 8, !tbaa !12
  %29 = getelementptr inbounds i8, ptr %22, i64 8
  store ptr %.020.lcssa, ptr %29, align 8, !tbaa !12
  store ptr null, ptr %26, align 8, !tbaa !12
  store ptr %.020.lcssa, ptr @last, align 8, !tbaa !5
  br label %30

30:                                               ; preds = %25, %._crit_edge
  ret void
}

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

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


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