// File: /workspace/github_c_makefile_star2_O1/icebert[P]pblat/lib/udc.c
// Function: udcRead
bits64 udcRead(struct udcFile *file, void *buf, bits64 size)
/* Read a block from file.  Return amount actually read. */
{
file->ios.udc.numReads++;
// if not caching, just fetch the data
if (!udcCacheEnabled() && !sameString(file->protocol, "transparent"))
    {
    int actualSize = file->prot->fetchData(file->url, file->offset, size, buf, file);
    file->offset += actualSize;
    file->ios.udc.bytesRead += actualSize;
    return actualSize;
    }
file->ios.udc.bytesRead += size;

/* Figure out region of file we're going to read, and clip it against file size. */
bits64 start = file->offset;
if (start > file->size)
    return 0;
bits64 end = start + size;
if (end > file->size)
    end = file->size;
size = end - start;
char *cbuf = buf;

/* use read-ahead buffer if present */
bits64 bytesRead = 0;

bits64 raStart;
bits64 raEnd;
while(TRUE)
    {
    if (file->sparseReadAhead)
	{
	raStart = file->sparseRAOffset;
	raEnd = raStart+READAHEADBUFSIZE;
	if (start >= raStart && start < raEnd)
	    {
	    // copy bytes out of rabuf
	    bits64 endInBuf = min(raEnd, end);
	    bits64 sizeInBuf = endInBuf - start;
	    memcpy(cbuf, file->sparseReadAheadBuf + (start-raStart), sizeInBuf);
	    cbuf += sizeInBuf;
	    bytesRead += sizeInBuf;
	    start = raEnd;
	    size -= sizeInBuf;
	    file->offset += sizeInBuf;
	    if (size == 0)
		break;
	    }
	file->sparseReadAhead = FALSE;
	ourMustLseek(&file->ios.sparse,file->fdSparse, start, SEEK_SET);
	}

    bits64 saveEnd = end;
    if (size < READAHEADBUFSIZE)
	{
	file->sparseReadAhead = TRUE;
	if (!file->sparseReadAheadBuf)
	    file->sparseReadAheadBuf = needMem(READAHEADBUFSIZE);
	file->sparseRAOffset = start;
	size = READAHEADBUFSIZE;
	end = start + size;
	if (end > file->size)
	    {
	    end = file->size;
	    size = end - start;
	    }
	}


    /* If we're outside of the window of file we already know is good, then have to
     * consult cache on disk, and maybe even fetch data remotely! */
    if (start < file->startData || end > file->endData)
	{

	if (!udcCachePreload(file, start, size))
	    {
	    verbose(4, "udcCachePreload failed");
	    bytesRead = 0;
	    break;
	    }

	/* Currently only need fseek here.  Would be safer, but possibly
	 * slower to move fseek so it is always executed in front of read, in
	 * case other code is moving around file pointer. */

	ourMustLseek(&file->ios.sparse,file->fdSparse, start, SEEK_SET);
	}

    if (file->sparseReadAhead)
	{
	ourMustRead(&file->ios.sparse,file->fdSparse, file->sparseReadAheadBuf, size);
	end = saveEnd;
	size = end - start;
	}
    else
	{
	ourMustRead(&file->ios.sparse,file->fdSparse, cbuf, size);
	file->offset += size;
	bytesRead += size;
	break;
	}
    }

return bytesRead;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
incq 0x110(%rdi)
movq %rdx, %r15
movq %rsi, %r14
cmpq $0x0, 0x80fc6(%rip)     # 0xc0c98
je 0x3fe9d
movq %r14, 0x10(%rsp)
addq %r15, 0x118(%rbx)
movq 0x28(%rbx), %rax
movq 0x30(%rbx), %r14
cmpq %rax, %r14
jbe 0x3fcff
movq $0x0, 0x8(%rsp)
jmp 0x3fee0
addq %r14, %r15
cmpq %rax, %r15
cmovaeq %rax, %r15
movq %r15, %r13
subq %r14, %r13
movq $0x0, 0x8(%rsp)
cmpl $0x0, 0x5c(%rbx)
je 0x3fd9a
movq 0x68(%rbx), %rax
leaq 0x1000(%rax), %rbp
movq %r14, %rsi
subq %rax, %rsi
setb %al
cmpq %rbp, %r14
setae %cl
orb %al, %cl
jne 0x3fd7c
cmpq %r15, %rbp
movq %r15, %r12
cmovbq %rbp, %r12
subq %r14, %r12
addq 0x60(%rbx), %rsi
movq 0x10(%rsp), %r14
movq %r14, %rdi
movq %r12, %rdx
callq 0x6570
addq %r12, 0x8(%rsp)
addq %r12, 0x30(%rbx)
subq %r12, %r13
je 0x3fee0
addq %r12, %r14
movq %r14, 0x10(%rsp)
movq %rbp, %r14
jmp 0x3fd7f
movq %r14, %rbp
movl $0x0, 0x5c(%rbx)
movl 0x58(%rbx), %edi
incq 0xe0(%rbx)
movq %rbp, %rsi
xorl %edx, %edx
callq 0x23232
movq %r15, %r12
cmpq $0xfff, %r13            # imm = 0xFFF
ja 0x3fde4
movl $0x1, 0x5c(%rbx)
cmpq $0x0, 0x60(%rbx)
jne 0x3fdc2
movl $0x1000, %edi           # imm = 0x1000
callq 0x2dc95
movq %rax, 0x60(%rbx)
movq %r14, 0x68(%rbx)
movl $0x1000, %ecx           # imm = 0x1000
leaq (%r14,%rcx), %rax
movq 0x28(%rbx), %r12
movq %r12, %r13
subq %r14, %r13
cmpq %r12, %rax
cmovbq %rax, %r12
cmovbeq %rcx, %r13
cmpq 0x78(%rbx), %r14
jb 0x3fdf3
cmpq 0x80(%rbx), %r12
jbe 0x3fe19
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x41397
testl %eax, %eax
je 0x3fe49
movl 0x58(%rbx), %edi
incq 0xe0(%rbx)
movq %r14, %rsi
xorl %edx, %edx
callq 0x23232
cmpl $0x0, 0x5c(%rbx)
je 0x3fe67
movl 0x58(%rbx), %edi
movq 0x60(%rbx), %rsi
incq 0xe8(%rbx)
addq %r13, 0xf0(%rbx)
movq %r13, %rdx
callq 0x23186
movq %r15, %r13
subq %r14, %r13
movb $0x1, %al
movq %r15, %r12
jmp 0x3fe90
movq $0x0, 0x8(%rsp)
movl $0x4, %edi
leaq 0x5502f(%rip), %rsi     # 0x94e8d
xorl %eax, %eax
callq 0x4246e
jmp 0x3fe8e
movl 0x58(%rbx), %edi
incq 0xe8(%rbx)
addq %r13, 0xf0(%rbx)
movq 0x10(%rsp), %rsi
movq %r13, %rdx
callq 0x23186
addq %r13, 0x30(%rbx)
addq %r13, 0x8(%rsp)
xorl %eax, %eax
movq %r12, %r15
testb %al, %al
jne 0x3fd18
jmp 0x3fee0
movq 0x10(%rbx), %rdi
leaq 0x54ea7(%rip), %rsi     # 0x94d4f
callq 0x6a50
testl %eax, %eax
je 0x3fcd8
movq 0x8(%rbx), %rdi
movq 0x18(%rbx), %rax
movq 0x30(%rbx), %rsi
movl %r15d, %edx
movq %r14, %rcx
movq %rbx, %r8
callq *0x8(%rax)
movslq %eax, %rcx
addq %rcx, 0x30(%rbx)
movq %rcx, 0x8(%rsp)
addq %rcx, 0x118(%rbx)
movq 0x8(%rsp), %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq