; LLVM IR from tangjz/acm-icpc :: TDL_training/2015-11-29/E - Furniture Factory - dby.ll (Z5buildv)

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

@S = external dso_local global i32, align 4
@T = external dso_local global i32, align 4
@q = external dso_local global [830 x i32], align 16
@level = external dso_local global [830 x i32], align 16
@first = external dso_local global [830 x ptr], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef zeroext i1 @_Z5buildv() #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
  %6 = load i32, ptr @S, align 4, !tbaa !5
  store i32 %6, ptr %1, align 4, !tbaa !5
  br label %7

7:                                                ; preds = %16, %0
  %8 = load i32, ptr %1, align 4, !tbaa !5
  %9 = load i32, ptr @T, align 4, !tbaa !5
  %10 = icmp sle i32 %8, %9
  br i1 %10, label %12, label %11

11:                                               ; preds = %7
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #2
  br label %19

12:                                               ; preds = %7
  %13 = load i32, ptr %1, align 4, !tbaa !5
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [830 x i32], ptr @level, i64 0, i64 %14
  store i32 -1, ptr %15, align 4, !tbaa !5
  br label %16

16:                                               ; preds = %12
  %17 = load i32, ptr %1, align 4, !tbaa !5
  %18 = add nsw i32 %17, 1
  store i32 %18, ptr %1, align 4, !tbaa !5
  br label %7, !llvm.loop !9

19:                                               ; preds = %11
  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
  store i32 0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  %20 = load i32, ptr @S, align 4, !tbaa !5
  %21 = load i32, ptr %3, align 4, !tbaa !5
  %22 = add nsw i32 %21, 1
  store i32 %22, ptr %3, align 4, !tbaa !5
  %23 = sext i32 %21 to i64
  %24 = getelementptr inbounds [830 x i32], ptr @q, i64 0, i64 %23
  store i32 %20, ptr %24, align 4, !tbaa !5
  %25 = load i32, ptr @S, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [830 x i32], ptr @level, i64 0, i64 %26
  store i32 0, ptr %27, align 4, !tbaa !5
  %28 = load i32, ptr @S, align 4, !tbaa !5
  store i32 %28, ptr %4, align 4, !tbaa !5
  br label %29

29:                                               ; preds = %76, %19
  %30 = load i32, ptr %2, align 4, !tbaa !5
  %31 = load i32, ptr %3, align 4, !tbaa !5
  %32 = icmp slt i32 %30, %31
  br i1 %32, label %33, label %82

33:                                               ; preds = %29
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %34 = load i32, ptr %4, align 4, !tbaa !5
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [830 x ptr], ptr @first, i64 0, i64 %35
  %37 = load ptr, ptr %36, align 8, !tbaa !11
  store ptr %37, ptr %5, align 8, !tbaa !11
  br label %38

38:                                               ; preds = %71, %33
  %39 = load ptr, ptr %5, align 8, !tbaa !11
  %40 = icmp ne ptr %39, null
  br i1 %40, label %42, label %41

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

42:                                               ; preds = %38
  %43 = load ptr, ptr %5, align 8, !tbaa !11
  %44 = getelementptr inbounds %struct.Edge, ptr %43, i32 0, i32 1
  %45 = load i32, ptr %44, align 4, !tbaa !13
  %46 = icmp sgt i32 %45, 0
  br i1 %46, label %47, label %70

47:                                               ; preds = %42
  %48 = load ptr, ptr %5, align 8, !tbaa !11
  %49 = getelementptr inbounds %struct.Edge, ptr %48, i32 0, i32 0
  %50 = load i32, ptr %49, align 8, !tbaa !15
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds [830 x i32], ptr @level, i64 0, i64 %51
  %53 = load i32, ptr %52, align 4, !tbaa !5
  %54 = icmp eq i32 %53, -1
  br i1 %54, label %55, label %70

55:                                               ; preds = %47
  %56 = load i32, ptr %4, align 4, !tbaa !5
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds [830 x i32], ptr @level, i64 0, i64 %57
  %59 = load i32, ptr %58, align 4, !tbaa !5
  %60 = add nsw i32 %59, 1
  %61 = load ptr, ptr %5, align 8, !tbaa !11
  %62 = getelementptr inbounds %struct.Edge, ptr %61, i32 0, i32 0
  %63 = load i32, ptr %62, align 8, !tbaa !15
  %64 = load i32, ptr %3, align 4, !tbaa !5
  %65 = add nsw i32 %64, 1
  store i32 %65, ptr %3, align 4, !tbaa !5
  %66 = sext i32 %64 to i64
  %67 = getelementptr inbounds [830 x i32], ptr @q, i64 0, i64 %66
  store i32 %63, ptr %67, align 4, !tbaa !5
  %68 = sext i32 %63 to i64
  %69 = getelementptr inbounds [830 x i32], ptr @level, i64 0, i64 %68
  store i32 %60, ptr %69, align 4, !tbaa !5
  br label %70

70:                                               ; preds = %55, %47, %42
  br label %71

71:                                               ; preds = %70
  %72 = load ptr, ptr %5, align 8, !tbaa !11
  %73 = getelementptr inbounds %struct.Edge, ptr %72, i32 0, i32 2
  %74 = load ptr, ptr %73, align 8, !tbaa !16
  store ptr %74, ptr %5, align 8, !tbaa !11
  br label %38, !llvm.loop !17

75:                                               ; preds = %41
  br label %76

76:                                               ; preds = %75
  %77 = load i32, ptr %2, align 4, !tbaa !5
  %78 = add nsw i32 %77, 1
  store i32 %78, ptr %2, align 4, !tbaa !5
  %79 = sext i32 %78 to i64
  %80 = getelementptr inbounds [830 x i32], ptr @q, i64 0, i64 %79
  %81 = load i32, ptr %80, align 4, !tbaa !5
  store i32 %81, ptr %4, align 4, !tbaa !5
  br label %29, !llvm.loop !18

82:                                               ; preds = %29
  %83 = load i32, ptr @T, align 4, !tbaa !5
  %84 = sext i32 %83 to i64
  %85 = getelementptr inbounds [830 x i32], ptr @level, i64 0, i64 %84
  %86 = load i32, ptr %85, align 4, !tbaa !5
  %87 = icmp ne i32 %86, -1
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #2
  ret i1 %87
}

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

@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 [830 x i32], align 16
@level = external dso_local local_unnamed_addr global [830 x i32], align 16
@first = external dso_local local_unnamed_addr global [830 x ptr], align 16

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

.lr.ph.preheader:                                 ; preds = %0
  %3 = shl nsw i64 %.pre, 2
  %scevgep = getelementptr i8, ptr @level, i64 %3
  %4 = sub i32 %2, %1
  %5 = zext i32 %4 to i64
  %6 = shl nuw nsw i64 %5, 2
  %7 = add nuw nsw i64 %6, 4
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(1) %scevgep, i8 -1, i64 %7, i1 false), !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %0, %.lr.ph.preheader
  store i32 %1, ptr @q, align 16, !tbaa !5
  %8 = getelementptr inbounds [830 x i32], ptr @level, i64 0, i64 %.pre
  store i32 0, ptr %8, align 4, !tbaa !5
  br label %9

9:                                                ; preds = %._crit_edge, %._crit_edge27
  %indvars.iv = phi i64 [ 0, %._crit_edge ], [ %indvars.iv.next, %._crit_edge27 ]
  %.01431 = phi i32 [ %1, %._crit_edge ], [ %14, %._crit_edge27 ]
  %.01530 = phi i32 [ 1, %._crit_edge ], [ %.1.lcssa, %._crit_edge27 ]
  %10 = sext i32 %.01431 to i64
  %11 = getelementptr inbounds [830 x ptr], ptr @first, i64 0, i64 %10
  %.021 = load ptr, ptr %11, align 8, !tbaa !9
  %.not1822 = icmp eq ptr %.021, null
  br i1 %.not1822, label %._crit_edge27, label %.lr.ph26

.lr.ph26:                                         ; preds = %9
  %12 = getelementptr inbounds [830 x i32], ptr @level, i64 0, i64 %10
  br label %17

._crit_edge27:                                    ; preds = %33, %9
  %.1.lcssa = phi i32 [ %.01530, %9 ], [ %.2, %33 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %13 = getelementptr inbounds [830 x i32], ptr @q, i64 0, i64 %indvars.iv.next
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %15 = sext i32 %.1.lcssa to i64
  %16 = icmp slt i64 %indvars.iv.next, %15
  br i1 %16, label %9, label %35, !llvm.loop !11

17:                                               ; preds = %.lr.ph26, %33
  %.024 = phi ptr [ %.021, %.lr.ph26 ], [ %.0, %33 ]
  %.123 = phi i32 [ %.01530, %.lr.ph26 ], [ %.2, %33 ]
  %18 = getelementptr inbounds i8, ptr %.024, i64 4
  %19 = load i32, ptr %18, align 4, !tbaa !13
  %20 = icmp sgt i32 %19, 0
  br i1 %20, label %21, label %33

21:                                               ; preds = %17
  %22 = load i32, ptr %.024, align 8, !tbaa !15
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [830 x i32], ptr @level, i64 0, i64 %23
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = icmp eq i32 %25, -1
  br i1 %26, label %27, label %33

27:                                               ; preds = %21
  %28 = load i32, ptr %12, align 4, !tbaa !5
  %29 = add nsw i32 %28, 1
  %30 = add nsw i32 %.123, 1
  %31 = sext i32 %.123 to i64
  %32 = getelementptr inbounds [830 x i32], ptr @q, i64 0, i64 %31
  store i32 %22, ptr %32, align 4, !tbaa !5
  store i32 %29, ptr %24, align 4, !tbaa !5
  br label %33

33:                                               ; preds = %17, %21, %27
  %.2 = phi i32 [ %30, %27 ], [ %.123, %21 ], [ %.123, %17 ]
  %34 = getelementptr inbounds i8, ptr %.024, i64 8
  %.0 = load ptr, ptr %34, align 8, !tbaa !9
  %.not18 = icmp eq ptr %.0, null
  br i1 %.not18, label %._crit_edge27, label %17, !llvm.loop !16

35:                                               ; preds = %._crit_edge27
  %36 = sext i32 %2 to i64
  %37 = getelementptr inbounds [830 x i32], ptr @level, i64 0, i64 %36
  %38 = load i32, ptr %37, align 4, !tbaa !5
  %39 = icmp ne i32 %38, -1
  ret i1 %39
}

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


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