; LLVM IR from AllAlgorithms/c :: algorithms/data-structures (bfs)

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

@front = external dso_local global i32, align 4
@rear = external dso_local global i32, align 4
@queue = external dso_local global [20 x i32], align 16
@val = external dso_local global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local void @bfs_traverse(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %4 = getelementptr inbounds %struct.btnode, ptr %3, i32 0, i32 0
  %5 = load i32, ptr %4, align 8, !tbaa !9
  store i32 %5, ptr @val, align 4, !tbaa !12
  %6 = load i32, ptr @front, align 4, !tbaa !12
  %7 = load i32, ptr @rear, align 4, !tbaa !12
  %8 = icmp sle i32 %6, %7
  br i1 %8, label %9, label %56

9:                                                ; preds = %1
  %10 = load ptr, ptr %2, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.btnode, ptr %10, i32 0, i32 0
  %12 = load i32, ptr %11, align 8, !tbaa !9
  %13 = load i32, ptr @front, align 4, !tbaa !12
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [20 x i32], ptr @queue, i64 0, i64 %14
  %16 = load i32, ptr %15, align 4, !tbaa !12
  %17 = icmp eq i32 %12, %16
  br i1 %17, label %18, label %56

18:                                               ; preds = %9
  %19 = load ptr, ptr %2, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.btnode, ptr %19, i32 0, i32 1
  %21 = load ptr, ptr %20, align 8, !tbaa !13
  %22 = icmp ne ptr %21, null
  br i1 %22, label %23, label %33

23:                                               ; preds = %18
  %24 = load ptr, ptr %2, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.btnode, ptr %24, i32 0, i32 1
  %26 = load ptr, ptr %25, align 8, !tbaa !13
  %27 = getelementptr inbounds %struct.btnode, ptr %26, i32 0, i32 0
  %28 = load i32, ptr %27, align 8, !tbaa !9
  %29 = load i32, ptr @rear, align 4, !tbaa !12
  %30 = add nsw i32 %29, 1
  store i32 %30, ptr @rear, align 4, !tbaa !12
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [20 x i32], ptr @queue, i64 0, i64 %31
  store i32 %28, ptr %32, align 4, !tbaa !12
  br label %33

33:                                               ; preds = %23, %18
  %34 = load ptr, ptr %2, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.btnode, ptr %34, i32 0, i32 2
  %36 = load ptr, ptr %35, align 8, !tbaa !14
  %37 = icmp ne ptr %36, null
  br i1 %37, label %43, label %38

38:                                               ; preds = %33
  %39 = load ptr, ptr %2, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.btnode, ptr %39, i32 0, i32 2
  %41 = load ptr, ptr %40, align 8, !tbaa !14
  %42 = icmp eq ptr %41, null
  br i1 %42, label %43, label %53

43:                                               ; preds = %38, %33
  %44 = load ptr, ptr %2, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.btnode, ptr %44, i32 0, i32 2
  %46 = load ptr, ptr %45, align 8, !tbaa !14
  %47 = getelementptr inbounds %struct.btnode, ptr %46, i32 0, i32 0
  %48 = load i32, ptr %47, align 8, !tbaa !9
  %49 = load i32, ptr @rear, align 4, !tbaa !12
  %50 = add nsw i32 %49, 1
  store i32 %50, ptr @rear, align 4, !tbaa !12
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds [20 x i32], ptr @queue, i64 0, i64 %51
  store i32 %48, ptr %52, align 4, !tbaa !12
  br label %53

53:                                               ; preds = %43, %38
  %54 = load i32, ptr @front, align 4, !tbaa !12
  %55 = add nsw i32 %54, 1
  store i32 %55, ptr @front, align 4, !tbaa !12
  br label %56

56:                                               ; preds = %53, %9, %1
  %57 = load ptr, ptr %2, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.btnode, ptr %57, i32 0, i32 1
  %59 = load ptr, ptr %58, align 8, !tbaa !13
  %60 = icmp ne ptr %59, null
  br i1 %60, label %61, label %65

61:                                               ; preds = %56
  %62 = load ptr, ptr %2, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.btnode, ptr %62, i32 0, i32 1
  %64 = load ptr, ptr %63, align 8, !tbaa !13
  call void @bfs_traverse(ptr noundef %64)
  br label %65

65:                                               ; preds = %61, %56
  %66 = load ptr, ptr %2, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.btnode, ptr %66, i32 0, i32 2
  %68 = load ptr, ptr %67, align 8, !tbaa !14
  %69 = icmp ne ptr %68, null
  br i1 %69, label %70, label %74

70:                                               ; preds = %65
  %71 = load ptr, ptr %2, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.btnode, ptr %71, i32 0, i32 2
  %73 = load ptr, ptr %72, align 8, !tbaa !14
  call void @bfs_traverse(ptr noundef %73)
  br label %74

74:                                               ; preds = %70, %65
  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, !11, i64 0}
