; LLVM IR from ritikbanger/Hacktoberfest2022-DSA :: 10. Linked List/c lang/SinglyLinkedList (loopInsert)

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

@head = external dso_local global ptr, align 8

; Function Attrs: nounwind uwtable
declare dso_local void @print_list() #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
define dso_local i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  %5 = call noalias ptr @malloc(i64 noundef 16) #3
  store ptr %5, ptr @head, align 8, !tbaa !5
  %6 = load ptr, ptr @head, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.node, ptr %6, i32 0, i32 0
  store i32 99, ptr %7, align 8, !tbaa !9
  %8 = load ptr, ptr @head, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.node, ptr %8, i32 0, i32 1
  store ptr null, ptr %9, align 8, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #4
  %10 = call noalias ptr @malloc(i64 noundef 16) #3
  store ptr %10, ptr %2, align 8, !tbaa !5
  %11 = load ptr, ptr %2, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.node, ptr %11, i32 0, i32 0
  store i32 98, ptr %12, align 8, !tbaa !9
  %13 = load ptr, ptr %2, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.node, ptr %13, i32 0, i32 1
  store ptr null, ptr %14, align 8, !tbaa !12
  %15 = load ptr, ptr %2, align 8, !tbaa !5
  %16 = load ptr, ptr @head, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.node, ptr %16, i32 0, i32 1
  store ptr %15, ptr %17, align 8, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  %18 = load ptr, ptr @head, align 8, !tbaa !5
  store ptr %18, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  store i32 97, ptr %4, align 4, !tbaa !13
  br label %19

19:                                               ; preds = %43, %0
  %20 = load i32, ptr %4, align 4, !tbaa !13
  %21 = icmp sge i32 %20, 90
  br i1 %21, label %23, label %22

22:                                               ; preds = %19
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  br label %46

23:                                               ; preds = %19
  %24 = call noalias ptr @malloc(i64 noundef 16) #3
  store ptr %24, ptr %2, align 8, !tbaa !5
  %25 = load i32, ptr %4, align 4, !tbaa !13
  %26 = load ptr, ptr %2, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.node, ptr %26, i32 0, i32 0
  store i32 %25, ptr %27, align 8, !tbaa !9
  %28 = load ptr, ptr %2, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.node, ptr %28, i32 0, i32 1
  store ptr null, ptr %29, align 8, !tbaa !12
  br label %30

30:                                               ; preds = %35, %23
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.node, ptr %31, i32 0, i32 1
  %33 = load ptr, ptr %32, align 8, !tbaa !12
  %34 = icmp ne ptr %33, null
  br i1 %34, label %35, label %39

35:                                               ; preds = %30
  %36 = load ptr, ptr %3, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.node, ptr %36, i32 0, i32 1
  %38 = load ptr, ptr %37, align 8, !tbaa !12
  store ptr %38, ptr %3, align 8, !tbaa !5
  br label %30, !llvm.loop !14

39:                                               ; preds = %30
  %40 = load ptr, ptr %2, align 8, !tbaa !5
  %41 = load ptr, ptr %3, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.node, ptr %41, i32 0, i32 1
  store ptr %40, ptr %42, align 8, !tbaa !12
  br label %43

43:                                               ; preds = %39
  %44 = load i32, ptr %4, align 4, !tbaa !13
  %45 = add nsw i32 %44, -1
  store i32 %45, ptr %4, align 4, !tbaa !13
  br label %19, !llvm.loop !16

46:                                               ; preds = %22
  call void @print_list()
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #4
  %47 = load i32, ptr %1, align 4
  ret i32 %47
}

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

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


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

@head = external dso_local local_unnamed_addr global ptr, align 8

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

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = tail call noalias dereferenceable_or_null(16) ptr @malloc(i64 noundef 16) #2
  store ptr %1, ptr @head, align 8, !tbaa !5
  store i32 99, ptr %1, align 8, !tbaa !9
  %2 = getelementptr inbounds i8, ptr %1, i64 8
  %3 = tail call noalias dereferenceable_or_null(16) ptr @malloc(i64 noundef 16) #2
  store i32 98, ptr %3, align 8, !tbaa !9
  %4 = getelementptr inbounds i8, ptr %3, i64 8
  store ptr null, ptr %4, align 8, !tbaa !12
  store ptr %3, ptr %2, align 8, !tbaa !12
  %5 = tail call noalias dereferenceable_or_null(16) ptr @malloc(i64 noundef 16) #2
  store i32 97, ptr %5, align 8, !tbaa !9
  %6 = getelementptr inbounds i8, ptr %5, i64 8
  store ptr null, ptr %6, align 8, !tbaa !12
  br label %7

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

10:                                               ; preds = %7
  %11 = getelementptr inbounds i8, ptr %.1, i64 8
  store ptr %5, ptr %11, align 8, !tbaa !12
  %12 = tail call noalias dereferenceable_or_null(16) ptr @malloc(i64 noundef 16) #2
  store i32 96, ptr %12, align 8, !tbaa !9
  %13 = getelementptr inbounds i8, ptr %12, i64 8
  store ptr null, ptr %13, align 8, !tbaa !12
  br label %14

14:                                               ; preds = %14, %10
  %.1.1 = phi ptr [ %.1, %10 ], [ %16, %14 ]
  %15 = getelementptr inbounds i8, ptr %.1.1, i64 8
  %16 = load ptr, ptr %15, align 8, !tbaa !12
  %.not.1 = icmp eq ptr %16, null
  br i1 %.not.1, label %17, label %14, !llvm.loop !13

