; 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_end(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 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = call zeroext i1 @tree_empty(ptr noundef %13)
  %15 = getelementptr inbounds %struct.BinaryTreeIter, ptr %0, i32 0, i32 5
  %16 = zext i1 %14 to i8
  store i8 %16, ptr %15, align 8, !tbaa !20
  %17 = getelementptr inbounds %struct.BinaryTreeIter, ptr %0, i32 0, i32 6
  store i8 1, ptr %17, 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 %59, 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 2
  %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 2
  %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 3
  store ptr %35, ptr %36, align 8, !tbaa !18
  %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 2
  store ptr %39, ptr %40, align 8, !tbaa !17
  br label %41

41:                                               ; preds = %47, %33
  %42 = getelementptr inbounds %struct.BinaryTreeIter, ptr %0, i32 0, i32 2
  %43 = load ptr, ptr %42, align 8, !tbaa !17
  %44 = getelementptr inbounds %struct.BinaryTreeNode, ptr %43, i32 0, i32 3
  %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 2
  %49 = load ptr, ptr %48, align 8, !tbaa !17
  %50 = getelementptr inbounds %struct.BinaryTreeNode, ptr %49, i32 0, i32 3
  %51 = load ptr, ptr %50, align 8, !tbaa !26
  %52 = getelementptr inbounds %struct.BinaryTreeIter, ptr %0, i32 0, i32 2
  store ptr %51, ptr %52, align 8, !tbaa !17
  br label %41, !llvm.loop !27

53:                                               ; preds = %41
  %54 = load ptr, ptr %3, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.BinaryTree, ptr %54, i32 0, i32 1
  %56 = load i64, ptr %55, align 8, !tbaa !28
  %57 = sub i64 %56, 1
  %58 = getelementptr inbounds %struct.BinaryTreeIter, ptr %0, i32 0, i32 4
  store i64 %57, ptr %58, align 8, !tbaa !19
  br label %59

59:                                               ; 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 8}
!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 16}
!27 = distinct !{!27, !25}
!28 = !{!14, !11, i64 8}


; 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_end(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 32
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(24) %5, i8 0, i64 24, i1 false)
  %8 = tail call zeroext i1 @tree_empty(ptr noundef nonnull %1)
  %9 = getelementptr inbounds i8, ptr %0, i64 40
  %10 = zext i1 %8 to i8
  store i8 %10, ptr %9, align 8, !tbaa !16
  %11 = getelementptr inbounds i8, ptr %0, i64 41
  store i8 1, ptr %11, align 1, !tbaa !17
  %12 = tail call zeroext i1 @tree_empty(ptr noundef nonnull %1)
  br i1 %12, label %25, label %.preheader

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

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

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

21:                                               ; preds = %18
  store ptr %storemerge, ptr %5, align 8, !tbaa !25
  %22 = getelementptr inbounds i8, ptr %1, i64 8
  %23 = load i64, ptr %22, align 8, !tbaa !26
  %24 = add i64 %23, -1
  store i64 %24, ptr %7, align 8, !tbaa !27
  br label %25

25:                                               ; preds = %21, %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 8}
!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 24}
!23 = !{!19, !7, i64 16}
!24 = distinct !{!24, !21}
!25 = !{!6, !7, i64 16}
!26 = !{!13, !10, i64 8}
!27 = !{!6, !10, i64 32}


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