; LLVM IR from AllenDowney/ExercisesInC :: exercises/ex06 (list)

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

; Function Attrs: nounwind uwtable
declare dso_local ptr @make_node(i32 noundef, ptr 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 void @print_list(ptr noundef) #0

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

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @reverse(ptr noundef) #0

; Function Attrs: nounwind uwtable
define dso_local i32 @main() #0 {
  %1 = alloca ptr, align 8
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #2
  %4 = call ptr @make_node(i32 noundef 1, ptr noundef null)
  store ptr %4, ptr %1, align 8, !tbaa !5
  %5 = call ptr @make_node(i32 noundef 2, ptr noundef null)
  %6 = load ptr, ptr %1, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.node, ptr %6, i32 0, i32 1
  store ptr %5, ptr %7, align 8, !tbaa !9
  %8 = call ptr @make_node(i32 noundef 3, ptr noundef null)
  %9 = load ptr, ptr %1, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.node, ptr %9, i32 0, i32 1
  %11 = load ptr, ptr %10, align 8, !tbaa !9
  %12 = getelementptr inbounds %struct.node, ptr %11, i32 0, i32 1
  store ptr %8, ptr %12, align 8, !tbaa !9
  %13 = call ptr @make_node(i32 noundef 4, ptr noundef null)
  %14 = load ptr, ptr %1, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.node, ptr %14, i32 0, i32 1
  %16 = load ptr, ptr %15, align 8, !tbaa !9
  %17 = getelementptr inbounds %struct.node, ptr %16, i32 0, i32 1
  %18 = load ptr, ptr %17, align 8, !tbaa !9
  %19 = getelementptr inbounds %struct.node, ptr %18, i32 0, i32 1
  store ptr %13, ptr %19, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #2
  store ptr %1, ptr %2, align 8, !tbaa !5
  %20 = load ptr, ptr %2, align 8, !tbaa !5
  call void @print_list(ptr noundef %20)
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  %21 = load ptr, ptr %2, align 8, !tbaa !5
  %22 = call i32 @pop(ptr noundef %21)
  store i32 %22, ptr %3, align 4, !tbaa !12
  %23 = load ptr, ptr %2, align 8, !tbaa !5
  call void @print_list(ptr noundef %23)
  %24 = load ptr, ptr %2, align 8, !tbaa !5
  %25 = load i32, ptr %3, align 4, !tbaa !12
  %26 = add nsw i32 %25, 10
  call void @push(ptr noundef %24, i32 noundef %26)
  %27 = load ptr, ptr %2, align 8, !tbaa !5
  call void @print_list(ptr noundef %27)
  %28 = load ptr, ptr %2, align 8, !tbaa !5
  %29 = call i32 @remove_by_value(ptr noundef %28, i32 noundef 3)
  %30 = load ptr, ptr %2, align 8, !tbaa !5
  call void @print_list(ptr noundef %30)
  %31 = load ptr, ptr %2, align 8, !tbaa !5
  %32 = call i32 @remove_by_value(ptr noundef %31, i32 noundef 7)
  %33 = load ptr, ptr %2, align 8, !tbaa !5
  call void @print_list(ptr noundef %33)
  %34 = load ptr, ptr %2, align 8, !tbaa !5
  call void @reverse(ptr noundef %34)
  %35 = load ptr, ptr %2, align 8, !tbaa !5
  call void @print_list(ptr noundef %35)
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #2
  ret i32 0
}

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 }

!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 = !{!11, !11, i64 0}


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

; Function Attrs: nounwind uwtable
declare dso_local ptr @make_node(i32 noundef, ptr 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 void @print_list(ptr noundef) local_unnamed_addr #0

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

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @reverse(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %1) #2
  %2 = tail call ptr @make_node(i32 noundef 1, ptr noundef null)
  store ptr %2, ptr %1, align 8, !tbaa !5
  %3 = tail call ptr @make_node(i32 noundef 2, ptr noundef null)
  %4 = getelementptr inbounds i8, ptr %2, i64 8
  store ptr %3, ptr %4, align 8, !tbaa !9
  %5 = tail call ptr @make_node(i32 noundef 3, ptr noundef null)
  %6 = load ptr, ptr %4, align 8, !tbaa !9
  %7 = getelementptr inbounds i8, ptr %6, i64 8
  store ptr %5, ptr %7, align 8, !tbaa !9
  %8 = tail call ptr @make_node(i32 noundef 4, ptr noundef null)
  %9 = load ptr, ptr %4, align 8, !tbaa !9
  %10 = getelementptr inbounds i8, ptr %9, i64 8
  %11 = load ptr, ptr %10, align 8, !tbaa !9
  %12 = getelementptr inbounds i8, ptr %11, i64 8
  store ptr %8, ptr %12, align 8, !tbaa !9
  call void @print_list(ptr noundef nonnull %1)
  %13 = call i32 @pop(ptr noundef nonnull %1)
  call void @print_list(ptr noundef nonnull %1)
  %14 = add nsw i32 %13, 10
  call void @push(ptr noundef nonnull %1, i32 noundef %14)
  call void @print_list(ptr noundef nonnull %1)
  %15 = call i32 @remove_by_value(ptr noundef nonnull %1, i32 noundef 3)
  call void @print_list(ptr noundef nonnull %1)
  %16 = call i32 @remove_by_value(ptr noundef nonnull %1, i32 noundef 7)
  call void @print_list(ptr noundef nonnull %1)
  call void @reverse(ptr noundef nonnull %1)
  call void @print_list(ptr noundef nonnull %1)
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %1) #2
  ret i32 0
}

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


; Effective O3 passes: GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,TailCallElimPass