; LLVM IR from mahendramahara/DSA-in-C-Programing :: LINKED-LIST (DeleteAtAnyPositionLinkedList)

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

@head = external dso_local global ptr, align 8
@.str = external hidden unnamed_addr constant [43 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [30 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [31 x i8], align 1

; 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 @deleteFromPosition(i32 noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  %8 = load i32, ptr %2, align 4, !tbaa !5
  %9 = icmp slt i32 %8, 1
  br i1 %9, label %13, label %10

10:                                               ; preds = %1
  %11 = load ptr, ptr @head, align 8, !tbaa !9
  %12 = icmp eq ptr %11, null
  br i1 %12, label %13, label %15

13:                                               ; preds = %10, %1
  %14 = call i32 (ptr, ...) @printf(ptr noundef @.str)
  br label %62

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

18:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  %19 = load ptr, ptr @head, align 8, !tbaa !9
  store ptr %19, ptr %3, align 8, !tbaa !9
  %20 = load ptr, ptr @head, align 8, !tbaa !9
  %21 = getelementptr inbounds %struct.Node, ptr %20, i32 0, i32 1
  %22 = load ptr, ptr %21, align 8, !tbaa !11
  store ptr %22, ptr @head, align 8, !tbaa !9
  %23 = load ptr, ptr %3, align 8, !tbaa !9
  call void @free(ptr noundef %23) #4
  %24 = load i32, ptr %2, align 4, !tbaa !5
  %25 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %24)
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  br label %62

26:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  %27 = load ptr, ptr @head, align 8, !tbaa !9
  store ptr %27, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  store ptr null, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  store i32 1, ptr %6, align 4, !tbaa !5
  br label %28

28:                                               ; preds = %37, %26
  %29 = load i32, ptr %6, align 4, !tbaa !5
  %30 = load i32, ptr %2, align 4, !tbaa !5
  %31 = icmp slt i32 %29, %30
  br i1 %31, label %32, label %35

32:                                               ; preds = %28
  %33 = load ptr, ptr %4, align 8, !tbaa !9
  %34 = icmp ne ptr %33, null
  br label %35

35:                                               ; preds = %32, %28
  %36 = phi i1 [ false, %28 ], [ %34, %32 ]
  br i1 %36, label %37, label %44

37:                                               ; preds = %35
  %38 = load ptr, ptr %4, align 8, !tbaa !9
  store ptr %38, ptr %5, align 8, !tbaa !9
  %39 = load ptr, ptr %4, align 8, !tbaa !9
  %40 = getelementptr inbounds %struct.Node, ptr %39, i32 0, i32 1
  %41 = load ptr, ptr %40, align 8, !tbaa !11
  store ptr %41, ptr %4, align 8, !tbaa !9
  %42 = load i32, ptr %6, align 4, !tbaa !5
  %43 = add nsw i32 %42, 1
  store i32 %43, ptr %6, align 4, !tbaa !5
  br label %28, !llvm.loop !13

44:                                               ; preds = %35
  %45 = load i32, ptr %6, align 4, !tbaa !5
  %46 = load i32, ptr %2, align 4, !tbaa !5
  %47 = icmp ne i32 %45, %46
  br i1 %47, label %48, label %51

48:                                               ; preds = %44
  %49 = load i32, ptr %2, align 4, !tbaa !5
  %50 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i32 noundef %49)
  store i32 1, ptr %7, align 4
  br label %60

51:                                               ; preds = %44
  %52 = load ptr, ptr %4, align 8, !tbaa !9
  %53 = getelementptr inbounds %struct.Node, ptr %52, i32 0, i32 1
  %54 = load ptr, ptr %53, align 8, !tbaa !11
  %55 = load ptr, ptr %5, align 8, !tbaa !9
  %56 = getelementptr inbounds %struct.Node, ptr %55, i32 0, i32 1
  store ptr %54, ptr %56, align 8, !tbaa !11
  %57 = load ptr, ptr %4, align 8, !tbaa !9
  call void @free(ptr noundef %57) #4
  %58 = load i32, ptr %2, align 4, !tbaa !5
  %59 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %58)
  store i32 0, ptr %7, align 4
  br label %60

60:                                               ; preds = %51, %48
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  %61 = load i32, ptr %7, align 4
  switch i32 %61, label %63 [
    i32 0, label %62
    i32 1, label %62
  ]

62:                                               ; preds = %60, %60, %18, %13
  ret void

63:                                               ; preds = %60
  unreachable
}

declare i32 @printf(ptr noundef, ...) #2

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

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 = { "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 "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 = { 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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !10, i64 8}
!12 = !{!"Node", !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"

@head = external dso_local local_unnamed_addr global ptr, align 8
@.str = external hidden unnamed_addr constant [43 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [30 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [31 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local void @deleteFromPosition(i32 noundef %0) local_unnamed_addr #0 {
  %2 = icmp slt i32 %0, 1
  %3 = load ptr, ptr @head, align 8
  %4 = icmp eq ptr %3, null
  %or.cond = select i1 %2, i1 true, i1 %4
  br i1 %or.cond, label %5, label %7

5:                                                ; preds = %1
  %6 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str)
  br label %26

7:                                                ; preds = %1
  %8 = icmp eq i32 %0, 1
  br i1 %8, label %9, label %.lr.ph

9:                                                ; preds = %7
  %10 = getelementptr inbounds i8, ptr %3, i64 8
  %11 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %11, ptr @head, align 8, !tbaa !11
  tail call void @free(ptr noundef nonnull %3) #3
  %12 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef 1)
  br label %26

.lr.ph:                                           ; preds = %7, %.lr.ph
  %.022 = phi i32 [ %15, %.lr.ph ], [ 1, %7 ]
  %.01821 = phi ptr [ %14, %.lr.ph ], [ %3, %7 ]
  %13 = getelementptr inbounds i8, ptr %.01821, i64 8
  %14 = load ptr, ptr %13, align 8, !tbaa !5
  %15 = add nuw nsw i32 %.022, 1
  %16 = icmp slt i32 %15, %0
  %17 = icmp ne ptr %14, null
  %18 = select i1 %16, i1 %17, i1 false
  br i1 %18, label %.lr.ph, label %._crit_edge, !llvm.loop !12

._crit_edge:                                      ; preds = %.lr.ph
  %.not = icmp eq i32 %15, %0
  br i1 %.not, label %21, label %19

19:                                               ; preds = %._crit_edge
  %20 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %0)
  br label %26

21:                                               ; preds = %._crit_edge
  %22 = getelementptr inbounds i8, ptr %14, i64 8
  %23 = load ptr, ptr %22, align 8, !tbaa !5
  %24 = getelementptr inbounds i8, ptr %.01821, i64 8
  store ptr %23, ptr %24, align 8, !tbaa !5
  tail call void @free(ptr noundef %14) #3
  %25 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %0)
  br label %26

26:                                               ; preds = %19, %21, %9, %5
  ret void
}

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

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

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


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