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

    <tr>
        <th>Summary</th>
        <td>
            [Flang] Incorrect FIR generated for select case statement
        </td>
    </tr>

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

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

    <tr>
      <th>Reporter</th>
      <td>
          jpenix-quic
      </td>
    </tr>
</table>

<pre>
    Flang seems to be generating incorrect FIR for select case constructs under certain circumstances. Please see the example below:

```
subroutine foo(num)
  implicit none

  integer, intent(in) :: num
  real, dimension(1) :: array
  array = 0.0

  select case (num)
  case (6)
    write(*,*) "Case 6!"
    where (array >= 0.0)
      array = 42
    end where
  case default
    write(*,*) "Default case!"
    array = 43
  end select

  write(*,*) array(1)
end subroutine foo

program test
  implicit none
  call foo(6)
end
```

Using gfortran to build, I see the following (expected) output:
```
 Case 6!
   42.0000000
```

Using flang to build, I am seeing the the following:
```
 Case 6!
 Default case!
 43.
```

In the FIR, it looks like the branch generated at the end of the `case (6)` body targets the block corresponding to the subsequent case statement rather than the block after the end of the select case. This seems associated with the case body ending with the `where` clause, but I'm not entirely sure. (For example, adding another write after the `end where` or making the assignment to `array` unconditional in the above example both result in the correct IR being generated.)

This issue relates to 459.GemsFDTD.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyNVcty2zgQ_BrqggqLhh6WDjx4rSil29ZW9gNAYkQhBgEFAGP777cBiBLppJJ1qWgS05hpzKPRWPleH7QwHfNEvWfBsoZYR4acCArLyrTWOWoDOxz_YSfrANTxsxWeWGuND25og2eDkeRYSy4IZVirXDv0PgjTki_Z35oiHDFYOBOjN9FfNCGUtq_F8qmo9kU1PjfV9Zc-_dA4O4AKIbgt-NYMfcF32ciYgh_VqsCMNTR1A5MJ1JEr-HN6NQGblcFeFiMun1j0dMU6EjoCperJeGUB2z5MoMI58T6C0wcse1aV1T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