// File: /workspace/github_c_makefile_star2_O1/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 $0x6b8, %rsp            # imm = 0x6B8
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %r12
leaq 0x5cb88(%rip), %rsi     # 0x8e406
callq 0x6800
movq %rax, %r13
testq %rax, %rax
jne 0x31a07
leaq 0x5e903(%rip), %rdx     # 0x90199
leaq 0xb0(%rsp), %r13
movq %r13, %rdi
movl $0x200, %esi            # imm = 0x200
movq %r12, %rcx
xorl %eax, %eax
callq 0x23c18
movq %r13, %rdi
movl $0x2f, %esi
callq 0x67c0
testq %rax, %rax
movq %r14, 0x18(%rsp)
movq %r15, 0x10(%rsp)
movq %r12, 0x8(%rsp)
je 0x318e4
movq %rax, %r13
incq %r13
movb $0x0, (%rax)
leaq 0xb0(%rsp), %rbp
jmp 0x318eb
leaq 0x66a75(%rip), %rbp     # 0x98360
leaq 0x2b0(%rsp), %rbx
movb $0x0, (%rbx)
movq %r13, %rdi
callq 0x62d0
leaq (%rax,%r13), %r12
addq $-0x5, %r12
callq 0x6790
movq %rax, %r14
leaq 0x4b0(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x6680
movq (%r14), %rax
movsbq (%r12), %rcx
testb $0x8, 0x1(%rax,%rcx,2)
jne 0x3194b
movq %rbx, %rdi
movl $0x200, %esi            # imm = 0x200
leaq 0x611f2(%rip), %rdx     # 0x92b31
movq %r15, %rcx
xorl %eax, %eax
callq 0x23c18
jmp 0x31967
movq %rbx, %rdi
movl $0x200, %esi            # imm = 0x200
leaq 0x611d1(%rip), %rdx     # 0x92b2b
movq %r15, %r8
xorl %eax, %eax
callq 0x23c18
decq %r12
movq %r15, %rdi
movl $0x200, %esi            # imm = 0x200
leaq 0x611c3(%rip), %rdx     # 0x92b39
movq %rbp, %rcx
movq %rbx, %r8
xorl %eax, %eax
callq 0x23c18
movq %r15, %rdi
leaq 0x20(%rsp), %rsi
callq 0x6d00
testl %eax, %eax
js 0x319d0
movq %r15, %rdi
movl $0x200, %esi            # imm = 0x200
leaq 0x61193(%rip), %rdx     # 0x92b36
movq %rbp, %rcx
movq %rbx, %r8
movq %r13, %r9
xorl %eax, %eax
callq 0x23c18
movq %r15, %rdi
leaq 0x5ca49(%rip), %rsi     # 0x8e406
callq 0x6800
testq %rax, %rax
je 0x31916
movq %rax, %r13
jmp 0x319d3
xorl %r13d, %r13d
testq %r13, %r13
movq 0x18(%rsp), %r14
movq 0x10(%rsp), %r15
movq 0x8(%rsp), %r12
jne 0x31a07
callq 0x6060
movl (%rax), %edi
callq 0x6230
leaq 0x61145(%rip), %rdi     # 0x92b3f
movq %r12, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x26b6f
xorl %eax, %eax
callq 0x25b5c
leaq 0x4(%rsp), %rbx
movl $0x4, %edx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x22e78
leaq 0x20(%rsp), %rsi
movl $0x4, %edx
movq %r13, %rdi
callq 0x22e78
movl (%rbx), %edi
cmpl $0x6be93d3a, %edi       # imm = 0x6BE93D3A
je 0x31a70
callq 0x236d8
movl %eax, 0x4(%rsp)
movl 0x20(%rsp), %edi
callq 0x236d8
movl %eax, 0x20(%rsp)
cmpl $0x6be93d3a, 0x4(%rsp)  # imm = 0x6BE93D3A
je 0x31a70
leaq 0x610f3(%rip), %rdi     # 0x92b59
movq %r12, %rsi
xorl %eax, %eax
callq 0x26b6f
movl 0x20(%rsp), %eax
movl %eax, (%r15)
movq %r13, (%r14)
addq $0x6b8, %rsp            # imm = 0x6B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq