; LLVM IR from ahampriyanshu/algo-ds-101 :: Data-Structures/LinkedList/Singly_Linked_List/Detect_Cycle (CycleDetection)

; 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.LinkedListNode = type { i32, ptr }

@stdout = external global ptr, align 8
@.str = external hidden unnamed_addr constant [12 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [3 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local ptr @_insert_node_into_singlylinkedlist(ptr noundef, ptr noundef, i32 noundef) #0

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

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

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @hasCycle(ptr noundef) #0

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

16:                                               ; preds = %0
  %17 = load ptr, ptr %3, align 8, !tbaa !5
  %18 = call noalias ptr @fopen(ptr noundef %17, ptr noundef @.str.1)
  store ptr %18, ptr %2, align 8, !tbaa !5
  br label %19

19:                                               ; preds = %16, %0
  call void @llvm.lifetime.start.p0(i64 1, ptr %4) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  store i32 0, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  store ptr null, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  store ptr null, ptr %7, align 8, !tbaa !5
  %20 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.2, ptr noundef %5)
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  store i32 0, ptr %8, align 4, !tbaa !9
  br label %21

21:                                               ; preds = %37, %19
  %22 = load i32, ptr %8, align 4, !tbaa !9
  %23 = load i32, ptr %5, align 4, !tbaa !9
  %24 = icmp slt i32 %22, %23
  br i1 %24, label %26, label %25

25:                                               ; preds = %21
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  br label %40

26:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  %27 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.3, ptr noundef %9)
  %28 = load ptr, ptr %6, align 8, !tbaa !5
  %29 = load ptr, ptr %7, align 8, !tbaa !5
  %30 = load i32, ptr %9, align 4, !tbaa !9
  %31 = call ptr @_insert_node_into_singlylinkedlist(ptr noundef %28, ptr noundef %29, i32 noundef %30)
  store ptr %31, ptr %7, align 8, !tbaa !5
  %32 = load i32, ptr %8, align 4, !tbaa !9
  %33 = icmp eq i32 %32, 0
  br i1 %33, label %34, label %36

34:                                               ; preds = %26
  %35 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %35, ptr %6, align 8, !tbaa !5
  br label %36

36:                                               ; preds = %34, %26
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  br label %37

37:                                               ; preds = %36
  %38 = load i32, ptr %8, align 4, !tbaa !9
  %39 = add nsw i32 %38, 1
  store i32 %39, ptr %8, align 4, !tbaa !9
  br label %21, !llvm.loop !11

40:                                               ; preds = %25
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  %41 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.3, ptr noundef %10)
  %42 = load i32, ptr %10, align 4, !tbaa !9
  %43 = icmp sgt i32 %42, -1
  br i1 %43, label %44, label %58

44:                                               ; preds = %40
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  %45 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %45, ptr %11, align 8, !tbaa !5
  br label %46

46:                                               ; preds = %50, %44
  %47 = load i32, ptr %10, align 4, !tbaa !9
  %48 = add nsw i32 %47, -1
  store i32 %48, ptr %10, align 4, !tbaa !9
  %49 = icmp ne i32 %47, 0
  br i1 %49, label %50, label %54

50:                                               ; preds = %46
  %51 = load ptr, ptr %11, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.LinkedListNode, ptr %51, i32 0, i32 1
  %53 = load ptr, ptr %52, align 8, !tbaa !13
  store ptr %53, ptr %11, align 8, !tbaa !5
  br label %46, !llvm.loop !15

54:                                               ; preds = %46
  %55 = load ptr, ptr %11, align 8, !tbaa !5
  %56 = load ptr, ptr %7, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.LinkedListNode, ptr %56, i32 0, i32 1
  store ptr %55, ptr %57, align 8, !tbaa !13
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  br label %58

58:                                               ; preds = %54, %40
  %59 = load ptr, ptr %6, align 8, !tbaa !5
  %60 = call zeroext i1 @hasCycle(ptr noundef %59)
  %61 = zext i1 %60 to i8
  store i8 %61, ptr %4, align 1, !tbaa !16
  %62 = load ptr, ptr %2, align 8, !tbaa !5
  %63 = load i8, ptr %4, align 1, !tbaa !16, !range !18, !noundef !19
  %64 = trunc i8 %63 to i1
  %65 = zext i1 %64 to i32
  %66 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %62, ptr noundef @.str.2, i32 noundef %65) #4
  %67 = load ptr, ptr %2, align 8, !tbaa !5
  %68 = call i32 @fclose(ptr noundef %67)
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 1, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #4
  ret i32 0
}

