// File: /workspace/github_c_makefile_star2_O1/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 $0x88, %rsp
movq %rcx, 0x18(%rsp)
movq %rdx, 0x28(%rsp)
movq %rsi, %rdx
movq %rdi, %r13
leaq 0x70(%rsp), %rdi
movq %r13, %rsi
callq 0x2310
movl $0x1, %r14d
leaq 0xc(%rsp), %rbx
movl %r14d, (%rbx)
movl $0x10, %edi
callq 0x22a0
movq %rax, %r15
leaq 0x10(%rsp), %r12
movq %rax, (%r12)
movslq 0x8(%r13), %rbp
shlq $0x2, %rbp
movq %rbp, %rdi
callq 0x22a0
movq %rax, (%r15)
movl %r14d, 0x8(%r15)
movq %r13, %r15
xorl %r14d, %r14d
movl %r14d, (%rax)
movq %rbp, %rdi
callq 0x22a0
movq %rax, 0x430(%r13)
movl %r14d, 0x438(%r13)
movq %r13, %rdi
xorl %esi, %esi
movq %rbx, %rdx
movq %r12, %rcx
callq 0x22c0
movl (%rbx), %ebp
testl %ebp, %ebp
jle 0x35d7
xorl %ebx, %ebx
movq 0x10(%rsp), %rax
movq %rbx, %rcx
shlq $0x4, %rcx
movq (%rax,%rcx), %rsi
movl 0x8(%rax,%rcx), %edx
movq %r15, %rdi
callq 0x21b0
movq 0x430(%r15), %rcx
movslq 0x438(%r15), %rdx
testq %rdx, %rdx
jle 0x35bd
xorl %esi, %esi
cmpl %eax, (%rcx,%rsi,4)
je 0x35ca
incq %rsi
cmpq %rsi, %rdx
jne 0x35b0
leal 0x1(%rdx), %esi
movl %esi, 0x438(%r15)
movl %eax, (%rcx,%rdx,4)
incq %rbx
movslq 0xc(%rsp), %rbp
cmpq %rbp, %rbx
jl 0x357f
movq 0x18(%rsp), %rax
leal -0x2(%rax), %ebx
movslq %eax, %r14
leaq -0x10(,%r14,8), %rdi
callq 0x22a0
movq %rax, %r13
leaq -0x8(,%r14,4), %rdi
callq 0x22a0
movq %rax, 0x20(%rsp)
movl %ebx, %r12d
cmpl $0x3, %r14d
jl 0x3640
movslq %ebp, %rbx
shlq $0x2, %rbx
leaq (,%r12,4), %rdx
xorl %r14d, %r14d
movq 0x20(%rsp), %rdi
xorl %esi, %esi
callq 0x2180
movq %rbx, %rdi
callq 0x22a0
movq %rax, (%r13,%r14,8)
incq %r14
cmpq %r14, %r12
jne 0x362b
movq %r12, 0x68(%rsp)
cmpl $0x1, %ebp
jne 0x36d2
movq 0x10(%rsp), %rbx
cmpl $0x3, 0x8(%rbx)
jl 0x36d2
movslq 0x8(%r15), %rdi
shlq $0x2, %rdi
callq 0x22a0
movq %rax, %r14
xorl %edx, %edx
cmpl $0x3, 0x8(%rbx)
movq 0x18(%rsp), %r12
jl 0x36bf
movq (%r15), %rax
movq (%rbx), %rcx
xorl %edx, %edx
movl $0x1, %esi
movl $0x4003, %edi           # imm = 0x4003
movslq (%rcx,%rsi,4), %r8
movq %r8, %r9
shlq $0x6, %r9
movl (%rax,%r9), %r9d
addl $0x12, %r9d
cmpl $0xe, %r9d
ja 0x36a7
btl %r9d, %edi
jb 0x36b0
movslq %edx, %r9
incl %edx
movl %r8d, (%r14,%r9,4)
incq %rsi
movslq 0x8(%rbx), %r8
decq %r8
cmpq %r8, %rsi
jl 0x3688
testl %edx, %edx
jle 0x38f3
movq %r15, %rdi
movq %r14, %rsi
jmp 0x381d
cmpl $0x2, %ebp
movq 0x18(%rsp), %r12
jl 0x3824
movslq 0x8(%r15), %rdi
shlq $0x2, %rdi
callq 0x22a0
xorl %edx, %edx
movq %rax, %r14
cmpl $0x3, %r12d
movq 0x28(%rsp), %rcx
jl 0x3813
leal -0x1(%r12), %esi
movq (%r15), %rdi
movq 0x10(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x38(%rsp)
xorl %edx, %edx
movl $0x1, %ebx
movl $0x4003, %r8d           # imm = 0x4003
movq %r15, 0x58(%rsp)
movq %r13, 0x30(%rsp)
movq %r14, 0x50(%rsp)
movq %rsi, 0x48(%rsp)
movq %rdi, 0x40(%rsp)
movslq (%rcx,%rbx,4), %rbp
movq %rbp, %rax
shlq $0x6, %rax
movl (%rdi,%rax), %eax
addl $0x12, %eax
cmpl $0xe, %eax
ja 0x375d
btl %eax, %r8d
jb 0x3807
cmpl $0x0, 0xc(%rsp)
jle 0x37fe
movq %rdx, 0x60(%rsp)
movl $0x1, %r15d
movq 0x38(%rsp), %r13
xorl %r12d, %r12d
movq -0x8(%r13), %rdi
movl (%r13), %edx
decl %edx
xorl %r14d, %r14d
xorl %esi, %esi
movl %ebp, %ecx
callq 0x22f0
testl %eax, %eax
je 0x37b7
movq 0x20(%rsp), %rdx
movslq -0x4(%rdx,%rbx,4), %rax
leal 0x1(%rax), %ecx
movl %ecx, -0x4(%rdx,%rbx,4)
movq 0x30(%rsp), %rcx
movq -0x8(%rcx,%rbx,8), %rcx
movl %r12d, (%rcx,%rax,4)
movl %r15d, %r14d
incq %r12
movslq 0xc(%rsp), %rax
addq $0x10, %r13
movl %r14d, %r15d
cmpq %rax, %r12
jl 0x377b
testl %r14d, %r14d
movq 0x18(%rsp), %r12
movq 0x58(%rsp), %r15
movq 0x30(%rsp), %r13
movq 0x28(%rsp), %rcx
movq 0x50(%rsp), %r14
movq 0x60(%rsp), %rdx
movq 0x48(%rsp), %rsi
movq 0x40(%rsp), %rdi
movl $0x4003, %r8d           # imm = 0x4003
je 0x3807
movslq %edx, %rax
incl %edx
movl %ebp, (%r14,%rax,4)
incq %rbx
cmpq %rsi, %rbx
jne 0x373d
testl %edx, %edx
jle 0x3827
movq %r15, %rdi
movq %r14, %rsi
callq 0x21b0
jmp 0x3834
xorl %r14d, %r14d
leaq 0x70(%rsp), %rsi
movq %r15, %rdi
callq 0x2370
movl %eax, %ebx
cmpl $0x2, 0xc(%rsp)
jl 0x3854
leaq 0x70(%rsp), %rdx
movq %r15, %rdi
movl %ebx, %esi
callq 0x2130
xorl %ecx, %ecx
negl %eax
sbbl %ecx, %ecx
orl %ecx, %ebx
testq %r14, %r14
je 0x3861
movq %r14, %rdi
callq 0x2070
cmpl $0x0, 0xc(%rsp)
jle 0x389a
xorl %r14d, %r14d
xorl %r15d, %r15d
movq 0x10(%rsp), %rax
movq (%rax,%r14), %rdi
callq 0x2070
movq 0x10(%rsp), %rax
movq $0x0, (%rax,%r14)
incq %r15
movslq 0xc(%rsp), %rax
addq $0x10, %r14
cmpq %rax, %r15
jl 0x386e
cmpl $0x3, %r12d
movq 0x68(%rsp), %r15
jl 0x38c3
xorl %r14d, %r14d
movq (%r13,%r14,8), %rdi
callq 0x2070
movq $0x0, (%r13,%r14,8)
incq %r14
cmpq %r14, %r15
jne 0x38a8
movq %r13, %rdi
callq 0x2070
movq 0x20(%rsp), %rdi
callq 0x2070
movq 0x10(%rsp), %rdi
callq 0x2070
movl %ebx, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x3836