; LLVM IR from gargarchit/Data_Structure_n_Algorithms :: Linked_List/3.2_InsertNth (InsertNth)

; 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.3 = external hidden unnamed_addr constant [40 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [42 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [25 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local ptr @getnode() #0

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

declare i32 @printf(ptr noundef, ...) #2

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

; 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 void @Insert_front() #0

; Function Attrs: nounwind uwtable
declare dso_local void @Insert_end() #0

; Function Attrs: nounwind uwtable
define dso_local void @InsertNth() #0 {
  %1 = alloca ptr, align 8
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = load ptr, ptr @head, align 8, !tbaa !5
  %9 = icmp eq ptr %8, null
  br i1 %9, label %10, label %13

10:                                               ; preds = %0
  %11 = call i32 (ptr, ...) @printf(ptr noundef @.str.3)
  %12 = call ptr @getnode()
  store ptr %12, ptr @head, align 8, !tbaa !5
  br label %77

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

17:                                               ; preds = %20, %13
  %18 = load ptr, ptr %1, align 8, !tbaa !5
  %19 = icmp ne ptr %18, null
  br i1 %19, label %20, label %26

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

26:                                               ; preds = %17
  %27 = load ptr, ptr @head, align 8, !tbaa !5
  store ptr %27, ptr %1, align 8, !tbaa !5
  %28 = load i32, ptr %4, align 4, !tbaa !9
  %29 = load i32, ptr %5, align 4, !tbaa !9
  %30 = add nsw i32 %29, 1
  %31 = icmp eq i32 %28, %30
  br i1 %31, label %32, label %33

32:                                               ; preds = %26
  call void @Insert_end()
  store i32 1, ptr %6, align 4
  br label %75

33:                                               ; preds = %26
  %34 = load i32, ptr %4, align 4, !tbaa !9
  %35 = icmp eq i32 %34, 1
  br i1 %35, label %36, label %37

36:                                               ; preds = %33
  call void @Insert_front()
  store i32 1, ptr %6, align 4
  br label %75

37:                                               ; preds = %33
  %38 = load i32, ptr %4, align 4, !tbaa !9
  %39 = icmp sle i32 %38, 0
  br i1 %39, label %45, label %40

40:                                               ; preds = %37
  %41 = load i32, ptr %4, align 4, !tbaa !9
  %42 = load i32, ptr %5, align 4, !tbaa !9
  %43 = add nsw i32 %42, 1
  %44 = icmp sgt i32 %41, %43
  br i1 %44, label %45, label %47

45:                                               ; preds = %40, %37
  %46 = call i32 (ptr, ...) @printf(ptr noundef @.str.7)
  store i32 1, ptr %6, align 4
  br label %75

47:                                               ; preds = %40
  br label %48

48:                                               ; preds = %47
  br label %49

49:                                               ; preds = %48
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %50 = call ptr @getnode()
  store ptr %50, ptr %7, align 8, !tbaa !5
  br label %51

51:                                               ; preds = %68, %49
  %52 = load ptr, ptr %1, align 8, !tbaa !5
  %53 = icmp ne ptr %52, null
  br i1 %53, label %54, label %73

54:                                               ; preds = %51
  %55 = load i32, ptr %3, align 4, !tbaa !9
  %56 = add nsw i32 %55, 1
  store i32 %56, ptr %3, align 4, !tbaa !9
  %57 = load i32, ptr %3, align 4, !tbaa !9
  %58 = load i32, ptr %4, align 4, !tbaa !9
  %59 = icmp eq i32 %57, %58
  br i1 %59, label %60, label %68

60:                                               ; preds = %54
  %61 = load ptr, ptr %7, align 8, !tbaa !5
  %62 = load ptr, ptr %2, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.node, ptr %62, i32 0, i32 1
  store ptr %61, ptr %63, align 8, !tbaa !11
  %64 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %64, ptr %2, align 8, !tbaa !5
  %65 = load ptr, ptr %1, align 8, !tbaa !5
  %66 = load ptr, ptr %7, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.node, ptr %66, i32 0, i32 1
  store ptr %65, ptr %67, align 8, !tbaa !11
  store i32 1, ptr %6, align 4
  br label %74

68:                                               ; preds = %54
  %69 = load ptr, ptr %1, align 8, !tbaa !5
  store ptr %69, ptr %2, align 8, !tbaa !5
  %70 = load ptr, ptr %1, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.node, ptr %70, i32 0, i32 1
  %72 = load ptr, ptr %71, align 8, !tbaa !11
  store ptr %72, ptr %1, align 8, !tbaa !5
  br label %51, !llvm.loop !15

73:                                               ; preds = %51
  store i32 0, ptr %6, align 4
  br label %74

74:                                               ; preds = %73, %60
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  br label %75

75:                                               ; preds = %74, %45, %36, %32
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  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 8, ptr %1) #3
  %76 = load i32, ptr %6, align 4
  switch i32 %76, label %78 [
    i32 0, label %77
    i32 1, label %77
  ]

77:                                               ; preds = %75, %75, %10
  ret void

78:                                               ; preds = %75
  unreachable
}

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

@.str.2 = external hidden unnamed_addr constant [3 x i8], align 1
@head = external dso_local local_unnamed_addr global ptr, align 8
@.str.3 = external hidden unnamed_addr constant [40 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [42 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [25 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local ptr @getnode() 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: 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

; 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 void @Insert_front() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @Insert_end() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @InsertNth() local_unnamed_addr #0 {
  %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 %7

4:                                                ; preds = %0
  %5 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3)
  %6 = tail call ptr @getnode()
  store ptr %6, ptr @head, align 8, !tbaa !5
  br label %40

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

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

14:                                               ; preds = %10
  %15 = load ptr, ptr @head, align 8, !tbaa !5
  %16 = load i32, ptr %1, align 4, !tbaa !14
  %17 = add nuw nsw i32 %.01424, 2
  %18 = icmp eq i32 %16, %17
  br i1 %18, label %19, label %20

19:                                               ; preds = %14
  call void @Insert_end()
  br label %.loopexit

20:                                               ; preds = %14
  %21 = icmp eq i32 %16, 1
  br i1 %21, label %22, label %23

22:                                               ; preds = %20
  call void @Insert_front()
  br label %.loopexit

23:                                               ; preds = %20
  %24 = icmp slt i32 %16, 1
  %25 = icmp sgt i32 %16, %17
  %or.cond = or i1 %24, %25
  br i1 %or.cond, label %26, label %28

26:                                               ; preds = %23
  %27 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.7)
  br label %.loopexit

28:                                               ; preds = %23
  %29 = call ptr @getnode()
  %.not2126 = icmp eq ptr %15, null
  br i1 %.not2126, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %28
  %30 = load i32, ptr %1, align 4, !tbaa !14
  %31 = icmp ne i32 %30, 1
  call void @llvm.assume(i1 %31)
  br label %.lr.ph37

32:                                               ; preds = %.lr.ph37
  %33 = add nuw nsw i32 %37, 1
  %34 = icmp eq i32 %33, %30
  br i1 %34, label %._crit_edge, label %.lr.ph37, !llvm.loop !15

._crit_edge:                                      ; preds = %32
  %35 = getelementptr inbounds i8, ptr %.12936, i64 8
  store ptr %29, ptr %35, align 8, !tbaa !9
  %36 = getelementptr inbounds i8, ptr %29, i64 8
  store ptr %39, ptr %36, align 8, !tbaa !9
  br label %.loopexit

.lr.ph37:                                         ; preds = %.lr.ph, %32
  %37 = phi i32 [ %33, %32 ], [ 1, %.lr.ph ]
  %.12936 = phi ptr [ %39, %32 ], [ %15, %.lr.ph ]
  %38 = getelementptr inbounds i8, ptr %.12936, i64 8
  %39 = load ptr, ptr %38, align 8, !tbaa !9
  %.not21 = icmp eq ptr %39, null
  br i1 %.not21, label %.loopexit, label %32, !llvm.loop !15

.loopexit:                                        ; preds = %.lr.ph37, %28, %._crit_edge, %26, %22, %19
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1) #4
  br label %40

40:                                               ; preds = %.loopexit, %4
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: write)
declare void @llvm.assume(i1 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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: write) }
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, !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 = !{!11, !11, i64 0}
!15 = distinct !{!15, !13}


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