// File: /workspace/github_c_makefile_star2_O0/hilbix[P]unbuffered/./tino/proc.h
// Function: tino_fd_move
static int
tino_fd_move(int *fds, int cnt)
{
  int	open[TINO_OPEN_MAX];
  int	i, max;

  cDP(("(%p,%d)", fds, cnt));
  /* Count the references to an fd.
   */
  TINO_FATAL_IF(cnt>TINO_OPEN_MAX);
  memset(open, 0, sizeof open);
  max	= 0;
  for (i=cnt; --i>=0; )
    {
      cDP(("() fd%d=%d", i, fds[i]));
      if (fds[i]>=0)
        {
          TINO_FATAL_IF(fds[i]>=TINO_OPEN_MAX);
          open[fds[i]]++;
        }
      if (fds[i]>max)
        max	= fds[i];
    }

  /* Now loop until all conflicts are solved
   */
  for (;;)
    {
      int	ok, conflict;

      /* DUP all the FDs which do not overwrite sources
       */
      ok	= 0;
      conflict	= -1;
      for (i=cnt; --i>=0; )
        if (fds[i]>=0 && fds[i]!=i)
          {
            if (open[i])
              conflict	= i;
            else
              {
                cDP(("() dup %d to %d", fds[i], i));
                dup2(fds[i], i);
                open[i]++;
                if (!--open[fds[i]])
                  {
                    ok	= 1;
                    cDP(("() close %d", fds[i]));
                    close(fds[i]);
                  }
                fds[i]	= i;
              }
          }
      /* we have closed something,
       * so reloop.
       */
      if (ok)
        continue;
      if (conflict<0)
        break;

      /* We have a conflict position
       * dup it anywhere else.
       * This *must* solve the conflict,
       * as we already have moved any FDs we can move.
       * So the dupped fd can be
       * neither a source nor a destination fd.
       */
      ok	= dup(conflict);
      cDP(("() dup %d to %d", conflict, ok));
      if (ok<0)
        TINO_FATAL(("cannot dup conflicting fd %d", conflict));
      TINO_FATAL_IF(ok>=TINO_OPEN_MAX);
      TINO_FATAL_IF(open[ok]);
      for (i=cnt; --i>=0; )
        if (fds[i]==conflict)
          {
            fds[i]	= ok;
            open[ok]++;
            open[conflict]--;
          }
      TINO_FATAL_IF(open[conflict]);
      /* close(conflict);
       * not needed as it is a destination of dup2() in the next loop.
       */
    }
  return max;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1020, %rsp           # imm = 0x1020
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
jmp 0xa324
jmp 0xa326
cmpl $0x400, -0xc(%rbp)      # imm = 0x400
jle 0xa360
jmp 0xa331
leaq 0x3932(%rip), %rdi      # 0xdc6a
movl $0x72, %esi
leaq 0x3934(%rip), %rdx      # 0xdc78
callq 0x3ff0
leaq 0x3935(%rip), %rdi      # 0xdc85
leaq 0x393c(%rip), %rsi      # 0xdc93
movb $0x0, %al
callq 0x35e0
jmp 0xa360
jmp 0xa362
leaq -0x1010(%rbp), %rdi
xorl %esi, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x2150
movl $0x0, -0x1018(%rbp)
movl -0xc(%rbp), %eax
movl %eax, -0x1014(%rbp)
movl -0x1014(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x1014(%rbp)
cmpl $0x0, %eax
jl 0xa44d
jmp 0xa3a2
jmp 0xa3a4
movq -0x8(%rbp), %rax
movslq -0x1014(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jl 0xa41e
jmp 0xa3b7
movq -0x8(%rbp), %rax
movslq -0x1014(%rbp), %rcx
cmpl $0x400, (%rax,%rcx,4)   # imm = 0x400
jl 0xa3fc
jmp 0xa3cd
leaq 0x3896(%rip), %rdi      # 0xdc6a
movl $0x7a, %esi
leaq 0x3898(%rip), %rdx      # 0xdc78
callq 0x3ff0
leaq 0x3899(%rip), %rdi      # 0xdc85
leaq 0x38b2(%rip), %rsi      # 0xdca5
movb $0x0, %al
callq 0x35e0
jmp 0xa3fc
jmp 0xa3fe
movq -0x8(%rbp), %rax
movslq -0x1014(%rbp), %rcx
movslq (%rax,%rcx,4), %rax
movl -0x1010(%rbp,%rax,4), %ecx
addl $0x1, %ecx
movl %ecx, -0x1010(%rbp,%rax,4)
movq -0x8(%rbp), %rax
movslq -0x1014(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cmpl -0x1018(%rbp), %eax
jle 0xa448
movq -0x8(%rbp), %rax
movslq -0x1014(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x1018(%rbp)
jmp 0xa388
jmp 0xa44f
movl $0x0, -0x101c(%rbp)
movl $0xffffffff, -0x1020(%rbp) # imm = 0xFFFFFFFF
movl -0xc(%rbp), %eax
movl %eax, -0x1014(%rbp)
movl -0x1014(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x1014(%rbp)
cmpl $0x0, %eax
jl 0xa569
movq -0x8(%rbp), %rax
movslq -0x1014(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jl 0xa564
movq -0x8(%rbp), %rax
movslq -0x1014(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cmpl -0x1014(%rbp), %eax
je 0xa564
movslq -0x1014(%rbp), %rax
cmpl $0x0, -0x1010(%rbp,%rax,4)
je 0xa4d5
movl -0x1014(%rbp), %eax
movl %eax, -0x1020(%rbp)
jmp 0xa562
jmp 0xa4d7
jmp 0xa4d9
movq -0x8(%rbp), %rax
movslq -0x1014(%rbp), %rcx
movl (%rax,%rcx,4), %edi
movl -0x1014(%rbp), %esi
callq 0x20f0
movslq -0x1014(%rbp), %rax
movl -0x1010(%rbp,%rax,4), %ecx
addl $0x1, %ecx
movl %ecx, -0x1010(%rbp,%rax,4)
movq -0x8(%rbp), %rax
movslq -0x1014(%rbp), %rcx
movslq (%rax,%rcx,4), %rcx
movl -0x1010(%rbp,%rcx,4), %eax
addl $-0x1, %eax
movl %eax, -0x1010(%rbp,%rcx,4)
cmpl $0x0, %eax
jne 0xa54e
movl $0x1, -0x101c(%rbp)
jmp 0xa53b
movq -0x8(%rbp), %rax
movslq -0x1014(%rbp), %rcx
movl (%rax,%rcx,4), %edi
callq 0x2170
movl -0x1014(%rbp), %edx
movq -0x8(%rbp), %rax
movslq -0x1014(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
jmp 0xa564
jmp 0xa46c
cmpl $0x0, -0x101c(%rbp)
je 0xa577
jmp 0xa44f
cmpl $0x0, -0x1020(%rbp)
jge 0xa585
jmp 0xa71c
movl -0x1020(%rbp), %edi
callq 0x2130
movl %eax, -0x101c(%rbp)
jmp 0xa598
cmpl $0x0, -0x101c(%rbp)
jge 0xa5d1
jmp 0xa5a3
leaq 0x36c0(%rip), %rdi      # 0xdc6a
movl $0xb0, %esi
leaq 0x36c2(%rip), %rdx      # 0xdc78
callq 0x3ff0
movl -0x1020(%rbp), %esi
leaq 0x36f3(%rip), %rdi      # 0xdcbb
movb $0x0, %al
callq 0x35e0
jmp 0xa5d1
jmp 0xa5d3
cmpl $0x400, -0x101c(%rbp)   # imm = 0x400
jl 0xa610
jmp 0xa5e1
leaq 0x3682(%rip), %rdi      # 0xdc6a
movl $0xb1, %esi
leaq 0x3684(%rip), %rdx      # 0xdc78
callq 0x3ff0
leaq 0x3685(%rip), %rdi      # 0xdc85
leaq 0x36d1(%rip), %rsi      # 0xdcd8
movb $0x0, %al
callq 0x35e0
jmp 0xa610
jmp 0xa612
jmp 0xa614
movslq -0x101c(%rbp), %rax
cmpl $0x0, -0x1010(%rbp,%rax,4)
je 0xa656
jmp 0xa627
leaq 0x363c(%rip), %rdi      # 0xdc6a
movl $0xb2, %esi
leaq 0x363e(%rip), %rdx      # 0xdc78
callq 0x3ff0
leaq 0x363f(%rip), %rdi      # 0xdc85
leaq 0x369d(%rip), %rsi      # 0xdcea
movb $0x0, %al
callq 0x35e0
jmp 0xa656
jmp 0xa658
movl -0xc(%rbp), %eax
movl %eax, -0x1014(%rbp)
movl -0x1014(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x1014(%rbp)
cmpl $0x0, %eax
jl 0xa6d1
movq -0x8(%rbp), %rax
movslq -0x1014(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cmpl -0x1020(%rbp), %eax
jne 0xa6cf
movl -0x101c(%rbp), %edx
movq -0x8(%rbp), %rax
movslq -0x1014(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movslq -0x101c(%rbp), %rax
movl -0x1010(%rbp,%rax,4), %ecx
addl $0x1, %ecx
movl %ecx, -0x1010(%rbp,%rax,4)
movslq -0x1020(%rbp), %rax
movl -0x1010(%rbp,%rax,4), %ecx
addl $-0x1, %ecx
movl %ecx, -0x1010(%rbp,%rax,4)
jmp 0xa661
jmp 0xa6d3
movslq -0x1020(%rbp), %rax
cmpl $0x0, -0x1010(%rbp,%rax,4)
je 0xa715
jmp 0xa6e6
leaq 0x357d(%rip), %rdi      # 0xdc6a
movl $0xba, %esi
leaq 0x357f(%rip), %rdx      # 0xdc78
callq 0x3ff0
leaq 0x3580(%rip), %rdi      # 0xdc85
leaq 0x35e7(%rip), %rsi      # 0xdcf3
movb $0x0, %al
callq 0x35e0
jmp 0xa715
jmp 0xa717
jmp 0xa44f
movl -0x1018(%rbp), %eax
addq $0x1020, %rsp           # imm = 0x1020
popq %rbp
retq
nopl (%rax,%rax)