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

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

@head = external dso_local global ptr, align 8
@.str = external hidden unnamed_addr constant [10 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [15 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [16 x i8], align 1

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

12:                                               ; preds = %2
  %13 = call i32 (ptr, ...) @printf(ptr noundef @.str)
  store i32 1, ptr %6, align 4
  br label %87

14:                                               ; preds = %2
  %15 = load i32, ptr %3, align 4, !tbaa !5
  %16 = load ptr, ptr %5, align 8, !tbaa !9
  %17 = getelementptr inbounds %struct.Node, ptr %16, i32 0, i32 0
  store i32 %15, ptr %17, align 8, !tbaa !11
  %18 = load i32, ptr %4, align 4, !tbaa !5
  %19 = icmp eq i32 %18, 0
  br i1 %19, label %20, label %34

20:                                               ; preds = %14
  %21 = load ptr, ptr @head, align 8, !tbaa !9
  %22 = load ptr, ptr %5, align 8, !tbaa !9
  %23 = getelementptr inbounds %struct.Node, ptr %22, i32 0, i32 1
  store ptr %21, ptr %23, align 8, !tbaa !13
  %24 = load ptr, ptr %5, align 8, !tbaa !9
  %25 = getelementptr inbounds %struct.Node, ptr %24, i32 0, i32 2
  store ptr null, ptr %25, align 8, !tbaa !14
  %26 = load ptr, ptr @head, align 8, !tbaa !9
  %27 = icmp ne ptr %26, null
  br i1 %27, label %28, label %32

28:                                               ; preds = %20
  %29 = load ptr, ptr %5, align 8, !tbaa !9
  %30 = load ptr, ptr @head, align 8, !tbaa !9
  %31 = getelementptr inbounds %struct.Node, ptr %30, i32 0, i32 2
  store ptr %29, ptr %31, align 8, !tbaa !14
  br label %32

32:                                               ; preds = %28, %20
  %33 = load ptr, ptr %5, align 8, !tbaa !9
  store ptr %33, ptr @head, align 8, !tbaa !9
  br label %85

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

36:                                               ; preds = %50, %34
  %37 = load i32, ptr %7, align 4, !tbaa !5
  %38 = load i32, ptr %4, align 4, !tbaa !5
  %39 = sub nsw i32 %38, 1
  %40 = icmp slt i32 %37, %39
  br i1 %40, label %41, label %44

41:                                               ; preds = %36
  %42 = load ptr, ptr %8, align 8, !tbaa !9
  %43 = icmp ne ptr %42, null
  br label %44

44:                                               ; preds = %41, %36
  %45 = phi i1 [ false, %36 ], [ %43, %41 ]
  br i1 %45, label %46, label %53

46:                                               ; preds = %44
  %47 = load ptr, ptr %8, align 8, !tbaa !9
  %48 = getelementptr inbounds %struct.Node, ptr %47, i32 0, i32 1
  %49 = load ptr, ptr %48, align 8, !tbaa !13
  store ptr %49, ptr %8, align 8, !tbaa !9
  br label %50

50:                                               ; preds = %46
  %51 = load i32, ptr %7, align 4, !tbaa !5
  %52 = add nsw i32 %51, 1
  store i32 %52, ptr %7, align 4, !tbaa !5
  br label %36, !llvm.loop !15

53:                                               ; preds = %44
  %54 = load ptr, ptr %8, align 8, !tbaa !9
  %55 = icmp eq ptr %54, null
  br i1 %55, label %56, label %59

56:                                               ; preds = %53
  %57 = call i32 (ptr, ...) @printf(ptr noundef @.str.1)
  %58 = load ptr, ptr %5, align 8, !tbaa !9
  call void @free(ptr noundef %58) #5
  store i32 1, ptr %6, align 4
  br label %82

59:                                               ; preds = %53
  %60 = load ptr, ptr %8, align 8, !tbaa !9
  %61 = load ptr, ptr %5, align 8, !tbaa !9
  %62 = getelementptr inbounds %struct.Node, ptr %61, i32 0, i32 2
  store ptr %60, ptr %62, align 8, !tbaa !14
  %63 = load ptr, ptr %8, align 8, !tbaa !9
  %64 = getelementptr inbounds %struct.Node, ptr %63, i32 0, i32 1
  %65 = load ptr, ptr %64, align 8, !tbaa !13
  %66 = load ptr, ptr %5, align 8, !tbaa !9
  %67 = getelementptr inbounds %struct.Node, ptr %66, i32 0, i32 1
  store ptr %65, ptr %67, align 8, !tbaa !13
  %68 = load ptr, ptr %5, align 8, !tbaa !9
  %69 = load ptr, ptr %8, align 8, !tbaa !9
  %70 = getelementptr inbounds %struct.Node, ptr %69, i32 0, i32 1
  store ptr %68, ptr %70, align 8, !tbaa !13
  %71 = load ptr, ptr %5, align 8, !tbaa !9
  %72 = getelementptr inbounds %struct.Node, ptr %71, i32 0, i32 1
  %73 = load ptr, ptr %72, align 8, !tbaa !13
  %74 = icmp ne ptr %73, null
  br i1 %74, label %75, label %81

75:                                               ; preds = %59
  %76 = load ptr, ptr %5, align 8, !tbaa !9
  %77 = load ptr, ptr %5, align 8, !tbaa !9
  %78 = getelementptr inbounds %struct.Node, ptr %77, i32 0, i32 1
  %79 = load ptr, ptr %78, align 8, !tbaa !13
  %80 = getelementptr inbounds %struct.Node, ptr %79, i32 0, i32 2
  store ptr %76, ptr %80, align 8, !tbaa !14
  br label %81

81:                                               ; preds = %75, %59
  store i32 0, ptr %6, align 4
  br label %82

82:                                               ; preds = %81, %56
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  %83 = load i32, ptr %6, align 4
  switch i32 %83, label %87 [
    i32 0, label %84
  ]

84:                                               ; preds = %82
  br label %85

85:                                               ; preds = %84, %32
  %86 = call i32 (ptr, ...) @printf(ptr noundef @.str.2)
  store i32 0, ptr %6, align 4
  br label %87

87:                                               ; preds = %85, %82, %12
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  %88 = load i32, ptr %6, align 4
  switch i32 %88, label %90 [
    i32 0, label %89
    i32 1, label %89
  ]

89:                                               ; preds = %87, %87
  ret void

90:                                               ; preds = %87
  unreachable
}

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

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

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

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

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

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { "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 "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 #5 = { nounwind }
attributes #6 = { 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 0}
!12 = !{!"Node", !6, i64 0, !10, i64 8, !10, i64 16}
!13 = !{!12, !10, i64 8}
!14 = !{!12, !10, i64 16}
!15 = distinct !{!15, !16}
!16 = !{!"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 [10 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [15 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [16 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local void @insert(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = tail call noalias dereferenceable_or_null(24) ptr @malloc(i64 noundef 24) #4
  %4 = icmp eq ptr %3, null
  br i1 %4, label %5, label %7

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

7:                                                ; preds = %2
  store i32 %0, ptr %3, align 8, !tbaa !5
  %8 = icmp eq i32 %1, 0
  br i1 %8, label %13, label %.preheader

.preheader:                                       ; preds = %7
  %9 = add nsw i32 %1, -1
  %.032 = load ptr, ptr @head, align 8, !tbaa !11
  %10 = icmp sgt i32 %1, 1
  %11 = icmp ne ptr %.032, null
  %12 = select i1 %10, i1 %11, i1 false
  br i1 %12, label %.lr.ph, label %._crit_edge

13:                                               ; preds = %7
  %14 = load ptr, ptr @head, align 8, !tbaa !11
  %15 = getelementptr inbounds i8, ptr %3, i64 8
  store ptr %14, ptr %15, align 8, !tbaa !12
  %16 = getelementptr inbounds i8, ptr %3, i64 16
  store ptr null, ptr %16, align 8, !tbaa !13
  %.not31 = icmp eq ptr %14, null
  br i1 %.not31, label %.thread.sink.split, label %17

17:                                               ; preds = %13
  %18 = getelementptr inbounds i8, ptr %14, i64 16
  store ptr %3, ptr %18, align 8, !tbaa !13
  br label %.thread.sink.split

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %.034 = phi ptr [ %.0, %.lr.ph ], [ %.032, %.preheader ]
  %.02433 = phi i32 [ %20, %.lr.ph ], [ 0, %.preheader ]
  %19 = getelementptr inbounds i8, ptr %.034, i64 8
  %20 = add nuw nsw i32 %.02433, 1
  %.0 = load ptr, ptr %19, align 8, !tbaa !11
  %21 = icmp slt i32 %20, %9
  %22 = icmp ne ptr %.0, null
  %23 = select i1 %21, i1 %22, i1 false
  br i1 %23, label %.lr.ph, label %._crit_edge, !llvm.loop !14

._crit_edge:                                      ; preds = %.lr.ph, %.preheader
  %.0.lcssa = phi ptr [ %.032, %.preheader ], [ %.0, %.lr.ph ]
  %.not30 = icmp eq ptr %.0.lcssa, null
  br i1 %.not30, label %32, label %24

24:                                               ; preds = %._crit_edge
  %25 = getelementptr inbounds i8, ptr %3, i64 16
  store ptr %.0.lcssa, ptr %25, align 8, !tbaa !13
  %26 = getelementptr inbounds i8, ptr %.0.lcssa, i64 8
  %27 = load ptr, ptr %26, align 8, !tbaa !12
  %28 = getelementptr inbounds i8, ptr %3, i64 8
  store ptr %27, ptr %28, align 8, !tbaa !12
  store ptr %3, ptr %26, align 8, !tbaa !12
  %29 = load ptr, ptr %28, align 8, !tbaa !12
  %.not = icmp eq ptr %29, null
  br i1 %.not, label %.thread, label %30

30:                                               ; preds = %24
  %31 = getelementptr inbounds i8, ptr %29, i64 16
  br label %.thread.sink.split

32:                                               ; preds = %._crit_edge
  %33 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1)
  tail call void @free(ptr noundef %3) #5
  br label %35

.thread.sink.split:                               ; preds = %13, %17, %30
  %.sink = phi ptr [ %31, %30 ], [ @head, %17 ], [ @head, %13 ]
  store ptr %3, ptr %.sink, align 8, !tbaa !11
  br label %.thread

.thread:                                          ; preds = %.thread.sink.split, %24
  %34 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2)
  br label %35

35:                                               ; preds = %32, %.thread, %5
  ret void
}

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

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) 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

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 = { 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 #2 = { 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 #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 = { nounwind allocsize(0) }
attributes #5 = { 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, !7, i64 0}
!6 = !{!"Node", !7, i64 0, !10, i64 8, !10, i64 16}
!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 = !{!6, !10, i64 8}
!13 = !{!6, !10, i64 16}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


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