; LLVM IR from wgtdkp/wgtcc :: test/data-structure (tree)

; 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.tree = type { i32, ptr, ptr }

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

; Function Attrs: nounwind uwtable
define dso_local ptr @tree_delete(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = icmp eq ptr %9, null
  br i1 %10, label %11, label %12

11:                                               ; preds = %2
  store ptr null, ptr %3, align 8
  br label %85

12:                                               ; preds = %2
  %13 = load i32, ptr %5, align 4, !tbaa !9
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.tree, ptr %14, i32 0, i32 0
  %16 = load i32, ptr %15, align 8, !tbaa !11
  %17 = icmp slt i32 %13, %16
  br i1 %17, label %18, label %26

18:                                               ; preds = %12
  %19 = load ptr, ptr %4, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.tree, ptr %19, i32 0, i32 1
  %21 = load ptr, ptr %20, align 8, !tbaa !13
  %22 = load i32, ptr %5, align 4, !tbaa !9
  %23 = call ptr @tree_delete(ptr noundef %21, i32 noundef %22)
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.tree, ptr %24, i32 0, i32 1
  store ptr %23, ptr %25, align 8, !tbaa !13
  br label %83

26:                                               ; preds = %12
  %27 = load i32, ptr %5, align 4, !tbaa !9
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.tree, ptr %28, i32 0, i32 0
  %30 = load i32, ptr %29, align 8, !tbaa !11
  %31 = icmp sgt i32 %27, %30
  br i1 %31, label %32, label %40

32:                                               ; preds = %26
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.tree, ptr %33, i32 0, i32 2
  %35 = load ptr, ptr %34, align 8, !tbaa !14
  %36 = load i32, ptr %5, align 4, !tbaa !9
  %37 = call ptr @tree_delete(ptr noundef %35, i32 noundef %36)
  %38 = load ptr, ptr %4, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.tree, ptr %38, i32 0, i32 2
  store ptr %37, ptr %39, align 8, !tbaa !14
  br label %82

40:                                               ; preds = %26
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.tree, ptr %41, i32 0, i32 2
  %43 = load ptr, ptr %42, align 8, !tbaa !14
  %44 = icmp eq ptr %43, null
  br i1 %44, label %45, label %51

45:                                               ; preds = %40
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %46 = load ptr, ptr %4, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.tree, ptr %46, i32 0, i32 1
  %48 = load ptr, ptr %47, align 8, !tbaa !13
  store ptr %48, ptr %6, align 8, !tbaa !5
  %49 = load ptr, ptr %4, align 8, !tbaa !5
  call void @free(ptr noundef %49) #3
  %50 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %50, ptr %3, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  br label %85

51:                                               ; preds = %40
  %52 = load ptr, ptr %4, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.tree, ptr %52, i32 0, i32 1
  %54 = load ptr, ptr %53, align 8, !tbaa !13
  %55 = icmp eq ptr %54, null
  br i1 %55, label %56, label %62

56:                                               ; preds = %51
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %57 = load ptr, ptr %4, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.tree, ptr %57, i32 0, i32 2
  %59 = load ptr, ptr %58, align 8, !tbaa !14
  store ptr %59, ptr %7, align 8, !tbaa !5
  %60 = load ptr, ptr %4, align 8, !tbaa !5
  call void @free(ptr noundef %60) #3
  %61 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %61, ptr %3, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  br label %85

62:                                               ; preds = %51
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %63 = load ptr, ptr %4, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.tree, ptr %63, i32 0, i32 1
  %65 = load ptr, ptr %64, align 8, !tbaa !13
  %66 = call ptr @tree_find_max(ptr noundef %65)
  store ptr %66, ptr %8, align 8, !tbaa !5
  %67 = load ptr, ptr %8, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.tree, ptr %67, i32 0, i32 0
  %69 = load i32, ptr %68, align 8, !tbaa !11
  %70 = load ptr, ptr %4, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.tree, ptr %70, i32 0, i32 0
  store i32 %69, ptr %71, align 8, !tbaa !11
  %72 = load ptr, ptr %4, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.tree, ptr %72, i32 0, i32 1
  %74 = load ptr, ptr %73, align 8, !tbaa !13
  %75 = load ptr, ptr %8, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.tree, ptr %75, i32 0, i32 0
  %77 = load i32, ptr %76, align 8, !tbaa !11
  %78 = call ptr @tree_delete(ptr noundef %74, i32 noundef %77)
  %79 = load ptr, ptr %4, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.tree, ptr %79, i32 0, i32 1
  store ptr %78, ptr %80, align 8, !tbaa !13
  %81 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %81, ptr %3, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  br label %85

82:                                               ; preds = %32
  br label %83

83:                                               ; preds = %82, %18
  %84 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %84, ptr %3, align 8
  br label %85

85:                                               ; preds = %83, %62, %56, %45, %11
  %86 = load ptr, ptr %3, align 8
  ret ptr %86
}

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

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

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 = { 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, !10, i64 0}
!12 = !{!"tree", !10, i64 0, !6, i64 8, !6, i64 16}
!13 = !{!12, !6, i64 8}
!14 = !{!12, !6, i64 16}


; 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 @tree_find_max(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local ptr @tree_delete(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp eq ptr %0, null
  br i1 %3, label %common.ret32, label %4

4:                                                ; preds = %2
  %5 = load i32, ptr %0, align 8, !tbaa !5
  %6 = icmp sgt i32 %5, %1
  br i1 %6, label %7, label %11

common.ret32:                                     ; preds = %2, %24, %21, %25, %15, %7
  %common.ret32.op = phi ptr [ %0, %7 ], [ %0, %15 ], [ %0, %25 ], [ %20, %21 ], [ %14, %24 ], [ null, %2 ]
  ret ptr %common.ret32.op

7:                                                ; preds = %4
  %8 = getelementptr inbounds i8, ptr %0, i64 8
  %9 = load ptr, ptr %8, align 8, !tbaa !11
  %10 = tail call ptr @tree_delete(ptr noundef %9, i32 noundef %1)
  store ptr %10, ptr %8, align 8, !tbaa !11
  br label %common.ret32

11:                                               ; preds = %4
  %12 = icmp slt i32 %5, %1
  %13 = getelementptr inbounds i8, ptr %0, i64 16
  %14 = load ptr, ptr %13, align 8, !tbaa !12
  br i1 %12, label %15, label %17

15:                                               ; preds = %11
  %16 = tail call ptr @tree_delete(ptr noundef %14, i32 noundef %1)
  store ptr %16, ptr %13, align 8, !tbaa !12
  br label %common.ret32

17:                                               ; preds = %11
  %18 = icmp eq ptr %14, null
  %19 = getelementptr inbounds i8, ptr %0, i64 8
  %20 = load ptr, ptr %19, align 8, !tbaa !11
  br i1 %18, label %21, label %22

21:                                               ; preds = %17
  tail call void @free(ptr noundef nonnull %0) #2
  br label %common.ret32

22:                                               ; preds = %17
  %23 = icmp eq ptr %20, null
  br i1 %23, label %24, label %25

24:                                               ; preds = %22
  tail call void @free(ptr noundef nonnull %0) #2
  br label %common.ret32

25:                                               ; preds = %22
  %26 = tail call ptr @tree_find_max(ptr noundef nonnull %20)
  %27 = load i32, ptr %26, align 8, !tbaa !5
  store i32 %27, ptr %0, align 8, !tbaa !5
  %28 = load ptr, ptr %19, align 8, !tbaa !11
  %29 = tail call ptr @tree_delete(ptr noundef %28, i32 noundef %27)
  store ptr %29, ptr %19, align 8, !tbaa !11
  br label %common.ret32
}

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture 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 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 #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, !7, i64 0}
!6 = !{!"tree", !7, i64 0, !10, i64 8, !10, i64 16}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !10, i64 8}
!12 = !{!6, !10, i64 16}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,SROAPass,SimplifyCFGPass,TailCallElimPass