!10 = !{!"btnode", !11, i64 0, !6, i64 8, !6, i64 16}
!11 = !{!"int", !7, i64 0}
!12 = !{!11, !11, i64 0}
!13 = !{!10, !6, i64 8}
!14 = !{!10, !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"

@front = external dso_local local_unnamed_addr global i32, align 4
@rear = external dso_local local_unnamed_addr global i32, align 4
@queue = external dso_local local_unnamed_addr global [20 x i32], align 16
@val = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: nofree nosync nounwind memory(readwrite, argmem: read, inaccessiblemem: none) uwtable
define dso_local void @bfs_traverse(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
  %.pre19 = load i32, ptr @front, align 4, !tbaa !5
  %.pre21 = load i32, ptr @rear, align 4, !tbaa !5
  br label %tailrecurse

tailrecurse:                                      ; preds = %33, %1
  %2 = phi i32 [ %.pre21, %1 ], [ %34, %33 ]
  %3 = phi i32 [ %.pre19, %1 ], [ %35, %33 ]
  %.tr = phi ptr [ %0, %1 ], [ %37, %33 ]
  %4 = load i32, ptr %.tr, align 8, !tbaa !9
  store i32 %4, ptr @val, align 4, !tbaa !5
  %.not = icmp sgt i32 %3, %2
  br i1 %.not, label %27, label %5

5:                                                ; preds = %tailrecurse
  %6 = sext i32 %3 to i64
  %7 = getelementptr inbounds [20 x i32], ptr @queue, i64 0, i64 %6
  %8 = load i32, ptr %7, align 4, !tbaa !5
  %9 = icmp eq i32 %4, %8
  br i1 %9, label %10, label %27

10:                                               ; preds = %5
  %11 = getelementptr inbounds i8, ptr %.tr, i64 8
  %12 = load ptr, ptr %11, align 8, !tbaa !12
  %.not15 = icmp eq ptr %12, null
  br i1 %.not15, label %18, label %13

13:                                               ; preds = %10
  %14 = load i32, ptr %12, align 8, !tbaa !9
  %15 = add nsw i32 %2, 1
  store i32 %15, ptr @rear, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [20 x i32], ptr @queue, i64 0, i64 %16
  store i32 %14, ptr %17, align 4, !tbaa !5
  br label %18

18:                                               ; preds = %13, %10
  %19 = phi i32 [ %15, %13 ], [ %2, %10 ]
  %20 = getelementptr inbounds i8, ptr %.tr, i64 16
  %21 = load ptr, ptr %20, align 8, !tbaa !13
  %22 = load i32, ptr %21, align 8, !tbaa !9
  %23 = add nsw i32 %19, 1
  store i32 %23, ptr @rear, align 4, !tbaa !5
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [20 x i32], ptr @queue, i64 0, i64 %24
  store i32 %22, ptr %25, align 4, !tbaa !5
  %26 = add nsw i32 %3, 1
  store i32 %26, ptr @front, align 4, !tbaa !5
  br label %27

27:                                               ; preds = %18, %5, %tailrecurse
  %28 = phi i32 [ %23, %18 ], [ %2, %5 ], [ %2, %tailrecurse ]
  %29 = phi i32 [ %26, %18 ], [ %3, %5 ], [ %3, %tailrecurse ]
  %30 = getelementptr inbounds i8, ptr %.tr, i64 8
  %31 = load ptr, ptr %30, align 8, !tbaa !12
  %.not17 = icmp eq ptr %31, null
  br i1 %.not17, label %33, label %32

32:                                               ; preds = %27
  tail call void @bfs_traverse(ptr noundef nonnull %31)
  %.pre = load i32, ptr @front, align 4, !tbaa !5
  %.pre20 = load i32, ptr @rear, align 4, !tbaa !5
  br label %33

33:                                               ; preds = %32, %27
  %34 = phi i32 [ %.pre20, %32 ], [ %28, %27 ]
  %35 = phi i32 [ %.pre, %32 ], [ %29, %27 ]
  %36 = getelementptr inbounds i8, ptr %.tr, i64 16
  %37 = load ptr, ptr %36, align 8, !tbaa !13
  %.not18 = icmp eq ptr %37, null
  br i1 %.not18, label %38, label %tailrecurse

38:                                               ; preds = %33
  ret void
}

attributes #0 = { nofree nosync nounwind memory(readwrite, argmem: read, inaccessiblemem: none) 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 0}
!10 = !{!"btnode", !6, i64 0, !11, i64 8, !11, i64 16}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!10, !11, i64 8}
!13 = !{!10, !11, i64 16}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass