; LLVM IR from graphitemaster/libintrusive :: example (avl)

; 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.avl_tree_s = type { ptr, ptr }
%struct.test_avl_t = type { i32, %struct.avl_node_s }
%struct.avl_node_s = type { i64, ptr, ptr, ptr, ptr }

@.str.1 = external hidden unnamed_addr constant [33 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [8 x i8], align 1

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

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

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

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

; Function Attrs: nounwind uwtable
define dso_local void @avl() #2 {
  %1 = alloca %struct.avl_tree_s, align 8
  %2 = alloca %struct.test_avl_t, align 8
  %3 = alloca %struct.test_avl_t, align 8
  %4 = alloca %struct.test_avl_t, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = call i32 (ptr, ...) @printf(ptr noundef @.str.2)
  call void @llvm.lifetime.start.p0(i64 16, ptr %1) #4
  call void @avl_init(ptr noundef %1, ptr noundef null)
  call void @llvm.lifetime.start.p0(i64 48, ptr %2) #4
  call void @llvm.memset.p0.i64(ptr align 8 %2, i8 0, i64 48, i1 false)
  %12 = getelementptr inbounds %struct.test_avl_t, ptr %2, i32 0, i32 0
  store i32 1, ptr %12, align 8
  call void @llvm.lifetime.start.p0(i64 48, ptr %3) #4
  call void @llvm.memset.p0.i64(ptr align 8 %3, i8 0, i64 48, i1 false)
  %13 = getelementptr inbounds %struct.test_avl_t, ptr %3, i32 0, i32 0
  store i32 2, ptr %13, align 8
  call void @llvm.lifetime.start.p0(i64 48, ptr %4) #4
  call void @llvm.memset.p0.i64(ptr align 8 %4, i8 0, i64 48, i1 false)
  %14 = getelementptr inbounds %struct.test_avl_t, ptr %4, i32 0, i32 0
  store i32 3, ptr %14, align 8
  %15 = getelementptr inbounds %struct.test_avl_t, ptr %2, i32 0, i32 1
  call void @avl_insert(ptr noundef %1, ptr noundef %15, ptr noundef @test_avl_compare)
  %16 = getelementptr inbounds %struct.test_avl_t, ptr %3, i32 0, i32 1
  call void @avl_insert(ptr noundef %1, ptr noundef %16, ptr noundef @test_avl_compare)
  %17 = getelementptr inbounds %struct.test_avl_t, ptr %4, i32 0, i32 1
  call void @avl_insert(ptr noundef %1, ptr noundef %17, ptr noundef @test_avl_compare)
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  %18 = call ptr @avl_head(ptr noundef %1)
  store ptr %18, ptr %5, align 8, !tbaa !5
  br label %19

19:                                               ; preds = %64, %0
  %20 = load ptr, ptr %5, align 8, !tbaa !5
  %21 = icmp ne ptr %20, null
  br i1 %21, label %22, label %68

22:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %23 = load ptr, ptr %5, align 8, !tbaa !5
  %24 = call ptr @avl_next(ptr noundef %23)
  store ptr %24, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %25 = load ptr, ptr %5, align 8, !tbaa !5
  %26 = call ptr @avl_prev(ptr noundef %25)
  store ptr %26, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = getelementptr inbounds i8, ptr %27, i64 -8
  store ptr %28, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %29 = load ptr, ptr %6, align 8, !tbaa !5
  %30 = icmp ne ptr %29, null
  br i1 %30, label %31, label %34

31:                                               ; preds = %22
  %32 = load ptr, ptr %6, align 8, !tbaa !5
  %33 = getelementptr inbounds i8, ptr %32, i64 -8
  br label %35

34:                                               ; preds = %22
  br label %35

35:                                               ; preds = %34, %31
  %36 = phi ptr [ %33, %31 ], [ null, %34 ]
  store ptr %36, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %37 = load ptr, ptr %7, align 8, !tbaa !5
  %38 = icmp ne ptr %37, null
  br i1 %38, label %39, label %42

39:                                               ; preds = %35
  %40 = load ptr, ptr %7, align 8, !tbaa !5
  %41 = getelementptr inbounds i8, ptr %40, i64 -8
  br label %43

42:                                               ; preds = %35
  br label %43

43:                                               ; preds = %42, %39
  %44 = phi ptr [ %41, %39 ], [ null, %42 ]
  store ptr %44, ptr %10, align 8, !tbaa !5
  %45 = load ptr, ptr %8, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.test_avl_t, ptr %45, i32 0, i32 0
  %47 = load i32, ptr %46, align 8, !tbaa !9
  %48 = load ptr, ptr %9, align 8, !tbaa !5
  %49 = icmp ne ptr %48, null
  br i1 %49, label %50, label %54

50:                                               ; preds = %43
  %51 = load ptr, ptr %9, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.test_avl_t, ptr %51, i32 0, i32 0
  %53 = load i32, ptr %52, align 8, !tbaa !9
  br label %55

54:                                               ; preds = %43
  br label %55

55:                                               ; preds = %54, %50
  %56 = phi i32 [ %53, %50 ], [ -1, %54 ]
  %57 = load ptr, ptr %10, align 8, !tbaa !5
  %58 = icmp ne ptr %57, null
  br i1 %58, label %59, label %63

59:                                               ; preds = %55
  %60 = load ptr, ptr %10, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.test_avl_t, ptr %60, i32 0, i32 0
  %62 = load i32, ptr %61, align 8, !tbaa !9
  br label %64

63:                                               ; preds = %55
  br label %64

64:                                               ; preds = %63, %59
  %65 = phi i32 [ %62, %59 ], [ -1, %63 ]
  %66 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %47, i32 noundef %56, i32 noundef %65)
  %67 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %67, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  br label %19, !llvm.loop !14

68:                                               ; preds = %19
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 48, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 48, ptr %3) #4
  call void @llvm.lifetime.end.p0(i64 48, ptr %2) #4
  call void @llvm.lifetime.end.p0(i64 16, ptr %1) #4
  ret void
}

declare void @avl_init(ptr noundef, ptr noundef) #1

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3

declare void @avl_insert(ptr noundef, ptr noundef, ptr noundef) #1

declare ptr @avl_head(ptr noundef) #1

declare ptr @avl_next(ptr noundef) #1

declare ptr @avl_prev(ptr noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 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 #3 = { nocallback nofree nounwind willreturn memory(argmem: 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, !11, i64 0}
!10 = !{!"", !11, i64 0, !12, i64 8}
!11 = !{!"int", !7, i64 0}
!12 = !{!"avl_node_s", !13, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32}
!13 = !{!"long", !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


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

%struct.avl_tree_s = type { ptr, ptr }
%struct.test_avl_t = type { i32, %struct.avl_node_s }
%struct.avl_node_s = type { i64, ptr, ptr, ptr, ptr }

@.str.1 = external hidden unnamed_addr constant [33 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [8 x i8], align 1

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

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

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
define dso_local void @avl() local_unnamed_addr #2 {
  %1 = alloca %struct.avl_tree_s, align 8
  %2 = alloca %struct.test_avl_t, align 8
  %3 = alloca %struct.test_avl_t, align 8
  %4 = alloca %struct.test_avl_t, align 8
  %5 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2)
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %1) #5
  call void @avl_init(ptr noundef nonnull %1, ptr noundef null) #5
  call void @llvm.lifetime.start.p0(i64 48, ptr nonnull %2) #5
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(48) %2, i8 0, i64 48, i1 false)
  store i32 1, ptr %2, align 8
  call void @llvm.lifetime.start.p0(i64 48, ptr nonnull %3) #5
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(48) %3, i8 0, i64 48, i1 false)
  store i32 2, ptr %3, align 8
  call void @llvm.lifetime.start.p0(i64 48, ptr nonnull %4) #5
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(48) %4, i8 0, i64 48, i1 false)
  store i32 3, ptr %4, align 8
  %6 = getelementptr inbounds i8, ptr %2, i64 8
  call void @avl_insert(ptr noundef nonnull %1, ptr noundef nonnull %6, ptr noundef nonnull @test_avl_compare) #5
  %7 = getelementptr inbounds i8, ptr %3, i64 8
  call void @avl_insert(ptr noundef nonnull %1, ptr noundef nonnull %7, ptr noundef nonnull @test_avl_compare) #5
  %8 = getelementptr inbounds i8, ptr %4, i64 8
  call void @avl_insert(ptr noundef nonnull %1, ptr noundef nonnull %8, ptr noundef nonnull @test_avl_compare) #5
  %9 = call ptr @avl_head(ptr noundef nonnull %1) #5
  %.not16 = icmp eq ptr %9, null
  br i1 %.not16, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %0, %22
  %.017 = phi ptr [ %10, %22 ], [ %9, %0 ]
  %10 = call ptr @avl_next(ptr noundef nonnull %.017) #5
  %11 = call ptr @avl_prev(ptr noundef nonnull %.017) #5
  %12 = getelementptr inbounds i8, ptr %.017, i64 -8
  %.not14 = icmp eq ptr %10, null
  %.not15 = icmp eq ptr %11, null
  %13 = getelementptr inbounds i8, ptr %11, i64 -8
  %14 = load i32, ptr %12, align 8, !tbaa !5
  br i1 %.not14, label %18, label %15

15:                                               ; preds = %.lr.ph
  %16 = getelementptr inbounds i8, ptr %10, i64 -8
  %17 = load i32, ptr %16, align 8, !tbaa !5
  br label %18

18:                                               ; preds = %.lr.ph, %15
  %19 = phi i32 [ %17, %15 ], [ -1, %.lr.ph ]
  br i1 %.not15, label %22, label %20

20:                                               ; preds = %18
  %21 = load i32, ptr %13, align 8, !tbaa !5
  br label %22

22:                                               ; preds = %18, %20
  %23 = phi i32 [ %21, %20 ], [ -1, %18 ]
  %24 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %14, i32 noundef %19, i32 noundef %23)
  br i1 %.not14, label %._crit_edge, label %.lr.ph, !llvm.loop !13

._crit_edge:                                      ; preds = %22, %0
  call void @llvm.lifetime.end.p0(i64 48, ptr nonnull %4) #5
  call void @llvm.lifetime.end.p0(i64 48, ptr nonnull %3) #5
  call void @llvm.lifetime.end.p0(i64 48, ptr nonnull %2) #5
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %1) #5
  ret void
}

declare void @avl_init(ptr noundef, ptr noundef) local_unnamed_addr #3

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #4

declare void @avl_insert(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #3

declare ptr @avl_head(ptr noundef) local_unnamed_addr #3

declare ptr @avl_next(ptr noundef) local_unnamed_addr #3

declare ptr @avl_prev(ptr noundef) local_unnamed_addr #3

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { 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 #3 = { "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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #5 = { 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 = !{!"", !7, i64 0, !10, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"avl_node_s", !11, i64 0, !12, i64 8, !12, i64 16, !12, i64 24, !12, i64 32}
!11 = !{!"long", !8, i64 0}
!12 = !{!"any pointer", !8, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


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