; LLVM IR from sbshah97/DS-Algo-Handbook :: Data-Structures/Linked_List/Doubly_Linked_List (doubly)

; 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, i32, ptr, ptr }

@head = external dso_local global ptr, align 8
@last = external dso_local 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 allocsize(0)
declare noalias ptr @malloc(i64 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local zeroext i1 @insertAfter(i32 noundef %0, i32 noundef %1, i32 noundef %2) #2 {
  %4 = alloca i1, align 1
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  store i32 %0, ptr %5, align 4, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %11 = load ptr, ptr @head, align 8, !tbaa !9
  store ptr %11, ptr %8, align 8, !tbaa !9
  %12 = load ptr, ptr @head, align 8, !tbaa !9
  %13 = icmp eq ptr %12, null
  br i1 %13, label %14, label %15

14:                                               ; preds = %3
  store i1 false, ptr %4, align 1
  store i32 1, ptr %9, align 4
  br label %66

15:                                               ; preds = %3
  br label %16

16:                                               ; preds = %32, %15
  %17 = load ptr, ptr %8, align 8, !tbaa !9
  %18 = getelementptr inbounds %struct.node, ptr %17, i32 0, i32 1
  %19 = load i32, ptr %18, align 4, !tbaa !11
  %20 = load i32, ptr %5, align 4, !tbaa !5
  %21 = icmp ne i32 %19, %20
  br i1 %21, label %22, label %33

22:                                               ; preds = %16
  %23 = load ptr, ptr %8, align 8, !tbaa !9
  %24 = getelementptr inbounds %struct.node, ptr %23, i32 0, i32 2
  %25 = load ptr, ptr %24, align 8, !tbaa !13
  %26 = icmp eq ptr %25, null
  br i1 %26, label %27, label %28

27:                                               ; preds = %22
  store i1 false, ptr %4, align 1
  store i32 1, ptr %9, align 4
  br label %66

28:                                               ; preds = %22
  %29 = load ptr, ptr %8, align 8, !tbaa !9
  %30 = getelementptr inbounds %struct.node, ptr %29, i32 0, i32 2
  %31 = load ptr, ptr %30, align 8, !tbaa !13
  store ptr %31, ptr %8, align 8, !tbaa !9
  br label %32

32:                                               ; preds = %28
  br label %16, !llvm.loop !14

33:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %34 = call noalias ptr @malloc(i64 noundef 24) #4
  store ptr %34, ptr %10, align 8, !tbaa !9
  %35 = load i32, ptr %5, align 4, !tbaa !5
  %36 = load ptr, ptr %10, align 8, !tbaa !9
  %37 = getelementptr inbounds %struct.node, ptr %36, i32 0, i32 1
  store i32 %35, ptr %37, align 4, !tbaa !11
  %38 = load i32, ptr %7, align 4, !tbaa !5
  %39 = load ptr, ptr %10, align 8, !tbaa !9
  %40 = getelementptr inbounds %struct.node, ptr %39, i32 0, i32 0
  store i32 %38, ptr %40, align 8, !tbaa !16
  %41 = load ptr, ptr %8, align 8, !tbaa !9
  %42 = load ptr, ptr @last, align 8, !tbaa !9
  %43 = icmp eq ptr %41, %42
  br i1 %43, label %44, label %48

44:                                               ; preds = %33
  %45 = load ptr, ptr %10, align 8, !tbaa !9
  %46 = getelementptr inbounds %struct.node, ptr %45, i32 0, i32 2
  store ptr null, ptr %46, align 8, !tbaa !13
  %47 = load ptr, ptr %10, align 8, !tbaa !9
  store ptr %47, ptr @last, align 8, !tbaa !9
  br label %59

48:                                               ; preds = %33
  %49 = load ptr, ptr %8, align 8, !tbaa !9
  %50 = getelementptr inbounds %struct.node, ptr %49, i32 0, i32 2
  %51 = load ptr, ptr %50, align 8, !tbaa !13
  %52 = load ptr, ptr %10, align 8, !tbaa !9
  %53 = getelementptr inbounds %struct.node, ptr %52, i32 0, i32 2
  store ptr %51, ptr %53, align 8, !tbaa !13
  %54 = load ptr, ptr %10, align 8, !tbaa !9
  %55 = load ptr, ptr %8, align 8, !tbaa !9
  %56 = getelementptr inbounds %struct.node, ptr %55, i32 0, i32 2
  %57 = load ptr, ptr %56, align 8, !tbaa !13
  %58 = getelementptr inbounds %struct.node, ptr %57, i32 0, i32 3
  store ptr %54, ptr %58, align 8, !tbaa !17
  br label %59

59:                                               ; preds = %48, %44
  %60 = load ptr, ptr %8, align 8, !tbaa !9
  %61 = load ptr, ptr %10, align 8, !tbaa !9
  %62 = getelementptr inbounds %struct.node, ptr %61, i32 0, i32 3
  store ptr %60, ptr %62, align 8, !tbaa !17
  %63 = load ptr, ptr %10, align 8, !tbaa !9
  %64 = load ptr, ptr %8, align 8, !tbaa !9
  %65 = getelementptr inbounds %struct.node, ptr %64, i32 0, i32 2
  store ptr %63, ptr %65, align 8, !tbaa !13
  store i1 true, ptr %4, align 1
  store i32 1, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  br label %66

66:                                               ; preds = %59, %27, %14
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %67 = load i1, ptr %4, align 1
  ret i1 %67
}

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

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


; 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
@last = external dso_local local_unnamed_addr global ptr, align 8

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #0

; Function Attrs: nofree nounwind uwtable
define dso_local noundef zeroext i1 @insertAfter(i32 noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #1 {
  %4 = load ptr, ptr @head, align 8, !tbaa !5
  %5 = icmp eq ptr %4, null
  br i1 %5, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %3, %8
  %.019 = phi ptr [ %10, %8 ], [ %4, %3 ]
  %6 = getelementptr inbounds i8, ptr %.019, i64 4
  %7 = load i32, ptr %6, align 4, !tbaa !9
  %.not = icmp eq i32 %7, %0
  br i1 %.not, label %12, label %8

8:                                                ; preds = %.preheader
  %9 = getelementptr inbounds i8, ptr %.019, i64 8
  %10 = load ptr, ptr %9, align 8, !tbaa !12
  %11 = icmp eq ptr %10, null
  br i1 %11, label %.loopexit, label %.preheader, !llvm.loop !13

12:                                               ; preds = %.preheader
  %13 = tail call noalias dereferenceable_or_null(24) ptr @malloc(i64 noundef 24) #2
  %14 = getelementptr inbounds i8, ptr %13, i64 4
  store i32 %0, ptr %14, align 4, !tbaa !9
  store i32 %2, ptr %13, align 8, !tbaa !15
  %15 = load ptr, ptr @last, align 8, !tbaa !5
  %16 = icmp eq ptr %.019, %15
  br i1 %16, label %17, label %19

17:                                               ; preds = %12
  %18 = getelementptr inbounds i8, ptr %13, i64 8
  store ptr null, ptr %18, align 8, !tbaa !12
  br label %24

19:                                               ; preds = %12
  %20 = getelementptr inbounds i8, ptr %.019, i64 8
  %21 = load ptr, ptr %20, align 8, !tbaa !12
  %22 = getelementptr inbounds i8, ptr %13, i64 8
  store ptr %21, ptr %22, align 8, !tbaa !12
  %23 = getelementptr inbounds i8, ptr %21, i64 16
  br label %24

24:                                               ; preds = %19, %17
  %.sink = phi ptr [ %23, %19 ], [ @last, %17 ]
  store ptr %13, ptr %.sink, align 8, !tbaa !5
  %25 = getelementptr inbounds i8, ptr %13, i64 16
  store ptr %.019, ptr %25, align 8, !tbaa !16
  %26 = getelementptr inbounds i8, ptr %.019, i64 8
  store ptr %13, ptr %26, align 8, !tbaa !12
  br label %.loopexit

.loopexit:                                        ; preds = %8, %3, %24
  %.0 = phi i1 [ true, %24 ], [ false, %3 ], [ false, %8 ]
  ret i1 %.0
}

attributes #0 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(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 = { 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 #2 = { nounwind allocsize(0) }

!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, !11, i64 4}
!10 = !{!"node", !11, i64 0, !11, i64 4, !6, i64 8, !6, i64 16}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !6, i64 8}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!10, !11, i64 0}
!16 = !{!10, !6, i64 16}


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