; LLVM IR from AlgoGenesis/C :: Linked_list/Circular_Linked_list/insertion/at_the_specific_position (program)

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

; Function Attrs: nounwind uwtable
define dso_local void @insertAtPosition(ptr noundef %0, i32 noundef %1, i32 noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store i32 %2, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %12 = call noalias ptr @malloc(i64 noundef 16) #4
  store ptr %12, ptr %7, align 8, !tbaa !5
  %13 = load i32, ptr %5, align 4, !tbaa !9
  %14 = load ptr, ptr %7, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.Node, ptr %14, i32 0, i32 0
  store i32 %13, ptr %15, align 8, !tbaa !11
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = load ptr, ptr %16, align 8, !tbaa !5
  %18 = icmp eq ptr %17, null
  br i1 %18, label %19, label %25

19:                                               ; preds = %3
  %20 = load ptr, ptr %7, align 8, !tbaa !5
  %21 = load ptr, ptr %7, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.Node, ptr %21, i32 0, i32 1
  store ptr %20, ptr %22, align 8, !tbaa !13
  %23 = load ptr, ptr %7, align 8, !tbaa !5
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %23, ptr %24, align 8, !tbaa !5
  store i32 1, ptr %8, align 4
  br label %86

25:                                               ; preds = %3
  %26 = load i32, ptr %6, align 4, !tbaa !9
  %27 = icmp eq i32 %26, 1
  br i1 %27, label %28, label %52

28:                                               ; preds = %25
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = load ptr, ptr %29, align 8, !tbaa !5
  store ptr %30, ptr %9, align 8, !tbaa !5
  br label %31

31:                                               ; preds = %38, %28
  %32 = load ptr, ptr %9, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.Node, ptr %32, i32 0, i32 1
  %34 = load ptr, ptr %33, align 8, !tbaa !13
  %35 = load ptr, ptr %4, align 8, !tbaa !5
  %36 = load ptr, ptr %35, align 8, !tbaa !5
  %37 = icmp ne ptr %34, %36
  br i1 %37, label %38, label %42

38:                                               ; preds = %31
  %39 = load ptr, ptr %9, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.Node, ptr %39, i32 0, i32 1
  %41 = load ptr, ptr %40, align 8, !tbaa !13
  store ptr %41, ptr %9, align 8, !tbaa !5
  br label %31, !llvm.loop !14

42:                                               ; preds = %31
  %43 = load ptr, ptr %4, align 8, !tbaa !5
  %44 = load ptr, ptr %43, align 8, !tbaa !5
  %45 = load ptr, ptr %7, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.Node, ptr %45, i32 0, i32 1
  store ptr %44, ptr %46, align 8, !tbaa !13
  %47 = load ptr, ptr %7, align 8, !tbaa !5
  %48 = load ptr, ptr %9, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.Node, ptr %48, i32 0, i32 1
  store ptr %47, ptr %49, align 8, !tbaa !13
  %50 = load ptr, ptr %7, align 8, !tbaa !5
  %51 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %50, ptr %51, align 8, !tbaa !5
  store i32 1, ptr %8, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  br label %86

52:                                               ; preds = %25
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %53 = load ptr, ptr %4, align 8, !tbaa !5
  %54 = load ptr, ptr %53, align 8, !tbaa !5
  store ptr %54, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  store i32 1, ptr %11, align 4, !tbaa !9
  br label %55

55:                                               ; preds = %74, %52
  %56 = load i32, ptr %11, align 4, !tbaa !9
  %57 = load i32, ptr %6, align 4, !tbaa !9
  %58 = sub nsw i32 %57, 1
  %59 = icmp slt i32 %56, %58
  br i1 %59, label %60, label %67

60:                                               ; preds = %55
  %61 = load ptr, ptr %10, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.Node, ptr %61, i32 0, i32 1
  %63 = load ptr, ptr %62, align 8, !tbaa !13
  %64 = load ptr, ptr %4, align 8, !tbaa !5
  %65 = load ptr, ptr %64, align 8, !tbaa !5
  %66 = icmp ne ptr %63, %65
  br label %67

67:                                               ; preds = %60, %55
  %68 = phi i1 [ false, %55 ], [ %66, %60 ]
  br i1 %68, label %70, label %69

69:                                               ; preds = %67
  store i32 4, ptr %8, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  br label %77

70:                                               ; preds = %67
  %71 = load ptr, ptr %10, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.Node, ptr %71, i32 0, i32 1
  %73 = load ptr, ptr %72, align 8, !tbaa !13
  store ptr %73, ptr %10, align 8, !tbaa !5
  br label %74

74:                                               ; preds = %70
  %75 = load i32, ptr %11, align 4, !tbaa !9
  %76 = add nsw i32 %75, 1
  store i32 %76, ptr %11, align 4, !tbaa !9
  br label %55, !llvm.loop !16

77:                                               ; preds = %69
  %78 = load ptr, ptr %10, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.Node, ptr %78, i32 0, i32 1
  %80 = load ptr, ptr %79, align 8, !tbaa !13
  %81 = load ptr, ptr %7, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.Node, ptr %81, i32 0, i32 1
  store ptr %80, ptr %82, align 8, !tbaa !13
  %83 = load ptr, ptr %7, align 8, !tbaa !5
  %84 = load ptr, ptr %10, align 8, !tbaa !5
  %85 = getelementptr inbounds %struct.Node, ptr %84, i32 0, i32 1
  store ptr %83, ptr %85, align 8, !tbaa !13
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  store i32 0, ptr %8, align 4
  br label %86

86:                                               ; preds = %77, %42, %19
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  %87 = load i32, ptr %8, align 4
  switch i32 %87, label %89 [
    i32 0, label %88
    i32 1, label %88
  ]

88:                                               ; preds = %86, %86
  ret void

89:                                               ; preds = %86
  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

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


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

; Function Attrs: nofree nounwind uwtable
define dso_local void @insertAtPosition(ptr nocapture noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = tail call noalias dereferenceable_or_null(16) ptr @malloc(i64 noundef 16) #2
  store i32 %1, ptr %4, align 8, !tbaa !5
  %5 = load ptr, ptr %0, align 8, !tbaa !11
  %6 = icmp eq ptr %5, null
  br i1 %6, label %7, label %9

7:                                                ; preds = %3
  %8 = getelementptr inbounds i8, ptr %4, i64 8
  store ptr %4, ptr %8, align 8, !tbaa !12
  br label %25

9:                                                ; preds = %3
  %10 = icmp eq i32 %2, 1
  br i1 %10, label %.preheader, label %.preheader36

.preheader36:                                     ; preds = %9
  %11 = icmp sgt i32 %2, 2
  br i1 %11, label %.lr.ph.preheader, label %.critedge

.lr.ph.preheader:                                 ; preds = %.preheader36
  %12 = add nsw i32 %2, -2
  br label %.lr.ph

.preheader:                                       ; preds = %9, %.preheader
  %.029 = phi ptr [ %14, %.preheader ], [ %5, %9 ]
  %13 = getelementptr inbounds i8, ptr %.029, i64 8
  %14 = load ptr, ptr %13, align 8, !tbaa !12
  %.not35 = icmp eq ptr %14, %5
  br i1 %.not35, label %15, label %.preheader, !llvm.loop !13

15:                                               ; preds = %.preheader
  %16 = getelementptr inbounds i8, ptr %.029, i64 8
  %17 = getelementptr inbounds i8, ptr %4, i64 8
  store ptr %5, ptr %17, align 8, !tbaa !12
  store ptr %4, ptr %16, align 8, !tbaa !12
  br label %25

18:                                               ; preds = %.lr.ph
  %19 = add nuw nsw i32 %.038, 1
  %exitcond.not = icmp eq i32 %.038, %12
  br i1 %exitcond.not, label %.critedge, label %.lr.ph, !llvm.loop !15

.lr.ph:                                           ; preds = %.lr.ph.preheader, %18
  %.038 = phi i32 [ %19, %18 ], [ 1, %.lr.ph.preheader ]
  %.02837 = phi ptr [ %21, %18 ], [ %5, %.lr.ph.preheader ]
  %20 = getelementptr inbounds i8, ptr %.02837, i64 8
  %21 = load ptr, ptr %20, align 8, !tbaa !12
  %.not = icmp eq ptr %21, %5
  br i1 %.not, label %.critedge, label %18

.critedge:                                        ; preds = %.lr.ph, %18, %.preheader36
  %.028.lcssa = phi ptr [ %5, %.preheader36 ], [ %21, %18 ], [ %.02837, %.lr.ph ]
  %22 = getelementptr inbounds i8, ptr %.028.lcssa, i64 8
  %23 = load ptr, ptr %22, align 8, !tbaa !12
  %24 = getelementptr inbounds i8, ptr %4, i64 8
  store ptr %23, ptr %24, align 8, !tbaa !12
  br label %25

25:                                               ; preds = %.critedge, %15, %7
  %.sink = phi ptr [ %22, %.critedge ], [ %0, %15 ], [ %0, %7 ]
  store ptr %4, ptr %.sink, align 8, !tbaa !11
  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

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


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