; LLVM IR from ravireddy07/cpp-book :: src/Code-Chef/VBR_Vibrating-Paths.ll (Z5doit2i)

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

@n = external dso_local global i32, align 4
@col = external dso_local global [100005 x i32], align 16
@vis2 = external dso_local global [100005 x i32], align 16

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z4findi(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: mustprogress uwtable
define dso_local void @_Z5doit2i(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  store i32 1, ptr %3, align 4, !tbaa !5
  br label %4

4:                                                ; preds = %42, %1
  %5 = load i32, ptr %3, align 4, !tbaa !5
  %6 = load i32, ptr @n, align 4, !tbaa !5
  %7 = icmp sle i32 %5, %6
  br i1 %7, label %9, label %8

8:                                                ; preds = %4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  br label %45

9:                                                ; preds = %4
  %10 = load i32, ptr %3, align 4, !tbaa !5
  %11 = call noundef i32 @_Z4findi(i32 noundef %10)
  %12 = load i32, ptr %2, align 4, !tbaa !5
  %13 = call noundef i32 @_Z4findi(i32 noundef %12)
  %14 = icmp eq i32 %11, %13
  br i1 %14, label %23, label %15

15:                                               ; preds = %9
  %16 = load i32, ptr %3, align 4, !tbaa !5
  %17 = call noundef i32 @_Z4findi(i32 noundef %16)
  %18 = load i32, ptr %2, align 4, !tbaa !5
  %19 = load i32, ptr @n, align 4, !tbaa !5
  %20 = add nsw i32 %18, %19
  %21 = call noundef i32 @_Z4findi(i32 noundef %20)
  %22 = icmp eq i32 %17, %21
  br i1 %22, label %23, label %41

23:                                               ; preds = %15, %9
  %24 = load i32, ptr %3, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [100005 x i32], ptr @vis2, i64 0, i64 %25
  store i32 1, ptr %26, align 4, !tbaa !5
  %27 = load i32, ptr %3, align 4, !tbaa !5
  %28 = call noundef i32 @_Z4findi(i32 noundef %27)
  %29 = load i32, ptr %2, align 4, !tbaa !5
  %30 = call noundef i32 @_Z4findi(i32 noundef %29)
  %31 = icmp eq i32 %28, %30
  br i1 %31, label %32, label %36

32:                                               ; preds = %23
  %33 = load i32, ptr %3, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [100005 x i32], ptr @col, i64 0, i64 %34
  store i32 1, ptr %35, align 4, !tbaa !5
  br label %40

36:                                               ; preds = %23
  %37 = load i32, ptr %3, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [100005 x i32], ptr @col, i64 0, i64 %38
  store i32 2, ptr %39, align 4, !tbaa !5
  br label %40

40:                                               ; preds = %36, %32
  br label %41

41:                                               ; preds = %40, %15
  br label %42

42:                                               ; preds = %41
  %43 = load i32, ptr %3, align 4, !tbaa !5
  %44 = add nsw i32 %43, 1
  store i32 %44, ptr %3, align 4, !tbaa !5
  br label %4, !llvm.loop !9

45:                                               ; preds = %8
  ret void
}

attributes #0 = { mustprogress 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++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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

@n = external dso_local local_unnamed_addr global i32, align 4
@col = external dso_local local_unnamed_addr global [100005 x i32], align 16
@vis2 = external dso_local local_unnamed_addr global [100005 x i32], align 16

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z4findi(i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5doit2i(i32 noundef %0) local_unnamed_addr #0 {
  %2 = load i32, ptr @n, align 4, !tbaa !5
  %.not11 = icmp slt i32 %2, 1
  br i1 %.not11, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %18, %1
  ret void

.lr.ph:                                           ; preds = %1, %18
  %indvars.iv = phi i64 [ %indvars.iv.next, %18 ], [ 1, %1 ]
  %3 = trunc nuw nsw i64 %indvars.iv to i32
  %4 = tail call noundef i32 @_Z4findi(i32 noundef %3)
  %5 = tail call noundef i32 @_Z4findi(i32 noundef %0)
  %6 = icmp eq i32 %4, %5
  br i1 %6, label %.sink.split, label %7

7:                                                ; preds = %.lr.ph
  %8 = tail call noundef i32 @_Z4findi(i32 noundef %3)
  %9 = load i32, ptr @n, align 4, !tbaa !5
  %10 = add nsw i32 %9, %0
  %11 = tail call noundef i32 @_Z4findi(i32 noundef %10)
  %12 = icmp eq i32 %8, %11
  br i1 %12, label %.sink.split, label %18

.sink.split:                                      ; preds = %7, %.lr.ph
  %13 = getelementptr inbounds [100005 x i32], ptr @vis2, i64 0, i64 %indvars.iv
  store i32 1, ptr %13, align 4, !tbaa !5
  %14 = tail call noundef i32 @_Z4findi(i32 noundef %3)
  %15 = tail call noundef i32 @_Z4findi(i32 noundef %0)
  %16 = icmp eq i32 %14, %15
  %17 = getelementptr inbounds [100005 x i32], ptr @col, i64 0, i64 %indvars.iv
  %. = select i1 %16, i32 1, i32 2
  store i32 %., ptr %17, align 4, !tbaa !5
  br label %18

18:                                               ; preds = %.sink.split, %7
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %19 = load i32, ptr @n, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %.not.not = icmp slt i64 %indvars.iv, %20
  br i1 %.not.not, label %.lr.ph, label %._crit_edge, !llvm.loop !9
}

attributes #0 = { mustprogress 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++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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