17:                                               ; preds = %14
  %18 = getelementptr inbounds i8, ptr %.1.1, i64 8
  store ptr %12, ptr %18, align 8, !tbaa !12
  %19 = tail call noalias dereferenceable_or_null(16) ptr @malloc(i64 noundef 16) #2
  store i32 95, ptr %19, align 8, !tbaa !9
  %20 = getelementptr inbounds i8, ptr %19, i64 8
  store ptr null, ptr %20, align 8, !tbaa !12
  br label %21

21:                                               ; preds = %21, %17
  %.1.2 = phi ptr [ %.1.1, %17 ], [ %23, %21 ]
  %22 = getelementptr inbounds i8, ptr %.1.2, i64 8
  %23 = load ptr, ptr %22, align 8, !tbaa !12
  %.not.2 = icmp eq ptr %23, null
  br i1 %.not.2, label %24, label %21, !llvm.loop !13

24:                                               ; preds = %21
  %25 = getelementptr inbounds i8, ptr %.1.2, i64 8
  store ptr %19, ptr %25, align 8, !tbaa !12
  %26 = tail call noalias dereferenceable_or_null(16) ptr @malloc(i64 noundef 16) #2
  store i32 94, ptr %26, align 8, !tbaa !9
  %27 = getelementptr inbounds i8, ptr %26, i64 8
  store ptr null, ptr %27, align 8, !tbaa !12
  br label %28

28:                                               ; preds = %28, %24
  %.1.3 = phi ptr [ %.1.2, %24 ], [ %30, %28 ]
  %29 = getelementptr inbounds i8, ptr %.1.3, i64 8
  %30 = load ptr, ptr %29, align 8, !tbaa !12
  %.not.3 = icmp eq ptr %30, null
  br i1 %.not.3, label %31, label %28, !llvm.loop !13

31:                                               ; preds = %28
  %32 = getelementptr inbounds i8, ptr %.1.3, i64 8
  store ptr %26, ptr %32, align 8, !tbaa !12
  %33 = tail call noalias dereferenceable_or_null(16) ptr @malloc(i64 noundef 16) #2
  store i32 93, ptr %33, align 8, !tbaa !9
  %34 = getelementptr inbounds i8, ptr %33, i64 8
  store ptr null, ptr %34, align 8, !tbaa !12
  br label %35

35:                                               ; preds = %35, %31
  %.1.4 = phi ptr [ %.1.3, %31 ], [ %37, %35 ]
  %36 = getelementptr inbounds i8, ptr %.1.4, i64 8
  %37 = load ptr, ptr %36, align 8, !tbaa !12
  %.not.4 = icmp eq ptr %37, null
  br i1 %.not.4, label %38, label %35, !llvm.loop !13

38:                                               ; preds = %35
  %39 = getelementptr inbounds i8, ptr %.1.4, i64 8
  store ptr %33, ptr %39, align 8, !tbaa !12
  %40 = tail call noalias dereferenceable_or_null(16) ptr @malloc(i64 noundef 16) #2
  store i32 92, ptr %40, align 8, !tbaa !9
  %41 = getelementptr inbounds i8, ptr %40, i64 8
  store ptr null, ptr %41, align 8, !tbaa !12
  br label %42

42:                                               ; preds = %42, %38
  %.1.5 = phi ptr [ %.1.4, %38 ], [ %44, %42 ]
  %43 = getelementptr inbounds i8, ptr %.1.5, i64 8
  %44 = load ptr, ptr %43, align 8, !tbaa !12
  %.not.5 = icmp eq ptr %44, null
  br i1 %.not.5, label %45, label %42, !llvm.loop !13

45:                                               ; preds = %42
  %46 = getelementptr inbounds i8, ptr %.1.5, i64 8
  store ptr %40, ptr %46, align 8, !tbaa !12
  %47 = tail call noalias dereferenceable_or_null(16) ptr @malloc(i64 noundef 16) #2
  store i32 91, ptr %47, align 8, !tbaa !9
  %48 = getelementptr inbounds i8, ptr %47, i64 8
  store ptr null, ptr %48, align 8, !tbaa !12
  br label %49

49:                                               ; preds = %49, %45
  %.1.6 = phi ptr [ %.1.5, %45 ], [ %51, %49 ]
  %50 = getelementptr inbounds i8, ptr %.1.6, i64 8
  %51 = load ptr, ptr %50, align 8, !tbaa !12
  %.not.6 = icmp eq ptr %51, null
  br i1 %.not.6, label %52, label %49, !llvm.loop !13

52:                                               ; preds = %49
  %53 = getelementptr inbounds i8, ptr %.1.6, i64 8
  store ptr %47, ptr %53, align 8, !tbaa !12
  %54 = tail call noalias dereferenceable_or_null(16) ptr @malloc(i64 noundef 16) #2
  store i32 90, ptr %54, align 8, !tbaa !9
  %55 = getelementptr inbounds i8, ptr %54, i64 8
  store ptr null, ptr %55, align 8, !tbaa !12
  br label %56

56:                                               ; preds = %56, %52
  %.1.7 = phi ptr [ %.1.6, %52 ], [ %58, %56 ]
  %57 = getelementptr inbounds i8, ptr %.1.7, i64 8
  %58 = load ptr, ptr %57, align 8, !tbaa !12
  %.not.7 = icmp eq ptr %58, null
  br i1 %.not.7, label %59, label %56, !llvm.loop !13

59:                                               ; preds = %56
  %60 = getelementptr inbounds i8, ptr %.1.7, i64 8
  store ptr %54, ptr %60, align 8, !tbaa !12
  tail call void @print_list()
  ret i32 0
}

; 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

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


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