<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/127762>127762</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [clang] Crash at -O2: Assertion `i < getNumSuccessors() && "Successo r # out of range for Branch!"' failed.
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          cardigan1008
      </td>
    </tr>
</table>

<pre>
    This code crashes at `-O2`:

```c
int a, b, d, e, f, h;
int i(int o, int q[], int r[]) {
  for (int g = 0; g < o; g++)
    if (q[g])
      if (r[g])
 return 0;
  return 1;
}
int s() {
  a = 0;
  switch (d) {
  case 3:
    return 0;
  case 43:
  case 2:
  case 9:
    return 1;
 }
}
int u() {
  long p = s();
  b = p;
  return 0;
}
int v(int o, int q) {
  int t, j = 0, k = 0;
  int l[] = {0, k, 4, 1, 0, 4, k, 2, k, 7, 9, 7, 7, 2, 9, 3, 6, 6, k, 4};
  int m[] = {j, j, 6, 8, j, 5, 4, 9, 7, 5, j, 9, 1, j, 3, 3};
  int n = i(o, l, m);
  if (n + o)
    t = q + 1;
  return t;
}
void w() {
  int c = 0;
  for (; c + a < u() + f; c++)
    if (0 == v(f, e) + c) {
      h = c;
      break;
    }
}
void main() {}
```

Compiler Explorer: https://godbolt.org/z/esW1xq3nP

Bisected to https://github.com/llvm/llvm-project/commit/38376dee92224c6657ef6d88413bfc77f4441268, which was committed by @fhahn 

Crash: 

```console
clang: /root/llvm-project/llvm/include/llvm/IR/Instructions.h:3117: llvm::BasicBlock* llvm::BranchInst::getSuccessor(unsigned int) const: Assertion `i < getNumSuccessors() && "Successor # out of range for Branch!"' failed.
```

Backtrace:

