; LLVM IR from AllenDowney/ExercisesInC :: exercises/ex13 (list_errors)

; 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.3 = external hidden unnamed_addr constant [10 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [87 x i8], align 1
@__PRETTY_FUNCTION__.main = external hidden unnamed_addr constant [11 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [11 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [7 x i8], align 1

; 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

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

; Function Attrs: nounwind
declare void @free(ptr noundef) #3

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #5
  %6 = call ptr @make_node(i32 noundef 2, ptr noundef null)
  store ptr %6, ptr %2, align 8, !tbaa !5
  %7 = call ptr @make_node(i32 noundef 4, ptr noundef null)
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.node, ptr %8, i32 0, i32 1
  store ptr %7, ptr %9, align 8, !tbaa !9
  %10 = call ptr @make_node(i32 noundef 6, ptr noundef null)
  %11 = load ptr, ptr %2, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.node, ptr %11, i32 0, i32 1
  %13 = load ptr, ptr %12, align 8, !tbaa !9
  %14 = getelementptr inbounds %struct.node, ptr %13, i32 0, i32 1
  store ptr %10, ptr %14, align 8, !tbaa !9
  %15 = call i32 @insert_by_index(ptr noundef %2, i32 noundef 1, i32 noundef 0)
  %16 = call i32 @insert_by_index(ptr noundef %2, i32 noundef 3, i32 noundef 2)
  %17 = call i32 @insert_by_index(ptr noundef %2, i32 noundef 5, i32 noundef 4)
  %18 = call i32 @insert_by_index(ptr noundef %2, i32 noundef 7, i32 noundef 6)
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #5
  %19 = call i32 @insert_by_index(ptr noundef %2, i32 noundef 9, i32 noundef 8)
  store i32 %19, ptr %3, align 4, !tbaa !12
  %20 = load i32, ptr %3, align 4, !tbaa !12
  %21 = icmp eq i32 %20, -1
  br i1 %21, label %22, label %23

22:                                               ; preds = %0
  br label %24

23:                                               ; preds = %0
  call void @__assert_fail(ptr noundef @.str.3, ptr noundef @.str.4, i32 noundef 196, ptr noundef @__PRETTY_FUNCTION__.main) #6
  unreachable

24:                                               ; preds = %22
  %25 = call i32 (ptr, ...) @printf(ptr noundef @.str.5)
  call void @print_list(ptr noundef %2)
  %26 = call i32 (ptr, ...) @printf(ptr noundef @.str.6)
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #5
  store ptr null, ptr %4, align 8, !tbaa !5
  %27 = call i32 @insert_by_index(ptr noundef %4, i32 noundef 1, i32 noundef 0)
  call void @print_list(ptr noundef %4)
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #5
  %28 = call ptr @make_something()
  store ptr %28, ptr %5, align 8, !tbaa !5
  %29 = load ptr, ptr %5, align 8, !tbaa !5
  call void @free(ptr noundef %29) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #5
  ret i32 0
}

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) #4

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 "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 = { noreturn 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 #5 = { nounwind }
attributes #6 = { noreturn 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"

@.str.3 = external hidden unnamed_addr constant [10 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [87 x i8], align 1
@__PRETTY_FUNCTION__.main = external hidden unnamed_addr constant [11 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [11 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [7 x i8], align 1

; 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: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #2

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #3

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

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

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

15:                                               ; preds = %0
  call void @__assert_fail(ptr noundef nonnull @.str.3, ptr noundef nonnull @.str.4, i32 noundef 196, ptr noundef nonnull @__PRETTY_FUNCTION__.main) #6
  unreachable

16:                                               ; preds = %0
  %17 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.5)
  call void @print_list(ptr noundef nonnull %1)
  %18 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.6)
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #5
  store ptr null, ptr %2, align 8, !tbaa !5
  %19 = call i32 @insert_by_index(ptr noundef nonnull %2, i32 noundef 1, i32 noundef 0)
  call void @print_list(ptr noundef nonnull %2)
  %20 = call ptr @make_something()
  call void @free(ptr noundef %20) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %1) #5
  ret i32 0
}

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #4

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 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, 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 #4 = { noreturn 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 #5 = { nounwind }
attributes #6 = { noreturn 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,SimplifyCFGPass,TailCallElimPass