; LLVM IR from CodersForLife/Data-Structures-Algorithms :: Structures/LinkedList.ll (Z14remove_elementj)

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

@_ZL4head = external hidden 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
declare void @free(ptr noundef) #1

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z14remove_elementj(i32 noundef %0) #2 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  %7 = load ptr, ptr @_ZL4head, align 8, !tbaa !9
  store ptr %7, ptr %3, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %8 = load ptr, ptr @_ZL4head, align 8, !tbaa !9
  store ptr %8, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  store i32 0, ptr %5, align 4, !tbaa !5
  br label %9

9:                                                ; preds = %28, %1
  %10 = load i32, ptr %5, align 4, !tbaa !5
  %11 = load i32, ptr %2, align 4, !tbaa !5
  %12 = icmp ult i32 %10, %11
  br i1 %12, label %14, label %13

13:                                               ; preds = %9
  store i32 2, ptr %6, align 4
  br label %31

14:                                               ; preds = %9
  %15 = load ptr, ptr %3, align 8, !tbaa !9
  %16 = icmp ne ptr %15, null
  br i1 %16, label %17, label %22

17:                                               ; preds = %14
  %18 = load ptr, ptr %3, align 8, !tbaa !9
  %19 = getelementptr inbounds %struct.Node, ptr %18, i32 0, i32 1
  %20 = load ptr, ptr %19, align 8, !tbaa !11
  %21 = icmp ne ptr %20, null
  br i1 %21, label %23, label %22

22:                                               ; preds = %17, %14
  store i32 1, ptr %6, align 4
  br label %31

23:                                               ; preds = %17
  %24 = load ptr, ptr %3, align 8, !tbaa !9
  store ptr %24, ptr %4, align 8, !tbaa !9
  %25 = load ptr, ptr %3, align 8, !tbaa !9
  %26 = getelementptr inbounds %struct.Node, ptr %25, i32 0, i32 1
  %27 = load ptr, ptr %26, align 8, !tbaa !11
  store ptr %27, ptr %3, align 8, !tbaa !9
  br label %28

28:                                               ; preds = %23
  %29 = load i32, ptr %5, align 4, !tbaa !5
  %30 = add nsw i32 %29, 1
  store i32 %30, ptr %5, align 4, !tbaa !5
  br label %9, !llvm.loop !13

31:                                               ; preds = %22, %13
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  %32 = load i32, ptr %6, align 4
  switch i32 %32, label %52 [
    i32 2, label %33
  ]

33:                                               ; preds = %31
  %34 = load i32, ptr %2, align 4, !tbaa !5
  %35 = icmp eq i32 %34, 0
  br i1 %35, label %36, label %41

36:                                               ; preds = %33
  %37 = load ptr, ptr %3, align 8, !tbaa !9
  %38 = getelementptr inbounds %struct.Node, ptr %37, i32 0, i32 1
  %39 = load ptr, ptr %38, align 8, !tbaa !11
  store ptr %39, ptr @_ZL4head, align 8, !tbaa !9
  %40 = load ptr, ptr %3, align 8, !tbaa !9
  call void @free(ptr noundef %40) #3
  br label %51

41:                                               ; preds = %33
  %42 = load ptr, ptr %3, align 8, !tbaa !9
  %43 = getelementptr inbounds %struct.Node, ptr %42, i32 0, i32 1
  %44 = load ptr, ptr %43, align 8, !tbaa !11
  store ptr %44, ptr %3, align 8, !tbaa !9
  %45 = load ptr, ptr %4, align 8, !tbaa !9
  %46 = getelementptr inbounds %struct.Node, ptr %45, i32 0, i32 1
  %47 = load ptr, ptr %46, align 8, !tbaa !11
  call void @free(ptr noundef %47) #3
  %48 = load ptr, ptr %3, align 8, !tbaa !9
  %49 = load ptr, ptr %4, align 8, !tbaa !9
  %50 = getelementptr inbounds %struct.Node, ptr %49, i32 0, i32 1
  store ptr %48, ptr %50, align 8, !tbaa !11
  br label %51

51:                                               ; preds = %41, %36
  store i32 0, ptr %6, align 4
  br label %52

52:                                               ; preds = %51, %31
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  %53 = load i32, ptr %6, align 4
  switch i32 %53, label %55 [
    i32 0, label %54
    i32 1, label %54
  ]

54:                                               ; preds = %52, %52
  ret void

55:                                               ; preds = %52
  unreachable
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = { 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !10, i64 8}
!12 = !{!"_ZTS4Node", !6, i64 0, !10, i64 8}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


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

@_ZL4head = external hidden local_unnamed_addr global ptr, align 8

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z14remove_elementj(i32 noundef %0) local_unnamed_addr #1 {
  %2 = load ptr, ptr @_ZL4head, align 8, !tbaa !5
  %.not1922.not = icmp eq i32 %0, 0
  br i1 %.not1922.not, label %8, label %.lr.ph

3:                                                ; preds = %5
  %4 = add nuw i32 %.01424, 1
  %exitcond.not = icmp eq i32 %4, %0
  br i1 %exitcond.not, label %.critedge, label %.lr.ph, !llvm.loop !9

.lr.ph:                                           ; preds = %1, %3
  %.01424 = phi i32 [ %4, %3 ], [ 0, %1 ]
  %.01623 = phi ptr [ %7, %3 ], [ %2, %1 ]
  %.not = icmp eq ptr %.01623, null
  br i1 %.not, label %.loopexit, label %5

5:                                                ; preds = %.lr.ph
  %6 = getelementptr inbounds i8, ptr %.01623, i64 8
  %7 = load ptr, ptr %6, align 8, !tbaa !11
  %.not18 = icmp eq ptr %7, null
  br i1 %.not18, label %.loopexit, label %3

8:                                                ; preds = %1
  %9 = getelementptr inbounds i8, ptr %2, i64 8
  %10 = load ptr, ptr %9, align 8, !tbaa !11
  store ptr %10, ptr @_ZL4head, align 8, !tbaa !5
  tail call void @free(ptr noundef %2) #2
  br label %.loopexit

.critedge:                                        ; preds = %3
  %11 = getelementptr inbounds i8, ptr %7, i64 8
  %12 = load ptr, ptr %11, align 8, !tbaa !11
  %13 = getelementptr inbounds i8, ptr %.01623, i64 8
  %14 = load ptr, ptr %13, align 8, !tbaa !11
  tail call void @free(ptr noundef %14) #2
  store ptr %12, ptr %13, align 8, !tbaa !11
  br label %.loopexit

.loopexit:                                        ; preds = %5, %.lr.ph, %8, %.critedge
  ret void
}

attributes #0 = { 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 #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 = { 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!12, !6, i64 8}
!12 = !{!"_ZTS4Node", !13, i64 0, !6, i64 8}
!13 = !{!"int", !7, i64 0}


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