; LLVM IR from tangjz/acm-icpc :: TDL_training/2015-11-13/K - How Many Maos Does the Guanxi Worth - dby.ll (Z4SPFAv)

; 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, ptr }

@v = external dso_local global [35 x i8], align 16
@in = external dso_local global [35 x i8], align 16
@n = external dso_local global i32, align 4
@S = external dso_local global i32, align 4
@T = external dso_local global i32, align 4
@q = external dso_local global [10500 x i32], align 16
@dis = external dso_local global [35 x i64], align 16
@first = external dso_local global [35 x ptr], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z4SPFAv() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #2
  store i32 0, ptr %1, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #2
  store i32 0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %6

6:                                                ; preds = %18, %0
  %7 = load i32, ptr %4, align 4, !tbaa !5
  %8 = load i32, ptr @n, align 4, !tbaa !5
  %9 = icmp slt i32 %7, %8
  br i1 %9, label %11, label %10

10:                                               ; preds = %6
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  br label %21

11:                                               ; preds = %6
  %12 = load i32, ptr %4, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [35 x i64], ptr @dis, i64 0, i64 %13
  store i64 100000000000, ptr %14, align 8, !tbaa !9
  %15 = load i32, ptr %4, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [35 x i8], ptr @in, i64 0, i64 %16
  store i8 0, ptr %17, align 1, !tbaa !11
  br label %18

18:                                               ; preds = %11
  %19 = load i32, ptr %4, align 4, !tbaa !5
  %20 = add nsw i32 %19, 1
  store i32 %20, ptr %4, align 4, !tbaa !5
  br label %6, !llvm.loop !13

21:                                               ; preds = %10
  %22 = load i32, ptr @S, align 4, !tbaa !5
  %23 = load i32, ptr %2, align 4, !tbaa !5
  %24 = add nsw i32 %23, 1
  store i32 %24, ptr %2, align 4, !tbaa !5
  %25 = sext i32 %23 to i64
  %26 = getelementptr inbounds [10500 x i32], ptr @q, i64 0, i64 %25
  store i32 %22, ptr %26, align 4, !tbaa !5
  %27 = load i32, ptr @S, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [35 x i64], ptr @dis, i64 0, i64 %28
  store i64 0, ptr %29, align 8, !tbaa !9
  %30 = load i32, ptr @S, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [35 x i8], ptr @in, i64 0, i64 %31
  store i8 1, ptr %32, align 1, !tbaa !11
  br label %33

33:                                               ; preds = %120, %21
  %34 = load i32, ptr %1, align 4, !tbaa !5
  %35 = load i32, ptr %2, align 4, !tbaa !5
  %36 = icmp slt i32 %34, %35
  br i1 %36, label %37, label %121

37:                                               ; preds = %33
  %38 = load i32, ptr %1, align 4, !tbaa !5
  %39 = add nsw i32 %38, 1
  store i32 %39, ptr %1, align 4, !tbaa !5
  %40 = sext i32 %38 to i64
  %41 = getelementptr inbounds [10500 x i32], ptr @q, i64 0, i64 %40
  %42 = load i32, ptr %41, align 4, !tbaa !5
  store i32 %42, ptr %3, align 4, !tbaa !5
  %43 = load i32, ptr %3, align 4, !tbaa !5
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds [35 x i8], ptr @in, i64 0, i64 %44
  store i8 0, ptr %45, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %46 = load i32, ptr %3, align 4, !tbaa !5
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds [35 x ptr], ptr @first, i64 0, i64 %47
  %49 = load ptr, ptr %48, align 8, !tbaa !15
  store ptr %49, ptr %5, align 8, !tbaa !15
  br label %50

50:                                               ; preds = %116, %37
  %51 = load ptr, ptr %5, align 8, !tbaa !15
  %52 = icmp ne ptr %51, null
  br i1 %52, label %54, label %53

53:                                               ; preds = %50
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  br label %120

54:                                               ; preds = %50
  %55 = load ptr, ptr %5, align 8, !tbaa !15
  %56 = getelementptr inbounds %struct.Edge, ptr %55, i32 0, i32 0
  %57 = load i32, ptr %56, align 8, !tbaa !17
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds [35 x i8], ptr @v, i64 0, i64 %58
  %60 = load i8, ptr %59, align 1, !tbaa !11, !range !19, !noundef !20
  %61 = trunc i8 %60 to i1
  br i1 %61, label %62, label %115

