; LLVM IR from Ayon-SSP/The-SDE-Prep :: 1]_Data Structures/2]_Linked List/Linked List Problems (Circular_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.node = type { i32, ptr }

@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1
@head = external dso_local global ptr, align 8
@.str.8 = external hidden unnamed_addr constant [41 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

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

declare i32 @__isoc99_scanf(ptr noundef, ...) #1

; 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 i32 @delete(ptr noundef %0) #2 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  %8 = call i32 (ptr, ...) @printf(ptr noundef @.str.8)
  %9 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef %4)
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  store ptr null, ptr %5, align 8, !tbaa !5
  %10 = load i32, ptr %4, align 4, !tbaa !9
  %11 = icmp slt i32 %10, 0
  br i1 %11, label %12, label %13

12:                                               ; preds = %1
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %58

13:                                               ; preds = %1
  %14 = load i32, ptr %4, align 4, !tbaa !9
  %15 = icmp eq i32 %14, 1
  br i1 %15, label %16, label %35

16:                                               ; preds = %13
  br label %17

17:                                               ; preds = %23, %16
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.node, ptr %18, i32 0, i32 1
  %20 = load ptr, ptr %19, align 8, !tbaa !11
  %21 = load ptr, ptr @head, align 8, !tbaa !5
  %22 = icmp ne ptr %20, %21
  br i1 %22, label %23, label %28

23:                                               ; preds = %17
  %24 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %24, ptr %5, align 8, !tbaa !5
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  %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 !5
  br label %17, !llvm.loop !13

28:                                               ; preds = %17
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.node, ptr %29, i32 0, i32 1
  %31 = load ptr, ptr %30, align 8, !tbaa !11
  %32 = load ptr, ptr %5, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.node, ptr %32, i32 0, i32 1
  store ptr %31, ptr %33, align 8, !tbaa !11
  store ptr null, ptr %3, align 8, !tbaa !5
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  call void @free(ptr noundef %34) #4
  br label %57

35:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %36

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

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

42:                                               ; preds = %36
  %43 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %43, ptr %5, align 8, !tbaa !5
  %44 = load ptr, ptr %3, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.node, ptr %44, i32 0, i32 1
  %46 = load ptr, ptr %45, align 8, !tbaa !11
  store ptr %46, ptr %3, 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 %36, !llvm.loop !15

50:                                               ; preds = %41
  %51 = load ptr, ptr %3, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.node, ptr %51, i32 0, i32 1
  %53 = load ptr, ptr %52, align 8, !tbaa !11
  %54 = load ptr, ptr %5, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.node, ptr %54, i32 0, i32 1
  store ptr %53, ptr %55, align 8, !tbaa !11
  store ptr null, ptr %3, align 8, !tbaa !5
  %56 = load ptr, ptr %3, align 8, !tbaa !5
  call void @free(ptr noundef %56) #4
  br label %57

57:                                               ; preds = %50, %28
  store i32 0, ptr %6, align 4
  br label %58

58:                                               ; preds = %57, %12
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  %59 = load i32, ptr %6, align 4
  switch i32 %59, label %62 [
    i32 0, label %60
    i32 1, label %60
  ]

60:                                               ; preds = %58, %58
  %61 = load i32, ptr %2, align 4
  ret i32 %61

62:                                               ; preds = %58
  unreachable
}

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 = { 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 = !{!"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 = !{!"node", !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"

@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1
@head = external dso_local local_unnamed_addr global ptr, align 8
@.str.8 = external hidden unnamed_addr constant [41 x i8], align 1

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

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

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

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

; Function Attrs: nofree nounwind uwtable
define dso_local noundef i32 @delete(ptr nocapture noundef %0) local_unnamed_addr #2 {
  %2 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %2) #4
  %3 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.8)
  %4 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull %2)
  %5 = load i32, ptr %2, align 4, !tbaa !5
  %6 = icmp slt i32 %5, 0
  br i1 %6, label %40, label %7

7:                                                ; preds = %1
  %8 = icmp eq i32 %5, 1
  br i1 %8, label %.preheader, label %.preheader19

.preheader19:                                     ; preds = %7
  %9 = icmp ne i32 %5, 0
  call void @llvm.assume(i1 %9)
  %10 = add nsw i32 %5, -2
  %11 = add nsw i32 %5, -1
  %xtraiter = and i32 %11, 7
  %12 = icmp ult i32 %10, 7
  br i1 %12, label %._crit_edge.unr-lcssa, label %.preheader19.new

