// File: /workspace/github_c_makefile_star2_O3/jondurbin[P]dre2/src/dre2_parse.c
// Function: dre2_starting_point
int dre2_starting_point( struct dre2 *graph, int *minimal, int *minimal_id, int minimal_count )
{
  int i, j, k, l, m;
  int min_iter, iter;
  int id, all, found, temp;
  int *required;
  int required_count;
  int best;
  int **found_in, *found_in_count, *found_in_id;
  int *swapper;
  int *remaining;
  int largest, last_largest;
  struct dre2_path *paths;
  int path_count;
  struct dre2_fl_cost cost;

  // First, check if the first or last node is acceptable.
  cost = dre2_first_or_last_cost( graph, minimal );

  // Initialize paths.
  path_count = 1;
  paths = ( struct dre2_path * )malloc( sizeof( struct dre2_path ) );
  paths[0].nodes = ( int * )malloc( sizeof( int ) * graph->count );
  paths[0].count = 1;
  paths[0].nodes[0] = 0;

  graph->starting_points = ( int * )malloc( sizeof( int ) * graph->count );
  graph->starting_count = 0;

  // Recursively find paths through the minimal graph.
  dre2_find_paths_recursive( graph, 0, &path_count, &paths );
  required = NULL;
  for ( i = 0; i < path_count; i++ )
  {
    best = dre2_best_choice( graph, paths[i].nodes, paths[i].count );
    if ( !dre2_contains_int( graph->starting_points, graph->starting_count, best ) )
      graph->starting_points[graph->starting_count++] = best;
  }

  // Allocate some memory.
  found_in = ( int ** )malloc( sizeof( int * ) * ( minimal_count - 2 ) );
  found_in_count = ( int * )malloc( sizeof( int ) * ( minimal_count - 2 ) );
  found_in_id = ( int * )malloc( sizeof( int ) * ( minimal_count - 2 ) );
  remaining = ( int * )malloc( sizeof( int ) * ( minimal_count - 2 ) );

  for ( i = 0; i < minimal_count - 2; i++ )
  {
    found_in_count[i] = 0;
    found_in[i] = ( int * )malloc( sizeof( int ) * path_count );
    found_in_id[i] = minimal_id[i + 1];
  }

  best = -1;
  if ( path_count == 1 && paths[0].count > 2 )
  {
    required_count = 0;
    required = ( int * )malloc( sizeof( int ) * graph->count );
    for ( i = 1; i < paths[0].count -1; i++ )
    {
      switch ( graph->v[paths[0].nodes[i]].c )
      {
        case DRE2_BOL:
        case DRE2_EOL:
        case DRE2_EOF:
          break;
        default:
          required[required_count++] = paths[0].nodes[i];
          break;
      }
    }
    if ( required_count > 0 )
      best = dre2_best_choice( graph, required, required_count );
    goto INITIAL_CLEANUP;
  } else if ( path_count > 1 )
  {
    required_count = 0;
    required = ( int * )malloc( sizeof( int ) * graph->count );
    for ( i = 1; i < minimal_count - 1; i++ )
    {
      all = true;
      id = minimal_id[i];
      switch ( graph->v[id].c )
      {
        case DRE2_BOL:
        case DRE2_EOL:
        case DRE2_EOF:
          break;
        default:
          for ( j = 0; j < path_count; j++ )
          {
            if ( !dre2_binsearch( paths[j].nodes, 0, paths[j].count - 1, id ) )
            {
              all = false;
            } else
            {
              found_in_count[i - 1]++;
              found_in[i - 1][found_in_count[i - 1] - 1] = j;
            }
          }
          if ( all )
            required[required_count++] = id;
          break;
      }
    }
    if ( required_count > 0 )
    {
      best = dre2_best_choice( graph, required, required_count );
      goto INITIAL_CLEANUP;
    }
  }

  best = dre2_first_or_last( graph, &cost );

  INITIAL_CLEANUP:
  if ( path_count > 1 && dre2_use_paths( graph, best, &cost ) )
    best = -1;

  if ( required != NULL )
  {
    free( required ); required = NULL;
  }

  for ( i = 0; i < path_count; i++ )
  {
    free( paths[i].nodes );
    paths[i].nodes = NULL;
  }
  for ( i = 0; i < minimal_count - 2; i++ )
  {
    free( found_in[i] ); found_in[i] = NULL;
  }
  free( found_in ); found_in = NULL;
  free( found_in_count ); found_in_count = NULL;
  free( found_in_id ); found_in_id = NULL;
  free( remaining ); remaining = NULL;
  free( paths ); paths = NULL;

  return best;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rcx, 0x20(%rsp)
movq %rdx, 0x28(%rsp)
movq %rsi, %rdx
movq %rdi, %r14
leaq 0x40(%rsp), %rdi
movq %r14, %rsi
callq 0x2300
movl $0x1, %r13d
leaq 0x4(%rsp), %r15
movl %r13d, (%r15)
movl $0x10, %edi
callq 0x22a0
movq %rax, %r12
leaq 0x10(%rsp), %rbp
movq %rax, (%rbp)
movslq 0x8(%r14), %rbx
shlq $0x2, %rbx
movq %rbx, %rdi
callq 0x22a0
movq %rax, (%r12)
movl %r13d, 0x8(%r12)
xorl %r12d, %r12d
movl %r12d, (%rax)
movq %rbx, %rdi
callq 0x22a0
movq %rax, 0x430(%r14)
movl %r12d, 0x438(%r14)
movq %r14, %rdi
xorl %esi, %esi
movq %r15, %rdx
movq %rbp, %rcx
callq 0x22c0
movl (%r15), %r12d
testl %r12d, %r12d
jle 0x3575
xorl %ebx, %ebx
movq 0x10(%rsp), %rax
movq %rbx, %rcx
shlq $0x4, %rcx
movq (%rax,%rcx), %rsi
movl 0x8(%rax,%rcx), %edx
movq %r14, %rdi
callq 0x21b0
movq 0x430(%r14), %rcx
movslq 0x438(%r14), %rdx
testq %rdx, %rdx
jle 0x355b
xorl %esi, %esi
cmpl %eax, (%rcx,%rsi,4)
je 0x3568
incq %rsi
cmpq %rsi, %rdx
jne 0x354e
leal 0x1(%rdx), %esi
movl %esi, 0x438(%r14)
movl %eax, (%rcx,%rdx,4)
incq %rbx
movslq 0x4(%rsp), %r12
cmpq %r12, %rbx
jl 0x351d
movq 0x20(%rsp), %rax
leal -0x2(%rax), %ebx
movslq %eax, %r13
leaq -0x10(,%r13,8), %rdi
callq 0x22a0
movq %rax, %r15
leaq -0x8(,%r13,4), %rdi
callq 0x22a0
movq %rax, 0x18(%rsp)
movl %ebx, %ebx
cmpl $0x3, %r13d
jl 0x35dc
movslq %r12d, %rbp
shlq $0x2, %rbp
leaq (,%rbx,4), %rdx
xorl %r13d, %r13d
movq 0x18(%rsp), %rdi
xorl %esi, %esi
callq 0x2180
movq %rbp, %rdi
callq 0x22a0
movq %rax, (%r15,%r13,8)
incq %r13
cmpq %r13, %rbx
jne 0x35c8
cmpl $0x1, %r12d
jne 0x366a
movq 0x10(%rsp), %rbp
movslq 0x8(%rbp), %r12
xorl %r13d, %r13d
cmpq $0x3, %r12
jl 0x37a2
movslq 0x8(%r14), %rdi
shlq $0x2, %rdi
callq 0x22a0
movq (%r14), %r9
movq (%rbp), %rcx
decq %r12
movl $0x1, %edx
movl $0x4003, %esi           # imm = 0x4003
movslq (%rcx,%rdx,4), %rdi
movq %rdi, %r8
shlq $0x6, %r8
movl (%r9,%r8), %r8d
addl $0x12, %r8d
cmpl $0xe, %r8d
ja 0x363c
btl %r8d, %esi
jb 0x3646
movslq %r13d, %r8
incl %r13d
movl %edi, (%rax,%r8,4)
incq %rdx
cmpq %r12, %rdx
jl 0x361d
testl %r13d, %r13d
movq %rax, 0x8(%rsp)
jle 0x3874
movq %r14, %rdi
movq %rax, %rsi
movl %r13d, %edx
jmp 0x379b
jle 0x37a2
movslq 0x8(%r14), %rdi
shlq $0x2, %rdi
callq 0x22a0
movq 0x20(%rsp), %rdx
cmpl $0x3, %edx
movq %rax, 0x8(%rsp)
jl 0x37ab
leal -0x1(%rdx), %r11d
movq (%r14), %rcx
movq 0x10(%rsp), %rsi
xorl %edx, %edx
movl $0x1, %edi
movq 0x28(%rsp), %r10
movq %r11, 0x30(%rsp)
movslq (%r10,%rdi,4), %r9
movq %r9, %r8
shlq $0x6, %r8
movl (%rcx,%r8), %r8d
addl $0x12, %r8d
cmpl $0xe, %r8d
ja 0x36d5
movl $0x4003, %ebp           # imm = 0x4003
btl %r8d, %ebp
jb 0x3785
testl %r12d, %r12d
jle 0x377c
movq %rdx, 0x38(%rsp)
movl $0x1, %r10d
xorl %r11d, %r11d
movq %r11, %rax
shlq $0x4, %rax
movl 0x8(%rsi,%rax), %r8d
testl %r8d, %r8d
jle 0x3731
movq (%rsi,%rax), %r13
decl %r8d
xorl %ebp, %ebp
leal (%r8,%rbp), %edx
movl %edx, %eax
shrl $0x1f, %eax
addl %edx, %eax
sarl %eax
movslq %eax, %rdx
cmpl %r9d, (%r13,%rdx,4)
jle 0x3727
leal -0x1(%rax), %r8d
cmpl %ebp, %eax
jg 0x3706
jmp 0x3731
jge 0x3741
leal 0x1(%rax), %ebp
cmpl %eax, %r8d
jg 0x3706
xorl %r10d, %r10d
incq %r11
movslq %r12d, %rax
cmpq %rax, %r11
jl 0x36ec
jmp 0x3763
movq 0x18(%rsp), %r8
movslq -0x4(%r8,%rdi,4), %rax
leal 0x1(%rax), %edx
movl %edx, -0x4(%r8,%rdi,4)
movq -0x8(%r15,%rdi,8), %rdx
movl %r11d, (%rdx,%rax,4)
movl 0x4(%rsp), %r12d
jmp 0x3734
testl %r10d, %r10d
movq 0x28(%rsp), %r10
movq 0x8(%rsp), %rax
movq 0x38(%rsp), %rdx
movq 0x30(%rsp), %r11
je 0x3785
movslq %edx, %r8
incl %edx
movl %r9d, (%rax,%r8,4)
incq %rdi
cmpq %r11, %rdi
jne 0x36ad
testl %edx, %edx
jle 0x37ab
movq %r14, %rdi
movq %rax, %rsi
callq 0x21b0
jmp 0x37b8
movq $0x0, 0x8(%rsp)
leaq 0x40(%rsp), %rsi
movq %r14, %rdi
callq 0x2360
movl %eax, %r13d
cmpl $0x2, 0x4(%rsp)
jl 0x37db
leaq 0x40(%rsp), %rdx
movq %r14, %rdi
movl %r13d, %esi
callq 0x2130
xorl %ecx, %ecx
negl %eax
sbbl %ecx, %ecx
orl %ecx, %r13d
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x37ea
callq 0x2070
cmpl $0x0, 0x4(%rsp)
jle 0x3823
xorl %r14d, %r14d
xorl %r12d, %r12d
movq 0x10(%rsp), %rax
movq (%rax,%r14), %rdi
callq 0x2070
movq 0x10(%rsp), %rax
movq $0x0, (%rax,%r14)
incq %r12
movslq 0x4(%rsp), %rax
addq $0x10, %r14
cmpq %rax, %r12
jl 0x37f7
cmpl $0x3, 0x20(%rsp)
jl 0x3846
xorl %r14d, %r14d
movq (%r15,%r14,8), %rdi
callq 0x2070
movq $0x0, (%r15,%r14,8)
incq %r14
cmpq %r14, %rbx
jne 0x382d
movq %r15, %rdi
callq 0x2070
movq 0x18(%rsp), %rdi
callq 0x2070
movq 0x10(%rsp), %rdi
callq 0x2070
movl %r13d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
jmp 0x37bb