// File: /workspace/github_c_makefile_star2_O2/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, %r13
movq %rsi, %r14
cmpq $0x0, 0x7adfb(%rip)     # 0xb4cb8
je 0x3a023
movq %r14, 0x8(%rsp)
addq %r13, 0x118(%rbx)
movq 0x28(%rbx), %rax
movq 0x30(%rbx), %r12
cmpq %rax, %r12
jbe 0x39ee4
xorl %r14d, %r14d
jmp 0x3a09a
addq %r12, %r13
cmpq %rax, %r13
cmovaeq %rax, %r13
leaq 0xe0(%rbx), %r14
movq $0x0, (%rsp)
movq %r14, 0x10(%rsp)
movq %r13, %r15
subq %r12, %r15
cmpl $0x0, 0x5c(%rbx)
je 0x39f87
movq 0x68(%rbx), %rax
leaq 0x1000(%rax), %rbp
movq %r12, %rsi
subq %rax, %rsi
setb %al
cmpq %rbp, %r12
setae %cl
orb %al, %cl
jne 0x39f70
cmpq %r13, %rbp
movq %r13, %r14
cmovbq %rbp, %r14
subq %r12, %r14
addq 0x60(%rbx), %rsi
movq 0x8(%rsp), %r12
movq %r12, %rdi
movq %r14, %rdx
callq 0x6570
addq %r14, (%rsp)
addq %r14, 0x30(%rbx)
subq %r14, %r15
je 0x3a096
addq %r14, %r12
movq %r12, 0x8(%rsp)
movq %rbp, %r12
movq 0x10(%rsp), %r14
jmp 0x39f73
movq %r12, %rbp
andl $0x0, 0x5c(%rbx)
movl 0x58(%rbx), %esi
movq %r14, %rdi
movq %rbp, %rdx
xorl %ecx, %ecx
callq 0x3b4c4
movq %r13, %rax
cmpq $0xfff, %r15            # imm = 0xFFF
ja 0x39fd1
movl $0x1, 0x5c(%rbx)
cmpq $0x0, 0x60(%rbx)
jne 0x39faf
movl $0x1000, %edi           # imm = 0x1000
callq 0x28bb0
movq %rax, 0x60(%rbx)
movq %r12, 0x68(%rbx)
movl $0x1000, %edx           # imm = 0x1000
leaq (%r12,%rdx), %rcx
movq 0x28(%rbx), %rax
movq %rax, %r15
subq %r12, %r15
cmpq %rax, %rcx
cmovbq %rcx, %rax
cmovbeq %rdx, %r15
cmpq 0x78(%rbx), %r12
jb 0x39fe0
cmpq 0x80(%rbx), %rax
jbe 0x3a006
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x3b4d3
testl %eax, %eax
je 0x3a080
movl 0x58(%rbx), %esi
movq %r14, %rdi
movq %r12, %rdx
xorl %ecx, %ecx
callq 0x3b4c4
cmpl $0x0, 0x5c(%rbx)
movl 0x58(%rbx), %esi
je 0x3a063
movq 0x60(%rbx), %rdx
movq %r14, %rdi
movq %r15, %rcx
callq 0x3b7f8
jmp 0x39f02
movq 0x10(%rbx), %rdi
leaq 0x49d09(%rip), %rsi     # 0x83d37
callq 0x6a50
testl %eax, %eax
je 0x39ec3
movq 0x8(%rbx), %rdi
movq 0x18(%rbx), %rax
movq 0x30(%rbx), %rsi
movl %r13d, %edx
movq %r14, %rcx
movq %rbx, %r8
callq *0x8(%rax)
movslq %eax, %r14
addq %r14, 0x30(%rbx)
addq %r14, 0x118(%rbx)
jmp 0x3a09a
movq %r14, %rdi
movq 0x8(%rsp), %rdx
movq %r15, %rcx
callq 0x3b7f8
addq %r15, 0x30(%rbx)
movq (%rsp), %r14
addq %r15, %r14
jmp 0x3a09a
leaq 0x49dee(%rip), %rsi     # 0x83e75
xorl %r14d, %r14d
pushq $0x4
popq %rdi
xorl %eax, %eax
callq 0x3c521
jmp 0x3a09a
movq (%rsp), %r14
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq