; LLVM IR from Ayon-SSP/The-SDE-Prep :: 1]_Data Structures/2]_Linked List/Linked List Problems (Doublelinkedlist)

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

@first = external dso_local global ptr, align 8
@.str = external hidden unnamed_addr constant [25 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [24 x i8], align 1

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

16:                                               ; preds = %40, %1
  %17 = load i32, ptr %5, align 4, !tbaa !5
  %18 = load i32, ptr %2, align 4, !tbaa !5
  %19 = icmp slt i32 %17, %18
  br i1 %19, label %20, label %43

20:                                               ; preds = %16
  %21 = call noalias ptr @malloc(i64 noundef 24) #5
  store ptr %21, ptr %3, align 8, !tbaa !9
  %22 = load i32, ptr %5, align 4, !tbaa !5
  %23 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i32 noundef %22)
  %24 = load ptr, ptr %3, align 8, !tbaa !9
  %25 = getelementptr inbounds %struct.Node, ptr %24, i32 0, i32 1
  %26 = load i32, ptr %25, align 8, !tbaa !14
  %27 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, i32 noundef %26)
  %28 = load ptr, ptr %4, align 8, !tbaa !9
  %29 = getelementptr inbounds %struct.Node, ptr %28, i32 0, i32 2
  %30 = load ptr, ptr %29, align 8, !tbaa !11
  %31 = load ptr, ptr %3, align 8, !tbaa !9
  %32 = getelementptr inbounds %struct.Node, ptr %31, i32 0, i32 2
  store ptr %30, ptr %32, align 8, !tbaa !11
  %33 = load ptr, ptr %4, align 8, !tbaa !9
  %34 = load ptr, ptr %3, align 8, !tbaa !9
  %35 = getelementptr inbounds %struct.Node, ptr %34, i32 0, i32 0
  store ptr %33, ptr %35, align 8, !tbaa !13
  %36 = load ptr, ptr %3, align 8, !tbaa !9
  %37 = load ptr, ptr %4, align 8, !tbaa !9
  %38 = getelementptr inbounds %struct.Node, ptr %37, i32 0, i32 2
  store ptr %36, ptr %38, align 8, !tbaa !11
  %39 = load ptr, ptr %3, align 8, !tbaa !9
  store ptr %39, ptr %4, align 8, !tbaa !9
  br label %40

40:                                               ; preds = %20
  %41 = load i32, ptr %5, align 4, !tbaa !5
  %42 = add nsw i32 %41, 1
  store i32 %42, ptr %5, align 4, !tbaa !5
  br label %16, !llvm.loop !15

43:                                               ; preds = %16
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  ret void
}

; 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

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

; 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 }
attributes #5 = { 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, !10, i64 16}
!12 = !{!"Node", !10, i64 0, !6, i64 8, !10, i64 16}
!13 = !{!12, !10, i64 0}
!14 = !{!12, !6, i64 8}
!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"

@first = external dso_local local_unnamed_addr global ptr, align 8
@.str = external hidden unnamed_addr constant [25 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [24 x i8], align 1

; Function Attrs: nofree nounwind uwtable
define dso_local void @create(i32 noundef %0) local_unnamed_addr #0 {
  %2 = tail call noalias dereferenceable_or_null(24) ptr @malloc(i64 noundef 24) #3
  store ptr %2, ptr @first, align 8, !tbaa !5
  %3 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str)
  %4 = load ptr, ptr @first, align 8, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %4, i64 8
  %6 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull %5)
  %7 = load ptr, ptr @first, align 8, !tbaa !5
  %8 = getelementptr inbounds i8, ptr %7, i64 16
  store ptr null, ptr %8, align 8, !tbaa !9
  store ptr null, ptr %7, align 8, !tbaa !12
  %9 = icmp sgt i32 %0, 1
  br i1 %9, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %1
  %10 = load ptr, ptr @first, align 8, !tbaa !5
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %.013 = phi i32 [ %19, %.lr.ph ], [ 1, %.lr.ph.preheader ]
  %.01112 = phi ptr [ %11, %.lr.ph ], [ %10, %.lr.ph.preheader ]
  %11 = tail call noalias dereferenceable_or_null(24) ptr @malloc(i64 noundef 24) #3
  %12 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %.013)
  %13 = getelementptr inbounds i8, ptr %11, i64 8
  %14 = load i32, ptr %13, align 8, !tbaa !13
  %15 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, i32 noundef %14)
  %16 = getelementptr inbounds i8, ptr %.01112, i64 16
  %17 = load ptr, ptr %16, align 8, !tbaa !9
  %18 = getelementptr inbounds i8, ptr %11, i64 16
  store ptr %17, ptr %18, align 8, !tbaa !9
  store ptr %.01112, ptr %11, align 8, !tbaa !12
  store ptr %11, ptr %16, align 8, !tbaa !9
  %19 = add nuw nsw i32 %.013, 1
  %exitcond.not = icmp eq i32 %19, %0
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !14

._crit_edge:                                      ; preds = %.lr.ph, %1
  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: nofree nounwind
declare noundef i32 @__isoc99_scanf(ptr nocapture noundef readonly, ...) local_unnamed_addr #2

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


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