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

; 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
@leaf = 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 @_Z3dfsii(i32 noundef %0, i32 noundef %1) #1 {
  %3 = alloca i1, align 1
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 0, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %10 = load i32, ptr %4, align 4, !tbaa !5
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds [4040 x i32], ptr @pre, i64 0, i64 %11
  %13 = load i32, ptr %12, align 4, !tbaa !5
  store i32 %13, ptr %7, align 4, !tbaa !5
  br label %14

14:                                               ; preds = %48, %2
  %15 = load i32, ptr %7, align 4, !tbaa !5
  %16 = icmp ne i32 %15, -1
  br i1 %16, label %18, label %17

17:                                               ; preds = %14
  store i32 2, ptr %8, align 4
  br label %54

18:                                               ; preds = %14
  %19 = load i32, ptr %7, align 4, !tbaa !5
  %20 = load i32, ptr %5, align 4, !tbaa !5
  %21 = xor i32 %20, 1
  %22 = icmp eq i32 %19, %21
  br i1 %22, label %23, label %24

23:                                               ; preds = %18
  br label %48

24:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  %25 = load i32, ptr %7, 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 %9, align 8, !tbaa !9
  %29 = load ptr, ptr %9, align 8, !tbaa !9
  %30 = load i32, ptr %29, align 4, !tbaa !5
  %31 = load i32, ptr %7, align 4, !tbaa !5
  %32 = call noundef zeroext i1 @_Z3dfsii(i32 noundef %30, i32 noundef %31)
  br i1 %32, label %34, label %33

33:                                               ; preds = %24
  store i1 false, ptr %3, align 1
  store i32 1, ptr %8, align 4
  br label %45

34:                                               ; preds = %24
  %35 = load i32, ptr %7, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [16160 x %struct.Edge], ptr @e, i64 0, i64 %36
  %38 = getelementptr inbounds %struct.Edge, ptr %37, i32 0, i32 2
  %39 = load i32, ptr %38, align 4, !tbaa !11
  %40 = icmp ne i32 %39, 0
  %41 = xor i1 %40, true
  %42 = zext i1 %41 to i32
  %43 = load i32, ptr %6, align 4, !tbaa !5
  %44 = add nsw i32 %43, %42
  store i32 %44, ptr %6, align 4, !tbaa !5
  store i32 0, ptr %8, align 4
  br label %45

45:                                               ; preds = %34, %33
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  %46 = load i32, ptr %8, align 4
  switch i32 %46, label %54 [
    i32 0, label %47
  ]

47:                                               ; preds = %45
  br label %48

48:                                               ; preds = %47, %23
  %49 = load i32, ptr %7, align 4, !tbaa !5
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds [16160 x %struct.Edge], ptr @e, i64 0, i64 %50
  %52 = getelementptr inbounds %struct.Edge, ptr %51, i32 0, i32 0
  %53 = load i32, ptr %52, align 4, !tbaa !13
  store i32 %53, ptr %7, align 4, !tbaa !5
  br label %14, !llvm.loop !14

54:                                               ; preds = %45, %17
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  %55 = load i32, ptr %8, align 4
  switch i32 %55, label %86 [
    i32 2, label %56
  ]

56:                                               ; preds = %54
  %57 = load i32, ptr %6, align 4, !tbaa !5
  %58 = icmp eq i32 %57, 2
  br i1 %58, label %59, label %72

59:                                               ; preds = %56
  %60 = load i32, ptr %5, align 4, !tbaa !5
  %61 = icmp ne i32 %60, -1
  br i1 %61, label %62, label %72

62:                                               ; preds = %59
  %63 = load i32, ptr %5, align 4, !tbaa !5
  %64 = xor i32 %63, 1
  %65 = sext i32 %64 to i64
  %66 = getelementptr inbounds [16160 x %struct.Edge], ptr @e, i64 0, i64 %65
  %67 = getelementptr inbounds %struct.Edge, ptr %66, i32 0, i32 2
  store i32 1, ptr %67, align 4, !tbaa !11
  %68 = load i32, ptr %5, align 4, !tbaa !5
  %69 = sext i32 %68 to i64
  %70 = getelementptr inbounds [16160 x %struct.Edge], ptr @e, i64 0, i64 %69
  %71 = getelementptr inbounds %struct.Edge, ptr %70, i32 0, i32 2
  store i32 1, ptr %71, align 4, !tbaa !11
  br label %72

72:                                               ; preds = %62, %59, %56
  %73 = load i32, ptr %4, align 4, !tbaa !5
  %74 = sext i32 %73 to i64
  %75 = getelementptr inbounds [4040 x i8], ptr @leaf, i64 0, i64 %74
  %76 = load i8, ptr %75, align 1, !tbaa !16, !range !18, !noundef !19
  %77 = trunc i8 %76 to i1
  br i1 %77, label %84, label %78

78:                                               ; preds = %72
  %79 = load i32, ptr %6, align 4, !tbaa !5
  %80 = icmp eq i32 %79, 1
  br i1 %80, label %84, label %81

