; LLVM IR from codemistic/Data-Structures-and-Algorithms :: CPP/LINKED LIST/SINGLY LINKED LIST/leetcode25.ll (Z7reverseP8ListNodei)

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

%class.ListNode = 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: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znwm(i64 noundef) #1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN8ListNodeC2Ei(ptr noundef nonnull align 8 dereferenceable(16), i32 noundef) unnamed_addr #2 align 2

declare i32 @__gxx_personality_v0(...)

; Function Attrs: nobuiltin nounwind
declare void @_ZdlPvm(ptr noundef, i64 noundef) #3

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z6lengthP8ListNode(ptr noundef) #2

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_Z7reverseP8ListNodei(ptr noundef %0, i32 noundef %1) #4 personality ptr @__gxx_personality_v0 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = icmp eq ptr %14, null
  br i1 %15, label %19, label %16

16:                                               ; preds = %2
  %17 = load i32, ptr %5, align 4, !tbaa !9
  %18 = icmp eq i32 %17, 1
  br i1 %18, label %19, label %21

19:                                               ; preds = %16, %2
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %20, ptr %3, align 8
  br label %79

21:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  %22 = call noalias noundef nonnull ptr @_Znwm(i64 noundef 16) #6
  invoke void @_ZN8ListNodeC2Ei(ptr noundef nonnull align 8 dereferenceable(16) %22, i32 noundef -1)
          to label %23 unwind label %46

23:                                               ; preds = %21
  store ptr %22, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #5
  %24 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %24, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #5
  %25 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %25, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #5
  %26 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %26, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #5
  %27 = load ptr, ptr %4, align 8, !tbaa !5
  %28 = call noundef i32 @_Z6lengthP8ListNode(ptr noundef %27)
  store i32 %28, ptr %12, align 4, !tbaa !9
  br label %29

29:                                               ; preds = %70, %23
  %30 = load i32, ptr %12, align 4, !tbaa !9
  %31 = load i32, ptr %5, align 4, !tbaa !9
  %32 = icmp sge i32 %30, %31
  br i1 %32, label %33, label %75

33:                                               ; preds = %29
  %34 = load ptr, ptr %9, align 8, !tbaa !5
  %35 = getelementptr inbounds %class.ListNode, ptr %34, i32 0, i32 1
  %36 = load ptr, ptr %35, align 8, !tbaa !11
  store ptr %36, ptr %10, align 8, !tbaa !5
  %37 = load ptr, ptr %10, align 8, !tbaa !5
  %38 = getelementptr inbounds %class.ListNode, ptr %37, i32 0, i32 1
  %39 = load ptr, ptr %38, align 8, !tbaa !11
  store ptr %39, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #5
  store i32 0, ptr %13, align 4, !tbaa !9
  br label %40

40:                                               ; preds = %67, %33
  %41 = load i32, ptr %13, align 4, !tbaa !9
  %42 = load i32, ptr %5, align 4, !tbaa !9
  %43 = sub nsw i32 %42, 1
  %44 = icmp slt i32 %41, %43
  br i1 %44, label %50, label %45

45:                                               ; preds = %40
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #5
  br label %70

46:                                               ; preds = %21
  %47 = landingpad { ptr, i32 }
          cleanup
  %48 = extractvalue { ptr, i32 } %47, 0
  store ptr %48, ptr %7, align 8
  %49 = extractvalue { ptr, i32 } %47, 1
  store i32 %49, ptr %8, align 4
  call void @_ZdlPvm(ptr noundef %22, i64 noundef 16) #7
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  br label %81

