; LLVM IR from MostafaTwfiq/C-DataStructures-And-Algorithms :: DataStructure/LinkedLists/Sources (LinkedList)

; 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.LinkedList = type { ptr, ptr, i32, ptr, ptr }
%struct.Node = type { ptr, ptr }

@stderr = external global ptr, align 8
@.str.2 = external hidden unnamed_addr constant [27 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [31 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [20 x i8], align 1
@.str.9 = external hidden unnamed_addr constant [42 x i8], align 1
@.str.11 = external hidden unnamed_addr constant [33 x i8], align 1

; Function Attrs: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #0

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) #1

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

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @linkedListIsEmpty(ptr noundef) #3

; Function Attrs: nounwind uwtable
declare dso_local i32 @linkedListGetLength(ptr noundef) #3

; Function Attrs: nounwind uwtable
declare dso_local void @linkedListDeleteFirst(ptr noundef) #3

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

; Function Attrs: nounwind uwtable
declare dso_local void @linkedListDeleteLast(ptr noundef) #3

; Function Attrs: nounwind uwtable
define dso_local void @linkedListDeleteAtIndex(ptr noundef %0, i32 noundef %1) #3 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = icmp eq ptr %8, null
  br i1 %9, label %10, label %13

10:                                               ; preds = %2
  %11 = load ptr, ptr @stderr, align 8, !tbaa !5
  %12 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %11, ptr noundef @.str.5, ptr noundef @.str.6, ptr noundef @.str.2) #4
  call void @exit(i32 noundef -83386157) #5
  unreachable

13:                                               ; preds = %2
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  %15 = call i32 @linkedListIsEmpty(ptr noundef %14)
  %16 = icmp ne i32 %15, 0
  br i1 %16, label %17, label %20

17:                                               ; preds = %13
  %18 = load ptr, ptr @stderr, align 8, !tbaa !5
  %19 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %18, ptr noundef @.str.11, ptr noundef @.str.2) #4
  call void @exit(i32 noundef -833816740) #5
  unreachable

20:                                               ; preds = %13
  %21 = load i32, ptr %4, align 4, !tbaa !9
  %22 = icmp slt i32 %21, 0
  br i1 %22, label %28, label %23

23:                                               ; preds = %20
  %24 = load i32, ptr %4, align 4, !tbaa !9
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  %26 = call i32 @linkedListGetLength(ptr noundef %25)
  %27 = icmp sge i32 %24, %26
  br i1 %27, label %28, label %31

28:                                               ; preds = %23, %20
  %29 = load ptr, ptr @stderr, align 8, !tbaa !5
  %30 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %29, ptr noundef @.str.9, ptr noundef @.str.2) #4
  call void @exit(i32 noundef -83385991) #5
  unreachable

31:                                               ; preds = %23
  br label %32

32:                                               ; preds = %31
  br label %33

33:                                               ; preds = %32
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.LinkedList, ptr %34, i32 0, i32 0
  %36 = load ptr, ptr %35, align 8, !tbaa !11
  store ptr %36, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  store ptr null, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %37

37:                                               ; preds = %47, %33
  %38 = load i32, ptr %7, align 4, !tbaa !9
  %39 = load i32, ptr %4, align 4, !tbaa !9
  %40 = icmp slt i32 %38, %39
  br i1 %40, label %42, label %41

41:                                               ; preds = %37
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  br label %50

42:                                               ; preds = %37
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %43, ptr %6, align 8, !tbaa !5
  %44 = load ptr, ptr %5, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.Node, ptr %44, i32 0, i32 1
  %46 = load ptr, ptr %45, align 8, !tbaa !13
  store ptr %46, ptr %5, align 8, !tbaa !5
  br label %47

47:                                               ; preds = %42
  %48 = load i32, ptr %7, align 4, !tbaa !9
  %49 = add nsw i32 %48, 1
  store i32 %49, ptr %7, align 4, !tbaa !9
  br label %37, !llvm.loop !15

50:                                               ; preds = %41
  %51 = load ptr, ptr %5, align 8, !tbaa !5
  %52 = load ptr, ptr %3, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.LinkedList, ptr %52, i32 0, i32 0
  %54 = load ptr, ptr %53, align 8, !tbaa !11
  %55 = icmp eq ptr %51, %54
  br i1 %55, label %56, label %58

56:                                               ; preds = %50
  %57 = load ptr, ptr %3, align 8, !tbaa !5
  call void @linkedListDeleteFirst(ptr noundef %57)
  br label %84

58:                                               ; preds = %50
  %59 = load ptr, ptr %5, align 8, !tbaa !5
  %60 = load ptr, ptr %3, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.LinkedList, ptr %60, i32 0, i32 1
  %62 = load ptr, ptr %61, align 8, !tbaa !17
  %63 = icmp eq ptr %59, %62
  br i1 %63, label %64, label %66

64:                                               ; preds = %58
  %65 = load ptr, ptr %3, align 8, !tbaa !5
  call void @linkedListDeleteLast(ptr noundef %65)
  br label %83

66:                                               ; preds = %58
  %67 = load ptr, ptr %5, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.Node, ptr %67, i32 0, i32 1
  %69 = load ptr, ptr %68, align 8, !tbaa !13
  %70 = load ptr, ptr %6, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.Node, ptr %70, i32 0, i32 1
  store ptr %69, ptr %71, align 8, !tbaa !13
  %72 = load ptr, ptr %3, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.LinkedList, ptr %72, i32 0, i32 2
  %74 = load i32, ptr %73, align 8, !tbaa !18
  %75 = add nsw i32 %74, -1
  store i32 %75, ptr %73, align 8, !tbaa !18
  %76 = load ptr, ptr %3, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.LinkedList, ptr %76, i32 0, i32 3
  %78 = load ptr, ptr %77, align 8, !tbaa !19
  %79 = load ptr, ptr %5, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.Node, ptr %79, i32 0, i32 0
  %81 = load ptr, ptr %80, align 8, !tbaa !20
  call void %78(ptr noundef %81)
  %82 = load ptr, ptr %5, align 8, !tbaa !5
  call void @free(ptr noundef %82) #4
  br label %83

83:                                               ; preds = %66, %64
  br label %84

84:                                               ; preds = %83, %56
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  ret void
}

attributes #0 = { 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 #1 = { noreturn 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 #4 = { nounwind }
attributes #5 = { noreturn 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 0}
!12 = !{!"LinkedList", !6, i64 0, !6, i64 8, !10, i64 16, !6, i64 24, !6, i64 32}
!13 = !{!14, !6, i64 8}
!14 = !{!"Node", !6, i64 0, !6, i64 8}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!12, !6, i64 8}
!18 = !{!12, !10, i64 16}
!19 = !{!12, !6, i64 24}
!20 = !{!14, !6, i64 0}


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

@stderr = external local_unnamed_addr global ptr, align 8
@.str.2 = external hidden unnamed_addr constant [27 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [31 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [20 x i8], align 1
@.str.9 = external hidden unnamed_addr constant [42 x i8], align 1
@.str.11 = external hidden unnamed_addr constant [33 x i8], align 1

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

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) local_unnamed_addr #1

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @linkedListDeleteFirst(ptr noundef) local_unnamed_addr #2

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

; Function Attrs: nounwind uwtable
declare dso_local void @linkedListDeleteLast(ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local void @linkedListDeleteAtIndex(ptr noundef %0, i32 noundef %1) local_unnamed_addr #2 {
  %3 = icmp eq ptr %0, null
  br i1 %3, label %4, label %7

4:                                                ; preds = %2
  %5 = load ptr, ptr @stderr, align 8, !tbaa !5
  %6 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %5, ptr noundef nonnull @.str.5, ptr noundef nonnull @.str.6, ptr noundef nonnull @.str.2) #4
  tail call void @exit(i32 noundef -83386157) #5
  unreachable

7:                                                ; preds = %2
  %8 = tail call i32 @linkedListIsEmpty(ptr noundef nonnull %0)
  %.not = icmp eq i32 %8, 0
  br i1 %.not, label %12, label %9

9:                                                ; preds = %7
  %10 = load ptr, ptr @stderr, align 8, !tbaa !5
  %11 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %10, ptr noundef nonnull @.str.11, ptr noundef nonnull @.str.2) #4
  tail call void @exit(i32 noundef -833816740) #5
  unreachable

12:                                               ; preds = %7
  %13 = icmp slt i32 %1, 0
  br i1 %13, label %17, label %14

14:                                               ; preds = %12
  %15 = tail call i32 @linkedListGetLength(ptr noundef nonnull %0)
  %.not26 = icmp sgt i32 %15, %1
  br i1 %.not26, label %.preheader, label %17

.preheader:                                       ; preds = %14
  %.02327 = load ptr, ptr %0, align 8, !tbaa !5
  %.not31 = icmp eq i32 %1, 0
  br i1 %.not31, label %._crit_edge.thread, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %.preheader
  %xtraiter = and i32 %1, 7
  %16 = icmp ult i32 %1, 8
  br i1 %16, label %._crit_edge.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i32 %1, 2147483640
  br label %.lr.ph

17:                                               ; preds = %14, %12
  %18 = load ptr, ptr @stderr, align 8, !tbaa !5
  %19 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %18, ptr noundef nonnull @.str.9, ptr noundef nonnull @.str.2) #4
  tail call void @exit(i32 noundef -83385991) #5
  unreachable

._crit_edge.unr-lcssa:                            ; preds = %.lr.ph, %.lr.ph.preheader
  %.02329.lcssa.ph = phi ptr [ poison, %.lr.ph.preheader ], [ %.023.6, %.lr.ph ]
  %.023.lcssa.ph = phi ptr [ poison, %.lr.ph.preheader ], [ %.023.7, %.lr.ph ]
  %.02329.unr = phi ptr [ %.02327, %.lr.ph.preheader ], [ %.023.7, %.lr.ph ]
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.unr-lcssa, %.lr.ph.epil
  %.02329.epil = phi ptr [ %.023.epil, %.lr.ph.epil ], [ %.02329.unr, %._crit_edge.unr-lcssa ]
  %epil.iter = phi i32 [ %epil.iter.next, %.lr.ph.epil ], [ 0, %._crit_edge.unr-lcssa ]
  %20 = getelementptr inbounds i8, ptr %.02329.epil, i64 8
  %.023.epil = load ptr, ptr %20, align 8, !tbaa !5
  %epil.iter.next = add i32 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i32 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge, label %.lr.ph.epil, !llvm.loop !9

._crit_edge:                                      ; preds = %.lr.ph.epil, %._crit_edge.unr-lcssa
  %.02329.lcssa = phi ptr [ %.02329.lcssa.ph, %._crit_edge.unr-lcssa ], [ %.02329.epil, %.lr.ph.epil ]
  %.023.lcssa = phi ptr [ %.023.lcssa.ph, %._crit_edge.unr-lcssa ], [ %.023.epil, %.lr.ph.epil ]
  %21 = icmp eq ptr %.023.lcssa, %.02327
  br i1 %21, label %._crit_edge.thread, label %30

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %.02329 = phi ptr [ %.02327, %.lr.ph.preheader.new ], [ %.023.7, %.lr.ph ]
  %niter = phi i32 [ 0, %.lr.ph.preheader.new ], [ %niter.next.7, %.lr.ph ]
  %22 = getelementptr inbounds i8, ptr %.02329, i64 8
  %.023 = load ptr, ptr %22, align 8, !tbaa !5
  %23 = getelementptr inbounds i8, ptr %.023, i64 8
  %.023.1 = load ptr, ptr %23, align 8, !tbaa !5
  %24 = getelementptr inbounds i8, ptr %.023.1, i64 8
  %.023.2 = load ptr, ptr %24, align 8, !tbaa !5
  %25 = getelementptr inbounds i8, ptr %.023.2, i64 8
  %.023.3 = load ptr, ptr %25, align 8, !tbaa !5
  %26 = getelementptr inbounds i8, ptr %.023.3, i64 8
  %.023.4 = load ptr, ptr %26, align 8, !tbaa !5
  %27 = getelementptr inbounds i8, ptr %.023.4, i64 8
  %.023.5 = load ptr, ptr %27, align 8, !tbaa !5
  %28 = getelementptr inbounds i8, ptr %.023.5, i64 8
  %.023.6 = load ptr, ptr %28, align 8, !tbaa !5
  %29 = getelementptr inbounds i8, ptr %.023.6, i64 8
  %.023.7 = load ptr, ptr %29, align 8, !tbaa !5
  %niter.next.7 = add i32 %niter, 8
  %niter.ncmp.7 = icmp eq i32 %niter.next.7, %unroll_iter
  br i1 %niter.ncmp.7, label %._crit_edge.unr-lcssa, label %.lr.ph, !llvm.loop !11

._crit_edge.thread:                               ; preds = %.preheader, %._crit_edge
  tail call void @linkedListDeleteFirst(ptr noundef nonnull %0)
  br label %45

30:                                               ; preds = %._crit_edge
  %31 = getelementptr inbounds i8, ptr %0, i64 8
  %32 = load ptr, ptr %31, align 8, !tbaa !13
  %33 = icmp eq ptr %.023.lcssa, %32
  br i1 %33, label %34, label %35

34:                                               ; preds = %30
  tail call void @linkedListDeleteLast(ptr noundef nonnull %0)
  br label %45

35:                                               ; preds = %30
  %36 = getelementptr inbounds i8, ptr %.023.lcssa, i64 8
  %37 = load ptr, ptr %36, align 8, !tbaa !16
  %38 = getelementptr inbounds i8, ptr %.02329.lcssa, i64 8
  store ptr %37, ptr %38, align 8, !tbaa !16
  %39 = getelementptr inbounds i8, ptr %0, i64 16
  %40 = load i32, ptr %39, align 8, !tbaa !18
  %41 = add nsw i32 %40, -1
  store i32 %41, ptr %39, align 8, !tbaa !18
  %42 = getelementptr inbounds i8, ptr %0, i64 24
  %43 = load ptr, ptr %42, align 8, !tbaa !19
  %44 = load ptr, ptr %.023.lcssa, align 8, !tbaa !20
  tail call void %43(ptr noundef %44) #6
  tail call void @free(ptr noundef %.023.lcssa) #6
  br label %45

45:                                               ; preds = %34, %35, %._crit_edge.thread
  ret void
}

attributes #0 = { 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 #1 = { noreturn 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 = { 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 = { 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 #4 = { cold nounwind }
attributes #5 = { noreturn nounwind }
attributes #6 = { 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.unroll.disable"}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!14, !6, i64 8}
!14 = !{!"LinkedList", !6, i64 0, !6, i64 8, !15, i64 16, !6, i64 24, !6, i64 32}
!15 = !{!"int", !7, i64 0}
!16 = !{!17, !6, i64 8}
!17 = !{!"Node", !6, i64 0, !6, i64 8}
!18 = !{!14, !15, i64 16}
!19 = !{!14, !6, i64 24}
!20 = !{!17, !6, i64 0}


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