; LLVM IR from tangjz/acm-icpc :: codeforces/gym/100624K.ll (Z3pfsi)

; 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.Edge = type { i32, i32, i32 }

@e = external dso_local global [16160 x %struct.Edge], align 16
@pre = external dso_local global [4040 x i32], align 16
@ans = external dso_local global i32, align 4
@que = external dso_local global [4040 x i32], align 16
@vis = external dso_local global [4040 x i8], align 16

; 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: mustprogress uwtable
define dso_local noundef zeroext i1 @_Z3pfsi(i32 noundef %0) #1 {
  %2 = alloca i1, align 1
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  store i32 %0, ptr %3, align 4, !tbaa !5
  %8 = load i32, ptr %3, align 4, !tbaa !5
  %9 = sext i32 %8 to i64
  %10 = getelementptr inbounds [4040 x i8], ptr @vis, i64 0, i64 %9
  store i8 1, ptr %10, align 1, !tbaa !9
  %11 = load i32, ptr %3, align 4, !tbaa !5
  %12 = load i32, ptr @ans, align 4, !tbaa !5
  %13 = add nsw i32 %12, 1
  store i32 %13, ptr @ans, align 4, !tbaa !5
  %14 = sext i32 %12 to i64
  %15 = getelementptr inbounds [4040 x i32], ptr @que, i64 0, i64 %14
  store i32 %11, ptr %15, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 0, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  %16 = load i32, ptr %3, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [4040 x i32], ptr @pre, i64 0, i64 %17
  %19 = load i32, ptr %18, align 4, !tbaa !5
  store i32 %19, ptr %5, align 4, !tbaa !5
  br label %20

20:                                               ; preds = %54, %1
  %21 = load i32, ptr %5, align 4, !tbaa !5
  %22 = icmp ne i32 %21, -1
  br i1 %22, label %24, label %23

23:                                               ; preds = %20
  store i32 2, ptr %6, align 4
  br label %60

24:                                               ; preds = %20
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  %25 = load i32, ptr %5, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [16160 x %struct.Edge], ptr @e, i64 0, i64 %26
  %28 = getelementptr inbounds %struct.Edge, ptr %27, i32 0, i32 1
  store ptr %28, ptr %7, align 8, !tbaa !11
  %29 = load i32, ptr %5, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [16160 x %struct.Edge], ptr @e, i64 0, i64 %30
  %32 = getelementptr inbounds %struct.Edge, ptr %31, i32 0, i32 2
  %33 = load i32, ptr %32, align 4, !tbaa !13
  %34 = icmp ne i32 %33, 0
  br i1 %34, label %35, label %36

35:                                               ; preds = %24
  store i32 4, ptr %6, align 4
  br label %51

36:                                               ; preds = %24
  %37 = load i32, ptr %4, align 4, !tbaa !5
  %38 = add nsw i32 %37, 1
  store i32 %38, ptr %4, align 4, !tbaa !5
  %39 = load ptr, ptr %7, align 8, !tbaa !11
  %40 = load i32, ptr %39, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [4040 x i8], ptr @vis, i64 0, i64 %41
  %43 = load i8, ptr %42, align 1, !tbaa !9, !range !15, !noundef !16
  %44 = trunc i8 %43 to i1
  br i1 %44, label %50, label %45

45:                                               ; preds = %36
  %46 = load ptr, ptr %7, align 8, !tbaa !11
  %47 = load i32, ptr %46, align 4, !tbaa !5
  %48 = call noundef zeroext i1 @_Z3pfsi(i32 noundef %47)
  br i1 %48, label %50, label %49

49:                                               ; preds = %45
  store i1 false, ptr %2, align 1
  store i32 1, ptr %6, align 4
  br label %51

50:                                               ; preds = %45, %36
  store i32 0, ptr %6, align 4
  br label %51

51:                                               ; preds = %50, %49, %35
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  %52 = load i32, ptr %6, align 4
  switch i32 %52, label %60 [
    i32 0, label %53
    i32 4, label %54
  ]

53:                                               ; preds = %51
  br label %54

54:                                               ; preds = %53, %51
  %55 = load i32, ptr %5, align 4, !tbaa !5
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [16160 x %struct.Edge], ptr @e, i64 0, i64 %56
  %58 = getelementptr inbounds %struct.Edge, ptr %57, i32 0, i32 0
  %59 = load i32, ptr %58, align 4, !tbaa !17
  store i32 %59, ptr %5, align 4, !tbaa !5
  br label %20, !llvm.loop !18

60:                                               ; preds = %51, %23
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  %61 = load i32, ptr %6, align 4
  switch i32 %61, label %65 [
    i32 2, label %62
  ]

62:                                               ; preds = %60
  %63 = load i32, ptr %4, align 4, !tbaa !5
  %64 = icmp eq i32 %63, 2
  store i1 %64, ptr %2, align 1
  store i32 1, ptr %6, align 4
  br label %65

65:                                               ; preds = %62, %60
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  %66 = load i1, ptr %2, align 1
  ret i1 %66
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = !{!10, !10, i64 0}
!10 = !{!"bool", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!14, !6, i64 8}
!14 = !{!"_ZTS4Edge", !6, i64 0, !6, i64 4, !6, i64 8}
!15 = !{i8 0, i8 2}
!16 = !{}
!17 = !{!14, !6, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"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"

%struct.Edge = type { i32, i32, i32 }

@e = external dso_local local_unnamed_addr global [16160 x %struct.Edge], align 16
@pre = external dso_local local_unnamed_addr global [4040 x i32], align 16
@ans = external dso_local local_unnamed_addr global i32, align 4
@que = external dso_local local_unnamed_addr global [4040 x i32], align 16
@vis = external dso_local local_unnamed_addr global [4040 x i8], align 16

; Function Attrs: mustprogress nofree nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef zeroext i1 @_Z3pfsi(i32 noundef %0) local_unnamed_addr #0 {
  %2 = sext i32 %0 to i64
  %3 = getelementptr inbounds [4040 x i8], ptr @vis, i64 0, i64 %2
  store i8 1, ptr %3, align 1, !tbaa !5
  %4 = load i32, ptr @ans, align 4, !tbaa !9
  %5 = add nsw i32 %4, 1
  store i32 %5, ptr @ans, align 4, !tbaa !9
  %6 = sext i32 %4 to i64
  %7 = getelementptr inbounds [4040 x i32], ptr @que, i64 0, i64 %6
  store i32 %0, ptr %7, align 4, !tbaa !9
  %8 = getelementptr inbounds [4040 x i32], ptr @pre, i64 0, i64 %2
  %.01523 = load i32, ptr %8, align 4, !tbaa !9
  %.not24 = icmp eq i32 %.01523, -1
  br i1 %.not24, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %1, %23
  %.01526 = phi i32 [ %.015, %23 ], [ %.01523, %1 ]
  %.01625 = phi i32 [ %.117.ph, %23 ], [ 0, %1 ]
  %9 = sext i32 %.01526 to i64
  %10 = getelementptr inbounds [16160 x %struct.Edge], ptr @e, i64 0, i64 %9
  %11 = getelementptr inbounds i8, ptr %10, i64 8
  %12 = load i32, ptr %11, align 4, !tbaa !11
  %.not19 = icmp eq i32 %12, 0
  br i1 %.not19, label %13, label %23

13:                                               ; preds = %.lr.ph
  %14 = getelementptr inbounds i8, ptr %10, i64 4
  %15 = add nsw i32 %.01625, 1
  %16 = load i32, ptr %14, align 4, !tbaa !9
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [4040 x i8], ptr @vis, i64 0, i64 %17
  %19 = load i8, ptr %18, align 1, !tbaa !5, !range !13, !noundef !14
  %20 = trunc nuw i8 %19 to i1
  br i1 %20, label %23, label %21

21:                                               ; preds = %13
  %22 = tail call noundef zeroext i1 @_Z3pfsi(i32 noundef %16)
  br i1 %22, label %23, label %._crit_edge.loopexit

23:                                               ; preds = %13, %21, %.lr.ph
  %.117.ph = phi i32 [ %.01625, %.lr.ph ], [ %15, %21 ], [ %15, %13 ]
  %.015 = load i32, ptr %10, align 4, !tbaa !9
  %.not = icmp eq i32 %.015, -1
  br i1 %.not, label %._crit_edge.loopexit, label %.lr.ph, !llvm.loop !15

._crit_edge.loopexit:                             ; preds = %21, %23
  %.not.lcssa.ph = phi i1 [ true, %23 ], [ false, %21 ]
  %.218.ph = phi i32 [ %.117.ph, %23 ], [ %15, %21 ]
  %24 = icmp eq i32 %.218.ph, 2
  %25 = select i1 %.not.lcssa.ph, i1 %24, i1 false
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %1
  %.not.lcssa = phi i1 [ false, %1 ], [ %25, %._crit_edge.loopexit ]
  ret i1 %.not.lcssa
}

attributes #0 = { mustprogress nofree nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) 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 = !{!"bool", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 8}
!12 = !{!"_ZTS4Edge", !10, i64 0, !10, i64 4, !10, i64 8}
!13 = !{i8 0, i8 2}
!14 = !{}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


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