; LLVM IR from HarshCasper/NeoAlgo :: C/ds (singly_linked_list)

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

@first = external dso_local global ptr, align 8
@last = external dso_local global ptr, align 8
@.str.13 = external hidden unnamed_addr constant [3 x i8], align 1
@newnode = external dso_local global ptr, align 8
@temp = external dso_local global ptr, align 8
@.str.26 = external hidden unnamed_addr constant [18 x i8], align 1
@.str.27 = external hidden unnamed_addr constant [31 x i8], align 1
@.str.28 = external hidden unnamed_addr constant [21 x i8], align 1
@ptr = external dso_local global ptr, align 8
@.str.29 = external hidden unnamed_addr constant [10 x i8], align 1
@prev = external dso_local global ptr, align 8
@.str.30 = external hidden unnamed_addr constant [25 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
declare dso_local ptr @create_node(i32 noundef) #2

; Function Attrs: nounwind uwtable
define dso_local void @insert_node_pos() #2 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  store i32 0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  %5 = call i32 (ptr, ...) @printf(ptr noundef @.str.27)
  %6 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.13, ptr noundef %2)
  %7 = load i32, ptr %2, align 4, !tbaa !5
  %8 = call ptr @create_node(i32 noundef %7)
  store ptr %8, ptr @newnode, align 8, !tbaa !9
  %9 = call i32 (ptr, ...) @printf(ptr noundef @.str.28)
  %10 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.13, ptr noundef %1)
  %11 = load ptr, ptr @first, align 8, !tbaa !9
  store ptr %11, ptr @ptr, align 8, !tbaa !9
  br label %12

12:                                               ; preds = %15, %0
  %13 = load ptr, ptr @ptr, align 8, !tbaa !9
  %14 = icmp ne ptr %13, null
  br i1 %14, label %15, label %21

15:                                               ; preds = %12
  %16 = load ptr, ptr @ptr, align 8, !tbaa !9
  %17 = getelementptr inbounds %struct.node, ptr %16, i32 0, i32 1
  %18 = load ptr, ptr %17, align 8, !tbaa !11
  store ptr %18, ptr @ptr, align 8, !tbaa !9
  %19 = load i32, ptr %3, align 4, !tbaa !5
  %20 = add nsw i32 %19, 1
  store i32 %20, ptr %3, align 4, !tbaa !5
  br label %12, !llvm.loop !13

21:                                               ; preds = %12
  %22 = load i32, ptr %1, align 4, !tbaa !5
  %23 = icmp eq i32 %22, 1
  br i1 %23, label %24, label %45

24:                                               ; preds = %21
  %25 = load ptr, ptr @first, align 8, !tbaa !9
  %26 = load ptr, ptr @last, align 8, !tbaa !9
  %27 = icmp eq ptr %25, %26
  br i1 %27, label %28, label %37

28:                                               ; preds = %24
  %29 = load ptr, ptr @first, align 8, !tbaa !9
  %30 = icmp eq ptr %29, null
  br i1 %30, label %31, label %37

31:                                               ; preds = %28
  %32 = load ptr, ptr @newnode, align 8, !tbaa !9
  store ptr %32, ptr @last, align 8, !tbaa !9
  store ptr %32, ptr @first, align 8, !tbaa !9
  %33 = load ptr, ptr @first, align 8, !tbaa !9
  %34 = getelementptr inbounds %struct.node, ptr %33, i32 0, i32 1
  store ptr null, ptr %34, align 8, !tbaa !11
  %35 = load ptr, ptr @last, align 8, !tbaa !9
  %36 = getelementptr inbounds %struct.node, ptr %35, i32 0, i32 1
  store ptr null, ptr %36, align 8, !tbaa !11
  br label %43

37:                                               ; preds = %28, %24
  %38 = load ptr, ptr @first, align 8, !tbaa !9
  store ptr %38, ptr @temp, align 8, !tbaa !9
  %39 = load ptr, ptr @newnode, align 8, !tbaa !9
  store ptr %39, ptr @first, align 8, !tbaa !9
  %40 = load ptr, ptr @temp, align 8, !tbaa !9
  %41 = load ptr, ptr @first, align 8, !tbaa !9
  %42 = getelementptr inbounds %struct.node, ptr %41, i32 0, i32 1
  store ptr %40, ptr %42, align 8, !tbaa !11
  br label %43

43:                                               ; preds = %37, %31
  %44 = call i32 (ptr, ...) @printf(ptr noundef @.str.29)
  br label %77

45:                                               ; preds = %21
  %46 = load i32, ptr %1, align 4, !tbaa !5
  %47 = icmp sgt i32 %46, 1
  br i1 %47, label %48, label %74

48:                                               ; preds = %45
  %49 = load i32, ptr %1, align 4, !tbaa !5
  %50 = load i32, ptr %3, align 4, !tbaa !5
  %51 = icmp sle i32 %49, %50
  br i1 %51, label %52, label %74

