; LLVM IR from gargarchit/Data_Structure_n_Algorithms :: Linked_List (2_GetNth)

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

@.str.2 = external hidden unnamed_addr constant [3 x i8], align 1
@head = external dso_local global ptr, align 8
@.str.4 = external hidden unnamed_addr constant [30 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [53 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [42 x i8], align 1
@.str.8 = external hidden unnamed_addr constant [39 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
define dso_local void @GetNth() #2 {
  %1 = alloca i32, align 4
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = load ptr, ptr @head, align 8, !tbaa !5
  %7 = icmp eq ptr %6, null
  br i1 %7, label %8, label %10

8:                                                ; preds = %0
  %9 = call i32 (ptr, ...) @printf(ptr noundef @.str.4)
  br label %56

10:                                               ; preds = %0
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  store i32 0, ptr %1, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #3
  %11 = load ptr, ptr @head, align 8, !tbaa !5
  store ptr %11, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  store i32 0, ptr %4, align 4, !tbaa !9
  %12 = call i32 (ptr, ...) @printf(ptr noundef @.str.6)
  %13 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.2, ptr noundef %3)
  br label %14

14:                                               ; preds = %17, %10
  %15 = load ptr, ptr %2, align 8, !tbaa !5
  %16 = icmp ne ptr %15, null
  br i1 %16, label %17, label %23

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

23:                                               ; preds = %14
  %24 = load ptr, ptr @head, align 8, !tbaa !5
  store ptr %24, ptr %2, align 8, !tbaa !5
  %25 = load i32, ptr %3, align 4, !tbaa !9
  %26 = load i32, ptr %4, align 4, !tbaa !9
  %27 = icmp sgt i32 %25, %26
  br i1 %27, label %31, label %28

28:                                               ; preds = %23
  %29 = load i32, ptr %3, align 4, !tbaa !9
  %30 = icmp sle i32 %29, 0
  br i1 %30, label %31, label %33

31:                                               ; preds = %28, %23
  %32 = call i32 (ptr, ...) @printf(ptr noundef @.str.7)
  store i32 1, ptr %5, align 4
  br label %54

33:                                               ; preds = %28
  br label %34

34:                                               ; preds = %49, %33
  %35 = load ptr, ptr %2, align 8, !tbaa !5
  %36 = icmp ne ptr %35, null
  br i1 %36, label %37, label %53

37:                                               ; preds = %34
  %38 = load i32, ptr %1, align 4, !tbaa !9
  %39 = add nsw i32 %38, 1
  store i32 %39, ptr %1, align 4, !tbaa !9
  %40 = load i32, ptr %3, align 4, !tbaa !9
  %41 = load i32, ptr %1, align 4, !tbaa !9
  %42 = icmp eq i32 %40, %41
  br i1 %42, label %43, label %49

43:                                               ; preds = %37
  %44 = load i32, ptr %3, align 4, !tbaa !9
  %45 = load ptr, ptr %2, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.node, ptr %45, i32 0, i32 0
  %47 = load i32, ptr %46, align 8, !tbaa !15
  %48 = call i32 (ptr, ...) @printf(ptr noundef @.str.8, i32 noundef %44, i32 noundef %47)
  store i32 1, ptr %5, align 4
  br label %54

49:                                               ; preds = %37
  %50 = load ptr, ptr %2, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.node, ptr %50, i32 0, i32 1
  %52 = load ptr, ptr %51, align 8, !tbaa !11
  store ptr %52, ptr %2, align 8, !tbaa !5
  br label %34, !llvm.loop !16

53:                                               ; preds = %34
  store i32 0, ptr %5, align 4
  br label %54

54:                                               ; preds = %53, %43, %31
  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 8, ptr %2) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  %55 = load i32, ptr %5, align 4
  switch i32 %55, label %57 [
    i32 0, label %56
    i32 1, label %56
  ]

56:                                               ; preds = %54, %54, %8
  ret void

57:                                               ; preds = %54
  unreachable
}

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

@.str.2 = external hidden unnamed_addr constant [3 x i8], align 1
@head = external dso_local local_unnamed_addr global ptr, align 8
@.str.4 = external hidden unnamed_addr constant [30 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [53 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [42 x i8], align 1
@.str.8 = external hidden unnamed_addr constant [39 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: nofree nounwind uwtable
define dso_local void @GetNth() local_unnamed_addr #2 {
  %1 = alloca i32, align 4
  %2 = load ptr, ptr @head, align 8, !tbaa !5
  %3 = icmp eq ptr %2, null
  br i1 %3, label %4, label %6

4:                                                ; preds = %0
  %5 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.4)
  br label %26

6:                                                ; preds = %0
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1) #3
  %7 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.6)
  %8 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.2, ptr noundef nonnull %1)
  br label %9

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

13:                                               ; preds = %9
  %14 = load i32, ptr %1, align 4
  %15 = icmp sgt i32 %14, %10
  %16 = icmp slt i32 %14, 1
  %or.cond = or i1 %15, %16
  br i1 %or.cond, label %17, label %.preheader

.preheader:                                       ; preds = %13
  %.118 = load ptr, ptr @head, align 8, !tbaa !5
  %.not1419 = icmp eq ptr %.118, null
  br i1 %.not1419, label %.loopexit, label %.lr.ph

17:                                               ; preds = %13
  %18 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.7)
  br label %.loopexit

.lr.ph:                                           ; preds = %.preheader, %24
  %.121 = phi ptr [ %.1, %24 ], [ %.118, %.preheader ]
  %.01020 = phi i32 [ %19, %24 ], [ 0, %.preheader ]
  %19 = add nuw nsw i32 %.01020, 1
  %20 = icmp eq i32 %14, %19
  br i1 %20, label %21, label %24

21:                                               ; preds = %.lr.ph
  %22 = load i32, ptr %.121, align 8, !tbaa !14
  %23 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.8, i32 noundef %14, i32 noundef %22)
  br label %.loopexit

24:                                               ; preds = %.lr.ph
  %25 = getelementptr inbounds i8, ptr %.121, i64 8
  %.1 = load ptr, ptr %25, align 8, !tbaa !5
  %.not14 = icmp eq ptr %.1, null
  br i1 %.not14, label %.loopexit, label %.lr.ph, !llvm.loop !15

.loopexit:                                        ; preds = %24, %.preheader, %21, %17
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1) #3
  br label %26

26:                                               ; preds = %.loopexit, %4
  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 = { 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 #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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 8}
!10 = !{!"node", !11, i64 0, !6, i64 8}
!11 = !{!"int", !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!10, !11, i64 0}
!15 = distinct !{!15, !13}


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