; LLVM IR from JessonYue/LeetCodeLearning :: Array/yangxin (linklist)

; 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.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: 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 ptr @removeNthFromEnd(ptr noundef %0, i32 noundef %1) #1 {
  %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 i32, align 4
  %11 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  %13 = icmp eq ptr %12, null
  br i1 %13, label %14, label %15

14:                                               ; preds = %2
  store ptr null, ptr %3, align 8
  br label %82

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

18:                                               ; preds = %15
  store ptr null, ptr %3, align 8
  br label %82

19:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %20, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %21 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %21, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 0, ptr %8, align 4, !tbaa !9
  br label %22

22:                                               ; preds = %31, %19
  %23 = load ptr, ptr %6, align 8, !tbaa !5
  %24 = icmp ne ptr %23, null
  br i1 %24, label %25, label %29

25:                                               ; preds = %22
  %26 = load i32, ptr %8, align 4, !tbaa !9
  %27 = load i32, ptr %5, align 4, !tbaa !9
  %28 = icmp slt i32 %26, %27
  br label %29

29:                                               ; preds = %25, %22
  %30 = phi i1 [ false, %22 ], [ %28, %25 ]
  br i1 %30, label %31, label %37

31:                                               ; preds = %29
  %32 = load ptr, ptr %6, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.ListNode, ptr %32, i32 0, i32 1
  %34 = load ptr, ptr %33, align 8, !tbaa !11
  store ptr %34, ptr %6, align 8, !tbaa !5
  %35 = load i32, ptr %8, align 4, !tbaa !9
  %36 = add nsw i32 %35, 1
  store i32 %36, ptr %8, align 4, !tbaa !9
  br label %22, !llvm.loop !13

37:                                               ; preds = %29
  %38 = load ptr, ptr %6, align 8, !tbaa !5
  %39 = icmp eq ptr %38, null
  br i1 %39, label %40, label %50

40:                                               ; preds = %37
  %41 = load i32, ptr %8, align 4, !tbaa !9
  %42 = load i32, ptr %5, align 4, !tbaa !9
  %43 = icmp eq i32 %41, %42
  br i1 %43, label %44, label %50

44:                                               ; preds = %40
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %45 = load ptr, ptr %4, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.ListNode, ptr %45, i32 0, i32 1
  %47 = load ptr, ptr %46, align 8, !tbaa !11
  store ptr %47, ptr %9, align 8, !tbaa !5
  %48 = load ptr, ptr %4, align 8, !tbaa !5
  call void @free(ptr noundef %48) #3
  %49 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %49, ptr %3, align 8
  store i32 1, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  br label %81

50:                                               ; preds = %40, %37
  %51 = load ptr, ptr %6, align 8, !tbaa !5
  %52 = icmp eq ptr %51, null
  br i1 %52, label %53, label %54

53:                                               ; preds = %50
  store ptr null, ptr %3, align 8
  store i32 1, ptr %10, align 4
  br label %81

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

58:                                               ; preds = %61, %54
  %59 = load ptr, ptr %6, align 8, !tbaa !5
  %60 = icmp ne ptr %59, null
  br i1 %60, label %61, label %68

61:                                               ; preds = %58
  %62 = load ptr, ptr %6, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.ListNode, ptr %62, i32 0, i32 1
  %64 = load ptr, ptr %63, align 8, !tbaa !11
  store ptr %64, ptr %6, align 8, !tbaa !5
  %65 = load ptr, ptr %7, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.ListNode, ptr %65, i32 0, i32 1
  %67 = load ptr, ptr %66, align 8, !tbaa !11
  store ptr %67, ptr %7, align 8, !tbaa !5
  br label %58, !llvm.loop !15

68:                                               ; preds = %58
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  %69 = load ptr, ptr %7, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.ListNode, ptr %69, i32 0, i32 1
  %71 = load ptr, ptr %70, align 8, !tbaa !11
  store ptr %71, ptr %11, align 8, !tbaa !5
  %72 = load ptr, ptr %7, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.ListNode, ptr %72, i32 0, i32 1
  %74 = load ptr, ptr %73, align 8, !tbaa !11
  %75 = getelementptr inbounds %struct.ListNode, ptr %74, i32 0, i32 1
  %76 = load ptr, ptr %75, align 8, !tbaa !11
  %77 = load ptr, ptr %7, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.ListNode, ptr %77, i32 0, i32 1
  store ptr %76, ptr %78, align 8, !tbaa !11
  %79 = load ptr, ptr %11, align 8, !tbaa !5
  call void @free(ptr noundef %79) #3
  %80 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %80, ptr %3, align 8
  store i32 1, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  br label %81

81:                                               ; preds = %68, %53, %44
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  br label %82

82:                                               ; preds = %81, %18, %14
  %83 = load ptr, ptr %3, align 8
  ret ptr %83
}

; Function Attrs: nounwind
declare void @free(ptr noundef) #2

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 "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 = { 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 = !{!"ListNode", !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: nounwind uwtable
define dso_local ptr @removeNthFromEnd(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp eq ptr %0, null
  %4 = icmp slt i32 %1, 1
  %or.cond31 = or i1 %3, %4
  br i1 %or.cond31, label %24, label %.preheader32

.preheader32:                                     ; preds = %2, %.preheader32
  %.02534 = phi ptr [ %6, %.preheader32 ], [ %0, %2 ]
  %.02733 = phi i32 [ %7, %.preheader32 ], [ 0, %2 ]
  %5 = getelementptr inbounds i8, ptr %.02534, i64 8
  %6 = load ptr, ptr %5, align 8, !tbaa !5
  %7 = add nuw nsw i32 %.02733, 1
  %8 = icmp ne ptr %6, null
  %9 = icmp slt i32 %7, %1
  %10 = select i1 %8, i1 %9, i1 false
  br i1 %10, label %.preheader32, label %11, !llvm.loop !11

11:                                               ; preds = %.preheader32
  %12 = icmp eq ptr %6, null
  %13 = icmp eq i32 %7, %1
  %or.cond = select i1 %12, i1 %13, i1 false
  br i1 %or.cond, label %14, label %17

14:                                               ; preds = %11
  %15 = getelementptr inbounds i8, ptr %0, i64 8
  %16 = load ptr, ptr %15, align 8, !tbaa !5
  br label %.sink.split

17:                                               ; preds = %11
  br i1 %12, label %24, label %.preheader

.preheader:                                       ; preds = %17
  %.126.in35 = getelementptr inbounds i8, ptr %6, i64 8
  %.12636 = load ptr, ptr %.126.in35, align 8, !tbaa !5
  %.not37 = icmp eq ptr %.12636, null
  br i1 %.not37, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %.12639 = phi ptr [ %.126, %.lr.ph ], [ %.12636, %.preheader ]
  %.02838 = phi ptr [ %19, %.lr.ph ], [ %0, %.preheader ]
  %18 = getelementptr inbounds i8, ptr %.02838, i64 8
  %19 = load ptr, ptr %18, align 8, !tbaa !5
  %.126.in = getelementptr inbounds i8, ptr %.12639, i64 8
  %.126 = load ptr, ptr %.126.in, align 8, !tbaa !5
  %.not = icmp eq ptr %.126, null
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !13

._crit_edge:                                      ; preds = %.lr.ph, %.preheader
  %.028.lcssa = phi ptr [ %0, %.preheader ], [ %19, %.lr.ph ]
  %20 = getelementptr inbounds i8, ptr %.028.lcssa, i64 8
  %21 = load ptr, ptr %20, align 8, !tbaa !5
  %22 = getelementptr inbounds i8, ptr %21, i64 8
  %23 = load ptr, ptr %22, align 8, !tbaa !5
  store ptr %23, ptr %20, align 8, !tbaa !5
  br label %.sink.split

.sink.split:                                      ; preds = %._crit_edge, %14
  %.sink = phi ptr [ %0, %14 ], [ %21, %._crit_edge ]
  %.1.ph = phi ptr [ %16, %14 ], [ %0, %._crit_edge ]
  tail call void @free(ptr noundef %.sink) #2
  br label %24

24:                                               ; preds = %.sink.split, %17, %2
  %.1 = phi ptr [ null, %2 ], [ null, %17 ], [ %.1.ph, %.sink.split ]
  ret ptr %.1
}

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #1

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" }
attributes #1 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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, !10, i64 8}
!6 = !{!"ListNode", !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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


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