; LLVM IR from ritikbanger/Hacktoberfest2022-DSA :: 10. Linked List/c lang (Linked List)

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

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

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

; Function Attrs: nounwind uwtable
define dso_local void @sort(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %9 = load ptr, ptr %2, align 8, !tbaa !5
  %10 = call i32 @length(ptr noundef %9)
  store i32 %10, ptr %6, align 4, !tbaa !9
  %11 = load ptr, ptr %2, align 8, !tbaa !5
  store ptr %11, ptr %7, align 8, !tbaa !5
  store i32 0, ptr %3, align 4, !tbaa !9
  br label %12

12:                                               ; preds = %58, %1
  %13 = load i32, ptr %3, align 4, !tbaa !9
  %14 = load i32, ptr %6, align 4, !tbaa !9
  %15 = sub nsw i32 %14, 1
  %16 = icmp slt i32 %13, %15
  br i1 %16, label %17, label %61

17:                                               ; preds = %12
  %18 = load ptr, ptr %7, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.node, ptr %18, i32 0, i32 1
  %20 = load ptr, ptr %19, align 8, !tbaa !11
  store ptr %20, ptr %8, align 8, !tbaa !5
  %21 = load i32, ptr %3, align 4, !tbaa !9
  %22 = add nsw i32 %21, 1
  store i32 %22, ptr %4, align 4, !tbaa !9
  br label %23

23:                                               ; preds = %51, %17
  %24 = load i32, ptr %4, align 4, !tbaa !9
  %25 = load i32, ptr %6, align 4, !tbaa !9
  %26 = icmp slt i32 %24, %25
  br i1 %26, label %27, label %54

27:                                               ; preds = %23
  %28 = load ptr, ptr %8, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.node, ptr %28, i32 0, i32 0
  %30 = load i32, ptr %29, align 8, !tbaa !13
  %31 = load ptr, ptr %7, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.node, ptr %31, i32 0, i32 0
  %33 = load i32, ptr %32, align 8, !tbaa !13
  %34 = icmp slt i32 %30, %33
  br i1 %34, label %35, label %47

35:                                               ; preds = %27
  %36 = load ptr, ptr %8, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.node, ptr %36, i32 0, i32 0
  %38 = load i32, ptr %37, align 8, !tbaa !13
  store i32 %38, ptr %5, align 4, !tbaa !9
  %39 = load ptr, ptr %7, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.node, ptr %39, i32 0, i32 0
  %41 = load i32, ptr %40, align 8, !tbaa !13
  %42 = load ptr, ptr %8, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.node, ptr %42, i32 0, i32 0
  store i32 %41, ptr %43, align 8, !tbaa !13
  %44 = load i32, ptr %5, align 4, !tbaa !9
  %45 = load ptr, ptr %7, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.node, ptr %45, i32 0, i32 0
  store i32 %44, ptr %46, align 8, !tbaa !13
  br label %47

47:                                               ; preds = %35, %27
  %48 = load ptr, ptr %8, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.node, ptr %48, i32 0, i32 1
  %50 = load ptr, ptr %49, align 8, !tbaa !11
  store ptr %50, ptr %8, align 8, !tbaa !5
  br label %51

51:                                               ; preds = %47
  %52 = load i32, ptr %4, align 4, !tbaa !9
  %53 = add nsw i32 %52, 1
  store i32 %53, ptr %4, align 4, !tbaa !9
  br label %23, !llvm.loop !14

54:                                               ; preds = %23
  %55 = load ptr, ptr %7, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.node, ptr %55, i32 0, i32 1
  %57 = load ptr, ptr %56, align 8, !tbaa !11
  store ptr %57, ptr %7, align 8, !tbaa !5
  br label %58

58:                                               ; preds = %54
  %59 = load i32, ptr %3, align 4, !tbaa !9
  %60 = add nsw i32 %59, 1
  store i32 %60, ptr %3, align 4, !tbaa !9
  br label %12, !llvm.loop !16

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

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

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

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

; Function Attrs: nounwind uwtable
define dso_local void @sort(ptr noundef %0) local_unnamed_addr #0 {
  %2 = tail call i32 @length(ptr noundef %0)
  %3 = icmp sgt i32 %2, 1
  br i1 %3, label %.lr.ph29.preheader, label %._crit_edge30

.lr.ph29.preheader:                               ; preds = %1
  %4 = add nsw i32 %2, -2
  br label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %._crit_edge, %.lr.ph29.preheader
  %.01827 = phi ptr [ %29, %._crit_edge ], [ %0, %.lr.ph29.preheader ]
  %.01926 = phi i32 [ %6, %._crit_edge ], [ 0, %.lr.ph29.preheader ]
  %5 = getelementptr inbounds i8, ptr %.01827, i64 8
  %6 = add nuw nsw i32 %.01926, 1
  %.pre = load i32, ptr %.01827, align 8, !tbaa !5
  %7 = sub nsw i32 %.01926, %2
  %8 = and i32 %7, 1
  %lcmp.mod.not.not = icmp eq i32 %8, 0
  br i1 %lcmp.mod.not.not, label %.lr.ph.prol, label %.lr.ph.prol.loopexit

.lr.ph.prol:                                      ; preds = %.lr.ph.preheader
  %.025.prol = load ptr, ptr %5, align 8, !tbaa !11
  %9 = load i32, ptr %.025.prol, align 8, !tbaa !5
  %10 = icmp slt i32 %9, %.pre
  br i1 %10, label %11, label %.lr.ph.prol.loopexit.unr-lcssa

11:                                               ; preds = %.lr.ph.prol
  store i32 %.pre, ptr %.025.prol, align 8, !tbaa !5
  store i32 %9, ptr %.01827, align 8, !tbaa !5
  br label %.lr.ph.prol.loopexit.unr-lcssa

.lr.ph.prol.loopexit.unr-lcssa:                   ; preds = %11, %.lr.ph.prol
  %12 = phi i32 [ %9, %11 ], [ %.pre, %.lr.ph.prol ]
  %13 = getelementptr inbounds i8, ptr %.025.prol, i64 8
  %14 = add nuw nsw i32 %.01926, 2
  br label %.lr.ph.prol.loopexit

.lr.ph.prol.loopexit:                             ; preds = %.lr.ph.prol.loopexit.unr-lcssa, %.lr.ph.preheader
  %.unr = phi i32 [ %.pre, %.lr.ph.preheader ], [ %12, %.lr.ph.prol.loopexit.unr-lcssa ]
  %.025.in.unr = phi ptr [ %5, %.lr.ph.preheader ], [ %13, %.lr.ph.prol.loopexit.unr-lcssa ]
  %.02024.unr = phi i32 [ %6, %.lr.ph.preheader ], [ %14, %.lr.ph.prol.loopexit.unr-lcssa ]
  %15 = icmp eq i32 %4, %.01926
  br i1 %15, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.prol.loopexit, %25
  %16 = phi i32 [ %26, %25 ], [ %.unr, %.lr.ph.prol.loopexit ]
  %.025.in = phi ptr [ %27, %25 ], [ %.025.in.unr, %.lr.ph.prol.loopexit ]
  %.02024 = phi i32 [ %28, %25 ], [ %.02024.unr, %.lr.ph.prol.loopexit ]
  %.025 = load ptr, ptr %.025.in, align 8, !tbaa !11
  %17 = load i32, ptr %.025, align 8, !tbaa !5
  %18 = icmp slt i32 %17, %16
  br i1 %18, label %19, label %.lr.ph.1

19:                                               ; preds = %.lr.ph
  store i32 %16, ptr %.025, align 8, !tbaa !5
  store i32 %17, ptr %.01827, align 8, !tbaa !5
  br label %.lr.ph.1

.lr.ph.1:                                         ; preds = %19, %.lr.ph
  %20 = phi i32 [ %17, %19 ], [ %16, %.lr.ph ]
  %21 = getelementptr inbounds i8, ptr %.025, i64 8
  %.025.1 = load ptr, ptr %21, align 8, !tbaa !11
  %22 = load i32, ptr %.025.1, align 8, !tbaa !5
  %23 = icmp slt i32 %22, %20
  br i1 %23, label %24, label %25

24:                                               ; preds = %.lr.ph.1
  store i32 %20, ptr %.025.1, align 8, !tbaa !5
  store i32 %22, ptr %.01827, align 8, !tbaa !5
  br label %25

25:                                               ; preds = %24, %.lr.ph.1
  %26 = phi i32 [ %22, %24 ], [ %20, %.lr.ph.1 ]
  %27 = getelementptr inbounds i8, ptr %.025.1, i64 8
  %28 = add nuw nsw i32 %.02024, 2
  %exitcond.not.1 = icmp eq i32 %28, %2
  br i1 %exitcond.not.1, label %._crit_edge, label %.lr.ph, !llvm.loop !12

._crit_edge:                                      ; preds = %25, %.lr.ph.prol.loopexit
  %29 = load ptr, ptr %5, align 8, !tbaa !11
  %exitcond31.not = icmp eq i32 %.01926, %4
  br i1 %exitcond31.not, label %._crit_edge30, label %.lr.ph.preheader, !llvm.loop !14

._crit_edge30:                                    ; preds = %._crit_edge, %1
  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, !7, i64 0}
!6 = !{!"node", !7, i64 0, !10, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !10, i64 8}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass