; LLVM IR from yourtion/LearningMasteringAlgorithms-C :: source (graph)

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

; 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
define dso_local i32 @graph_ins_edge(ptr noundef %0, ptr noundef %1, ptr noundef %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.Graph_, ptr %11, i32 0, i32 4
  %13 = getelementptr inbounds %struct.List_, ptr %12, i32 0, i32 3
  %14 = load ptr, ptr %13, align 8, !tbaa !9
  store ptr %14, ptr %8, align 8, !tbaa !5
  br label %15

15:                                               ; preds = %32, %3
  %16 = load ptr, ptr %8, align 8, !tbaa !5
  %17 = icmp ne ptr %16, null
  br i1 %17, label %18, label %36

18:                                               ; preds = %15
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.Graph_, ptr %19, i32 0, i32 2
  %21 = load ptr, ptr %20, align 8, !tbaa !13
  %22 = load ptr, ptr %7, align 8, !tbaa !5
  %23 = load ptr, ptr %8, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.ListElmt_, ptr %23, i32 0, i32 0
  %25 = load ptr, ptr %24, align 8, !tbaa !14
  %26 = getelementptr inbounds %struct.AdjList_, ptr %25, i32 0, i32 0
  %27 = load ptr, ptr %26, align 8, !tbaa !16
  %28 = call i32 %21(ptr noundef %22, ptr noundef %27)
  %29 = icmp ne i32 %28, 0
  br i1 %29, label %30, label %31

30:                                               ; preds = %18
  br label %36

31:                                               ; preds = %18
  br label %32

32:                                               ; preds = %31
  %33 = load ptr, ptr %8, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.ListElmt_, ptr %33, i32 0, i32 1
  %35 = load ptr, ptr %34, align 8, !tbaa !18
  store ptr %35, ptr %8, align 8, !tbaa !5
  br label %15, !llvm.loop !19

36:                                               ; preds = %30, %15
  %37 = load ptr, ptr %8, align 8, !tbaa !5
  %38 = icmp eq ptr %37, null
  br i1 %38, label %39, label %40

39:                                               ; preds = %36
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %85

40:                                               ; preds = %36
  %41 = load ptr, ptr %5, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.Graph_, ptr %41, i32 0, i32 4
  %43 = getelementptr inbounds %struct.List_, ptr %42, i32 0, i32 3
  %44 = load ptr, ptr %43, align 8, !tbaa !9
  store ptr %44, ptr %8, align 8, !tbaa !5
  br label %45

45:                                               ; preds = %62, %40
  %46 = load ptr, ptr %8, align 8, !tbaa !5
  %47 = icmp ne ptr %46, null
  br i1 %47, label %48, label %66

48:                                               ; preds = %45
  %49 = load ptr, ptr %5, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.Graph_, ptr %49, i32 0, i32 2
  %51 = load ptr, ptr %50, align 8, !tbaa !13
  %52 = load ptr, ptr %6, align 8, !tbaa !5
  %53 = load ptr, ptr %8, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.ListElmt_, ptr %53, i32 0, i32 0
  %55 = load ptr, ptr %54, align 8, !tbaa !14
  %56 = getelementptr inbounds %struct.AdjList_, ptr %55, i32 0, i32 0
  %57 = load ptr, ptr %56, align 8, !tbaa !16
  %58 = call i32 %51(ptr noundef %52, ptr noundef %57)
  %59 = icmp ne i32 %58, 0
  br i1 %59, label %60, label %61

60:                                               ; preds = %48
  br label %66

61:                                               ; preds = %48
  br label %62

62:                                               ; preds = %61
  %63 = load ptr, ptr %8, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.ListElmt_, ptr %63, i32 0, i32 1
  %65 = load ptr, ptr %64, align 8, !tbaa !18
  store ptr %65, ptr %8, align 8, !tbaa !5
  br label %45, !llvm.loop !21

66:                                               ; preds = %60, %45
  %67 = load ptr, ptr %8, align 8, !tbaa !5
  %68 = icmp eq ptr %67, null
  br i1 %68, label %69, label %70

69:                                               ; preds = %66
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %85

70:                                               ; preds = %66
  %71 = load ptr, ptr %8, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.ListElmt_, ptr %71, i32 0, i32 0
  %73 = load ptr, ptr %72, align 8, !tbaa !14
  %74 = getelementptr inbounds %struct.AdjList_, ptr %73, i32 0, i32 1
  %75 = load ptr, ptr %7, align 8, !tbaa !5
  %76 = call i32 @set_insert(ptr noundef %74, ptr noundef %75)
  store i32 %76, ptr %9, align 4, !tbaa !22
  %77 = icmp ne i32 %76, 0
  br i1 %77, label %78, label %80

78:                                               ; preds = %70
  %79 = load i32, ptr %9, align 4, !tbaa !22
  store i32 %79, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %85

80:                                               ; preds = %70
  %81 = load ptr, ptr %5, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.Graph_, ptr %81, i32 0, i32 1
  %83 = load i32, ptr %82, align 4, !tbaa !23
  %84 = add nsw i32 %83, 1
  store i32 %84, ptr %82, align 4, !tbaa !23
  store i32 0, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %85

85:                                               ; preds = %80, %78, %69, %39
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %86 = load i32, ptr %4, align 4
  ret i32 %86
}

declare i32 @set_insert(ptr noundef, ptr noundef) #2

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 = { "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 = !{!10, !6, i64 8}
!14 = !{!15, !6, i64 0}
!15 = !{!"ListElmt_", !6, i64 0, !6, i64 8}
!16 = !{!17, !6, i64 0}
!17 = !{!"AdjList_", !6, i64 0, !12, i64 8}
!18 = !{!15, !6, i64 8}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = distinct !{!21, !20}
!22 = !{!11, !11, i64 0}
!23 = !{!10, !11, i64 4}


; 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 i32 @graph_ins_edge(ptr nocapture noundef %0, ptr noundef %1, ptr noundef %2) local_unnamed_addr #0 {
  %4 = getelementptr inbounds i8, ptr %0, i64 48
  %.01828 = load ptr, ptr %4, align 8, !tbaa !5
  %cond29 = icmp eq ptr %.01828, null
  br i1 %cond29, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %3
  %5 = getelementptr inbounds i8, ptr %0, i64 8
  br label %8

6:                                                ; preds = %8
  %7 = getelementptr inbounds i8, ptr %.01830, i64 8
  %.018 = load ptr, ptr %7, align 8, !tbaa !5
  %cond = icmp eq ptr %.018, null
  br i1 %cond, label %.loopexit, label %8, !llvm.loop !9

8:                                                ; preds = %.lr.ph, %6
  %.01830 = phi ptr [ %.01828, %.lr.ph ], [ %.018, %6 ]
  %9 = load ptr, ptr %5, align 8, !tbaa !11
  %10 = load ptr, ptr %.01830, align 8, !tbaa !15
  %11 = load ptr, ptr %10, align 8, !tbaa !17
  %12 = tail call i32 %9(ptr noundef %2, ptr noundef %11) #2
  %.not20 = icmp eq i32 %12, 0
  br i1 %.not20, label %6, label %.preheader

.preheader:                                       ; preds = %8
  %.131 = load ptr, ptr %4, align 8, !tbaa !5
  %cond2532 = icmp eq ptr %.131, null
  br i1 %cond2532, label %.loopexit, label %.lr.ph34

.lr.ph34:                                         ; preds = %.preheader, %17
  %.133 = phi ptr [ %.1, %17 ], [ %.131, %.preheader ]
  %13 = load ptr, ptr %5, align 8, !tbaa !11
  %14 = load ptr, ptr %.133, align 8, !tbaa !15
  %15 = load ptr, ptr %14, align 8, !tbaa !17
  %16 = tail call i32 %13(ptr noundef %1, ptr noundef %15) #2
  %.not22 = icmp eq i32 %16, 0
  br i1 %.not22, label %17, label %19

17:                                               ; preds = %.lr.ph34
  %18 = getelementptr inbounds i8, ptr %.133, i64 8
  %.1 = load ptr, ptr %18, align 8, !tbaa !5
  %cond25 = icmp eq ptr %.1, null
  br i1 %cond25, label %.loopexit, label %.lr.ph34, !llvm.loop !19

19:                                               ; preds = %.lr.ph34
  %20 = load ptr, ptr %.133, align 8, !tbaa !15
  %21 = getelementptr inbounds i8, ptr %20, i64 8
  %22 = tail call i32 @set_insert(ptr noundef nonnull %21, ptr noundef %2) #2
  %.not23 = icmp eq i32 %22, 0
  br i1 %.not23, label %23, label %.loopexit

23:                                               ; preds = %19
  %24 = getelementptr inbounds i8, ptr %0, i64 4
  %25 = load i32, ptr %24, align 4, !tbaa !20
  %26 = add nsw i32 %25, 1
  store i32 %26, ptr %24, align 4, !tbaa !20
  br label %.loopexit

.loopexit:                                        ; preds = %6, %17, %3, %.preheader, %19, %23
  %.0 = phi i32 [ 0, %23 ], [ %22, %19 ], [ -1, %.preheader ], [ -1, %3 ], [ -1, %17 ], [ -1, %6 ]
  ret i32 %.0
}

declare i32 @set_insert(ptr noundef, 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!12, !6, i64 8}
!12 = !{!"Graph_", !13, i64 0, !13, i64 4, !6, i64 8, !6, i64 16, !14, i64 24}
!13 = !{!"int", !7, i64 0}
!14 = !{!"List_", !13, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32}
!15 = !{!16, !6, i64 0}
!16 = !{!"ListElmt_", !6, i64 0, !6, i64 8}
!17 = !{!18, !6, i64 0}
!18 = !{!"AdjList_", !6, i64 0, !14, i64 8}
!19 = distinct !{!19, !10}
!20 = !{!12, !13, i64 4}


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