62:                                               ; preds = %54
  %63 = load i32, ptr %3, align 4, !tbaa !5
  %64 = sext i32 %63 to i64
  %65 = getelementptr inbounds [35 x i64], ptr @dis, i64 0, i64 %64
  %66 = load i64, ptr %65, align 8, !tbaa !9
  %67 = load ptr, ptr %5, align 8, !tbaa !15
  %68 = getelementptr inbounds %struct.Edge, ptr %67, i32 0, i32 1
  %69 = load i32, ptr %68, align 4, !tbaa !21
  %70 = sext i32 %69 to i64
  %71 = add nsw i64 %66, %70
  %72 = load ptr, ptr %5, align 8, !tbaa !15
  %73 = getelementptr inbounds %struct.Edge, ptr %72, i32 0, i32 0
  %74 = load i32, ptr %73, align 8, !tbaa !17
  %75 = sext i32 %74 to i64
  %76 = getelementptr inbounds [35 x i64], ptr @dis, i64 0, i64 %75
  %77 = load i64, ptr %76, align 8, !tbaa !9
  %78 = icmp slt i64 %71, %77
  br i1 %78, label %79, label %115

79:                                               ; preds = %62
  %80 = load i32, ptr %3, align 4, !tbaa !5
  %81 = sext i32 %80 to i64
  %82 = getelementptr inbounds [35 x i64], ptr @dis, i64 0, i64 %81
  %83 = load i64, ptr %82, align 8, !tbaa !9
  %84 = load ptr, ptr %5, align 8, !tbaa !15
  %85 = getelementptr inbounds %struct.Edge, ptr %84, i32 0, i32 1
  %86 = load i32, ptr %85, align 4, !tbaa !21
  %87 = sext i32 %86 to i64
  %88 = add nsw i64 %83, %87
  %89 = load ptr, ptr %5, align 8, !tbaa !15
  %90 = getelementptr inbounds %struct.Edge, ptr %89, i32 0, i32 0
  %91 = load i32, ptr %90, align 8, !tbaa !17
  %92 = sext i32 %91 to i64
  %93 = getelementptr inbounds [35 x i64], ptr @dis, i64 0, i64 %92
  store i64 %88, ptr %93, align 8, !tbaa !9
  %94 = load ptr, ptr %5, align 8, !tbaa !15
  %95 = getelementptr inbounds %struct.Edge, ptr %94, i32 0, i32 0
  %96 = load i32, ptr %95, align 8, !tbaa !17
  %97 = sext i32 %96 to i64
  %98 = getelementptr inbounds [35 x i8], ptr @in, i64 0, i64 %97
  %99 = load i8, ptr %98, align 1, !tbaa !11, !range !19, !noundef !20
  %100 = trunc i8 %99 to i1
  br i1 %100, label %114, label %101

101:                                              ; preds = %79
  %102 = load ptr, ptr %5, align 8, !tbaa !15
  %103 = getelementptr inbounds %struct.Edge, ptr %102, i32 0, i32 0
  %104 = load i32, ptr %103, align 8, !tbaa !17
  %105 = sext i32 %104 to i64
  %106 = getelementptr inbounds [35 x i8], ptr @in, i64 0, i64 %105
  store i8 1, ptr %106, align 1, !tbaa !11
  %107 = load ptr, ptr %5, align 8, !tbaa !15
  %108 = getelementptr inbounds %struct.Edge, ptr %107, i32 0, i32 0
  %109 = load i32, ptr %108, align 8, !tbaa !17
  %110 = load i32, ptr %2, align 4, !tbaa !5
  %111 = add nsw i32 %110, 1
  store i32 %111, ptr %2, align 4, !tbaa !5
  %112 = sext i32 %110 to i64
  %113 = getelementptr inbounds [10500 x i32], ptr @q, i64 0, i64 %112
  store i32 %109, ptr %113, align 4, !tbaa !5
  br label %114

114:                                              ; preds = %101, %79
  br label %115

115:                                              ; preds = %114, %62, %54
  br label %116

116:                                              ; preds = %115
  %117 = load ptr, ptr %5, align 8, !tbaa !15
  %118 = getelementptr inbounds %struct.Edge, ptr %117, i32 0, i32 2
  %119 = load ptr, ptr %118, align 8, !tbaa !22
  store ptr %119, ptr %5, align 8, !tbaa !15
  br label %50, !llvm.loop !23

