// File: /workspace/github_c_makefile_star2_O2/icebert[P]pblat/lib/nib.c
// Function: nibOpenVerify
void nibOpenVerify(char *fileName, FILE **retFile, int *retSize)
/* Open file and verify it's in good nibble format. */
{
bits32 size;
bits32 sig;
FILE *f = fopen(fileName, "rb");
char buffer[512];
char buffer2[512];
char buffer3[512];

if (f == NULL)
    {
    /* see if nib is down a few directories ala faSplit -outDirDepth */
    char *ptr = NULL;
    char *dir, *file;
    struct stat statBuf;

    /* divide fileName into file and directory components */
    safef(buffer, sizeof(buffer), "%s", fileName);
    if ((ptr = strrchr(buffer, '/')) != NULL)
	{
	*ptr++ = 0;
	dir = buffer;
	file = ptr;
	}
    else
	{
	dir = "";
	file = buffer;
	}
    
    buffer3[0] = 0;
    /* start at the end of the fileName (minus .nib) */
    for(ptr = &file[strlen(file) - 5]; ; )
	{
	strcpy(buffer2, buffer3);
	if (isdigit(*ptr))
	    {
	    /* if we have a digit in the fileName, see if there is a directory with this name */
	    safef(buffer3, sizeof(buffer3), "%c/%s",*ptr,buffer2);
	    ptr--;
	    }
	else
	    /* we've run out of digits in the fileName, just add 0's */
	    safef(buffer3, sizeof(buffer3), "0/%s",buffer2);

	/* check to see if this directory exists */
	safef(buffer2, sizeof(buffer2), "%s/%s", dir, buffer3);
	if (stat(buffer2, &statBuf) < 0)
	    break;

	/* directory exists, see if our file is down there */
	safef(buffer2, sizeof(buffer2), "%s/%s/%s", dir, buffer3, file);
	if  ((f = fopen(buffer2, "rb")) != NULL)
	    break;
	}
    if (f == NULL)
	errAbort("Can't open %s to read: %s", fileName,  strerror(errno));
    }
dnaUtilOpen();
mustReadOne(f, sig);
mustReadOne(f, size);
if (sig != nibSig)
    {
    sig = byteSwap32(sig);
    size = byteSwap32(size);
    if (sig != nibSig)
	errAbort("%s is not a good .nib file.",  fileName);
    }
*retSize = size;
*retFile = f;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x6c8, %rsp            # imm = 0x6C8
movq %rdx, %r13
movq %rsi, %r12
movq %rdi, %rbp
leaq 0x50f18(%rip), %rsi     # 0x7d3e6
callq 0x6800
movq %rax, %r15
testq %rax, %rax
jne 0x2c65d
movq %r13, 0x18(%rsp)
leaq 0x52c9e(%rip), %rdx     # 0x7f189
leaq 0xc0(%rsp), %rbx
movq %rbx, %rdi
movl $0x200, %esi            # imm = 0x200
movq %rbp, %rcx
xorl %eax, %eax
callq 0x1fab2
pushq $0x2f
popq %rsi
movq %rbx, %rdi
callq 0x67c0
testq %rax, %rax
movq %r12, 0x20(%rsp)
movq %rbp, 0x10(%rsp)
je 0x2c532
movq %rax, %rbx
incq %rbx
movb $0x0, (%rax)
leaq 0xc0(%rsp), %r13
jmp 0x2c539
leaq 0x5ae44(%rip), %r13     # 0x8737d
leaq 0x2c0(%rsp), %rbp
movb $0x0, (%rbp)
movq %rbx, %rdi
callq 0x62d0
movq %rbx, 0x28(%rsp)
leaq (%rax,%rbx), %r12
addq $-0x5, %r12
callq 0x6790
movq %rax, %rbx
leaq 0x4c0(%rsp), %r14
movq %r14, %rdi
movq %rbp, %rsi
callq 0x6680
movq (%rbx), %rax
movsbq (%r12), %rcx
testb $0x8, 0x1(%rax,%rcx,2)
jne 0x2c59f
movq %rbp, %rdi
movl $0x200, %esi            # imm = 0x200
leaq 0x5556f(%rip), %rdx     # 0x81b02
movq %r14, %rcx
xorl %eax, %eax
callq 0x1fab2
jmp 0x2c5bb
movq %rbp, %rdi
movl $0x200, %esi            # imm = 0x200
leaq 0x5554e(%rip), %rdx     # 0x81afc
movq %r14, %r8
xorl %eax, %eax
callq 0x1fab2
decq %r12
movq %r14, %rdi
movl $0x200, %esi            # imm = 0x200
leaq 0x55540(%rip), %rdx     # 0x81b0a
movq %r13, %rcx
movq %rbp, %r8
xorl %eax, %eax
callq 0x1fab2
movq %r14, %rdi
leaq 0x30(%rsp), %rsi
callq 0x6d00
testl %eax, %eax
js 0x2c62b
movq %r14, %rdi
movl $0x200, %esi            # imm = 0x200
leaq 0x55510(%rip), %rdx     # 0x81b07
movq %r13, %rcx
movq %rbp, %r8
movq 0x28(%rsp), %r9
xorl %eax, %eax
callq 0x1fab2
movq %r14, %rdi
leaq 0x50dd3(%rip), %rsi     # 0x7d3e6
callq 0x6800
testq %rax, %rax
je 0x2c56a
movq %rax, %r15
movq 0x10(%rsp), %rbp
jmp 0x2c653
callq 0x6060
movl (%rax), %edi
callq 0x6230
leaq 0x554d2(%rip), %rdi     # 0x81b10
xorl %r15d, %r15d
movq 0x10(%rsp), %rbp
movq %rbp, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x223fb
movq 0x20(%rsp), %r12
movq 0x18(%rsp), %r13
xorl %eax, %eax
callq 0x21578
leaq 0xc(%rsp), %rbx
pushq $0x4
popq %r14
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x1ed97
leaq 0x30(%rsp), %rsi
movq %r15, %rdi
movq %r14, %rdx
callq 0x1ed97
movl (%rbx), %edi
cmpl $0x6be93d3a, %edi       # imm = 0x6BE93D3A
je 0x2c6c6
callq 0x1f5a0
movl %eax, 0xc(%rsp)
movl 0x30(%rsp), %edi
callq 0x1f5a0
movl %eax, 0x30(%rsp)
cmpl $0x6be93d3a, 0xc(%rsp)  # imm = 0x6BE93D3A
je 0x2c6c6
leaq 0x5546e(%rip), %rdi     # 0x81b2a
movq %rbp, %rsi
xorl %eax, %eax
callq 0x223fb
movl 0x30(%rsp), %eax
movl %eax, (%r13)
movq %r15, (%r12)
addq $0x6c8, %rsp            # imm = 0x6C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq