// File: tomfunkhouser[P]gaps/pkgs/png/pngwutil.c
// Function: png_check_keyword
png_size_t /* PRIVATE */
png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
{
   png_size_t key_len;
   png_charp kp, dp;
   int kflag;
   int kwarn=0;

   png_debug(1, "in png_check_keyword");

   *new_key = NULL;

   if (key == NULL || (key_len = png_strlen(key)) == 0)
   {
      png_warning(png_ptr, "zero length keyword");
      return ((png_size_t)0);
   }

   png_debug1(2, "Keyword to be checked is '%s'", key);

   *new_key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32)(key_len + 2));
   if (*new_key == NULL)
   {
      png_warning(png_ptr, "Out of memory while procesing keyword");
      return ((png_size_t)0);
   }

   /* Replace non-printing characters with a blank and print a warning */
   for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
   {
      if ((png_byte)*kp < 0x20 ||
         ((png_byte)*kp > 0x7E && (png_byte)*kp < 0xA1))
      {
#ifdef PNG_STDIO_SUPPORTED
         char msg[40];

         png_snprintf(msg, 40,
           "invalid keyword character 0x%02X", (png_byte)*kp);
         png_warning(png_ptr, msg);
#else
         png_warning(png_ptr, "invalid character in keyword");
#endif
         *dp = ' ';
      }
      else
      {
         *dp = *kp;
      }
   }
   *dp = '\0';

   /* Remove any trailing white space. */
   kp = *new_key + key_len - 1;
   if (*kp == ' ')
   {
      png_warning(png_ptr, "trailing spaces removed from keyword");

      while (*kp == ' ')
      {
         *(kp--) = '\0';
         key_len--;
      }
   }

   /* Remove any leading white space. */
   kp = *new_key;
   if (*kp == ' ')
   {
      png_warning(png_ptr, "leading spaces removed from keyword");

      while (*kp == ' ')
      {
         kp++;
         key_len--;
      }
   }

   png_debug1(2, "Checking for multiple internal spaces in '%s'", kp);

   /* Remove multiple internal spaces. */
   for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
   {
      if (*kp == ' ' && kflag == 0)
      {
         *(dp++) = *kp;
         kflag = 1;
      }
      else if (*kp == ' ')
      {
         key_len--;
         kwarn=1;
      }
      else
      {
         *(dp++) = *kp;
         kflag = 0;
      }
   }
   *dp = '\0';
   if (kwarn)
      png_warning(png_ptr, "extra interior spaces removed from keyword");

   if (key_len == 0)
   {
      png_free(png_ptr, *new_key);
      png_warning(png_ptr, "Zero length keyword");
   }

   if (key_len > 79)
   {
      png_warning(png_ptr, "keyword length must be 1 - 79 characters");
      (*new_key)[79] = '\0';
      key_len = 79;
   }

   return (key_len);
}

// Assembly:
subq $0x68, %rsp
movq %rdi, 0x58(%rsp)
movq %rsi, 0x50(%rsp)
movq %rdx, 0x48(%rsp)
movl $0x0, 0x28(%rsp)
movq 0x48(%rsp), %rax
movq $0x0, (%rax)
cmpq $0x0, 0x50(%rsp)
je 0x12bf84
movq 0x50(%rsp), %rdi
callq 0xf200
movq %rax, 0x40(%rsp)
cmpq $0x0, %rax
jne 0x12bfa3
movq 0x58(%rsp), %rdi
leaq 0x14aa9(%rip), %rsi     # 0x140a39
callq 0x113940
movq $0x0, 0x60(%rsp)
jmp 0x12c2ac
movq 0x58(%rsp), %rdi
movq 0x40(%rsp), %rax
addq $0x2, %rax
movl %eax, %eax
movl %eax, %esi
callq 0x113760
movq %rax, %rcx
movq 0x48(%rsp), %rax
movq %rcx, (%rax)
movq 0x48(%rsp), %rax
cmpq $0x0, (%rax)
jne 0x12bfef
movq 0x58(%rsp), %rdi
leaq 0x14a71(%rip), %rsi     # 0x140a4d
callq 0x113940
movq $0x0, 0x60(%rsp)
jmp 0x12c2ac
movq 0x50(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x48(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x30(%rsp)
movq 0x38(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0x12c0a6
movq 0x38(%rsp), %rax
movzbl (%rax), %eax
cmpl $0x20, %eax
jl 0x12c040
movq 0x38(%rsp), %rax
movzbl (%rax), %eax
cmpl $0x7e, %eax
jle 0x12c075
movq 0x38(%rsp), %rax
movzbl (%rax), %eax
cmpl $0xa1, %eax
jge 0x12c075
movq %rsp, %rdi
movq 0x38(%rsp), %rax
movzbl (%rax), %ecx
movl $0x28, %esi
leaq 0x14a1c(%rip), %rdx     # 0x140a73
movb $0x0, %al
callq 0xf800
movq 0x58(%rsp), %rdi
movq %rsp, %rsi
callq 0x113940
movq 0x30(%rsp), %rax
movb $0x20, (%rax)
jmp 0x12c083
movq 0x38(%rsp), %rax
movb (%rax), %cl
movq 0x30(%rsp), %rax
movb %cl, (%rax)
jmp 0x12c085
movq 0x38(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x38(%rsp)
movq 0x30(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x30(%rsp)
jmp 0x12c006
movq 0x30(%rsp), %rax
movb $0x0, (%rax)
movq 0x48(%rsp), %rax
movq (%rax), %rax
addq 0x40(%rsp), %rax
addq $-0x1, %rax
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x20, %eax
jne 0x12c115
movq 0x58(%rsp), %rdi
leaq 0x149b7(%rip), %rsi     # 0x140a94
callq 0x113940
movq 0x38(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x20, %eax
jne 0x12c113
movq 0x38(%rsp), %rax
movq %rax, %rcx
addq $-0x1, %rcx
movq %rcx, 0x38(%rsp)
movb $0x0, (%rax)
movq 0x40(%rsp), %rax
addq $-0x1, %rax
movq %rax, 0x40(%rsp)
jmp 0x12c0e2
jmp 0x12c115
movq 0x48(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x20, %eax
jne 0x12c16d
movq 0x58(%rsp), %rdi
leaq 0x1497e(%rip), %rsi     # 0x140ab9
callq 0x113940
movq 0x38(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x20, %eax
jne 0x12c16b
movq 0x38(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x38(%rsp)
movq 0x40(%rsp), %rax
addq $-0x1, %rax
movq %rax, 0x40(%rsp)
jmp 0x12c140
jmp 0x12c16d
movl $0x0, 0x2c(%rsp)
movq 0x48(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x30(%rsp)
movq 0x38(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0x12c229
movq 0x38(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x20, %eax
jne 0x12c1cb
cmpl $0x0, 0x2c(%rsp)
jne 0x12c1cb
movq 0x38(%rsp), %rax
movb (%rax), %cl
movq 0x30(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x30(%rsp)
movb %cl, (%rax)
movl $0x1, 0x2c(%rsp)
jmp 0x12c214
movq 0x38(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x20, %eax
jne 0x12c1f0
movq 0x40(%rsp), %rax
addq $-0x1, %rax
movq %rax, 0x40(%rsp)
movl $0x1, 0x28(%rsp)
jmp 0x12c212
movq 0x38(%rsp), %rax
movb (%rax), %cl
movq 0x30(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x30(%rsp)
movb %cl, (%rax)
movl $0x0, 0x2c(%rsp)
jmp 0x12c214
jmp 0x12c216
movq 0x38(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x38(%rsp)
jmp 0x12c182
movq 0x30(%rsp), %rax
movb $0x0, (%rax)
cmpl $0x0, 0x28(%rsp)
je 0x12c249
movq 0x58(%rsp), %rdi
leaq 0x14899(%rip), %rsi     # 0x140add
callq 0x113940
cmpq $0x0, 0x40(%rsp)
jne 0x12c274
movq 0x58(%rsp), %rdi
movq 0x48(%rsp), %rax
movq (%rax), %rsi
callq 0x1136d0
movq 0x58(%rsp), %rdi
leaq 0x14899(%rip), %rsi     # 0x140b08
callq 0x113940
cmpq $0x4f, 0x40(%rsp)
jbe 0x12c2a2
movq 0x58(%rsp), %rdi
leaq 0x14894(%rip), %rsi     # 0x140b1c
callq 0x113940
movq 0x48(%rsp), %rax
movq (%rax), %rax
movb $0x0, 0x4f(%rax)
movq $0x4f, 0x40(%rsp)
movq 0x40(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x60(%rsp), %rax
addq $0x68, %rsp
retq
nopw %cs:(%rax,%rax)