50:                                               ; preds = %40
  %51 = load ptr, ptr %11, align 8, !tbaa !5
  %52 = getelementptr inbounds %class.ListNode, ptr %51, i32 0, i32 1
  %53 = load ptr, ptr %52, align 8, !tbaa !11
  %54 = load ptr, ptr %10, align 8, !tbaa !5
  %55 = getelementptr inbounds %class.ListNode, ptr %54, i32 0, i32 1
  store ptr %53, ptr %55, align 8, !tbaa !11
  %56 = load ptr, ptr %9, align 8, !tbaa !5
  %57 = getelementptr inbounds %class.ListNode, ptr %56, i32 0, i32 1
  %58 = load ptr, ptr %57, align 8, !tbaa !11
  %59 = load ptr, ptr %11, align 8, !tbaa !5
  %60 = getelementptr inbounds %class.ListNode, ptr %59, i32 0, i32 1
  store ptr %58, ptr %60, align 8, !tbaa !11
  %61 = load ptr, ptr %11, align 8, !tbaa !5
  %62 = load ptr, ptr %9, align 8, !tbaa !5
  %63 = getelementptr inbounds %class.ListNode, ptr %62, i32 0, i32 1
  store ptr %61, ptr %63, align 8, !tbaa !11
  %64 = load ptr, ptr %10, align 8, !tbaa !5
  %65 = getelementptr inbounds %class.ListNode, ptr %64, i32 0, i32 1
  %66 = load ptr, ptr %65, align 8, !tbaa !11
  store ptr %66, ptr %11, align 8, !tbaa !5
  br label %67

67:                                               ; preds = %50
  %68 = load i32, ptr %13, align 4, !tbaa !9
  %69 = add nsw i32 %68, 1
  store i32 %69, ptr %13, align 4, !tbaa !9
  br label %40, !llvm.loop !13

70:                                               ; preds = %45
  %71 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %71, ptr %9, align 8, !tbaa !5
  %72 = load i32, ptr %12, align 4, !tbaa !9
  %73 = load i32, ptr %5, align 4, !tbaa !9
  %74 = sub nsw i32 %72, %73
  store i32 %74, ptr %12, align 4, !tbaa !9
  br label %29, !llvm.loop !15

75:                                               ; preds = %29
  %76 = load ptr, ptr %6, align 8, !tbaa !5
  %77 = getelementptr inbounds %class.ListNode, ptr %76, i32 0, i32 1
  %78 = load ptr, ptr %77, align 8, !tbaa !11
  store ptr %78, ptr %3, align 8
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  br label %79

79:                                               ; preds = %75, %19
  %80 = load ptr, ptr %3, align 8
  ret ptr %80

81:                                               ; preds = %46
  %82 = load ptr, ptr %7, align 8
  %83 = load i32, ptr %8, align 4
  %84 = insertvalue { ptr, i32 } poison, ptr %82, 0
  %85 = insertvalue { ptr, i32 } %84, i32 %83, 1
  resume { ptr, i32 } %85
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nobuiltin 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 #2 = { mustprogress 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 #3 = { nobuiltin 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 #4 = { mustprogress 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 #5 = { nounwind }
attributes #6 = { builtin allocsize(0) }
attributes #7 = { builtin 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 8}
!12 = !{!"_ZTS8ListNode", !10, i64 0, !6, i64 8}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !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"