120:                                              ; preds = %53
  br label %33, !llvm.loop !24

121:                                              ; preds = %33
  %122 = load i32, ptr @T, align 4, !tbaa !5
  %123 = sext i32 %122 to i64
  %124 = getelementptr inbounds [35 x i64], ptr @dis, i64 0, i64 %123
  %125 = load i64, ptr %124, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #2
  ret i64 %125
}

; 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

attributes #0 = { mustprogress 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"bool", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!16, !16, i64 0}
!16 = !{!"any pointer", !7, i64 0}
!17 = !{!18, !6, i64 0}
!18 = !{!"_ZTS4Edge", !6, i64 0, !6, i64 4, !16, i64 8}
!19 = !{i8 0, i8 2}
!20 = !{}
!21 = !{!18, !6, i64 4}
!22 = !{!18, !16, i64 8}
!23 = distinct !{!23, !14}
!24 = distinct !{!24, !14}


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

@v = external dso_local local_unnamed_addr global [35 x i8], align 16
@in = external dso_local local_unnamed_addr global [35 x i8], align 16
@n = external dso_local local_unnamed_addr global i32, align 4
@S = external dso_local local_unnamed_addr global i32, align 4
@T = external dso_local local_unnamed_addr global i32, align 4
@q = external dso_local local_unnamed_addr global [10500 x i32], align 16
@dis = external dso_local local_unnamed_addr global [35 x i64], align 16
@first = external dso_local local_unnamed_addr global [35 x ptr], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: read, inaccessiblemem: none) uwtable
define dso_local noundef i64 @_Z4SPFAv() local_unnamed_addr #0 {
  %1 = load i32, ptr @n, align 4, !tbaa !5
  %2 = icmp sgt i32 %1, 0
  br i1 %2, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %0
  %3 = zext nneg i32 %1 to i64
  tail call void @llvm.memset.p0.i64(ptr nonnull align 16 @in, i8 0, i64 %3, i1 false), !tbaa !9
  %min.iters.check = icmp ult i32 %1, 4
  br i1 %min.iters.check, label %.lr.ph.preheader39, label %vector.ph

.lr.ph.preheader39:                               ; preds = %middle.block, %.lr.ph.preheader
  %indvars.iv.ph = phi i64 [ 0, %.lr.ph.preheader ], [ %n.vec, %middle.block ]
  br label %.lr.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %3, 2147483644
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %4 = getelementptr inbounds [35 x i64], ptr @dis, i64 0, i64 %index
  %5 = getelementptr inbounds i8, ptr %4, i64 16
  store <2 x i64> <i64 100000000000, i64 100000000000>, ptr %4, align 16, !tbaa !11
  store <2 x i64> <i64 100000000000, i64 100000000000>, ptr %5, align 16, !tbaa !11
  %index.next = add nuw i64 %index, 4
  %6 = icmp eq i64 %index.next, %n.vec
  br i1 %6, label %middle.block, label %vector.body, !llvm.loop !13

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %n.vec, %3
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph.preheader39

._crit_edge:                                      ; preds = %.lr.ph, %middle.block, %0
  %7 = load i32, ptr @S, align 4, !tbaa !5
  store i32 %7, ptr @q, align 16, !tbaa !5
  %8 = sext i32 %7 to i64
  %9 = getelementptr inbounds [35 x i64], ptr @dis, i64 0, i64 %8
  store i64 0, ptr %9, align 8, !tbaa !11
  %10 = getelementptr inbounds [35 x i8], ptr @in, i64 0, i64 %8
  store i8 1, ptr %10, align 1, !tbaa !9
  br label %14

.lr.ph:                                           ; preds = %.lr.ph.preheader39, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ %indvars.iv.ph, %.lr.ph.preheader39 ]
  %11 = getelementptr inbounds [35 x i64], ptr @dis, i64 0, i64 %indvars.iv
  store i64 100000000000, ptr %11, align 8, !tbaa !11
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %3
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !17

.loopexit:                                        ; preds = %44, %14
  %.1.lcssa = phi i32 [ %.02433, %14 ], [ %.2, %44 ]
  %12 = sext i32 %.1.lcssa to i64
  %13 = icmp slt i64 %indvars.iv.next37, %12
  br i1 %13, label %14, label %46, !llvm.loop !18

14:                                               ; preds = %._crit_edge, %.loopexit
  %indvars.iv36 = phi i64 [ 0, %._crit_edge ], [ %indvars.iv.next37, %.loopexit ]
  %.02433 = phi i32 [ 1, %._crit_edge ], [ %.1.lcssa, %.loopexit ]
  %indvars.iv.next37 = add nuw nsw i64 %indvars.iv36, 1
  %15 = getelementptr inbounds [10500 x i32], ptr @q, i64 0, i64 %indvars.iv36
  %16 = load i32, ptr %15, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [35 x i8], ptr @in, i64 0, i64 %17
  store i8 0, ptr %18, align 1, !tbaa !9
  %19 = getelementptr inbounds [35 x ptr], ptr @first, i64 0, i64 %17
  %.027 = load ptr, ptr %19, align 8, !tbaa !19
  %.not28 = icmp eq ptr %.027, null
  br i1 %.not28, label %.loopexit, label %.lr.ph32

.lr.ph32:                                         ; preds = %14
  %20 = getelementptr inbounds [35 x i64], ptr @dis, i64 0, i64 %17
  br label %21

21:                                               ; preds = %.lr.ph32, %44
  %.030 = phi ptr [ %.027, %.lr.ph32 ], [ %.0, %44 ]
  %.129 = phi i32 [ %.02433, %.lr.ph32 ], [ %.2, %44 ]
  %22 = load i32, ptr %.030, align 8, !tbaa !21
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [35 x i8], ptr @v, i64 0, i64 %23
  %25 = load i8, ptr %24, align 1, !tbaa !9, !range !23, !noundef !24
  %26 = trunc nuw i8 %25 to i1
  br i1 %26, label %27, label %44

27:                                               ; preds = %21
  %28 = load i64, ptr %20, align 8, !tbaa !11
  %29 = getelementptr inbounds i8, ptr %.030, i64 4
  %30 = load i32, ptr %29, align 4, !tbaa !25
  %31 = sext i32 %30 to i64
  %32 = add nsw i64 %28, %31
  %33 = getelementptr inbounds [35 x i64], ptr @dis, i64 0, i64 %23
  %34 = load i64, ptr %33, align 8, !tbaa !11
  %35 = icmp slt i64 %32, %34
  br i1 %35, label %36, label %44

36:                                               ; preds = %27
  store i64 %32, ptr %33, align 8, !tbaa !11
  %37 = getelementptr inbounds [35 x i8], ptr @in, i64 0, i64 %23
  %38 = load i8, ptr %37, align 1, !tbaa !9, !range !23, !noundef !24
  %39 = trunc nuw i8 %38 to i1
  br i1 %39, label %44, label %40

40:                                               ; preds = %36
  store i8 1, ptr %37, align 1, !tbaa !9
  %41 = add nsw i32 %.129, 1
  %42 = sext i32 %.129 to i64
  %43 = getelementptr inbounds [10500 x i32], ptr @q, i64 0, i64 %42
  store i32 %22, ptr %43, align 4, !tbaa !5
  br label %44

44:                                               ; preds = %21, %27, %40, %36
  %.2 = phi i32 [ %.129, %36 ], [ %41, %40 ], [ %.129, %27 ], [ %.129, %21 ]
  %45 = getelementptr inbounds i8, ptr %.030, i64 8
  %.0 = load ptr, ptr %45, align 8, !tbaa !19
  %.not = icmp eq ptr %.0, null
  br i1 %.not, label %.loopexit, label %21, !llvm.loop !26

46:                                               ; preds = %.loopexit
  %47 = load i32, ptr @T, align 4, !tbaa !5
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [35 x i64], ptr @dis, i64 0, i64 %48
  %50 = load i64, ptr %49, align 8, !tbaa !11
  ret i64 %50
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: read, 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" }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }

!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 = !{!"long long", !7, i64 0}
!13 = distinct !{!13, !14, !15, !16}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!"llvm.loop.isvectorized", i32 1}
!16 = !{!"llvm.loop.unroll.runtime.disable"}
!17 = distinct !{!17, !14, !16, !15}
!18 = distinct !{!18, !14}
!19 = !{!20, !20, i64 0}
!20 = !{!"any pointer", !7, i64 0}
!21 = !{!22, !6, i64 0}
!22 = !{!"_ZTS4Edge", !6, i64 0, !6, i64 4, !20, i64 8}
!23 = !{i8 0, i8 2}
!24 = !{}
!25 = !{!22, !6, i64 4}
!26 = distinct !{!26, !14}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopIdiomRecognizePass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass