// File: /workspace/github_c_makefile_star2_O1/jondurbin[P]dre2/src/dre2_parse.c
// Function: dre2_range
struct dre2_range_return dre2_range( unsigned char *re, int length, int pos )
{
  unsigned char *min_buffer, *max_buffer;
  min_buffer = ( unsigned char * )calloc( 5, sizeof( unsigned char ) );
  max_buffer = ( unsigned char * )calloc( 5, sizeof( unsigned char ) );

  pos++;

  int t = pos;

  // Is this modifier valid?
  int valid = true;
  // Are we still working on the first part of the range?
  int w_min = true;
  // Number of characters in the min string.
  int cmin = 0;
  // Number of characters in the max string.
  int cmax = 0;

  unsigned char c;
  while ( pos < length )
  {
    c = re[pos];
    if ( c == ',' )
    {
      if ( t == pos )
      {
        valid = false;
        break;
      }
      if ( !w_min )
      {
        valid = false;
        break;
      }
      w_min = false;
    } else if ( c == '}' )
    {
      break;
    } else if ( c < '0' || c > '9' )
    {
      valid = false;
      break;
    } else
    {
      int j;
      if ( w_min )
      {
        cmin++;
        min_buffer[cmin - 1] = c;
      } else
      {
        cmax++;
        max_buffer[cmax - 1] = c;
      }
    }
    pos++;
  }
  if ( !w_min )
  {
    // See if it's unbounded on the right, e.g. {1,}
    if ( cmax == false )
    {
      max_buffer[0] = '+';
    }
  } else
  {
    // See if it's an exact size, e.g. {3}
    strcat( max_buffer, min_buffer );
  }

  // Setup the return structure.
  struct dre2_range_return ret;
  ret.min = DRE2_INFINITE;
  ret.max = DRE2_INFINITE;
  ret.pos = DRE2_INFINITE;
  ret.valid = false;

  // Make sure it's valid.
  if ( !valid )
  {
    printf( "Invalid range {}\n" );
    return ret;
  }

  // Convert strings to integers.
  int min = string_to_int( min_buffer );
  int max = string_to_int( max_buffer );

  // Make sure it's a valid size range.
  if ( min > max && max != DRE2_INFINITE )
  {
    printf("Invalid range, min is greater than max!\n");
    return ret;
  }
  if ( min > 100 || max > 100 )
  {
    printf( "Way too high of a range, use + or *.\n" );
    return ret;
  }

  // Free buffers.
  free( min_buffer );
  free( max_buffer );
  min_buffer = NULL;
  max_buffer = NULL;

  // Setup the return structure and return it.
  ret.min = min;
  ret.max = max;
  ret.pos = pos;
  ret.valid = true;
  return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %edx, %r15d
movl %esi, %r14d
movq %rdi, %r12
movl $0x1, %r13d
movl $0x5, %edi
movl $0x1, %esi
callq 0x2200
movq %rax, %rbx
movl $0x5, %edi
movl $0x1, %esi
callq 0x2200
leal 0x1(%r15), %ebp
xorl %r10d, %r10d
cmpl %r14d, %ebp
jge 0x2af0
movslq %r15d, %rcx
addq %rcx, %r12
movl %r15d, %ecx
movl %r14d, %edx
negl %edx
addq %rcx, %rdx
movl $0x1, %ecx
xorl %esi, %esi
movzbl (%r12,%r13), %edi
cmpl $0x2c, %edi
je 0x2ac3
cmpl $0x7d, %edi
je 0x2b01
leal -0x3a(%rdi), %r8d
cmpb $-0xa, %r8b
jb 0x2af9
testl %ecx, %ecx
je 0x2ad1
movl %esi, %r8d
incl %esi
movq %rbx, %r9
jmp 0x2ada
cmpq $0x1, %r13
je 0x2af9
testl %ecx, %ecx
je 0x2af9
xorl %ecx, %ecx
jmp 0x2ae1
movl %r10d, %r8d
incl %r10d
movq %rax, %r9
movslq %r8d, %r8
movb %dil, (%r9,%r8)
incq %r13
movl %edx, %edi
addl %r13d, %edi
jne 0x2a9c
xorl %r12d, %r12d
jmp 0x2b0a
movb $0x1, %dl
xorl %ecx, %ecx
xorl %r12d, %r12d
jmp 0x2b18
addl %r13d, %r15d
movb $0x1, %r12b
jmp 0x2b07
addl %r13d, %r15d
xorl %r12d, %r12d
movl %r15d, %r14d
testl %ecx, %ecx
sete %cl
testl %r10d, %r10d
sete %dl
movl %r14d, %ebp
testb %cl, %cl
je 0x2b25
testb %dl, %dl
je 0x2b36
movb $0x2b, (%rax)
jmp 0x2b36
movq %rax, %rdi
movq %rbx, %rsi
movq %rax, %r14
callq 0x2330
movq %r14, %rax
testb %r12b, %r12b
je 0x2b5d
movabsq $-0x100000000, %r14     # imm = 0xFFFFFFFF00000000
leaq 0x867e(%rip), %rdi      # 0xb1ca
callq 0x20d0
movq $-0x1, %rbx
jmp 0x2bfe
movq %rbx, %rdi
movq %rax, %r15
callq 0x2210
movl %eax, %r14d
movq %r15, %r12
movq %r15, %rdi
callq 0x2210
movl %eax, %r15d
cmpl %eax, %r14d
setle %al
cmpl $-0x1, %r15d
sete %cl
orb %al, %cl
jne 0x2ba2
movabsq $-0x100000000, %r15     # imm = 0xFFFFFFFF00000000
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
leaq 0x8660(%rip), %rdi      # 0xb200
jmp 0x2bca
cmpl $0x65, %r14d
setl %al
cmpl $0x65, %r15d
setl %cl
testb %cl, %al
jne 0x2bd7
movabsq $-0x100000000, %r15     # imm = 0xFFFFFFFF00000000
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
leaq 0x8611(%rip), %rdi      # 0xb1db
callq 0x20d0
xorl %r14d, %r14d
movq %r15, %rbp
jmp 0x2bf8
movq %rbx, %rdi
callq 0x2070
movq %r12, %rdi
callq 0x2070
shlq $0x20, %r15
movl %r14d, %ebx
shlq $0x20, %rbp
movl $0x1, %r14d
orq %rbp, %r14
orq %r15, %rbx
movq %rbx, %rax
movq %r14, %rdx
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq