; LLVM IR from AlgoGenesis/C :: Mathematical Algorithms/Euler Algorithm (Euler's Algo)

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

@top = external dso_local global i32, align 4
@pathIndex = external dso_local global i32, align 4
@stack = external dso_local global [100 x i32], align 16
@n = external dso_local global i32, align 4
@graph = external dso_local global [100 x [100 x i32]], align 16
@path = external dso_local global [100 x i32], align 16

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @pop() #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 void @findEulerian(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  %5 = load i32, ptr %2, align 4, !tbaa !5
  call void @push(i32 noundef %5)
  br label %6

6:                                                ; preds = %59, %1
  %7 = load i32, ptr @top, align 4, !tbaa !5
  %8 = icmp ne i32 %7, -1
  br i1 %8, label %9, label %60

9:                                                ; preds = %6
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  %10 = load i32, ptr @top, align 4, !tbaa !5
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds [100 x i32], ptr @stack, i64 0, i64 %11
  %13 = load i32, ptr %12, align 4, !tbaa !5
  store i32 %13, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %14

14:                                               ; preds = %46, %9
  %15 = load i32, ptr %4, align 4, !tbaa !5
  %16 = load i32, ptr @n, align 4, !tbaa !5
  %17 = icmp slt i32 %15, %16
  br i1 %17, label %18, label %49

18:                                               ; preds = %14
  %19 = load i32, ptr %3, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [100 x [100 x i32]], ptr @graph, i64 0, i64 %20
  %22 = load i32, ptr %4, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [100 x i32], ptr %21, i64 0, i64 %23
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = icmp sgt i32 %25, 0
  br i1 %26, label %27, label %45

27:                                               ; preds = %18
  %28 = load i32, ptr %3, align 4, !tbaa !5
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [100 x [100 x i32]], ptr @graph, i64 0, i64 %29
  %31 = load i32, ptr %4, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [100 x i32], ptr %30, i64 0, i64 %32
  %34 = load i32, ptr %33, align 4, !tbaa !5
  %35 = add nsw i32 %34, -1
  store i32 %35, ptr %33, align 4, !tbaa !5
  %36 = load i32, ptr %4, align 4, !tbaa !5
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [100 x [100 x i32]], ptr @graph, i64 0, i64 %37
  %39 = load i32, ptr %3, align 4, !tbaa !5
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [100 x i32], ptr %38, i64 0, i64 %40
  %42 = load i32, ptr %41, align 4, !tbaa !5
  %43 = add nsw i32 %42, -1
  store i32 %43, ptr %41, align 4, !tbaa !5
  %44 = load i32, ptr %4, align 4, !tbaa !5
  call void @push(i32 noundef %44)
  br label %49

45:                                               ; preds = %18
  br label %46

46:                                               ; preds = %45
  %47 = load i32, ptr %4, align 4, !tbaa !5
  %48 = add nsw i32 %47, 1
  store i32 %48, ptr %4, align 4, !tbaa !5
  br label %14, !llvm.loop !9

49:                                               ; preds = %27, %14
  %50 = load i32, ptr %4, align 4, !tbaa !5
  %51 = load i32, ptr @n, align 4, !tbaa !5
  %52 = icmp eq i32 %50, %51
  br i1 %52, label %53, label %59

53:                                               ; preds = %49
  %54 = call i32 @pop()
  %55 = load i32, ptr @pathIndex, align 4, !tbaa !5
  %56 = add nsw i32 %55, 1
  store i32 %56, ptr @pathIndex, align 4, !tbaa !5
  %57 = sext i32 %55 to i64
  %58 = getelementptr inbounds [100 x i32], ptr @path, i64 0, i64 %57
  store i32 %54, ptr %58, align 4, !tbaa !5
  br label %59

59:                                               ; preds = %53, %49
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  br label %6, !llvm.loop !11

60:                                               ; preds = %6
  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" }
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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


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

@top = external dso_local local_unnamed_addr global i32, align 4
@pathIndex = external dso_local local_unnamed_addr global i32, align 4
@stack = external dso_local local_unnamed_addr global [100 x i32], align 16
@n = external dso_local local_unnamed_addr global i32, align 4
@graph = external dso_local local_unnamed_addr global [100 x [100 x i32]], align 16
@path = external dso_local local_unnamed_addr global [100 x i32], align 16

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @pop() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @findEulerian(i32 noundef %0) local_unnamed_addr #0 {
  tail call void @push(i32 noundef %0)
  %2 = load i32, ptr @top, align 4, !tbaa !5
  %.not19 = icmp eq i32 %2, -1
  br i1 %.not19, label %._crit_edge, label %.lr.ph20.preheader

.lr.ph20.preheader:                               ; preds = %1
  %.pre29 = load i32, ptr @n, align 4, !tbaa !5
  br label %.lr.ph20

.lr.ph20:                                         ; preds = %.lr.ph20.preheader, %28
  %3 = phi i32 [ %29, %28 ], [ %.pre29, %.lr.ph20.preheader ]
  %4 = phi i32 [ %30, %28 ], [ %2, %.lr.ph20.preheader ]
  %5 = icmp sgt i32 %3, 0
  br i1 %5, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %.lr.ph20
  %6 = sext i32 %4 to i64
  %7 = getelementptr inbounds [100 x i32], ptr @stack, i64 0, i64 %6
  %8 = load i32, ptr %7, align 4, !tbaa !5
  %9 = sext i32 %8 to i64
  %wide.trip.count = zext nneg i32 %3 to i64
  br label %10

10:                                               ; preds = %.lr.ph, %20
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %20 ]
  %11 = getelementptr inbounds [100 x [100 x i32]], ptr @graph, i64 0, i64 %9, i64 %indvars.iv
  %12 = load i32, ptr %11, align 4, !tbaa !5
  %13 = icmp sgt i32 %12, 0
  br i1 %13, label %14, label %20

14:                                               ; preds = %10
  %15 = trunc nuw nsw i64 %indvars.iv to i32
  %16 = add nsw i32 %12, -1
  store i32 %16, ptr %11, align 4, !tbaa !5
  %17 = getelementptr inbounds [100 x [100 x i32]], ptr @graph, i64 0, i64 %indvars.iv, i64 %9
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = add nsw i32 %18, -1
  store i32 %19, ptr %17, align 4, !tbaa !5
  tail call void @push(i32 noundef %15)
  %.pre30 = load i32, ptr @n, align 4, !tbaa !5
  br label %.loopexit

20:                                               ; preds = %10
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %.loopexit.thread, label %10, !llvm.loop !9

.loopexit:                                        ; preds = %.lr.ph20, %14
  %21 = phi i32 [ %.pre30, %14 ], [ %3, %.lr.ph20 ]
  %.014 = phi i32 [ %15, %14 ], [ 0, %.lr.ph20 ]
  %22 = icmp eq i32 %.014, %21
  br i1 %22, label %.loopexit.thread, label %28

.loopexit.thread:                                 ; preds = %20, %.loopexit
  %23 = tail call i32 @pop()
  %24 = load i32, ptr @pathIndex, align 4, !tbaa !5
  %25 = add nsw i32 %24, 1
  store i32 %25, ptr @pathIndex, align 4, !tbaa !5
  %26 = sext i32 %24 to i64
  %27 = getelementptr inbounds [100 x i32], ptr @path, i64 0, i64 %26
  store i32 %23, ptr %27, align 4, !tbaa !5
  %.pre = load i32, ptr @n, align 4, !tbaa !5
  br label %28

28:                                               ; preds = %.loopexit.thread, %.loopexit
  %29 = phi i32 [ %.pre, %.loopexit.thread ], [ %21, %.loopexit ]
  %30 = load i32, ptr @top, align 4, !tbaa !5
  %.not = icmp eq i32 %30, -1
  br i1 %.not, label %._crit_edge, label %.lr.ph20, !llvm.loop !11

._crit_edge:                                      ; preds = %28, %1
  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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


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