52:                                               ; preds = %48
  %53 = load ptr, ptr @first, align 8, !tbaa !9
  store ptr %53, ptr @ptr, align 8, !tbaa !9
  store i32 1, ptr %4, align 4, !tbaa !5
  br label %54

54:                                               ; preds = %63, %52
  %55 = load i32, ptr %4, align 4, !tbaa !5
  %56 = load i32, ptr %1, align 4, !tbaa !5
  %57 = icmp slt i32 %55, %56
  br i1 %57, label %58, label %66

58:                                               ; preds = %54
  %59 = load ptr, ptr @ptr, align 8, !tbaa !9
  store ptr %59, ptr @prev, align 8, !tbaa !9
  %60 = load ptr, ptr @ptr, align 8, !tbaa !9
  %61 = getelementptr inbounds %struct.node, ptr %60, i32 0, i32 1
  %62 = load ptr, ptr %61, align 8, !tbaa !11
  store ptr %62, ptr @ptr, align 8, !tbaa !9
  br label %63

63:                                               ; preds = %58
  %64 = load i32, ptr %4, align 4, !tbaa !5
  %65 = add nsw i32 %64, 1
  store i32 %65, ptr %4, align 4, !tbaa !5
  br label %54, !llvm.loop !15

66:                                               ; preds = %54
  %67 = load ptr, ptr @newnode, align 8, !tbaa !9
  %68 = load ptr, ptr @prev, align 8, !tbaa !9
  %69 = getelementptr inbounds %struct.node, ptr %68, i32 0, i32 1
  store ptr %67, ptr %69, align 8, !tbaa !11
  %70 = load ptr, ptr @ptr, align 8, !tbaa !9
  %71 = load ptr, ptr @newnode, align 8, !tbaa !9
  %72 = getelementptr inbounds %struct.node, ptr %71, i32 0, i32 1
  store ptr %70, ptr %72, align 8, !tbaa !11
  %73 = call i32 (ptr, ...) @printf(ptr noundef @.str.26)
  br label %76

74:                                               ; preds = %48, %45
  %75 = call i32 (ptr, ...) @printf(ptr noundef @.str.30)
  br label %76

76:                                               ; preds = %74, %66
  br label %77

77:                                               ; preds = %76, %43
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  ret void
}

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 }

!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, !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"

@first = external dso_local local_unnamed_addr global ptr, align 8
@last = external dso_local local_unnamed_addr global ptr, align 8
@.str.13 = external hidden unnamed_addr constant [3 x i8], align 1
@newnode = external dso_local local_unnamed_addr global ptr, align 8
@temp = external dso_local local_unnamed_addr global ptr, align 8
@.str.26 = external hidden unnamed_addr constant [18 x i8], align 1
@.str.27 = external hidden unnamed_addr constant [31 x i8], align 1
@.str.28 = external hidden unnamed_addr constant [21 x i8], align 1
@ptr = external dso_local local_unnamed_addr global ptr, align 8
@.str.29 = external hidden unnamed_addr constant [10 x i8], align 1
@prev = external dso_local local_unnamed_addr global ptr, align 8
@.str.30 = external hidden unnamed_addr constant [25 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: nounwind uwtable
declare dso_local ptr @create_node(i32 noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local void @insert_node_pos() local_unnamed_addr #2 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %2) #3
  %3 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.27)
  %4 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.13, ptr noundef nonnull %2)
  %5 = load i32, ptr %2, align 4, !tbaa !5
  %6 = call ptr @create_node(i32 noundef %5)
  store ptr %6, ptr @newnode, align 8, !tbaa !9
  %7 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.28)
  %8 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.13, ptr noundef nonnull %1)
  %9 = load ptr, ptr @first, align 8
  store ptr %9, ptr @ptr, align 8, !tbaa !9
  %.not10 = icmp eq ptr %9, null
  br i1 %.not10, label %14, label %.lr.ph

.lr.ph:                                           ; preds = %0, %.lr.ph
  %.0511 = phi i32 [ %13, %.lr.ph ], [ 0, %0 ]
  %10 = phi ptr [ %12, %.lr.ph ], [ %9, %0 ]
  %11 = getelementptr inbounds i8, ptr %10, i64 8
  %12 = load ptr, ptr %11, align 8, !tbaa !11
  %13 = add nuw nsw i32 %.0511, 1
  %.not = icmp eq ptr %12, null
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !13

._crit_edge:                                      ; preds = %.lr.ph
  store ptr null, ptr @ptr, align 8, !tbaa !9
  br label %14

14:                                               ; preds = %._crit_edge, %0
  %.05.lcssa = phi i32 [ %13, %._crit_edge ], [ 0, %0 ]
  %15 = load i32, ptr %1, align 4, !tbaa !5
  %16 = icmp eq i32 %15, 1
  br i1 %16, label %17, label %26