```console
Stack dump:
0.      Program arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/clang -gdwarf-4 -g -o /app/output.s -mllvm --x86-asm-syntax=intel -fno-verbose-asm -S --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics -fno-crash-diagnostics -O2 <source>
1.      <eof> parser at end of file
2.      Optimizer
3.      Running pass "function<eager-inv>(float2int,lower-constant-intrinsics,loop(loop-rotate<header-duplication;no-prepare-for-lto>,loop-deletion),loop-distribute,inject-tli-mappings,loop-vectorize<no-interleave-forced-only;no-vectorize-forced-only;>,infer-alignment,loop-load-elim,instcombine<max-iterations=1;no-verify-fixpoint>,simplifycfg<bonus-inst-threshold=1;forward-switch-cond;switch-range-to-icmp;switch-to-lookup;no-keep-loops;hoist-common-insts;no-hoist-loads-stores-with-cond-faulting;sink-common-insts;speculate-blocks;simplify-cond-branch;no-speculate-unpredictables>,slp-vectorizer,vector-combine,instcombine<max-iterations=1;no-verify-fixpoint>,loop-unroll<O2>,transform-warning,sroa<preserve-cfg>,infer-alignment,instcombine<max-iterations=1;no-verify-fixpoint>,loop-mssa(licm<allowspeculation>),alignment-from-assumptions,loop-sink,instsimplify,div-rem-pairs,tailcallelim,simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;no-switch-to-lookup;keep-loops;no-hoist-common-insts;hoist-loads-stores-with-cond-faulting;no-sink-common-insts;speculate-blocks;simplify-cond-branch;speculate-unpredictables>)" on module "<source>"
4.      Running pass "loop-vectorize<no-interleave-forced-only;no-vectorize-forced-only;>" on function "w"
 #0 0x0000000003e9f068 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x3e9f068)
 #1 0x0000000003e9cd24 llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x3e9cd24)
 #2 0x0000000003de9128 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #3 0x0000766dece42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x0000766dece969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #5 0x0000766dece42476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #6 0x0000766dece287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #7 0x0000766dece2871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
 #8 0x0000766dece39e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
 #9 0x0000000005b93fce (anonymous namespace)::PlainCFGBuilder::buildPlainCFG(llvm::DenseMap<llvm::VPBlockBase*, llvm::BasicBlock*, llvm::DenseMapInfo<llvm::VPBlockBase*, void>, llvm::detail::DenseMapPair<llvm::VPBlockBase*, llvm::BasicBlock*>>&) VPlanHCFGBuilder.cpp:0:0
#10 0x0000000005b9432b llvm::VPlanHCFGBuilder::buildPlainCFG() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x5b9432b)
#11 0x0000000005b943b2 llvm::VPlanHCFGBuilder::buildHierarchicalCFG() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x5b943b2)
#12 0x0000000005a4d231 llvm::LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(llvm::VFRange&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x5a4d231)
#13 0x0000000005a5035a llvm::LoopVectorizationPlanner::buildVPlansWithVPRecipes(llvm::ElementCount, llvm::ElementCount) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x5a5035a)
#14 0x0000000005a508a7 llvm::LoopVectorizationPlanner::plan(llvm::ElementCount, unsigned int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x5a508a7)
#15 0x0000000005a522df llvm::LoopVectorizePass::processLoop(llvm::Loop*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x5a522df)
#16 0x0000000005a549a9 llvm::LoopVectorizePass::runImpl(llvm::Function&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x5a549a9)
#17 0x0000000005a55023 llvm::LoopVectorizePass::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x5a55023)
#18 0x000000000557565e llvm::detail::PassModel<llvm::Function, llvm::LoopVectorizePass, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x557565e)
#19 0x00000000038433c0 llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x38433c0)
#20 0x00000000011b9b4e llvm::detail::PassModel<llvm::Function, llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x11b9b4e)
#21 0x0000000003841cdb llvm::ModuleToFunctionPassAdaptor::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x3841cdb)
#22 0x00000000011b8e9e llvm::detail::PassModel<llvm::Module, llvm::ModuleToFunctionPassAdaptor, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x11b8e9e)
#23 0x00000000038416e0 llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x38416e0)
#24 0x000000000415c764 (anonymous namespace)::EmitAssemblyHelper::RunOptimizationPipeline(clang::BackendAction, std::unique_ptr<llvm::raw_pwrite_stream, std::default_delete<llvm::raw_pwrite_stream>>&, std::unique_ptr<llvm::ToolOutputFile, std::default_delete<llvm::ToolOutputFile>>&, clang::BackendConsumer*) BackendUtil.cpp:0:0
#25 0x000000000416021e clang::emitBackendOutput(clang::CompilerInstance&, clang::CodeGenOptions&, llvm::StringRef, llvm::Module*, clang::BackendAction, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>, std::unique_ptr<llvm::raw_pwrite_stream, std::default_delete<llvm::raw_pwrite_stream>>, clang::BackendConsumer*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x416021e)
#26 0x0000000004860280 clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4860280)
#27 0x00000000064ac8bc clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x64ac8bc)
#28 0x0000000004860688 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4860688)
#29 0x0000000004b274b5 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4b274b5)
#30 0x0000000004aaae6e clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4aaae6e)
#31 0x0000000004c15f0e clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4c15f0e)
#32 0x0000000000d6562f cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0xd6562f)
#33 0x0000000000d5d0aa ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#34 0x00000000048a6c99 void llvm::function_ref<void ()>::callback_fn<clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const::'lambda'()>(long) Job.cpp:0:0
#35 0x0000000003de95d4 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x3de95d4)
#36 0x00000000048a728f clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const (.part.0) Job.cpp:0:0
#37 0x000000000486a40d clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x486a40d)
#38 0x000000000486b48e clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x486b48e)
#39 0x0000000004873445 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x4873445)
#40 0x0000000000d62423 clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0xd62423)
#41 0x0000000000c2b604 main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0xc2b604)
#42 0x0000766dece29d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#43 0x0000766dece29e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#44 0x0000000000d5cb55 _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0xd5cb55)
```

</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzkW0tz47ay_jX0BkUXCVIktfBCkscnk0ruuMY-c5YuEAQlxCDAAKAf-fW3GnzTssfOKKl7c1waSsSj--sPjUaD4BBj-F4yduGttt7q8ow09qD0BSW64HsiwyDIznJVPF_cHrhBVBUMUU3MgRlELPKSwP-CvSTwoo0XuE8StB_qBRsuLSIe3qEcLgVcGFxKuBy8aNu14R7O4FtBOfz4vUXT3-r-do28FDohVCqNul575EWXKPCirfu5Q8r99PDWfdauPUK8hA4ged_K6sr7Gj2v0cw2Wj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