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

; 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

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

; Function Attrs: nounwind uwtable
define dso_local zeroext i1 @tree_iter_advance(ptr noundef %0, i64 noundef %1) #1 {
  %3 = alloca i1, align 1
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  %7 = load ptr, ptr %4, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.BinaryTreeIter, ptr %7, i32 0, i32 6
  %9 = load i8, ptr %8, align 1, !tbaa !11, !range !14, !noundef !15
  %10 = trunc i8 %9 to i1
  br i1 %10, label %11, label %12

11:                                               ; preds = %2
  store i1 false, ptr %3, align 1
  br label %52

12:                                               ; preds = %2
  %13 = load ptr, ptr %4, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.BinaryTreeIter, ptr %13, i32 0, i32 1
  %15 = load ptr, ptr %14, align 8, !tbaa !16
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.BinaryTreeIter, ptr %16, i32 0, i32 3
  %18 = load ptr, ptr %17, align 8, !tbaa !17
  %19 = icmp eq ptr %15, %18
  br i1 %19, label %20, label %23

20:                                               ; preds = %12
  %21 = load ptr, ptr %4, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.BinaryTreeIter, ptr %21, i32 0, i32 6
  store i8 1, ptr %22, align 1, !tbaa !11
  store i1 false, ptr %3, align 1
  br label %52

23:                                               ; preds = %12
  %24 = load i64, ptr %5, align 8, !tbaa !9
  %25 = icmp eq i64 %24, 0
  br i1 %25, label %38, label %26

26:                                               ; preds = %23
  %27 = load ptr, ptr %4, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.BinaryTreeIter, ptr %27, i32 0, i32 4
  %29 = load i64, ptr %28, align 8, !tbaa !18
  %30 = load i64, ptr %5, align 8, !tbaa !9
  %31 = add i64 %29, %30
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.BinaryTreeIter, ptr %32, i32 0, i32 0
  %34 = load ptr, ptr %33, align 8, !tbaa !19
  %35 = getelementptr inbounds %struct.BinaryTree, ptr %34, i32 0, i32 1
  %36 = load i64, ptr %35, align 8, !tbaa !20
  %37 = icmp uge i64 %31, %36
  br i1 %37, label %38, label %39

38:                                               ; preds = %26, %23
  store i1 false, ptr %3, align 1
  br label %52

39:                                               ; preds = %26
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  store i64 0, ptr %6, align 8, !tbaa !9
  br label %40

40:                                               ; preds = %48, %39
  %41 = load i64, ptr %6, align 8, !tbaa !9
  %42 = load i64, ptr %5, align 8, !tbaa !9
  %43 = icmp ult i64 %41, %42
  br i1 %43, label %45, label %44

44:                                               ; preds = %40
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  br label %51

45:                                               ; preds = %40
  %46 = load ptr, ptr %4, align 8, !tbaa !5
  %47 = call zeroext i1 @tree_iter_next(ptr noundef %46)
  br label %48

48:                                               ; preds = %45
  %49 = load i64, ptr %6, align 8, !tbaa !9
  %50 = add i64 %49, 1
  store i64 %50, ptr %6, align 8, !tbaa !9
  br label %40, !llvm.loop !23

51:                                               ; preds = %44
  store i1 true, ptr %3, align 1
  br label %52

52:                                               ; preds = %51, %38, %20, %11
  %53 = load i1, ptr %3, align 1
  ret i1 %53
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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, !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 = !{!"long", !7, i64 0}
!11 = !{!12, !13, i64 41}
!12 = !{!"BinaryTreeIter", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !10, i64 32, !13, i64 40, !13, i64 41}
!13 = !{!"_Bool", !7, i64 0}
!14 = !{i8 0, i8 2}
!15 = !{}
!16 = !{!12, !6, i64 8}
!17 = !{!12, !6, i64 24}
!18 = !{!12, !10, i64 32}
!19 = !{!12, !6, i64 0}
!20 = !{!21, !10, i64 8}
!21 = !{!"BinaryTree", !6, i64 0, !10, i64 8, !22, i64 16, !6, i64 24, !6, i64 32}
!22 = !{!"int", !7, i64 0}
!23 = distinct !{!23, !24}
!24 = !{!"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"

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

; Function Attrs: nounwind uwtable
define dso_local noundef zeroext i1 @tree_iter_advance(ptr noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 41
  %4 = load i8, ptr %3, align 1, !tbaa !5, !range !12, !noundef !13
  %5 = trunc nuw i8 %4 to i1
  br i1 %5, label %.loopexit, label %6

6:                                                ; preds = %2
  %7 = getelementptr inbounds i8, ptr %0, i64 8
  %8 = load ptr, ptr %7, align 8, !tbaa !14
  %9 = getelementptr inbounds i8, ptr %0, i64 24
  %10 = load ptr, ptr %9, align 8, !tbaa !15
  %11 = icmp eq ptr %8, %10
  br i1 %11, label %12, label %13

12:                                               ; preds = %6
  store i8 1, ptr %3, align 1, !tbaa !5
  br label %.loopexit

13:                                               ; preds = %6
  %14 = icmp eq i64 %1, 0
  br i1 %14, label %.loopexit, label %15

15:                                               ; preds = %13
  %16 = getelementptr inbounds i8, ptr %0, i64 32
  %17 = load i64, ptr %16, align 8, !tbaa !16
  %18 = add i64 %17, %1
  %19 = load ptr, ptr %0, align 8, !tbaa !17
  %20 = getelementptr inbounds i8, ptr %19, i64 8
  %21 = load i64, ptr %20, align 8, !tbaa !18
  %.not = icmp ult i64 %18, %21
  br i1 %.not, label %.preheader, label %.loopexit

.preheader:                                       ; preds = %15, %.preheader
  %.014 = phi i64 [ %23, %.preheader ], [ 0, %15 ]
  %22 = tail call zeroext i1 @tree_iter_next(ptr noundef nonnull %0)
  %23 = add nuw i64 %.014, 1
  %exitcond.not = icmp eq i64 %23, %1
  br i1 %exitcond.not, label %.loopexit, label %.preheader, !llvm.loop !21

.loopexit:                                        ; preds = %.preheader, %13, %15, %2, %12
  %.012 = phi i1 [ false, %12 ], [ false, %2 ], [ false, %15 ], [ false, %13 ], [ true, %.preheader ]
  ret i1 %.012
}

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, !11, i64 41}
!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 = !{i8 0, i8 2}
!13 = !{}
!14 = !{!6, !7, i64 8}
!15 = !{!6, !7, i64 24}
!16 = !{!6, !10, i64 32}
!17 = !{!6, !7, i64 0}
!18 = !{!19, !10, i64 8}
!19 = !{!"BinaryTree", !7, i64 0, !10, i64 8, !20, i64 16, !7, i64 24, !7, i64 32}
!20 = !{!"int", !8, i64 0}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}


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