.preheader19.new:                                 ; preds = %.preheader19
  %unroll_iter = and i32 %11, -8
  br label %.lr.ph

.preheader:                                       ; preds = %7
  %13 = load ptr, ptr @head, align 8, !tbaa !9
  br label %14

14:                                               ; preds = %.preheader, %14
  %.014 = phi ptr [ %16, %14 ], [ %0, %.preheader ]
  %.013 = phi ptr [ %.014, %14 ], [ null, %.preheader ]
  %15 = getelementptr inbounds i8, ptr %.014, i64 8
  %16 = load ptr, ptr %15, align 8, !tbaa !11
  %.not = icmp eq ptr %16, %13
  br i1 %.not, label %17, label %14, !llvm.loop !13

17:                                               ; preds = %14
  %18 = getelementptr inbounds i8, ptr %.013, i64 8
  store ptr %16, ptr %18, align 8, !tbaa !11
  br label %40

._crit_edge.unr-lcssa:                            ; preds = %.lr.ph, %.preheader19
  %.11520.lcssa.ph = phi ptr [ poison, %.preheader19 ], [ %37, %.lr.ph ]
  %.lcssa27.ph = phi ptr [ poison, %.preheader19 ], [ %39, %.lr.ph ]
  %.11520.unr = phi ptr [ %0, %.preheader19 ], [ %39, %.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
  %.11520.epil = phi ptr [ %20, %.lr.ph.epil ], [ %.11520.unr, %._crit_edge.unr-lcssa ]
  %epil.iter = phi i32 [ %epil.iter.next, %.lr.ph.epil ], [ 0, %._crit_edge.unr-lcssa ]
  %19 = getelementptr inbounds i8, ptr %.11520.epil, i64 8
  %20 = load ptr, ptr %19, align 8, !tbaa !11
  %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 !15

._crit_edge:                                      ; preds = %.lr.ph.epil, %._crit_edge.unr-lcssa
  %.11520.lcssa = phi ptr [ %.11520.lcssa.ph, %._crit_edge.unr-lcssa ], [ %.11520.epil, %.lr.ph.epil ]
  %.lcssa27 = phi ptr [ %.lcssa27.ph, %._crit_edge.unr-lcssa ], [ %20, %.lr.ph.epil ]
  %21 = getelementptr inbounds i8, ptr %.11520.lcssa, i64 8
  %22 = getelementptr inbounds i8, ptr %.lcssa27, i64 8
  %23 = load ptr, ptr %22, align 8, !tbaa !11
  store ptr %23, ptr %21, align 8, !tbaa !11
  br label %40

.lr.ph:                                           ; preds = %.lr.ph, %.preheader19.new
  %.11520 = phi ptr [ %0, %.preheader19.new ], [ %39, %.lr.ph ]
  %niter = phi i32 [ 0, %.preheader19.new ], [ %niter.next.7, %.lr.ph ]
  %24 = getelementptr inbounds i8, ptr %.11520, i64 8
  %25 = load ptr, ptr %24, align 8, !tbaa !11
  %26 = getelementptr inbounds i8, ptr %25, i64 8
  %27 = load ptr, ptr %26, align 8, !tbaa !11
  %28 = getelementptr inbounds i8, ptr %27, i64 8
  %29 = load ptr, ptr %28, align 8, !tbaa !11
  %30 = getelementptr inbounds i8, ptr %29, i64 8
  %31 = load ptr, ptr %30, align 8, !tbaa !11
  %32 = getelementptr inbounds i8, ptr %31, i64 8
  %33 = load ptr, ptr %32, align 8, !tbaa !11
  %34 = getelementptr inbounds i8, ptr %33, i64 8
  %35 = load ptr, ptr %34, align 8, !tbaa !11
  %36 = getelementptr inbounds i8, ptr %35, i64 8
  %37 = load ptr, ptr %36, align 8, !tbaa !11
  %38 = getelementptr inbounds i8, ptr %37, i64 8
  %39 = load ptr, ptr %38, align 8, !tbaa !11
  %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 !17

40:                                               ; preds = %17, %._crit_edge, %1
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %2) #4
  ret i32 -1
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: write)
declare void @llvm.assume(i1 noundef) #3

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { nofree 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 = { nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: write) }
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"}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.unroll.disable"}
!17 = distinct !{!17, !14}


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