17:                                               ; preds = %14
  %18 = load ptr, ptr @last, align 8, !tbaa !9
  %19 = icmp eq ptr %18, null
  %or.cond = and i1 %.not10, %19
  br i1 %or.cond, label %20, label %22

20:                                               ; preds = %17
  %21 = load ptr, ptr @newnode, align 8, !tbaa !9
  store ptr %21, ptr @last, align 8, !tbaa !9
  br label %24

22:                                               ; preds = %17
  store ptr %9, ptr @temp, align 8, !tbaa !9
  %23 = load ptr, ptr @newnode, align 8, !tbaa !9
  br label %24

24:                                               ; preds = %22, %20
  %.sink24 = phi ptr [ %23, %22 ], [ %21, %20 ]
  %.sink22 = phi ptr [ %9, %22 ], [ null, %20 ]
  %25 = getelementptr inbounds i8, ptr %.sink24, i64 8
  store ptr %.sink22, ptr %25, align 8, !tbaa !11
  store ptr %.sink24, ptr @first, align 8
  br label %54

26:                                               ; preds = %14
  %27 = icmp slt i32 %15, 2
  %.not8 = icmp sgt i32 %15, %.05.lcssa
  %or.cond9 = select i1 %27, i1 true, i1 %.not8
  br i1 %or.cond9, label %54, label %.preheader.preheader

.preheader.preheader:                             ; preds = %26
  %28 = add nsw i32 %15, -1
  %29 = add nsw i32 %15, -2
  %xtraiter = and i32 %28, 7
  %30 = icmp ult i32 %29, 7
  br i1 %30, label %.unr-lcssa, label %.preheader.preheader.new

.preheader.preheader.new:                         ; preds = %.preheader.preheader
  %unroll_iter = and i32 %28, -8
  br label %.preheader

.preheader:                                       ; preds = %.preheader, %.preheader.preheader.new
  %31 = phi ptr [ %9, %.preheader.preheader.new ], [ %47, %.preheader ]
  %niter = phi i32 [ 0, %.preheader.preheader.new ], [ %niter.next.7, %.preheader ]
  %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
  %40 = getelementptr inbounds i8, ptr %39, i64 8
  %41 = load ptr, ptr %40, align 8, !tbaa !11
  %42 = getelementptr inbounds i8, ptr %41, i64 8
  %43 = load ptr, ptr %42, align 8, !tbaa !11
  %44 = getelementptr inbounds i8, ptr %43, i64 8
  %45 = load ptr, ptr %44, align 8, !tbaa !11
  %46 = getelementptr inbounds i8, ptr %45, i64 8
  %47 = load ptr, ptr %46, 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 %.unr-lcssa, label %.preheader, !llvm.loop !15

.unr-lcssa:                                       ; preds = %.preheader, %.preheader.preheader
  %.lcssa27.ph = phi ptr [ poison, %.preheader.preheader ], [ %45, %.preheader ]
  %.lcssa.ph = phi ptr [ poison, %.preheader.preheader ], [ %47, %.preheader ]
  %.unr = phi ptr [ %9, %.preheader.preheader ], [ %47, %.preheader ]
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.epilog-lcssa, label %.preheader.epil

.preheader.epil:                                  ; preds = %.unr-lcssa, %.preheader.epil
  %48 = phi ptr [ %50, %.preheader.epil ], [ %.unr, %.unr-lcssa ]
  %epil.iter = phi i32 [ %epil.iter.next, %.preheader.epil ], [ 0, %.unr-lcssa ]
  %49 = getelementptr inbounds i8, ptr %48, i64 8
  %50 = load ptr, ptr %49, 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 %.epilog-lcssa, label %.preheader.epil, !llvm.loop !16

.epilog-lcssa:                                    ; preds = %.preheader.epil, %.unr-lcssa
  %.lcssa27 = phi ptr [ %.lcssa27.ph, %.unr-lcssa ], [ %48, %.preheader.epil ]
  %.lcssa = phi ptr [ %.lcssa.ph, %.unr-lcssa ], [ %50, %.preheader.epil ]
  %51 = getelementptr inbounds i8, ptr %.lcssa27, i64 8
  store ptr %.lcssa27, ptr @prev, align 8, !tbaa !9
  store ptr %.lcssa, ptr @ptr, align 8, !tbaa !9
  %52 = load ptr, ptr @newnode, align 8, !tbaa !9
  store ptr %52, ptr %51, align 8, !tbaa !11
  %53 = getelementptr inbounds i8, ptr %52, i64 8
  store ptr %.lcssa, ptr %53, align 8, !tbaa !11
  br label %54

54:                                               ; preds = %26, %.epilog-lcssa, %24
  %.str.26.sink = phi ptr [ @.str.26, %.epilog-lcssa ], [ @.str.29, %24 ], [ @.str.30, %26 ]
  %55 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) %.str.26.sink)
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %2) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1) #3
  ret void
}

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 = { 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 }

!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, !14}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.unroll.disable"}


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