// File: /workspace/github_c_makefile_star2_O3/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, %ebp
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
movq %rax, %r14
leal 0x1(%r15), %eax
cmpl %ebp, %eax
jge 0x2aa6
movslq %r15d, %rax
addq %rax, %r12
movl %r15d, %eax
movl %ebp, %edx
negl %edx
addq %rax, %rdx
movl $0x1, %ecx
xorl %eax, %eax
xorl %esi, %esi
movzbl (%r12,%r13), %edi
cmpl $0x2c, %edi
je 0x2a75
cmpl $0x7d, %edi
je 0x2abe
leal -0x3a(%rdi), %r8d
cmpb $-0xa, %r8b
jb 0x2ab6
testl %ecx, %ecx
je 0x2a83
movl %esi, %r8d
incl %esi
movq %rbx, %r9
jmp 0x2a8d
cmpq $0x1, %r13
je 0x2ab6
testl %ecx, %ecx
je 0x2ab6
xorl %ecx, %ecx
jmp 0x2a94
movl %eax, %r8d
incl %eax
xorl %ecx, %ecx
movq %r14, %r9
movslq %r8d, %r8
movb %dil, (%r9,%r8)
incq %r13
movl %edx, %edi
addl %r13d, %edi
jne 0x2a4e
xorl %r12d, %r12d
movl %ebp, %r13d
jmp 0x2ac7
movq %rax, %r13
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2320
jmp 0x2b07
addl %r13d, %r15d
movb $0x1, %r12b
jmp 0x2ac4
addl %r13d, %r15d
xorl %r12d, %r12d
movl %r15d, %r13d
testl %ecx, %ecx
je 0x2ad8
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2320
jmp 0x2ae0
testl %eax, %eax
jne 0x2ae0
movb $0x2b, (%r14)
testb %r12b, %r12b
je 0x2b07
movabsq $-0x100000000, %r14     # imm = 0xFFFFFFFF00000000
leaq 0x86d4(%rip), %rdi      # 0xb1ca
callq 0x20d0
movq $-0x1, %rbx
jmp 0x2ba1
movq %rbx, %rdi
callq 0x2210
movl %eax, %ebp
movq %r14, %rdi
callq 0x2210
movl %eax, %r15d
cmpl %eax, %ebp
setle %al
cmpl $-0x1, %r15d
sete %cl
orb %al, %cl
jne 0x2b44
movabsq $-0x100000000, %r15     # imm = 0xFFFFFFFF00000000
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
leaq 0x86be(%rip), %rdi      # 0xb200
jmp 0x2b6b
cmpl $0x65, %ebp
setl %al
cmpl $0x65, %r15d
setl %cl
testb %cl, %al
jne 0x2b78
movabsq $-0x100000000, %r15     # imm = 0xFFFFFFFF00000000
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
leaq 0x8670(%rip), %rdi      # 0xb1db
callq 0x20d0
xorl %r14d, %r14d
movq %r15, %rax
jmp 0x2b9b
movq %rbx, %rdi
callq 0x2070
movq %r14, %rdi
callq 0x2070
shlq $0x20, %r15
movl %ebp, %ebx
movq %r13, %rax
shlq $0x20, %rax
movl $0x1, %r14d
orq %rax, %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