// File: /workspace/github_c_makefile_star2_O3/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, %r12
movq %rsi, %r14
movq %rdi, %r13
leaq 0x5c8f8(%rip), %rsi     # 0x8e446
callq 0x6800
movq %rax, %r15
testq %rax, %rax
jne 0x31cdf
leaq 0x5e6f3(%rip), %rdx     # 0x90259
leaq 0xc0(%rsp), %rbx
movq %rbx, %rdi
movl $0x200, %esi            # imm = 0x200
movq %r13, 0x10(%rsp)
movq %r13, %rcx
xorl %eax, %eax
callq 0x23f33
movq %rbx, %rdi
movl $0x2f, %esi
callq 0x67c0
testq %rax, %rax
movq %r14, 0x20(%rsp)
movq %r12, 0x18(%rsp)
je 0x31bb4
movq %rax, %rbx
incq %rbx
movb $0x0, (%rax)
leaq 0xc0(%rsp), %r13
jmp 0x31bbb
leaq 0x66899(%rip), %r13     # 0x98454
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 0x31c21
movq %rbp, %rdi
movl $0x200, %esi            # imm = 0x200
leaq 0x60fed(%rip), %rdx     # 0x92c02
movq %r14, %rcx
xorl %eax, %eax
callq 0x23f33
jmp 0x31c3d
movq %rbp, %rdi
movl $0x200, %esi            # imm = 0x200
leaq 0x60fcc(%rip), %rdx     # 0x92bfc
movq %r14, %r8
xorl %eax, %eax
callq 0x23f33
decq %r12
movq %r14, %rdi
movl $0x200, %esi            # imm = 0x200
leaq 0x60fbe(%rip), %rdx     # 0x92c0a
movq %r13, %rcx
movq %rbp, %r8
xorl %eax, %eax
callq 0x23f33
movq %r14, %rdi
leaq 0x30(%rsp), %rsi
callq 0x6d00
testl %eax, %eax
js 0x31cad
movq %r14, %rdi
movl $0x200, %esi            # imm = 0x200
leaq 0x60f8e(%rip), %rdx     # 0x92c07
movq %r13, %rcx
movq %rbp, %r8
movq 0x28(%rsp), %r9
xorl %eax, %eax
callq 0x23f33
movq %r14, %rdi
leaq 0x5c7b1(%rip), %rsi     # 0x8e446
callq 0x6800
testq %rax, %rax
je 0x31bec
movq %rax, %r15
movq 0x10(%rsp), %r13
jmp 0x31cd5
callq 0x6060
movl (%rax), %edi
callq 0x6230
leaq 0x60f50(%rip), %rdi     # 0x92c10
xorl %r15d, %r15d
movq 0x10(%rsp), %r13
movq %r13, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x26e94
movq 0x20(%rsp), %r14
movq 0x18(%rsp), %r12
xorl %eax, %eax
callq 0x25e47
leaq 0xc(%rsp), %rbx
movl $0x4, %edx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x23192
leaq 0x30(%rsp), %rsi
movl $0x4, %edx
movq %r15, %rdi
callq 0x23192
movl (%rbx), %edi
cmpl $0x6be93d3a, %edi       # imm = 0x6BE93D3A
je 0x31d48
callq 0x239ea
movl %eax, 0xc(%rsp)
movl 0x30(%rsp), %edi
callq 0x239ea
movl %eax, 0x30(%rsp)
cmpl $0x6be93d3a, 0xc(%rsp)  # imm = 0x6BE93D3A
je 0x31d48
leaq 0x60eec(%rip), %rdi     # 0x92c2a
movq %r13, %rsi
xorl %eax, %eax
callq 0x26e94
movl 0x30(%rsp), %eax
movl %eax, (%r12)
movq %r15, (%r14)
addq $0x6c8, %rsp            # imm = 0x6C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq