; LLVM IR from lolosssss/leetcode :: c (203_remove_linked_list_elements)

; 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: nounwind uwtable
define dso_local ptr @removeElements(ptr noundef %0, i32 noundef %1) #0 {
  %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
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %9, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  store ptr null, ptr %7, align 8, !tbaa !5
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  %11 = icmp eq ptr %10, null
  br i1 %11, label %12, label %13

12:                                               ; preds = %2
  store ptr null, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %67

13:                                               ; preds = %2
  br label %14

14:                                               ; preds = %25, %13
  %15 = load ptr, ptr %6, align 8, !tbaa !5
  %16 = icmp ne ptr %15, null
  br i1 %16, label %17, label %23

17:                                               ; preds = %14
  %18 = load ptr, ptr %6, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.ListNode, ptr %18, i32 0, i32 0
  %20 = load i32, ptr %19, align 8, !tbaa !11
  %21 = load i32, ptr %5, align 4, !tbaa !9
  %22 = icmp eq i32 %20, %21
  br label %23

23:                                               ; preds = %17, %14
  %24 = phi i1 [ false, %14 ], [ %22, %17 ]
  br i1 %24, label %25, label %31

25:                                               ; preds = %23
  %26 = load ptr, ptr %6, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.ListNode, ptr %26, i32 0, i32 1
  %28 = load ptr, ptr %27, align 8, !tbaa !13
  store ptr %28, ptr %4, align 8, !tbaa !5
  %29 = load ptr, ptr %6, align 8, !tbaa !5
  call void @free(ptr noundef %29) #3
  %30 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %30, ptr %6, align 8, !tbaa !5
  br label %14, !llvm.loop !14

31:                                               ; preds = %23
  %32 = load ptr, ptr %6, align 8, !tbaa !5
  %33 = icmp eq ptr %32, null
  br i1 %33, label %34, label %35

34:                                               ; preds = %31
  store ptr null, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %67

35:                                               ; preds = %31
  %36 = load ptr, ptr %6, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.ListNode, ptr %36, i32 0, i32 1
  %38 = load ptr, ptr %37, align 8, !tbaa !13
  store ptr %38, ptr %6, align 8, !tbaa !5
  %39 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %39, ptr %7, align 8, !tbaa !5
  br label %40

40:                                               ; preds = %64, %35
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = icmp ne ptr %41, null
  br i1 %42, label %43, label %65

43:                                               ; preds = %40
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.ListNode, ptr %44, i32 0, i32 0
  %46 = load i32, ptr %45, align 8, !tbaa !11
  %47 = load i32, ptr %5, align 4, !tbaa !9
  %48 = icmp eq i32 %46, %47
  br i1 %48, label %49, label %59

49:                                               ; preds = %43
  %50 = load ptr, ptr %6, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.ListNode, ptr %50, i32 0, i32 1
  %52 = load ptr, ptr %51, align 8, !tbaa !13
  %53 = load ptr, ptr %7, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.ListNode, ptr %53, i32 0, i32 1
  store ptr %52, ptr %54, align 8, !tbaa !13
  %55 = load ptr, ptr %6, align 8, !tbaa !5
  call void @free(ptr noundef %55) #3
  %56 = load ptr, ptr %7, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.ListNode, ptr %56, i32 0, i32 1
  %58 = load ptr, ptr %57, align 8, !tbaa !13
  store ptr %58, ptr %6, align 8, !tbaa !5
  br label %64

59:                                               ; preds = %43
  %60 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %60, ptr %7, align 8, !tbaa !5
  %61 = load ptr, ptr %6, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.ListNode, ptr %61, i32 0, i32 1
  %63 = load ptr, ptr %62, align 8, !tbaa !13
  store ptr %63, ptr %6, align 8, !tbaa !5
  br label %64

64:                                               ; preds = %59, %49
  br label %40, !llvm.loop !16

65:                                               ; preds = %40
  %66 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %66, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %67

67:                                               ; preds = %65, %34, %12
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  %68 = load ptr, ptr %3, align 8
  ret ptr %68
}

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

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

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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, !10, i64 0}
!12 = !{!"ListNode", !10, i64 0, !6, i64 8}
!13 = !{!12, !6, i64 8}
!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
define dso_local noundef ptr @removeElements(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp eq ptr %0, null
  br i1 %3, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %2, %8
  %.02129 = phi ptr [ %7, %8 ], [ %0, %2 ]
  %4 = load i32, ptr %.02129, align 8, !tbaa !5
  %5 = icmp eq i32 %4, %1
  %6 = getelementptr inbounds i8, ptr %.02129, i64 8
  %7 = load ptr, ptr %6, align 8, !tbaa !11
  br i1 %5, label %8, label %.critedge

8:                                                ; preds = %.preheader
  tail call void @free(ptr noundef nonnull %.02129) #2
  %cond = icmp eq ptr %7, null
  br i1 %cond, label %.loopexit, label %.preheader, !llvm.loop !12

.critedge:                                        ; preds = %.preheader
  %.not2631 = icmp eq ptr %7, null
  br i1 %.not2631, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %.critedge, %15
  %.12233 = phi ptr [ %.122, %15 ], [ %7, %.critedge ]
  %.032 = phi ptr [ %.1, %15 ], [ %.02129, %.critedge ]
  %9 = load i32, ptr %.12233, align 8, !tbaa !5
  %10 = icmp eq i32 %9, %1
  %11 = getelementptr inbounds i8, ptr %.12233, i64 8
  br i1 %10, label %12, label %15

12:                                               ; preds = %.lr.ph
  %13 = load ptr, ptr %11, align 8, !tbaa !11
  %14 = getelementptr inbounds i8, ptr %.032, i64 8
  store ptr %13, ptr %14, align 8, !tbaa !11
  tail call void @free(ptr noundef nonnull %.12233) #2
  br label %15

15:                                               ; preds = %.lr.ph, %12
  %.2.in = phi ptr [ %14, %12 ], [ %11, %.lr.ph ]
  %.1 = phi ptr [ %.032, %12 ], [ %.12233, %.lr.ph ]
  %.122 = load ptr, ptr %.2.in, align 8, !tbaa !11
  %.not26 = icmp eq ptr %.122, null
  br i1 %.not26, label %.loopexit, label %.lr.ph, !llvm.loop !14

.loopexit:                                        ; preds = %8, %15, %.critedge, %2
  %.023 = phi ptr [ null, %2 ], [ %.02129, %.critedge ], [ %.02129, %15 ], [ null, %8 ]
  ret ptr %.023
}

; 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, !7, i64 0}
!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 = !{!6, !10, i64 8}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}


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