; Function Attrs: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znwm(i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN8ListNodeC2Ei(ptr noundef nonnull align 8 dereferenceable(16), i32 noundef) unnamed_addr #1 align 2

declare i32 @__gxx_personality_v0(...)

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

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_Z7reverseP8ListNodei(ptr noundef %0, i32 noundef %1) local_unnamed_addr #2 personality ptr @__gxx_personality_v0 {
  %3 = icmp eq ptr %0, null
  %4 = icmp eq i32 %1, 1
  %or.cond = or i1 %3, %4
  br i1 %or.cond, label %26, label %5

5:                                                ; preds = %2
  %6 = tail call noalias noundef nonnull dereferenceable(16) ptr @_Znwm(i64 noundef 16) #3
  tail call void @_ZN8ListNodeC2Ei(ptr noundef nonnull align 8 dereferenceable(16) %6, i32 noundef -1)
  %7 = tail call noundef i32 @_Z6lengthP8ListNode(ptr noundef nonnull %0)
  %.not33 = icmp sge i32 %7, %1
  %8 = icmp sgt i32 %1, 1
  %or.cond40 = and i1 %.not33, %8
  br i1 %or.cond40, label %.lr.ph.us.preheader, label %._crit_edge38

.lr.ph.us.preheader:                              ; preds = %5
  %9 = add nsw i32 %1, -1
  %xtraiter = and i32 %9, 1
  %10 = icmp eq i32 %1, 2
  %unroll_iter = and i32 %9, -2
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br label %.lr.ph.us

.lr.ph.us:                                        ; preds = %.lr.ph.us.preheader, %._crit_edge.us
  %.02635.us = phi i32 [ %23, %._crit_edge.us ], [ %7, %.lr.ph.us.preheader ]
  %.02834.us = phi ptr [ %12, %._crit_edge.us ], [ %6, %.lr.ph.us.preheader ]
  %11 = getelementptr inbounds i8, ptr %.02834.us, i64 8
  %12 = load ptr, ptr %11, align 8, !tbaa !5
  %13 = getelementptr inbounds i8, ptr %12, i64 8
  br i1 %10, label %._crit_edge.us.unr-lcssa, label %.lr.ph.us.new

.lr.ph.us.new:                                    ; preds = %.lr.ph.us, %.lr.ph.us.new
  %niter = phi i32 [ %niter.next.1, %.lr.ph.us.new ], [ 0, %.lr.ph.us ]
  %.027.us = load ptr, ptr %13, align 8, !tbaa !5
  %14 = getelementptr inbounds i8, ptr %.027.us, i64 8
  %15 = load ptr, ptr %14, align 8, !tbaa !5
  store ptr %15, ptr %13, align 8, !tbaa !5
  %16 = load ptr, ptr %11, align 8, !tbaa !5
  store ptr %16, ptr %14, align 8, !tbaa !5
  store ptr %.027.us, ptr %11, align 8, !tbaa !5
  %.027.us.1 = load ptr, ptr %13, align 8, !tbaa !5
  %17 = getelementptr inbounds i8, ptr %.027.us.1, i64 8
  %18 = load ptr, ptr %17, align 8, !tbaa !5
  store ptr %18, ptr %13, align 8, !tbaa !5
  %19 = load ptr, ptr %11, align 8, !tbaa !5
  store ptr %19, ptr %17, align 8, !tbaa !5
  store ptr %.027.us.1, ptr %11, align 8, !tbaa !5
  %niter.next.1 = add i32 %niter, 2
  %niter.ncmp.1 = icmp eq i32 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.us.unr-lcssa, label %.lr.ph.us.new, !llvm.loop !11

._crit_edge.us.unr-lcssa:                         ; preds = %.lr.ph.us.new, %.lr.ph.us
  br i1 %lcmp.mod.not, label %._crit_edge.us, label %._crit_edge.us.epilog-lcssa

._crit_edge.us.epilog-lcssa:                      ; preds = %._crit_edge.us.unr-lcssa
  %.027.us.epil = load ptr, ptr %13, align 8, !tbaa !5
  %20 = getelementptr inbounds i8, ptr %.027.us.epil, i64 8
  %21 = load ptr, ptr %20, align 8, !tbaa !5
  store ptr %21, ptr %13, align 8, !tbaa !5
  %22 = load ptr, ptr %11, align 8, !tbaa !5
  store ptr %22, ptr %20, align 8, !tbaa !5
  store ptr %.027.us.epil, ptr %11, align 8, !tbaa !5
  br label %._crit_edge.us

._crit_edge.us:                                   ; preds = %._crit_edge.us.unr-lcssa, %._crit_edge.us.epilog-lcssa
  %23 = sub nsw i32 %.02635.us, %1
  %.not.us = icmp slt i32 %23, %1
  br i1 %.not.us, label %._crit_edge38, label %.lr.ph.us, !llvm.loop !13

._crit_edge38:                                    ; preds = %._crit_edge.us, %5
  %24 = getelementptr inbounds i8, ptr %6, i64 8
  %25 = load ptr, ptr %24, align 8, !tbaa !5
  br label %26

26:                                               ; preds = %2, %._crit_edge38
  %.029 = phi ptr [ %25, %._crit_edge38 ], [ %0, %2 ]
  ret ptr %.029
}

attributes #0 = { nobuiltin 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 #1 = { mustprogress 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 = { mustprogress 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 #3 = { builtin 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, !10, i64 8}
!6 = !{!"_ZTS8ListNode", !7, i64 0, !10, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


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