// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/ply.cpp
// Function: get_words(char*, int, int, char*, char**, int*)
int get_words(char *orig_line, int max_line_size, int max_words, char *words_buffer, char **words, int *nwords)
{
  char *ptr,*ptr2;

  /* initialize result */
  words_buffer[0] = '\0';
  *nwords = 0;

  /* add space at end of buffer so that surely terminate search for spaces */
  orig_line[max_line_size-2] = ' ';
  orig_line[max_line_size-1] = '\0';

  /* convert line-feed and tabs into spaces */
  /* (this guarentees that there will be a space before the */
  /*  null character at the end of the string) */
  for (ptr = orig_line, ptr2 = words_buffer; *ptr != '\0'; ptr++, ptr2++) {
    *ptr2 = *ptr;
    if (*ptr == '\t') {
      *ptr = ' ';
      *ptr2 = ' ';
    }
    else if (*ptr == '\n') {
      *ptr2 = ' ';
      *ptr = '\0';
      break;
    }
    else if (*ptr == '\r') {
      *ptr2 = ' ';
      *ptr = '\0';
    }
  }

  /* find the words in the line */
  ptr = words_buffer;
  while (*ptr != '\0') {

    /* jump over leading spaces */
    while (*ptr == ' ')
      ptr++;

    /* break if we reach the end */
    if (*ptr == '\0')
      break;

    /* save pointer to beginning of word */
    words[*nwords] = ptr;
    (*nwords)++;

    /* jump over non-spaces */
    while (*ptr != ' ')
      ptr++;

    /* place a null character here to mark the end of the word */
    *ptr++ = '\0';

    // Check if found max words
    if (*nwords >= max_words) break;
  }

  // Return success
  return 1;
}

// Assembly:
movq %rdi, -0x8(%rsp)
movl %esi, -0xc(%rsp)
movl %edx, -0x10(%rsp)
movq %rcx, -0x18(%rsp)
movq %r8, -0x20(%rsp)
movq %r9, -0x28(%rsp)
movq -0x18(%rsp), %rax
movb $0x0, (%rax)
movq -0x28(%rsp), %rax
movl $0x0, (%rax)
movq -0x8(%rsp), %rax
movl -0xc(%rsp), %ecx
subl $0x2, %ecx
movslq %ecx, %rcx
movb $0x20, (%rax,%rcx)
movq -0x8(%rsp), %rax
movl -0xc(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movb $0x0, (%rax,%rcx)
movq -0x8(%rsp), %rax
movq %rax, -0x30(%rsp)
movq -0x18(%rsp), %rax
movq %rax, -0x38(%rsp)
movq -0x30(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0x733fa
movq -0x30(%rsp), %rax
movb (%rax), %cl
movq -0x38(%rsp), %rax
movb %cl, (%rax)
movq -0x30(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x9, %eax
jne 0x73397
movq -0x30(%rsp), %rax
movb $0x20, (%rax)
movq -0x38(%rsp), %rax
movb $0x20, (%rax)
jmp 0x733d7
movq -0x30(%rsp), %rax
movsbl (%rax), %eax
cmpl $0xa, %eax
jne 0x733b6
movq -0x38(%rsp), %rax
movb $0x20, (%rax)
movq -0x30(%rsp), %rax
movb $0x0, (%rax)
jmp 0x733fa
movq -0x30(%rsp), %rax
movsbl (%rax), %eax
cmpl $0xd, %eax
jne 0x733d3
movq -0x38(%rsp), %rax
movb $0x20, (%rax)
movq -0x30(%rsp), %rax
movb $0x0, (%rax)
jmp 0x733d5
jmp 0x733d7
jmp 0x733d9
movq -0x30(%rsp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rsp)
movq -0x38(%rsp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rsp)
jmp 0x73359
movq -0x18(%rsp), %rax
movq %rax, -0x30(%rsp)
movq -0x30(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0x734aa
jmp 0x73417
movq -0x30(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x20, %eax
jne 0x73434
movq -0x30(%rsp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rsp)
jmp 0x73417
movq -0x30(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x73443
jmp 0x734aa
movq -0x30(%rsp), %rdx
movq -0x20(%rsp), %rax
movq -0x28(%rsp), %rcx
movslq (%rcx), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x28(%rsp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movq -0x30(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x20, %eax
je 0x73482
movq -0x30(%rsp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rsp)
jmp 0x73465
movq -0x30(%rsp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x30(%rsp)
movb $0x0, (%rax)
movq -0x28(%rsp), %rax
movl (%rax), %eax
cmpl -0x10(%rsp), %eax
jl 0x734a5
jmp 0x734aa
jmp 0x73404
movl $0x1, %eax
retq