; LLVM IR from yourtion/LearningMasteringAlgorithms-C :: source (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.Graph_ = type { i32, i32, ptr, ptr, %struct.List_ }
%struct.List_ = type { i32, ptr, ptr, ptr, ptr }
%struct.ListElmt_ = type { ptr, ptr }
%struct.AdjList_ = type { ptr, %struct.List_ }
%struct.DfsVertex_ = type { ptr, i32 }

; Function Attrs: nounwind uwtable
define dso_local i32 @dfs(ptr noundef %0, ptr noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.Graph_, ptr %9, i32 0, i32 4
  %11 = getelementptr inbounds %struct.List_, ptr %10, i32 0, i32 3
  %12 = load ptr, ptr %11, align 8, !tbaa !9
  store ptr %12, ptr %7, align 8, !tbaa !5
  br label %13

13:                                               ; preds = %24, %2
  %14 = load ptr, ptr %7, align 8, !tbaa !5
  %15 = icmp ne ptr %14, null
  br i1 %15, label %16, label %28

16:                                               ; preds = %13
  %17 = load ptr, ptr %7, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.ListElmt_, ptr %17, i32 0, i32 0
  %19 = load ptr, ptr %18, align 8, !tbaa !13
  %20 = getelementptr inbounds %struct.AdjList_, ptr %19, i32 0, i32 0
  %21 = load ptr, ptr %20, align 8, !tbaa !15
  store ptr %21, ptr %6, align 8, !tbaa !5
  %22 = load ptr, ptr %6, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.DfsVertex_, ptr %22, i32 0, i32 1
  store i32 0, ptr %23, align 8, !tbaa !17
  br label %24

24:                                               ; preds = %16
  %25 = load ptr, ptr %7, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.ListElmt_, ptr %25, i32 0, i32 1
  %27 = load ptr, ptr %26, align 8, !tbaa !19
  store ptr %27, ptr %7, align 8, !tbaa !5
  br label %13, !llvm.loop !20

28:                                               ; preds = %13
  %29 = load ptr, ptr %5, align 8, !tbaa !5
  call void @list_init(ptr noundef %29, ptr noundef null)
  %30 = load ptr, ptr %4, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.Graph_, ptr %30, i32 0, i32 4
  %32 = getelementptr inbounds %struct.List_, ptr %31, i32 0, i32 3
  %33 = load ptr, ptr %32, align 8, !tbaa !9
  store ptr %33, ptr %7, align 8, !tbaa !5
  br label %34

34:                                               ; preds = %59, %28
  %35 = load ptr, ptr %7, align 8, !tbaa !5
  %36 = icmp ne ptr %35, null
  br i1 %36, label %37, label %63

37:                                               ; preds = %34
  %38 = load ptr, ptr %7, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.ListElmt_, ptr %38, i32 0, i32 0
  %40 = load ptr, ptr %39, align 8, !tbaa !13
  %41 = getelementptr inbounds %struct.AdjList_, ptr %40, i32 0, i32 0
  %42 = load ptr, ptr %41, align 8, !tbaa !15
  store ptr %42, ptr %6, align 8, !tbaa !5
  %43 = load ptr, ptr %6, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.DfsVertex_, ptr %43, i32 0, i32 1
  %45 = load i32, ptr %44, align 8, !tbaa !17
  %46 = icmp eq i32 %45, 0
  br i1 %46, label %47, label %58

47:                                               ; preds = %37
  %48 = load ptr, ptr %4, align 8, !tbaa !5
  %49 = load ptr, ptr %7, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.ListElmt_, ptr %49, i32 0, i32 0
  %51 = load ptr, ptr %50, align 8, !tbaa !13
  %52 = load ptr, ptr %5, align 8, !tbaa !5
  %53 = call i32 @dfs_main(ptr noundef %48, ptr noundef %51, ptr noundef %52)
  %54 = icmp ne i32 %53, 0
  br i1 %54, label %55, label %57

55:                                               ; preds = %47
  %56 = load ptr, ptr %5, align 8, !tbaa !5
  call void @list_destroy(ptr noundef %56)
  store i32 -1, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %64

57:                                               ; preds = %47
  br label %58

58:                                               ; preds = %57, %37
  br label %59

59:                                               ; preds = %58
  %60 = load ptr, ptr %7, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.ListElmt_, ptr %60, i32 0, i32 1
  %62 = load ptr, ptr %61, align 8, !tbaa !19
  store ptr %62, ptr %7, align 8, !tbaa !5
  br label %34, !llvm.loop !22

63:                                               ; preds = %34
  store i32 0, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %64

64:                                               ; preds = %63, %55
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  %65 = load i32, ptr %3, align 4
  ret i32 %65
}

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

declare void @list_init(ptr noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
declare hidden i32 @dfs_main(ptr noundef, ptr noundef, ptr noundef) #0

declare void @list_destroy(ptr noundef) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { "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 = { 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, !6, i64 48}
!10 = !{!"Graph_", !11, i64 0, !11, i64 4, !6, i64 8, !6, i64 16, !12, i64 24}
!11 = !{!"int", !7, i64 0}
!12 = !{!"List_", !11, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32}
!13 = !{!14, !6, i64 0}
!14 = !{!"ListElmt_", !6, i64 0, !6, i64 8}
!15 = !{!16, !6, i64 0}
!16 = !{!"AdjList_", !6, i64 0, !12, i64 8}
!17 = !{!18, !11, i64 8}
!18 = !{!"DfsVertex_", !6, i64 0, !11, i64 8}
!19 = !{!14, !6, i64 8}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}
!22 = distinct !{!22, !21}


; 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
define dso_local range(i32 -1, 1) i32 @dfs(ptr noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 48
  %.018 = load ptr, ptr %3, align 8, !tbaa !5
  %.not19 = icmp eq ptr %.018, null
  br i1 %.not19, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %2, %.lr.ph
  %.020 = phi ptr [ %.0, %.lr.ph ], [ %.018, %2 ]
  %4 = load ptr, ptr %.020, align 8, !tbaa !9
  %5 = load ptr, ptr %4, align 8, !tbaa !11
  %6 = getelementptr inbounds i8, ptr %5, i64 8
  store i32 0, ptr %6, align 8, !tbaa !15
  %7 = getelementptr inbounds i8, ptr %.020, i64 8
  %.0 = load ptr, ptr %7, align 8, !tbaa !5
  %.not = icmp eq ptr %.0, null
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !17

._crit_edge:                                      ; preds = %.lr.ph, %2
  tail call void @list_init(ptr noundef %1, ptr noundef null) #2
  %.121 = load ptr, ptr %3, align 8, !tbaa !5
  %.not1622 = icmp eq ptr %.121, null
  br i1 %.not1622, label %.loopexit, label %.lr.ph25

.lr.ph25:                                         ; preds = %._crit_edge, %16
  %.123 = phi ptr [ %.1, %16 ], [ %.121, %._crit_edge ]
  %8 = load ptr, ptr %.123, align 8, !tbaa !9
  %9 = load ptr, ptr %8, align 8, !tbaa !11
  %10 = getelementptr inbounds i8, ptr %9, i64 8
  %11 = load i32, ptr %10, align 8, !tbaa !15
  %12 = icmp eq i32 %11, 0
  br i1 %12, label %13, label %16

13:                                               ; preds = %.lr.ph25
  %14 = tail call i32 @dfs_main(ptr noundef %0, ptr noundef nonnull %8, ptr noundef %1)
  %.not17 = icmp eq i32 %14, 0
  br i1 %.not17, label %16, label %15

15:                                               ; preds = %13
  tail call void @list_destroy(ptr noundef %1) #2
  br label %.loopexit

16:                                               ; preds = %.lr.ph25, %13
  %17 = getelementptr inbounds i8, ptr %.123, i64 8
  %.1 = load ptr, ptr %17, align 8, !tbaa !5
  %.not16 = icmp eq ptr %.1, null
  br i1 %.not16, label %.loopexit, label %.lr.ph25, !llvm.loop !19

.loopexit:                                        ; preds = %16, %._crit_edge, %15
  %.015 = phi i32 [ -1, %15 ], [ 0, %._crit_edge ], [ 0, %16 ]
  ret i32 %.015
}

declare void @list_init(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden i32 @dfs_main(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

declare void @list_destroy(ptr noundef) local_unnamed_addr #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 = { "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, !6, i64 0}
!10 = !{!"ListElmt_", !6, i64 0, !6, i64 8}
!11 = !{!12, !6, i64 0}
!12 = !{!"AdjList_", !6, i64 0, !13, i64 8}
!13 = !{!"List_", !14, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32}
!14 = !{!"int", !7, i64 0}
!15 = !{!16, !14, i64 8}
!16 = !{!"DfsVertex_", !6, i64 0, !14, i64 8}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = distinct !{!19, !18}


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