; LLVM IR from LeoVen/C-Macro-Collections :: examples (dfs)

; 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.BinaryTreeIter = type { ptr, ptr, ptr, ptr, i64, i8, i8 }
%struct.BinaryTree = type { ptr, i64, i32, ptr, ptr }
%struct.BinaryTreeNode = type { i32, i8, ptr, ptr, ptr }

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @tree_empty(ptr noundef) #0

; Function Attrs: nounwind uwtable
define dso_local void @tree_iter_start(ptr dead_on_unwind noalias writable sret(%struct.BinaryTreeIter) align 8 %0, ptr noundef %1) #0 {
  %3 = alloca ptr, align 8
  store ptr %1, ptr %3, align 8, !tbaa !5
  %4 = load ptr, ptr %3, align 8, !tbaa !5
  %5 = getelementptr inbounds %struct.BinaryTreeIter, ptr %0, i32 0, i32 0
  store ptr %4, ptr %5, align 8, !tbaa !9
  %6 = load ptr, ptr %3, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.BinaryTree, ptr %6, i32 0, i32 0
  %8 = load ptr, ptr %7, align 8, !tbaa !13
  %9 = getelementptr inbounds %struct.BinaryTreeIter, ptr %0, i32 0, i32 1
  store ptr %8, ptr %9, align 8, !tbaa !16
  %10 = getelementptr inbounds %struct.BinaryTreeIter, ptr %0, i32 0, i32 2
  store ptr null, ptr %10, align 8, !tbaa !17
  %11 = getelementptr inbounds %struct.BinaryTreeIter, ptr %0, i32 0, i32 3
  store ptr null, ptr %11, align 8, !tbaa !18
  %12 = getelementptr inbounds %struct.BinaryTreeIter, ptr %0, i32 0, i32 4
  store i64 0, ptr %12, align 8, !tbaa !19
  %13 = getelementptr inbounds %struct.BinaryTreeIter, ptr %0, i32 0, i32 5
  store i8 1, ptr %13, align 8, !tbaa !20
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  %15 = call zeroext i1 @tree_empty(ptr noundef %14)
  %16 = getelementptr inbounds %struct.BinaryTreeIter, ptr %0, i32 0, i32 6
  %17 = zext i1 %15 to i8
  store i8 %17, ptr %16, align 1, !tbaa !21
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = call zeroext i1 @tree_empty(ptr noundef %18)
  br i1 %19, label %54, label %20

20:                                               ; preds = %2
  br label %21

21:                                               ; preds = %27, %20
  %22 = getelementptr inbounds %struct.BinaryTreeIter, ptr %0, i32 0, i32 1
  %23 = load ptr, ptr %22, align 8, !tbaa !16
  %24 = getelementptr inbounds %struct.BinaryTreeNode, ptr %23, i32 0, i32 3
  %25 = load ptr, ptr %24, align 8, !tbaa !22
  %26 = icmp ne ptr %25, null
  br i1 %26, label %27, label %33

27:                                               ; preds = %21
  %28 = getelementptr inbounds %struct.BinaryTreeIter, ptr %0, i32 0, i32 1
  %29 = load ptr, ptr %28, align 8, !tbaa !16
  %30 = getelementptr inbounds %struct.BinaryTreeNode, ptr %29, i32 0, i32 3
  %31 = load ptr, ptr %30, align 8, !tbaa !22
  %32 = getelementptr inbounds %struct.BinaryTreeIter, ptr %0, i32 0, i32 1
  store ptr %31, ptr %32, align 8, !tbaa !16
  br label %21, !llvm.loop !24

33:                                               ; preds = %21
  %34 = getelementptr inbounds %struct.BinaryTreeIter, ptr %0, i32 0, i32 1
  %35 = load ptr, ptr %34, align 8, !tbaa !16
  %36 = getelementptr inbounds %struct.BinaryTreeIter, ptr %0, i32 0, i32 2
  store ptr %35, ptr %36, align 8, !tbaa !17
  %37 = load ptr, ptr %3, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.BinaryTree, ptr %37, i32 0, i32 0
  %39 = load ptr, ptr %38, align 8, !tbaa !13
  %40 = getelementptr inbounds %struct.BinaryTreeIter, ptr %0, i32 0, i32 3
  store ptr %39, ptr %40, align 8, !tbaa !18
  br label %41

41:                                               ; preds = %47, %33
  %42 = getelementptr inbounds %struct.BinaryTreeIter, ptr %0, i32 0, i32 3
  %43 = load ptr, ptr %42, align 8, !tbaa !18
  %44 = getelementptr inbounds %struct.BinaryTreeNode, ptr %43, i32 0, i32 2
  %45 = load ptr, ptr %44, align 8, !tbaa !26
  %46 = icmp ne ptr %45, null
  br i1 %46, label %47, label %53

