; LLVM IR from AlgoGenesis/C :: Graph Algorithms/Graph_Reversal (program)

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

; Function Attrs: nounwind uwtable
declare dso_local void @pushBack(ptr noundef, i32 noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local void @initGraph(ptr noundef, i32 noundef) #0

; 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

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

11:                                               ; preds = %54, %1
  %12 = load i32, ptr %4, align 4, !tbaa !12
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.Graph, ptr %13, i32 0, i32 1
  %15 = load i32, ptr %14, align 8, !tbaa !9
  %16 = icmp slt i32 %12, %15
  br i1 %16, label %18, label %17

17:                                               ; preds = %11
  store i32 2, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  br label %57

18:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 0, ptr %6, align 4, !tbaa !12
  br label %19

19:                                               ; preds = %50, %18
  %20 = load i32, ptr %6, align 4, !tbaa !12
  %21 = load ptr, ptr %3, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.Graph, ptr %21, i32 0, i32 0
  %23 = load ptr, ptr %22, align 8, !tbaa !13
  %24 = load i32, ptr %4, align 4, !tbaa !12
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds %struct.AdjList, ptr %23, i64 %25
  %27 = getelementptr inbounds %struct.AdjList, ptr %26, i32 0, i32 1
  %28 = load i32, ptr %27, align 8, !tbaa !14
  %29 = icmp slt i32 %20, %28
  br i1 %29, label %31, label %30

30:                                               ; preds = %19
  store i32 5, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %53

31:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.Graph, ptr %32, i32 0, i32 0
  %34 = load ptr, ptr %33, align 8, !tbaa !13
  %35 = load i32, ptr %4, align 4, !tbaa !12
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds %struct.AdjList, ptr %34, i64 %36
  %38 = getelementptr inbounds %struct.AdjList, ptr %37, i32 0, i32 0
  %39 = load ptr, ptr %38, align 8, !tbaa !16
  %40 = load i32, ptr %6, align 4, !tbaa !12
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds i32, ptr %39, i64 %41
  %43 = load i32, ptr %42, align 4, !tbaa !12
  store i32 %43, ptr %7, align 4, !tbaa !12
  %44 = getelementptr inbounds %struct.Graph, ptr %2, i32 0, i32 0
  %45 = load ptr, ptr %44, align 8, !tbaa !13
  %46 = load i32, ptr %7, align 4, !tbaa !12
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds %struct.AdjList, ptr %45, i64 %47
  %49 = load i32, ptr %4, align 4, !tbaa !12
  call void @pushBack(ptr noundef %48, i32 noundef %49)
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %50

50:                                               ; preds = %31
  %51 = load i32, ptr %6, align 4, !tbaa !12
  %52 = add nsw i32 %51, 1
  store i32 %52, ptr %6, align 4, !tbaa !12
  br label %19, !llvm.loop !17

53:                                               ; preds = %30
  br label %54

54:                                               ; preds = %53
  %55 = load i32, ptr %4, align 4, !tbaa !12
  %56 = add nsw i32 %55, 1
  store i32 %56, ptr %4, align 4, !tbaa !12
  br label %11, !llvm.loop !19

57:                                               ; preds = %17
  %58 = load { ptr, i32 }, ptr %2, align 8
  ret { ptr, i32 } %58
}

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


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

%struct.Graph = type { ptr, i32 }
%struct.AdjList = type { ptr, i32, i32 }

; Function Attrs: nounwind uwtable
declare dso_local void @pushBack(ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @initGraph(ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local { ptr, i32 } @solve(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
  %2 = alloca %struct.Graph, align 8
  %3 = getelementptr inbounds i8, ptr %0, i64 8
  %4 = load i32, ptr %3, align 8, !tbaa !5
  call void @initGraph(ptr noundef nonnull %2, i32 noundef %4)
  %5 = load i32, ptr %3, align 8, !tbaa !5
  %6 = icmp sgt i32 %5, 0
  br i1 %6, label %.preheader.preheader, label %._crit_edge15

.preheader.preheader:                             ; preds = %1
  %.pre = load ptr, ptr %0, align 8, !tbaa !11
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %._crit_edge
  %7 = phi i32 [ %5, %.preheader.preheader ], [ %14, %._crit_edge ]
  %8 = phi ptr [ %.pre, %.preheader.preheader ], [ %15, %._crit_edge ]
  %indvars.iv17 = phi i64 [ 0, %.preheader.preheader ], [ %indvars.iv.next18, %._crit_edge ]
  %9 = getelementptr inbounds %struct.AdjList, ptr %8, i64 %indvars.iv17
  %10 = getelementptr inbounds i8, ptr %9, i64 8
  %11 = load i32, ptr %10, align 8, !tbaa !12
  %12 = icmp sgt i32 %11, 0
  br i1 %12, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %.preheader
  %13 = trunc nuw nsw i64 %indvars.iv17 to i32
  br label %.lr.ph

._crit_edge15:                                    ; preds = %._crit_edge, %1
  %.fca.0.load = load ptr, ptr %2, align 8
  %.fca.0.insert = insertvalue { ptr, i32 } poison, ptr %.fca.0.load, 0
  %.fca.1.gep = getelementptr inbounds i8, ptr %2, i64 8
  %.fca.1.load = load i32, ptr %.fca.1.gep, align 8
  %.fca.1.insert = insertvalue { ptr, i32 } %.fca.0.insert, i32 %.fca.1.load, 1
  ret { ptr, i32 } %.fca.1.insert

._crit_edge.loopexit:                             ; preds = %.lr.ph
  %.pre20 = load i32, ptr %3, align 8, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %.preheader
  %14 = phi i32 [ %.pre20, %._crit_edge.loopexit ], [ %7, %.preheader ]
  %15 = phi ptr [ %25, %._crit_edge.loopexit ], [ %8, %.preheader ]
  %indvars.iv.next18 = add nuw nsw i64 %indvars.iv17, 1
  %16 = sext i32 %14 to i64
  %17 = icmp slt i64 %indvars.iv.next18, %16
  br i1 %17, label %.preheader, label %._crit_edge15, !llvm.loop !14

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next, %.lr.ph ]
  %18 = phi ptr [ %9, %.lr.ph.preheader ], [ %26, %.lr.ph ]
  %19 = load ptr, ptr %18, align 8, !tbaa !16
  %20 = getelementptr inbounds i32, ptr %19, i64 %indvars.iv
  %21 = load i32, ptr %20, align 4, !tbaa !17
  %22 = load ptr, ptr %2, align 8, !tbaa !11
  %23 = sext i32 %21 to i64
  %24 = getelementptr inbounds %struct.AdjList, ptr %22, i64 %23
  call void @pushBack(ptr noundef %24, i32 noundef %13)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %25 = load ptr, ptr %0, align 8, !tbaa !11
  %26 = getelementptr inbounds %struct.AdjList, ptr %25, i64 %indvars.iv17
  %27 = getelementptr inbounds i8, ptr %26, i64 8
  %28 = load i32, ptr %27, align 8, !tbaa !12
  %29 = sext i32 %28 to i64
  %30 = icmp slt i64 %indvars.iv.next, %29
  br i1 %30, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !18
}

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, !10, i64 8}
!6 = !{!"", !7, i64 0, !10, i64 8}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!6, !7, i64 0}
!12 = !{!13, !10, i64 8}
!13 = !{!"", !7, i64 0, !10, i64 8, !10, i64 12}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!13, !7, i64 0}
!17 = !{!10, !10, i64 0}
!18 = distinct !{!18, !15}


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