; LLVM IR from div-bargali/Data-Structures-and-Algorithms :: C/Data-Structures/LinkedList (linkedlist)

; 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, i32, ptr }

@head = 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
declare dso_local i32 @length() #1

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

11:                                               ; preds = %67, %0
  %12 = load i32, ptr %1, align 4, !tbaa !5
  %13 = load i32, ptr %8, align 4, !tbaa !5
  %14 = sub nsw i32 %13, 1
  %15 = icmp slt i32 %12, %14
  br i1 %15, label %16, label %72

16:                                               ; preds = %11
  %17 = load ptr, ptr @head, align 8, !tbaa !9
  store ptr %17, ptr %6, align 8, !tbaa !9
  %18 = load ptr, ptr @head, align 8, !tbaa !9
  %19 = getelementptr inbounds %struct.node, ptr %18, i32 0, i32 2
  %20 = load ptr, ptr %19, align 8, !tbaa !11
  store ptr %20, ptr %7, align 8, !tbaa !9
  store i32 1, ptr %2, align 4, !tbaa !5
  br label %21

21:                                               ; preds = %63, %16
  %22 = load i32, ptr %2, align 4, !tbaa !5
  %23 = load i32, ptr %3, align 4, !tbaa !5
  %24 = icmp slt i32 %22, %23
  br i1 %24, label %25, label %66

25:                                               ; preds = %21
  %26 = load ptr, ptr %6, align 8, !tbaa !9
  %27 = getelementptr inbounds %struct.node, ptr %26, i32 0, i32 0
  %28 = load i32, ptr %27, align 8, !tbaa !13
  %29 = load ptr, ptr %7, align 8, !tbaa !9
  %30 = getelementptr inbounds %struct.node, ptr %29, i32 0, i32 0
  %31 = load i32, ptr %30, align 8, !tbaa !13
  %32 = icmp sgt i32 %28, %31
  br i1 %32, label %33, label %56

33:                                               ; preds = %25
  %34 = load ptr, ptr %6, align 8, !tbaa !9
  %35 = getelementptr inbounds %struct.node, ptr %34, i32 0, i32 0
  %36 = load i32, ptr %35, align 8, !tbaa !13
  store i32 %36, ptr %5, align 4, !tbaa !5
  %37 = load ptr, ptr %7, align 8, !tbaa !9
  %38 = getelementptr inbounds %struct.node, ptr %37, i32 0, i32 0
  %39 = load i32, ptr %38, align 8, !tbaa !13
  %40 = load ptr, ptr %6, align 8, !tbaa !9
  %41 = getelementptr inbounds %struct.node, ptr %40, i32 0, i32 0
  store i32 %39, ptr %41, align 8, !tbaa !13
  %42 = load i32, ptr %5, align 4, !tbaa !5
  %43 = load ptr, ptr %7, align 8, !tbaa !9
  %44 = getelementptr inbounds %struct.node, ptr %43, i32 0, i32 0
  store i32 %42, ptr %44, align 8, !tbaa !13
  %45 = load ptr, ptr %6, align 8, !tbaa !9
  %46 = getelementptr inbounds %struct.node, ptr %45, i32 0, i32 1
  %47 = load i32, ptr %46, align 4, !tbaa !14
  store i32 %47, ptr %4, align 4, !tbaa !5
  %48 = load ptr, ptr %7, align 8, !tbaa !9
  %49 = getelementptr inbounds %struct.node, ptr %48, i32 0, i32 1
  %50 = load i32, ptr %49, align 4, !tbaa !14
  %51 = load ptr, ptr %6, align 8, !tbaa !9
  %52 = getelementptr inbounds %struct.node, ptr %51, i32 0, i32 1
  store i32 %50, ptr %52, align 4, !tbaa !14
  %53 = load i32, ptr %4, align 4, !tbaa !5
  %54 = load ptr, ptr %7, align 8, !tbaa !9
  %55 = getelementptr inbounds %struct.node, ptr %54, i32 0, i32 1
  store i32 %53, ptr %55, align 4, !tbaa !14
  br label %56

56:                                               ; preds = %33, %25
  %57 = load ptr, ptr %6, align 8, !tbaa !9
  %58 = getelementptr inbounds %struct.node, ptr %57, i32 0, i32 2
  %59 = load ptr, ptr %58, align 8, !tbaa !11
  store ptr %59, ptr %6, align 8, !tbaa !9
  %60 = load ptr, ptr %7, align 8, !tbaa !9
  %61 = getelementptr inbounds %struct.node, ptr %60, i32 0, i32 2
  %62 = load ptr, ptr %61, align 8, !tbaa !11
  store ptr %62, ptr %7, align 8, !tbaa !9
  br label %63

63:                                               ; preds = %56
  %64 = load i32, ptr %2, align 4, !tbaa !5
  %65 = add nsw i32 %64, 1
  store i32 %65, ptr %2, align 4, !tbaa !5
  br label %21, !llvm.loop !15

66:                                               ; preds = %21
  br label %67

67:                                               ; preds = %66
  %68 = load i32, ptr %1, align 4, !tbaa !5
  %69 = add nsw i32 %68, 1
  store i32 %69, ptr %1, align 4, !tbaa !5
  %70 = load i32, ptr %3, align 4, !tbaa !5
  %71 = add nsw i32 %70, -1
  store i32 %71, ptr %3, align 4, !tbaa !5
  br label %11, !llvm.loop !17

72:                                               ; preds = %11
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 8, 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
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #2
  call void @llvm.lifetime.end.p0(i64 4, 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 = !{!"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 = !{!12, !10, i64 8}
!12 = !{!"node", !6, i64 0, !6, i64 4, !10, i64 8}
!13 = !{!12, !6, i64 0}
!14 = !{!12, !6, i64 4}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !16}


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

@head = external dso_local local_unnamed_addr global ptr, align 8

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

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

.lr.ph35:                                         ; preds = %0
  %3 = load ptr, ptr @head, align 8, !tbaa !5
  %.021.in27 = getelementptr inbounds i8, ptr %3, i64 8
  %.02128 = load ptr, ptr %.021.in27, align 8, !tbaa !9
  %4 = add nsw i32 %1, -2
  br label %5

5:                                                ; preds = %.lr.ph35, %._crit_edge
  %.033 = phi i32 [ 0, %.lr.ph35 ], [ %19, %._crit_edge ]
  %.02432 = phi i32 [ %1, %.lr.ph35 ], [ %20, %._crit_edge ]
  %6 = icmp sgt i32 %.02432, 1
  br i1 %6, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %5, %15
  %.02131 = phi ptr [ %.021, %15 ], [ %.02128, %5 ]
  %.02230 = phi ptr [ %17, %15 ], [ %3, %5 ]
  %.02329 = phi i32 [ %18, %15 ], [ 1, %5 ]
  %7 = load i32, ptr %.02230, align 8, !tbaa !12
  %8 = load i32, ptr %.02131, align 8, !tbaa !12
  %9 = icmp sgt i32 %7, %8
  br i1 %9, label %10, label %15

10:                                               ; preds = %.lr.ph
  store i32 %8, ptr %.02230, align 8, !tbaa !12
  store i32 %7, ptr %.02131, align 8, !tbaa !12
  %11 = getelementptr inbounds i8, ptr %.02230, i64 4
  %12 = load i32, ptr %11, align 4, !tbaa !13
  %13 = getelementptr inbounds i8, ptr %.02131, i64 4
  %14 = load i32, ptr %13, align 4, !tbaa !13
  store i32 %14, ptr %11, align 4, !tbaa !13
  store i32 %12, ptr %13, align 4, !tbaa !13
  br label %15

15:                                               ; preds = %10, %.lr.ph
  %16 = getelementptr inbounds i8, ptr %.02230, i64 8
  %17 = load ptr, ptr %16, align 8, !tbaa !9
  %18 = add nuw nsw i32 %.02329, 1
  %.021.in = getelementptr inbounds i8, ptr %.02131, i64 8
  %.021 = load ptr, ptr %.021.in, align 8, !tbaa !9
  %exitcond.not = icmp eq i32 %18, %.02432
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !14

._crit_edge:                                      ; preds = %15, %5
  %19 = add nuw nsw i32 %.033, 1
  %20 = add nsw i32 %.02432, -1
  %exitcond37.not = icmp eq i32 %.033, %4
  br i1 %exitcond37.not, label %._crit_edge36, label %5, !llvm.loop !16

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


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