; Function Attrs: nounwind
declare ptr @getenv(ptr noundef) #2

declare noalias ptr @fopen(ptr noundef, ptr noundef) #3

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

; Function Attrs: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #2

declare i32 @fclose(ptr noundef) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 = { "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 }

!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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!14, !6, i64 8}
!14 = !{!"LinkedListNode", !10, i64 0, !6, i64 8}
!15 = distinct !{!15, !12}
!16 = !{!17, !17, i64 0}
!17 = !{!"_Bool", !7, i64 0}
!18 = !{i8 0, i8 2}
!19 = !{}


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

@stdout = external local_unnamed_addr global ptr, align 8
@.str = external hidden unnamed_addr constant [12 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [3 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local ptr @_insert_node_into_singlylinkedlist(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #0

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

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

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @hasCycle(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = load ptr, ptr @stdout, align 8, !tbaa !5
  %5 = tail call ptr @getenv(ptr noundef nonnull @.str) #4
  %.not = icmp eq ptr %5, null
  br i1 %.not, label %8, label %6

6:                                                ; preds = %0
  %7 = tail call noalias ptr @fopen(ptr noundef nonnull %5, ptr noundef nonnull @.str.1)
  br label %8

8:                                                ; preds = %6, %0
  %.018 = phi ptr [ %7, %6 ], [ %4, %0 ]
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1) #4
  store i32 0, ptr %1, align 4, !tbaa !9
  %9 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.2, ptr noundef nonnull %1)
  %10 = load i32, ptr %1, align 4, !tbaa !9
  %11 = icmp sgt i32 %10, 0
  br i1 %11, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %8
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %2) #4
  %12 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.3, ptr noundef nonnull %2)
  %13 = load i32, ptr %2, align 4, !tbaa !9
  %14 = call ptr @_insert_node_into_singlylinkedlist(ptr noundef null, ptr noundef null, i32 noundef %13)
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %2) #4
  %15 = load i32, ptr %1, align 4, !tbaa !9
  %16 = icmp sgt i32 %15, 1
  br i1 %16, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph, %.lr.ph.preheader, %8
  %.017.lcssa = phi ptr [ null, %8 ], [ %14, %.lr.ph.preheader ], [ %14, %.lr.ph ]
  %.016.lcssa = phi ptr [ null, %8 ], [ %14, %.lr.ph.preheader ], [ %26, %.lr.ph ]
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %3) #4
  %17 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.3, ptr noundef nonnull %3)
  %18 = load i32, ptr %3, align 4, !tbaa !9
  %19 = icmp sgt i32 %18, -1
  br i1 %19, label %.preheader, label %48

.preheader:                                       ; preds = %._crit_edge
  %.not2025 = icmp eq i32 %18, 0
  br i1 %.not2025, label %._crit_edge28, label %.lr.ph27.preheader

.lr.ph27.preheader:                               ; preds = %.preheader
  %xtraiter = and i32 %18, 7
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.lr.ph27.prol.loopexit, label %.lr.ph27.prol

.lr.ph27.prol:                                    ; preds = %.lr.ph27.preheader, %.lr.ph27.prol
  %.in.prol = phi i32 [ %20, %.lr.ph27.prol ], [ %18, %.lr.ph27.preheader ]
  %.026.prol = phi ptr [ %22, %.lr.ph27.prol ], [ %.017.lcssa, %.lr.ph27.preheader ]
  %prol.iter = phi i32 [ %prol.iter.next, %.lr.ph27.prol ], [ 0, %.lr.ph27.preheader ]
  %20 = add nsw i32 %.in.prol, -1
  %21 = getelementptr inbounds i8, ptr %.026.prol, i64 8
  %22 = load ptr, ptr %21, align 8, !tbaa !11
  %prol.iter.next = add i32 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i32 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %.lr.ph27.prol.loopexit, label %.lr.ph27.prol, !llvm.loop !13

.lr.ph27.prol.loopexit:                           ; preds = %.lr.ph27.prol, %.lr.ph27.preheader
  %.lcssa.unr = phi ptr [ poison, %.lr.ph27.preheader ], [ %22, %.lr.ph27.prol ]
  %.in.unr = phi i32 [ %18, %.lr.ph27.preheader ], [ %20, %.lr.ph27.prol ]
  %.026.unr = phi ptr [ %.017.lcssa, %.lr.ph27.preheader ], [ %22, %.lr.ph27.prol ]
  %23 = icmp ult i32 %18, 8
  br i1 %23, label %._crit_edge28, label %.lr.ph27

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %.01523 = phi i32 [ %27, %.lr.ph ], [ 1, %.lr.ph.preheader ]
  %.01622 = phi ptr [ %26, %.lr.ph ], [ %14, %.lr.ph.preheader ]
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %2) #4
  %24 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.3, ptr noundef nonnull %2)
  %25 = load i32, ptr %2, align 4, !tbaa !9
  %26 = call ptr @_insert_node_into_singlylinkedlist(ptr noundef %14, ptr noundef %.01622, i32 noundef %25)
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %2) #4
  %27 = add nuw nsw i32 %.01523, 1
  %28 = load i32, ptr %1, align 4, !tbaa !9
  %29 = icmp slt i32 %27, %28
  br i1 %29, label %.lr.ph, label %._crit_edge, !llvm.loop !15

.lr.ph27:                                         ; preds = %.lr.ph27.prol.loopexit, %.lr.ph27
  %.in = phi i32 [ %44, %.lr.ph27 ], [ %.in.unr, %.lr.ph27.prol.loopexit ]
  %.026 = phi ptr [ %46, %.lr.ph27 ], [ %.026.unr, %.lr.ph27.prol.loopexit ]
  %30 = getelementptr inbounds i8, ptr %.026, i64 8
  %31 = load ptr, ptr %30, align 8, !tbaa !11
  %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 = add nsw i32 %.in, -8
  %45 = getelementptr inbounds i8, ptr %43, i64 8
  %46 = load ptr, ptr %45, align 8, !tbaa !11
  %.not20.7 = icmp eq i32 %44, 0
  br i1 %.not20.7, label %._crit_edge28, label %.lr.ph27, !llvm.loop !18

._crit_edge28:                                    ; preds = %.lr.ph27.prol.loopexit, %.lr.ph27, %.preheader
  %.0.lcssa = phi ptr [ %.017.lcssa, %.preheader ], [ %.lcssa.unr, %.lr.ph27.prol.loopexit ], [ %46, %.lr.ph27 ]
  store i32 -1, ptr %3, align 4, !tbaa !9
  %47 = getelementptr inbounds i8, ptr %.016.lcssa, i64 8
  store ptr %.0.lcssa, ptr %47, align 8, !tbaa !11
  br label %48

48:                                               ; preds = %._crit_edge28, %._crit_edge
  %49 = call zeroext i1 @hasCycle(ptr noundef %.017.lcssa)
  %50 = zext i1 %49 to i32
  %51 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %.018, ptr noundef nonnull @.str.2, i32 noundef %50) #4
  %52 = call i32 @fclose(ptr noundef %.018)
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %3) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1) #4
  ret i32 0
}

; Function Attrs: nofree nounwind memory(read)
declare noundef ptr @getenv(ptr nocapture noundef) local_unnamed_addr #2

; Function Attrs: nofree nounwind
declare noalias noundef ptr @fopen(ptr nocapture noundef readonly, ptr nocapture noundef readonly) local_unnamed_addr #3

; Function Attrs: nofree nounwind
declare noundef i32 @__isoc99_scanf(ptr nocapture noundef readonly, ...) local_unnamed_addr #3

; Function Attrs: nofree nounwind
declare noundef i32 @fprintf(ptr nocapture noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #3

; Function Attrs: nofree nounwind
declare noundef i32 @fclose(ptr 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 nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nofree nounwind memory(read) "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 = { 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 #4 = { 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 = !{!"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, !6, i64 8}
!12 = !{!"LinkedListNode", !10, i64 0, !6, i64 8}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.unroll.disable"}
!15 = distinct !{!15, !16, !17}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!"llvm.loop.peeled.count", i32 1}
!18 = distinct !{!18, !16}


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