47:                                               ; preds = %41
  %48 = getelementptr inbounds %struct.BinaryTreeIter, ptr %0, i32 0, i32 3
  %49 = load ptr, ptr %48, align 8, !tbaa !18
  %50 = getelementptr inbounds %struct.BinaryTreeNode, ptr %49, i32 0, i32 2
  %51 = load ptr, ptr %50, align 8, !tbaa !26
  %52 = getelementptr inbounds %struct.BinaryTreeIter, ptr %0, i32 0, i32 3
  store ptr %51, ptr %52, align 8, !tbaa !18
  br label %41, !llvm.loop !27

53:                                               ; preds = %41
  br label %54

54:                                               ; preds = %53, %2
  ret void
}

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

!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 0}
!10 = !{!"BinaryTreeIter", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !11, i64 32, !12, i64 40, !12, i64 41}
!11 = !{!"long", !7, i64 0}
!12 = !{!"_Bool", !7, i64 0}
!13 = !{!14, !6, i64 0}
!14 = !{!"BinaryTree", !6, i64 0, !11, i64 8, !15, i64 16, !6, i64 24, !6, i64 32}
!15 = !{!"int", !7, i64 0}
!16 = !{!10, !6, i64 8}
!17 = !{!10, !6, i64 16}
!18 = !{!10, !6, i64 24}
!19 = !{!10, !11, i64 32}
!20 = !{!10, !12, i64 40}
!21 = !{!10, !12, i64 41}
!22 = !{!23, !6, i64 16}
!23 = !{!"BinaryTreeNode", !15, i64 0, !7, i64 4, !6, i64 8, !6, i64 16, !6, i64 24}
!24 = distinct !{!24, !25}
!25 = !{!"llvm.loop.mustprogress"}
!26 = !{!23, !6, i64 8}
!27 = distinct !{!27, !25}


; 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.BinaryTreeIter = type { ptr, ptr, ptr, ptr, i64, i8, i8 }

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @tree_empty(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @tree_iter_start(ptr dead_on_unwind noalias nocapture writable writeonly sret(%struct.BinaryTreeIter) align 8 %0, ptr noundef %1) local_unnamed_addr #0 {
  store ptr %1, ptr %0, align 8, !tbaa !5
  %3 = load ptr, ptr %1, align 8, !tbaa !12
  %4 = getelementptr inbounds i8, ptr %0, i64 8
  store ptr %3, ptr %4, align 8, !tbaa !15
  %5 = getelementptr inbounds i8, ptr %0, i64 16
  %6 = getelementptr inbounds i8, ptr %0, i64 24
  %7 = getelementptr inbounds i8, ptr %0, i64 40
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(24) %5, i8 0, i64 24, i1 false)
  store i8 1, ptr %7, align 8, !tbaa !16
  %8 = tail call zeroext i1 @tree_empty(ptr noundef nonnull %1)
  %9 = getelementptr inbounds i8, ptr %0, i64 41
  %10 = zext i1 %8 to i8
  store i8 %10, ptr %9, align 1, !tbaa !17
  %11 = tail call zeroext i1 @tree_empty(ptr noundef nonnull %1)
  br i1 %11, label %20, label %.preheader

.preheader:                                       ; preds = %2, %.preheader
  %12 = phi ptr [ %14, %.preheader ], [ %3, %2 ]
  %13 = getelementptr inbounds i8, ptr %12, i64 16
  %14 = load ptr, ptr %13, align 8, !tbaa !18
  %.not = icmp eq ptr %14, null
  br i1 %.not, label %15, label %.preheader, !llvm.loop !20

15:                                               ; preds = %.preheader
  store ptr %12, ptr %4, align 8, !tbaa !15
  store ptr %12, ptr %5, align 8, !tbaa !22
  %16 = load ptr, ptr %1, align 8, !tbaa !12
  br label %17

17:                                               ; preds = %17, %15
  %storemerge = phi ptr [ %16, %15 ], [ %19, %17 ]
  %18 = getelementptr inbounds i8, ptr %storemerge, i64 8
  %19 = load ptr, ptr %18, align 8, !tbaa !23
  %.not6 = icmp eq ptr %19, null
  br i1 %.not6, label %.loopexit, label %17, !llvm.loop !24

.loopexit:                                        ; preds = %17
  store ptr %storemerge, ptr %6, align 8, !tbaa !25
  br label %20

20:                                               ; preds = %.loopexit, %2
  ret void
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #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 nounwind willreturn memory(argmem: write) }

!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 = !{!"BinaryTreeIter", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !10, i64 32, !11, i64 40, !11, i64 41}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"_Bool", !8, i64 0}
!12 = !{!13, !7, i64 0}
!13 = !{!"BinaryTree", !7, i64 0, !10, i64 8, !14, i64 16, !7, i64 24, !7, i64 32}
!14 = !{!"int", !8, i64 0}
!15 = !{!6, !7, i64 8}
!16 = !{!6, !11, i64 40}
!17 = !{!6, !11, i64 41}
!18 = !{!19, !7, i64 16}
!19 = !{!"BinaryTreeNode", !14, i64 0, !8, i64 4, !7, i64 8, !7, i64 16, !7, i64 24}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}
!22 = !{!6, !7, i64 16}
!23 = !{!19, !7, i64 8}
!24 = distinct !{!24, !21}
!25 = !{!6, !7, i64 24}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,MemCpyOptPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass