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

; 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.List_ = type { i32, ptr, ptr, ptr, ptr }
%struct.ListElmt_ = type { ptr, ptr }
%struct.PathVertex_ = type { ptr, double, i32, double, ptr }

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

18:                                               ; preds = %39, %4
  %19 = load ptr, ptr %12, align 8, !tbaa !5
  %20 = icmp ne ptr %19, null
  br i1 %20, label %21, label %43

21:                                               ; preds = %18
  %22 = load ptr, ptr %9, align 8, !tbaa !5
  %23 = load ptr, ptr %12, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.ListElmt_, ptr %23, i32 0, i32 0
  %25 = load ptr, ptr %24, align 8, !tbaa !13
  %26 = load ptr, ptr %7, align 8, !tbaa !5
  %27 = call i32 %22(ptr noundef %25, ptr noundef %26)
  %28 = icmp ne i32 %27, 0
  br i1 %28, label %29, label %38

29:                                               ; preds = %21
  %30 = load ptr, ptr %12, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.ListElmt_, ptr %30, i32 0, i32 0
  %32 = load ptr, ptr %31, align 8, !tbaa !13
  store ptr %32, ptr %10, align 8, !tbaa !5
  %33 = load ptr, ptr %12, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.ListElmt_, ptr %33, i32 0, i32 0
  %35 = load ptr, ptr %34, align 8, !tbaa !13
  %36 = getelementptr inbounds %struct.PathVertex_, ptr %35, i32 0, i32 4
  %37 = load ptr, ptr %36, align 8, !tbaa !15
  store ptr %37, ptr %11, align 8, !tbaa !5
  store i32 1, ptr %13, align 4, !tbaa !9
  br label %43

38:                                               ; preds = %21
  br label %39

39:                                               ; preds = %38
  %40 = load ptr, ptr %12, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.ListElmt_, ptr %40, i32 0, i32 1
  %42 = load ptr, ptr %41, align 8, !tbaa !18
  store ptr %42, ptr %12, align 8, !tbaa !5
  br label %18, !llvm.loop !19

43:                                               ; preds = %29, %18
  %44 = load i32, ptr %13, align 4, !tbaa !9
  %45 = icmp ne i32 %44, 0
  br i1 %45, label %47, label %46

46:                                               ; preds = %43
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %14, align 4
  br label %88

47:                                               ; preds = %43
  br label %48

48:                                               ; preds = %84, %47
  %49 = load ptr, ptr %11, align 8, !tbaa !5
  %50 = icmp ne ptr %49, null
  br i1 %50, label %51, label %85

51:                                               ; preds = %48
  %52 = load ptr, ptr %12, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.ListElmt_, ptr %52, i32 0, i32 0
  %54 = load ptr, ptr %53, align 8, !tbaa !13
  store ptr %54, ptr %10, align 8, !tbaa !5
  store i32 0, ptr %13, align 4, !tbaa !9
  %55 = load ptr, ptr %6, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.List_, ptr %55, i32 0, i32 3
  %57 = load ptr, ptr %56, align 8, !tbaa !11
  store ptr %57, ptr %12, align 8, !tbaa !5
  br label %58

58:                                               ; preds = %76, %51
  %59 = load ptr, ptr %12, align 8, !tbaa !5
  %60 = icmp ne ptr %59, null
  br i1 %60, label %61, label %80

61:                                               ; preds = %58
  %62 = load ptr, ptr %9, align 8, !tbaa !5
  %63 = load ptr, ptr %12, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.ListElmt_, ptr %63, i32 0, i32 0
  %65 = load ptr, ptr %64, align 8, !tbaa !13
  %66 = load ptr, ptr %11, align 8, !tbaa !5
  %67 = call i32 %62(ptr noundef %65, ptr noundef %66)
  %68 = icmp ne i32 %67, 0
  br i1 %68, label %69, label %75

69:                                               ; preds = %61
  %70 = load ptr, ptr %12, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.ListElmt_, ptr %70, i32 0, i32 0
  %72 = load ptr, ptr %71, align 8, !tbaa !13
  %73 = getelementptr inbounds %struct.PathVertex_, ptr %72, i32 0, i32 4
  %74 = load ptr, ptr %73, align 8, !tbaa !15
  store ptr %74, ptr %11, align 8, !tbaa !5
  store i32 1, ptr %13, align 4, !tbaa !9
  br label %80

75:                                               ; preds = %61
  br label %76

76:                                               ; preds = %75
  %77 = load ptr, ptr %12, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.ListElmt_, ptr %77, i32 0, i32 1
  %79 = load ptr, ptr %78, align 8, !tbaa !18
  store ptr %79, ptr %12, align 8, !tbaa !5
  br label %58, !llvm.loop !21

80:                                               ; preds = %69, %58
  %81 = load i32, ptr %13, align 4, !tbaa !9
  %82 = icmp ne i32 %81, 0
  br i1 %82, label %84, label %83

83:                                               ; preds = %80
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %14, align 4
  br label %88

84:                                               ; preds = %80
  br label %48, !llvm.loop !22

85:                                               ; preds = %48
  %86 = load ptr, ptr %10, align 8, !tbaa !5
  %87 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %86, ptr %87, align 8, !tbaa !5
  store i32 0, ptr %5, align 4
  store i32 1, ptr %14, align 4
  br label %88

88:                                               ; preds = %85, %83, %46
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  %89 = load i32, ptr %5, align 4
  ret i32 %89
}

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

; 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 = { 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 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 24}
!12 = !{!"List_", !10, 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 32}
!16 = !{!"PathVertex_", !6, i64 0, !17, i64 8, !10, i64 16, !17, i64 24, !6, i64 32}
!17 = !{!"double", !7, i64 0}
!18 = !{!14, !6, i64 8}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = distinct !{!21, !20}
!22 = distinct !{!22, !20}


; 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 @route(ptr nocapture noundef readonly %0, ptr noundef %1, ptr nocapture noundef writeonly %2, ptr nocapture noundef readonly %3) local_unnamed_addr #0 {
  %5 = getelementptr inbounds i8, ptr %0, i64 24
  %.02143 = load ptr, ptr %5, align 8, !tbaa !5
  %.not44 = icmp eq ptr %.02143, null
  br i1 %.not44, label %.thread, label %.lr.ph

.lr.ph:                                           ; preds = %4, %8
  %.02145 = phi ptr [ %.021, %8 ], [ %.02143, %4 ]
  %6 = load ptr, ptr %.02145, align 8, !tbaa !9
  %7 = tail call i32 %3(ptr noundef %6, ptr noundef %1) #1
  %.not29 = icmp eq i32 %7, 0
  br i1 %.not29, label %8, label %10

8:                                                ; preds = %.lr.ph
  %9 = getelementptr inbounds i8, ptr %.02145, i64 8
  %.021 = load ptr, ptr %9, align 8, !tbaa !5
  %.not = icmp eq ptr %.021, null
  br i1 %.not, label %.thread, label %.lr.ph, !llvm.loop !11

10:                                               ; preds = %.lr.ph
  %11 = load ptr, ptr %.02145, align 8, !tbaa !9
  %.124.in51 = getelementptr inbounds i8, ptr %11, i64 32
  %.12452 = load ptr, ptr %.124.in51, align 8, !tbaa !13
  %.not3153 = icmp eq ptr %.12452, null
  br i1 %.not3153, label %._crit_edge, label %.lr.ph56

.lr.ph56:                                         ; preds = %10, %17
  %12 = phi ptr [ %18, %17 ], [ %11, %10 ]
  %.12455 = phi ptr [ %.124, %17 ], [ %.12452, %10 ]
  %.246 = load ptr, ptr %5, align 8, !tbaa !5
  %.not3247 = icmp eq ptr %.246, null
  br i1 %.not3247, label %.thread, label %.lr.ph49

.lr.ph49:                                         ; preds = %.lr.ph56, %15
  %.248 = phi ptr [ %.2, %15 ], [ %.246, %.lr.ph56 ]
  %13 = load ptr, ptr %.248, align 8, !tbaa !9
  %14 = tail call i32 %3(ptr noundef %13, ptr noundef nonnull %.12455) #1
  %.not33 = icmp eq i32 %14, 0
  br i1 %.not33, label %15, label %17

15:                                               ; preds = %.lr.ph49
  %16 = getelementptr inbounds i8, ptr %.248, i64 8
  %.2 = load ptr, ptr %16, align 8, !tbaa !5
  %.not32 = icmp eq ptr %.2, null
  br i1 %.not32, label %.thread, label %.lr.ph49, !llvm.loop !17

17:                                               ; preds = %.lr.ph49
  %18 = load ptr, ptr %.248, align 8, !tbaa !9
  %.124.in = getelementptr inbounds i8, ptr %18, i64 32
  %.124 = load ptr, ptr %.124.in, align 8, !tbaa !13
  %.not31 = icmp eq ptr %.124, null
  br i1 %.not31, label %._crit_edge, label %.lr.ph56, !llvm.loop !18

._crit_edge:                                      ; preds = %17, %10
  %.127.lcssa = phi ptr [ %11, %10 ], [ %12, %17 ]
  store ptr %.127.lcssa, ptr %2, align 8, !tbaa !5
  br label %.thread

.thread:                                          ; preds = %8, %.lr.ph56, %15, %4, %._crit_edge
  %.028 = phi i32 [ 0, %._crit_edge ], [ -1, %4 ], [ -1, %15 ], [ -1, %.lr.ph56 ], [ -1, %8 ]
  ret i32 %.028
}

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 = { 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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!14, !6, i64 32}
!14 = !{!"PathVertex_", !6, i64 0, !15, i64 8, !16, i64 16, !15, i64 24, !6, i64 32}
!15 = !{!"double", !7, i64 0}
!16 = !{!"int", !7, i64 0}
!17 = distinct !{!17, !12}
!18 = distinct !{!18, !12}


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