81:                                               ; preds = %78
  %82 = load i32, ptr %6, align 4, !tbaa !5
  %83 = icmp eq i32 %82, 2
  br label %84

84:                                               ; preds = %81, %78, %72
  %85 = phi i1 [ true, %78 ], [ true, %72 ], [ %83, %81 ]
  store i1 %85, ptr %3, align 1
  store i32 1, ptr %8, align 4
  br label %86

86:                                               ; preds = %84, %54
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  %87 = load i1, ptr %3, align 1
  ret i1 %87
}

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 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !6, i64 8}
!12 = !{!"_ZTS4Edge", !6, i64 0, !6, i64 4, !6, i64 8}
!13 = !{!12, !6, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!17, !17, i64 0}
!17 = !{!"bool", !7, i64 0}
!18 = !{i8 0, i8 2}
!19 = !{}


; 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
@leaf = 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 @_Z3dfsii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = sext i32 %0 to i64
  %4 = getelementptr inbounds [4040 x i32], ptr @pre, i64 0, i64 %3
  %.02537 = load i32, ptr %4, align 4, !tbaa !5
  %.not38 = icmp eq i32 %.02537, -1
  br i1 %.not38, label %._crit_edge.thread, label %.lr.ph

.lr.ph:                                           ; preds = %2, %.lr.ph._crit_edge
  %.02540 = phi i32 [ %.025, %.lr.ph._crit_edge ], [ %.02537, %2 ]
  %.02639 = phi i32 [ %.228, %.lr.ph._crit_edge ], [ 0, %2 ]
  %5 = xor i32 %.02540, %1
  %6 = icmp eq i32 %5, 1
  %.pre = sext i32 %.02540 to i64
  br i1 %6, label %.lr.ph._crit_edge, label %7

7:                                                ; preds = %.lr.ph
  %8 = getelementptr inbounds [16160 x %struct.Edge], ptr @e, i64 0, i64 %.pre
  %9 = getelementptr inbounds i8, ptr %8, i64 4
  %10 = load i32, ptr %9, align 4, !tbaa !5
  %11 = tail call noundef zeroext i1 @_Z3dfsii(i32 noundef %10, i32 noundef %.02540)
  br i1 %11, label %.thread, label %.loopexit

.thread:                                          ; preds = %7
  %12 = getelementptr inbounds i8, ptr %8, i64 8
  %13 = load i32, ptr %12, align 4, !tbaa !9
  %.not31 = icmp eq i32 %13, 0
  %14 = zext i1 %.not31 to i32
  %15 = add nsw i32 %.02639, %14
  br label %.lr.ph._crit_edge

.lr.ph._crit_edge:                                ; preds = %.lr.ph, %.thread
  %.228 = phi i32 [ %15, %.thread ], [ %.02639, %.lr.ph ]
  %16 = getelementptr inbounds [16160 x %struct.Edge], ptr @e, i64 0, i64 %.pre
  %.025 = load i32, ptr %16, align 4, !tbaa !5
  %.not = icmp eq i32 %.025, -1
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !11

._crit_edge:                                      ; preds = %.lr.ph._crit_edge
  %17 = icmp eq i32 %.228, 2
  %18 = icmp ne i32 %1, -1
  %or.cond = and i1 %18, %17
  br i1 %or.cond, label %19, label %._crit_edge.thread

19:                                               ; preds = %._crit_edge
  %20 = xor i32 %1, 1
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [16160 x %struct.Edge], ptr @e, i64 0, i64 %21, i32 2
  store i32 1, ptr %22, align 4, !tbaa !9
  %23 = sext i32 %1 to i64
  %24 = getelementptr inbounds [16160 x %struct.Edge], ptr @e, i64 0, i64 %23, i32 2
  store i32 1, ptr %24, align 4, !tbaa !9
  br label %._crit_edge.thread

._crit_edge.thread:                               ; preds = %2, %19, %._crit_edge
  %.026.lcssa43 = phi i32 [ 2, %19 ], [ %.228, %._crit_edge ], [ 0, %2 ]
  %25 = getelementptr inbounds [4040 x i8], ptr @leaf, i64 0, i64 %3
  %26 = load i8, ptr %25, align 1, !tbaa !13, !range !15, !noundef !16
  %27 = trunc nuw i8 %26 to i1
  %28 = add i32 %.026.lcssa43, -1
  %29 = icmp ult i32 %28, 2
  %spec.select = or i1 %29, %27
  br label %.loopexit

.loopexit:                                        ; preds = %7, %._crit_edge.thread
  %.4 = phi i1 [ %spec.select, %._crit_edge.thread ], [ false, %7 ]
  ret i1 %.4
}

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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !6, i64 8}
!10 = !{!"_ZTS4Edge", !6, i64 0, !6, i64 4, !6, i64 8}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!14, !14, i64 0}
!14 = !{!"bool", !7, i64 0}
!15 = !{i8 0, i8 2}
!16 = !{}


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