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

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

@start = external dso_local global ptr, align 8
@count = external dso_local global i32, align 4
@currptr = external dso_local global ptr, align 8
@lastptr = 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 @reverse() #1 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #2
  %4 = load ptr, ptr @start, align 8, !tbaa !5
  store ptr %4, ptr @currptr, align 8, !tbaa !5
  %5 = load ptr, ptr @start, align 8, !tbaa !5
  store ptr %5, ptr @lastptr, align 8, !tbaa !5
  br label %6

6:                                                ; preds = %11, %0
  %7 = load ptr, ptr @lastptr, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.node, ptr %7, i32 0, i32 1
  %9 = load ptr, ptr %8, align 8, !tbaa !9
  %10 = icmp ne ptr %9, null
  br i1 %10, label %11, label %17

11:                                               ; preds = %6
  %12 = load ptr, ptr @lastptr, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.node, ptr %12, i32 0, i32 1
  %14 = load ptr, ptr %13, align 8, !tbaa !9
  store ptr %14, ptr @lastptr, align 8, !tbaa !5
  %15 = load i32, ptr @count, align 4, !tbaa !12
  %16 = add nsw i32 %15, 1
  store i32 %16, ptr @count, align 4, !tbaa !12
  br label %6, !llvm.loop !13

17:                                               ; preds = %6
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  store i32 0, ptr %2, align 4, !tbaa !12
  br label %18

18:                                               ; preds = %55, %17
  %19 = load i32, ptr %2, align 4, !tbaa !12
  %20 = load i32, ptr @count, align 4, !tbaa !12
  %21 = sub nsw i32 %20, 1
  %22 = icmp slt i32 %19, %21
  br i1 %22, label %23, label %58

23:                                               ; preds = %18
  store i32 0, ptr %3, align 4, !tbaa !12
  br label %24

24:                                               ; preds = %50, %23
  %25 = load i32, ptr %3, align 4, !tbaa !12
  %26 = load i32, ptr @count, align 4, !tbaa !12
  %27 = load i32, ptr %2, align 4, !tbaa !12
  %28 = sub nsw i32 %26, %27
  %29 = sub nsw i32 %28, 1
  %30 = icmp slt i32 %25, %29
  br i1 %30, label %31, label %53

31:                                               ; preds = %24
  %32 = load ptr, ptr @currptr, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.node, ptr %32, i32 0, i32 0
  %34 = load i32, ptr %33, align 8, !tbaa !15
  store i32 %34, ptr %1, align 4, !tbaa !12
  %35 = load ptr, ptr @currptr, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.node, ptr %35, i32 0, i32 1
  %37 = load ptr, ptr %36, align 8, !tbaa !9
  %38 = getelementptr inbounds %struct.node, ptr %37, i32 0, i32 0
  %39 = load i32, ptr %38, align 8, !tbaa !15
  %40 = load ptr, ptr @currptr, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.node, ptr %40, i32 0, i32 0
  store i32 %39, ptr %41, align 8, !tbaa !15
  %42 = load i32, ptr %1, align 4, !tbaa !12
  %43 = load ptr, ptr @currptr, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.node, ptr %43, i32 0, i32 1
  %45 = load ptr, ptr %44, align 8, !tbaa !9
  %46 = getelementptr inbounds %struct.node, ptr %45, i32 0, i32 0
  store i32 %42, ptr %46, align 8, !tbaa !15
  %47 = load ptr, ptr @currptr, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.node, ptr %47, i32 0, i32 1
  %49 = load ptr, ptr %48, align 8, !tbaa !9
  store ptr %49, ptr @currptr, align 8, !tbaa !5
  br label %50

50:                                               ; preds = %31
  %51 = load i32, ptr %3, align 4, !tbaa !12
  %52 = add nsw i32 %51, 1
  store i32 %52, ptr %3, align 4, !tbaa !12
  br label %24, !llvm.loop !16

53:                                               ; preds = %24
  %54 = load ptr, ptr @start, align 8, !tbaa !5
  store ptr %54, ptr @currptr, align 8, !tbaa !5
  br label %55

55:                                               ; preds = %53
  %56 = load i32, ptr %2, align 4, !tbaa !12
  %57 = add nsw i32 %56, 1
  store i32 %57, ptr %2, align 4, !tbaa !12
  br label %18, !llvm.loop !17

58:                                               ; preds = %18
  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 = !{!"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, !6, i64 8}
!11 = !{!"int", !7, i64 0}
!12 = !{!11, !11, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!10, !11, i64 0}
!16 = distinct !{!16, !14}
!17 = distinct !{!17, !14}


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

@start = external dso_local local_unnamed_addr global ptr, align 8
@count = external dso_local local_unnamed_addr global i32, align 4
@currptr = external dso_local local_unnamed_addr global ptr, align 8
@lastptr = 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 @reverse() local_unnamed_addr #0 {
  %1 = load ptr, ptr @start, align 8, !tbaa !5
  store ptr %1, ptr @currptr, align 8, !tbaa !5
  store ptr %1, ptr @lastptr, align 8, !tbaa !5
  %count.promoted = load i32, ptr @count, align 4, !tbaa !9
  %2 = getelementptr inbounds i8, ptr %1, i64 8
  %3 = load ptr, ptr %2, align 8, !tbaa !11
  %.not9 = icmp eq ptr %3, null
  br i1 %.not9, label %.preheader8, label %.lr.ph

..preheader8_crit_edge:                           ; preds = %.lr.ph
  store ptr %6, ptr @lastptr, align 8, !tbaa !5
  store i32 %8, ptr @count, align 4, !tbaa !9
  br label %.preheader8

.preheader8:                                      ; preds = %..preheader8_crit_edge, %0
  %4 = phi i32 [ %8, %..preheader8_crit_edge ], [ %count.promoted, %0 ]
  %5 = icmp sgt i32 %4, 1
  br i1 %5, label %.preheader, label %27

.lr.ph:                                           ; preds = %0, %.lr.ph
  %6 = phi ptr [ %10, %.lr.ph ], [ %3, %0 ]
  %7 = phi i32 [ %8, %.lr.ph ], [ %count.promoted, %0 ]
  %8 = add nsw i32 %7, 1
  %9 = getelementptr inbounds i8, ptr %6, i64 8
  %10 = load ptr, ptr %9, align 8, !tbaa !11
  %.not = icmp eq ptr %10, null
  br i1 %.not, label %..preheader8_crit_edge, label %.lr.ph, !llvm.loop !13

.preheader:                                       ; preds = %.preheader8, %._crit_edge
  %11 = phi i32 [ %23, %._crit_edge ], [ %4, %.preheader8 ]
  %.0617 = phi i32 [ %24, %._crit_edge ], [ 0, %.preheader8 ]
  %12 = xor i32 %.0617, -1
  %13 = sub i32 %.0617, %11
  %14 = icmp slt i32 %13, -1
  br i1 %14, label %.lr.ph12.preheader, label %._crit_edge

.lr.ph12.preheader:                               ; preds = %.preheader
  %.pre = load i32, ptr %1, align 8, !tbaa !15
  br label %.lr.ph12

.lr.ph12:                                         ; preds = %.lr.ph12.preheader, %.lr.ph12
  %.011 = phi i32 [ %19, %.lr.ph12 ], [ 0, %.lr.ph12.preheader ]
  %15 = phi ptr [ %17, %.lr.ph12 ], [ %1, %.lr.ph12.preheader ]
  %16 = getelementptr inbounds i8, ptr %15, i64 8
  %17 = load ptr, ptr %16, align 8, !tbaa !11
  %18 = load i32, ptr %17, align 8, !tbaa !15
  store i32 %18, ptr %15, align 8, !tbaa !15
  store i32 %.pre, ptr %17, align 8, !tbaa !15
  %19 = add nuw nsw i32 %.011, 1
  %20 = load i32, ptr @count, align 4, !tbaa !9
  %21 = add i32 %20, %12
  %22 = icmp slt i32 %19, %21
  br i1 %22, label %.lr.ph12, label %._crit_edge, !llvm.loop !16

._crit_edge:                                      ; preds = %.lr.ph12, %.preheader
  %23 = phi i32 [ %11, %.preheader ], [ %20, %.lr.ph12 ]
  %24 = add nuw nsw i32 %.0617, 1
  %25 = add nsw i32 %23, -1
  %26 = icmp slt i32 %24, %25
  br i1 %26, label %.preheader, label %._crit_edge18, !llvm.loop !17

._crit_edge18:                                    ; preds = %._crit_edge
  store ptr %1, ptr @currptr, align 8, !tbaa !5
  br label %27

27:                                               ; preds = %._crit_edge18, %.preheader8
  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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 8}
!12 = !{!"node", !10, i64 0, !6, i64 8}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!12, !10, i64 0}
!16 = distinct !{!16, !14}
!17 = distinct !{!17, !14}


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