[Question]
  [
Given the spiral of size `S` and the step `N`, output the "square" `S*S` spiral having `N` asterisks, built from the outer to inner radius clockwise.
Test cases (examples) below.
1. Input: `4 3`
Output:
```
***
```
2. Input: `4 6`
Output:
```
****
   *
   *
```
3. Input: `4 11`
Output:
```
****
   *
*  *
****
```
4. Input: `6 18`
Output:
```
******
     *
     *
*    *
*    *
******
```
5. Input: `6 22`
Output:
```
******
***  *
*    *
*    *
*    *
******
```
6. Input: `6 27`
Output:
```
******
******
*   **
*   **
*   **
******
```
7. Input: `1 1`
Output:
```
*
```
It's not necessary to handle the cases when:
* provided `N` asterisks can't "fit" in the spiral of given `S*S` dimensions.
* either `N` or `S` is zero.
The challenge is code-golf, shortest bytes answer wins, any languages can be used.
Your output may have as many trailing (but not leading) spaces/newlines as you wish.
  
[Answer]
# [MATL](https://github.com/lmendo/MATL), ~~17~~ 16 bytes
```
UGlYLGoQ&P->42*c
```
[Try it online!](https://tio.run/##y00syfn/P9Q9J9LHPT9QLUDXzsRIK/n/fzMuIyMA)
### Explanation (with example)
Consider inputs `4` and `11` as an example.
```
U       % Implicit input: S. Push S^2
        % STACK: 16
G       % Push S again
        % STACK: 16, 4
lYL     % Outward, clockwise, east-first spiral of that size
        % STACK: 16,
                 [ 7  8  9 10;
                   6  1  2 11;
                   5  4  3 12;
                  16 15 14 13]
GoQ     % Push S, compute parity, add 1. Gives 1 for even S, 2 for odd
        % STACK: 16,
                 [ 7  8  9 10;
                   6  1  2 11;
                   5  4  3 12;
                  16 15 14 13],
                 1
&P      % Flip along that dimension (1 is vertical, 2 is horizontal).
        % This corrects for the orientation of the spiral
        % STACK: 16,
                 [16 15 14 13;
                   5  4  3 12;
                   6  1  2 11;
                   7  8  9 10]
-       % Subtract, element-wise. The upper-left corner becomes 0
        % STACK: [ 0  1  2  3
                  11 12 13  4
                  10 15 14  5
                   9  8  7  6]
>       % Implicit input (below): N. Greater than?, element-wise.
        % This transforms the first N entries, starting from
        % upper-left, inward, east-first, into 1, and the rest
        % into 0
        % STACK: [1 1 1 1;
                  0 0 0 1;
                  1 0 0 1;
                  1 1 1 1]
42*     % Multiply each entry by 42
        % STACK: [42 42 42 42;
                   0  0  0 42;
                  42  0  0 42;
                  42 42 42 42]
c       % Convert to char. Char 0 will be displayed as space.
        % Implicit display
        % STACK: ['****';
                  '   *';
                  '*  *';
                  '****']
```
[Answer]
# [Stax](https://github.com/tomtheisen/stax), 19 [bytes](https://github.com/tomtheisen/stax/blob/master/docs/packed.md#packed-stax)
```
±♪☺ÿzMæ¡♠à╣♂7☼V♀§9↓
```
[Run and debug it](https://staxlang.xyz/#p=f10d01987a4d91ad0685b90b370f560c153919&i=6+22)
It starts by building a string that has all the characters in the result with all the asterisks left-aligned. Then it it takes increasingly large slices off the end of the string, and "wraps" them around a grid as it rotates the grid. 
Here's the same program, unpacked, ungolfed, and commented.
```
'**     repeat "*" specified number of times
,J(     square the top of the input stack, and right-pad string to that length
z       push an empty array - this is the result grid built up in the loop
{       begin a block to loop
  ~     push grid to the input stack
  ihNv  push -(i / 2) - 1 where i is the 0-based iteration index using integer division
  :/]   split the string at that index and wrap the second half in a singleton array
  ,     pop the grid from the input stack
  rM+   rotate the grid clockwise, then prepend the split string as the new first row
  n     copy what's left of the original string to top of stack for the loop condition
w       while; execute block until condition is truthy
m       display resulting grid
```
[Run and debug it](https://staxlang.xyz/#c=%27**+++%09repeat+%22*%22+specified+number+of+times%0A,J%28+++%09square+the+top+of+the+input+stack,+and+right-pad+string+to+that+length%0Az+++++%09push+an+empty+array+-+this+is+the+result+grid+built+up+in+the+loop%0A%7B+++++%09begin+a+block+to+loop%0A++%7E+++%09push+grid+to+the+input+stack%0A++ihNv%09push+-%28i+%2F+2%29+-+1+where+i+is+the+0-based+iteration+index+using+integer+division%0A++%3A%2F]+%09split+the+string+at+that+index+and+wrap+the+second+half+in+a+singleton+array%0A++,+++%09pop+the+grid+from+the+input+stack%0A++rM%2B+%09rotate+the+grid+clockwise,+then+prepend+the+split+string+as+the+new+first+row%0A++n+++%09copy+what%27s+left+of+the+original+string+to+top+of+stack+for+the+loop+condition%0Aw+++++%09while%3B+execute+block+until+condition+is+truthy%0Am+++++%09display+resulting+grid&i=6+22&a=1)
[Answer]
# [APL (Dyalog)](https://www.dyalog.com/), 65 bytes
```
' *'[1+⎕>⊖∘⌽⍣o(⊖×⍨-,⍨⍴∘(⍋+\)×⍨↑(⌈2÷⍨×⍨),(+⍨⍴1,⊢,¯1,-)(/⍨)2/⍳)o←⎕]
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT///qKM97b@6gpZ6tKH2o76pdo@6pj3qmPGoZ@@j3sX5GkDe4emPelfo6gCJR71bgFIaj3q7tWM0wcKP2iZqPOrpMDq8HcgBi2jqaGhDlBrqPOpapHNovaGOrqaGPkjKCEhu1sx/1DYBaFEsyOb/aVwmXMZcXCDKDEIZGoJoUyhtxmVoAaGNjKC0OYg25DIEAA "APL (Dyalog Unicode) – Try It Online")
The code for the spiral matrix is taken from [another answer of mine](https://codegolf.stackexchange.com/a/125974/65326).
[Answer]
# [Python 2](https://docs.python.org/2/), 117 bytes
```
n,k=input()
i=0;d=1
l=(' '*n+'\n')*n
exec"l=l[:i]+'*'+l[i+1:];d=[~n/d*cmp(d*d,2),d][' '<l[i+d:]<'*'];i+=d;"*k
print l
```
[Try it online!](https://tio.run/##Fcu9DsIgEADgnadouhx/iZahQylPgky9Jl6KJ2kw0cVXR9y/r3zq/cmuNbZHIC6vKpWgcPUYJpGDhAE0G7gxKM1if@/bmEOOCyUDGkyOZKYldR2/fEG9PYpEjdYpiyn2vP4FLmntOnkyAf2oD1FO4jrk1mbr5h8 "Python 2 – Try It Online")
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 34 bytes
```
ＮθＦＥ⮌Ｅ⊗Ｎ∨ι¹÷⁺ι¬¬κ²«Ｆ‹θι≔θι×ι*≧⁻ιθ↷
```
[Try it online!](https://tio.run/##TU47DoMwDJ3hFBGTU9GhDF2YKrEgFYpQL8AnBauQQD4sVc@ehrB0sOX3s92NjexEM1mb88Xo0swtk7DSNHwJSaBoFqjZxqRifs6EaSfWw7@Z0pg8JGBMLnSfc64z3LBnUE1G7XwpNOz19nriOvmEgT9wZ0rBGhN03E0pHPiB0jCoJHINT5yZXxKdIrrT7o3DWOMwaiiQG@USMVl9CDehD8XBr7VJEl7teZt@ "Charcoal – Try It Online") Link is to verbose version of code. Explanation:
```
Ｎθ
```
Input `N`.
```
ＦＥ⮌Ｅ⊗Ｎ∨ι¹÷⁺ι¬¬κ²«
```
The lengths of the spiral arms (excluding corners) are `S-1`, `S-1`, `S-1`, `S-2`, `S-2`, `S-3`, ..., `3`, `2`, `2`, `1`, `1`, `1`. This is formed by starting with the range from `0` up to but excluding `2S`, changing the 0 to 1, reversing it, adding 1 to each element after the first, and finally integer dividing all the elements by 2. This list is then looped over.
```
Ｆ‹θι≔θι
```
If there are fewer stars left to draw than the length of the next arm, reduce the arm to that length.
```
×ι*
```
Draw the appropriate number of stars.
```
≧⁻ιθ
```
Subtract from the number of stars remaining.
```
↷
```
Rotate the drawing direction by 90° clockwise.
[Answer]
# PHP, 118 bytes
adjusted and golfed [my solution for the Alphabet Spiral](https://codegolf.stackexchange.com/a/94794/55735).
```
for($w=$n=$argv[$s=1],$r="*";--$argv[2];$r[$p+=$s*$d+$s]="*")if(!$c=++$c%$n)($d^=$w)?$n--:$s=-$s;echo wordwrap($r,$w);
```
Run with `php -nr '<code>' <S> <N>` or [try it online](http://sandbox.onlinephpfunctions.com/code/4f67ff1bbfaaa67e1c0d5e61f281614639ae67fa).
[Answer]
# [Python 2](https://docs.python.org/2/), 150 bytes
```
S,N=input()
X=y=n=0
Y=x=c=-1
s=eval(`[[' ']*S]*S`)
exec"if n%S<1:S-=c%2<1;X,Y=-Y,X;c+=1;n=0\nx+=X;y+=Y;s[y][x]='*';n+=1\n"*N
for i in s:print`i`[2::5]
```
[Try it online!](https://tio.run/##DczdCoMgGIDhc69CgrBfWMI20L5b6KSTxAKHFBPGV1QbevUueM8eeLdwvlfkMfZVBw6375nlZIAACDeiwIOFuiEHzL/XJzNaM8qmor8yOZn9bBO3UEz7thF9DTblbSOHSkGtqkHaEhp5fUb0JQwylKDkocOk/QSsYBIvHzEpOrKsO3XUIT3Etjs8jTOaC3GfYnxUlD// "Python 2 – Try It Online")
[Answer]
# J, ~~60~~ 56 Bytes
-4 Bytes by modifying the build proccess for the spiral so that subtracting it from y^2 was unnecessary
```
4 :'''* ''{~x<|."1|.(|:@|.,<:@{:@{:-i.@#)^:(+:<:y),.*:y'
```
[Try it online!](https://tio.run/##JY3BCsIwEAV/5VEPm7Rxy0apZY2QLxGKCFYKir1YGvrr0Soz55l7zuOzf3UDToo9lIhKEM3LOyQuJLFJGhO7oHFe3fYcN/asptKgk3Vc6kT5erk9YByO@MdsXQjMDg1EIC28hz9ALBx/JyvND8kf "J – Try It Online")
Explanation coming ~~soon~~ now.
### Explanation:
```
4 :'''* ''{~x<|."1|.(|:@|.,<:@{:@{:-i.@#)^:(+:<:y),.*:y'  | Explicit dyad definition
                    (|:@|.,<:@{:@{:-i.@#)^:(+:<:y),.*:y   | Generate a y by y inward spiral
                                                  ,.*:y   | The matrix [[y^2]]
                    (                   )^:(+:<:y)        | 2*(y-1) times...
                     |:@|.                                | Rotate
                          ,                               | Append
                                    i.@#                  | [0..len(n)-1]
                           <:@{:@{:-                      | Subtracted from the previous value and decremented
              |."1|.                                      | Flip around antidiagonal
            x>                                            | Test if each entry is less than x
    '' *''{~                                              | ' ' for 0, '*' for 1
```
Examples:
```
   3 :'(|:@|.,<:@{:@{:-i.@#)^:(+:<:y),.*:y' 4
7  8  9 10
6 15 16 11
5 14 13 12
4  3  2  1
   3 :'|."1|.(|:@|.,<:@{:@{:-i.@#)^:(+:<:y),.*:y' 4
1  2  3 4
12 13 14 5
11 16 15 6
10  9  8 7
   11(4 :'x<|."1|.(|:@|.,<:@{:@{:-i.@#)^:(+:<:y),.*:y') 4
0 0 0 0
1 1 1 0
0 1 1 0
0 0 0 0
   11(4 :'''* ''{~x<|."1|.(|:@|.,<:@{:@{:-i.@#)^:(+:<:y),.*:y') 4
****
   *
*  *
****
```
[Answer]
# [Kotlin](https://kotlinlang.org), ~~361~~ ~~355~~ ~~353~~ 334 bytes
6 bytes saved thanks to Jonathan  
2 bytes saved changing to when  
19 bytes saved switching to lambda & tracking outer edges
```
{s:Int,n:Int->var a=Array(s,{_->Array(s,{_->' '})})
var r=0
var c=0
var d=0
var e=0
var f=1
var g=s-1
var h=g
for(i in 1..n){a[r][c]='*'
when(d){0->if(c<g)c++
else{d=1
r++
g--}
1->if(r<h)r++
else{d=2
c--
h--}
2->if(c>e)c--
else{d=3
r--
e++}
3->if(r>f)r--
else{d=0
c++
f++}}}
for(i in 0..s-1){for(j in 0..s-1)print(a[i][j])
println()}}
```
[Try it online!](https://tio.run/##nVdbb9s2FH7Xr2Dz0FiOr0nRDUZsoFi3oUDXAkvzMARBQEuUzEamPFKK4xr@7dl3SOqSxLmsfrAoniu/c@HRdV5kUt0Nu@w3LXghYpbkmhULaViUx4KleZawaMGzTKhUTIJFUazMZDgkItEGpuDRtbgFC@iDKF8O/y2FKWSuzHD8fjwaj4fcFEJLc903K6l5Fvwpb4SCDcE0j2VpWJ4wI38Idsa4ii0BEiv2pcfysliVhd06MP@WXIsDdtY9Y04TW/AbqVL2hVUmTI/NS5kVLNH50opBg8CBciaVwsJbjLI8ul5LIwZB8A3usogbYVhH3PLlKhMmZHOR5WtQGX6fFJyYsHfsxL1/tV5N3Eu3233A9X4/V9cuaN0s7guOx6@S7NaLh7bfs/GvT6holLQcaGnct@zuMXB8/LwBPF/Q@pKBX140UC/t63PLxwbGbC/IQfCpODRM5QVTIhLGcL2hrEFax5mwmeRSZL0QygutdH4jY5RMK//ApQ4LdpDI4gAp55LZJSuyPLWZTwkcy6VQhqrE55iQYNVQhfI7Yyi/H0LnlJ1kuao/5suyT6XXY2aR64Kyd77BA8Vj1lCxlgplwNWGZajJkqfCeoWMZqURMXT@k5e6Kq0l31A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zvf@n/p7vba/6s/a6uuftvTod2Wfkn7F/Cv9MpmP7TKem71aLaRrA/Y6s7sdwyy80ELmc0vVGqdCJw@2oP5NJJzpNwwiZQ8m4jaFL4yXt93fB2NL16SLUDf04iJDBC6IfO/mZCGnL008CTS9HR7vgxMnPklA39FFAxhLQd7vGSYopArqlje@tDR/VC3l58R2BrIO42929OMDZL03ad@NBNXTZ7WZIckS74TR@KAqxXNlOoWl03aMKUzbaBKLu50EM0wlmaUwthqwU@KJwVe1tkZ7PqI9O@ObNoMgR/45Lytr4Xo6ganW8Us/yCGMMjUsyVTThSNuNds6hjtA6x4T7bYEC4PNMNHPulxyJW1BprhccwzASXCab5mxvK1fmAiFohi8UBg20zXA@AOIJL/HhbeUA0njUTLleif2cypK2mPUSLQO1VIFDfdIqeeOydDyqOqWHbTyq@bxiz0n0Lv1VAjWONWV4XH25US362drPiVUHQJxcB6CaDu7@Aw "Kotlin – Try It Online")
[Answer]
# Java 10, ~~284~~ ~~282~~ ~~281~~ 263 bytes
```
s->n->{var c=new char[s][s];for(var d:c)java.util.Arrays.fill(d,' ');for(int i=0,j=0,y=0,x=1,u=s-1,l=0;n-->0;c[j][i]=42,i+=x,j+=y,l+=i==l&x==0?1:0,u-=i==l&j==l&y<1?1:0)if(x!=0){var b=x>0?i<u:i>l;y=b?0:x;x=b?x:0;}else{var b=y>0?j<u:j>l;x=b?0:-y;y=b?y:0;}return c;}
```
A fun challenge!
Try it online [here](https://tio.run/##jdJNb5swGAfwM/0U3mUF8SKTVl0FMdEulXrYqUfEwXFIas91kG0yrIrPnj28lGqaolQCbPz8jI31F/REY7H7fW7areQMMUmNQb8oV@j9xpsHjaUWmtOR79AblPwXq7k6lBWi@mCCQXoCPpS0lstk3ypm@VElT3Nn/axsfah1hL6E2CvVZVVWRYFMwzWViKCziQsVF@8nqhEjqv4zKVPBle@P2h8Ku4wFnyv81Jo6k@y5lP4uukW3wQi5sogTHAm4HdwdSaOWmDiNJMG5iuMC56wUVckrcr@KeEi6SITERTIknBD5vSMEb9IMR208DYjh4dbpMBjwvd99IzgYd7olXYE3fN1mvJC5I9sNzrq8g7bLcN7X0tSzc@AEOAGuG13sxglugLq2rVaI5f3Z83I47AaO3/rT6SS0aaTz74O5cxcE18jDdZKmF83DYh6vm9XqC@bHRZMua10kj8tf4dH0N4M6UVv/k9tp5ke2EJtSuyQCMoZzaNaIJbJWB/sKb2E4qYWJiYmBQUAWKUAOzntxxtZvybG1ybwgMEjTuPn/qlL5Y6Efdt2f/wI).
Thanks to [Kevin Cruijssen](https://codegolf.stackexchange.com/users/52210/kevin-cruijssen "Kevin Cruijssen") for golfing 18 bytes.
Ungolfed version:
```
s -> n -> { // lambda taking two integer arguments in currying syntax
    var c = new char[s][s]; // the matrix containing the spiral
    for(var d : c) // for every row
        java.util.Arrays.fill(d, ' '); // fill it with spaces
    for(int i = 0, j = 0, // the coordinates of the next '*'
            y = 0, x = 1, // the direction to move in
            u = s-1, l = 0; // the upper and lower bounds
        n-- > 0; // decrecement the length of the spiral and repeat as many times
        c[j][i] = 42, // draw the '*', 42 is ASCII code
        i += x, j += y, // move to the next cell
        l += i == l & x == 0 ? 1 : 0, // adjust lower bound if necessary
        u -= i == l & j == l & y < 1 ? 1 : 0) // adjust upper bound if necessary
        if(x != 0) { // if moving in x direction
            var b = x > 0 ? i < u : i > l; // if we hit the bounds
            y = b ? 0 : x; // flip directions,
            x = b ? x : 0; // turning around
        } else { // if moving in y direction
            var b = y > 0 ? j < u : j > l; // if we hit the bounds
            x = b ? 0 : -y; // flip directions,
            y = b ? y : 0;  // turning around
        }
    return c; // return the matrix
}
```
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), 167 164 163 bytes
* thanks to @Erik the Outgolfer and @nicael for spaces (3 bytes)
* thanks to @micha for join``split`,` instead of map (1 byte)
```
(l,s)=>{a=(b=[...Array(l)]).map(x=>b.map(_=>" "))
for(d=1,x=y=D=0;s--;x+=d,y+=D)a[y][x]="*",(a[y+D]||[])[x+d]!=" "?[d,D]=[-D,d]:0
return a.join`
`.split`,`.join``}
```
[Try it online!](https://tio.run/##JYzRCoIwFEDf/YryaTfnsB4KimsE@4sx2moaynKyWUyyb7eot3POw2n1U4erb/oh75yp5hpnYmkALF8ayQUFY@zkvR6JBQnsrnsSsbz84IxlukgBktp5YnBNI47IsTiEPD/EDA0dM@SgxShFlJiuUkq@knE5TUKCiJmRS/wujsJQLlHknBq5LxJfDQ/fLTRrXdOpRLHQ22ZQVP2Des9X1wVnK2bdjdRkSzc7gPkD "JavaScript (Node.js) – Try It Online")
]