// File: FreeSlave[P]vhlt/hlrad/qrad.cpp
// Function: SubdividePatch(patch_s*)
static void     SubdividePatch(patch_t* patch)
{
    dplane_t        planes[2];
    dplane_t*       plA = &planes[0];
    dplane_t*       plB = &planes[1];
    Winding**       winding;
    unsigned        x;
    patch_t*        new_patch;

    memset(windingArray, 0, sizeof(windingArray));
    g_numwindings = 0;

    getGridPlanes(patch, planes);
    cutWindingWithGrid(patch, plA, plB);

    x = 0;
    patch->next = NULL;
    winding = windingArray;
    while (*winding == NULL)
    {
        winding++;
        x++;
    }
    patch->winding = *winding;
    winding++;
    x++;
    patch->area = patch->winding->getArea();
    patch->winding->getCenter(patch->origin);
    PlacePatchInside(patch);
#ifdef HLRAD_ACCURATEBOUNCE
	UpdateEmitterInfo (patch);
#endif

    new_patch = g_patches + g_num_patches;
    for (; x < g_numwindings; x++, winding++)
    {
        if (*winding)
        {
            memcpy(new_patch, patch, sizeof(patch_t));

            new_patch->winding = *winding;
            new_patch->area = new_patch->winding->getArea();
            new_patch->winding->getCenter(new_patch->origin);
            PlacePatchInside(new_patch);
#ifdef HLRAD_ACCURATEBOUNCE
			UpdateEmitterInfo (new_patch);
#endif

            new_patch++;
            g_num_patches++;
            hlassume(g_num_patches < MAX_PATCHES, assume_MAX_PATCHES);
        }
    }

    // ATTENTION: We let SortPatches relink all the ->next correctly! instead of doing it here too which is somewhat complicated
}

// Assembly:
subq $0x68, %rsp
movq %rdi, 0x60(%rsp)
leaq 0x30(%rsp), %rax
movq %rax, 0x28(%rsp)
leaq 0x30(%rsp), %rax
addq $0x14, %rax
movq %rax, 0x20(%rsp)
leaq 0x5517a58(%rip), %rdi   # 0x556fbe0
xorl %esi, %esi
movl $0x20000, %edx          # imm = 0x20000
callq 0x41b0
movl $0x0, 0x5537a42(%rip)   # 0x558fbe0
movq 0x60(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x584a0
movq 0x60(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq 0x20(%rsp), %rdx
callq 0x58730
movl $0x0, 0x14(%rsp)
movq 0x60(%rsp), %rax
movq $0x0, (%rax)
leaq 0x5517a04(%rip), %rax   # 0x556fbe0
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rax
cmpq $0x0, (%rax)
jne 0x58207
movq 0x18(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x18(%rsp)
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0x581e1
movq 0x18(%rsp), %rax
movq (%rax), %rcx
movq 0x60(%rsp), %rax
movq %rcx, 0x28(%rax)
movq 0x18(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x18(%rsp)
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
movq 0x60(%rsp), %rax
movq 0x28(%rax), %rdi
callq 0xdcb0
movq 0x60(%rsp), %rax
movss %xmm0, 0x14(%rax)
movq 0x60(%rsp), %rax
movq 0x28(%rax), %rdi
movq 0x60(%rsp), %rsi
addq $0x8, %rsi
callq 0xe060
movq 0x60(%rsp), %rdi
callq 0x577e0
movq 0x60(%rsp), %rdi
callq 0x57f00
movq 0x5395785(%rip), %rax   # 0x53eda00
movl 0x5415787(%rip), %ecx   # 0x546da08
imulq $0x138, %rcx, %rcx      # imm = 0x138
addq %rcx, %rax
movq %rax, 0x8(%rsp)
movl 0x14(%rsp), %eax
cmpl 0x5537946(%rip), %eax   # 0x558fbe0
jae 0x58373
movq 0x18(%rsp), %rax
cmpq $0x0, (%rax)
je 0x58353
movq 0x8(%rsp), %rdi
movq 0x60(%rsp), %rsi
movl $0x138, %edx            # imm = 0x138
callq 0x4280
movq 0x18(%rsp), %rax
movq (%rax), %rcx
movq 0x8(%rsp), %rax
movq %rcx, 0x28(%rax)
movq 0x8(%rsp), %rax
movq 0x28(%rax), %rdi
callq 0xdcb0
movq 0x8(%rsp), %rax
movss %xmm0, 0x14(%rax)
movq 0x8(%rsp), %rax
movq 0x28(%rax), %rdi
movq 0x8(%rsp), %rsi
addq $0x8, %rsi
callq 0xe060
movq 0x8(%rsp), %rdi
callq 0x577e0
movq 0x8(%rsp), %rdi
callq 0x57f00
movq 0x8(%rsp), %rax
addq $0x138, %rax            # imm = 0x138
movq %rax, 0x8(%rsp)
movl 0x54156db(%rip), %eax   # 0x546da08
addl $0x1, %eax
movl %eax, 0x54156d2(%rip)   # 0x546da08
cmpl $0xffff0, 0x54156c8(%rip) # imm = 0xFFFF0
setb %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x1b, %esi
callq 0xbf00
jmp 0x58355
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
movq 0x18(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x18(%rsp)
jmp 0x58290
addq $0x68, %rsp
retq
nopl (%rax,%rax)