// File: /workspace/github_c_makefile_star2_O3/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, 0x80cb6(%rip)     # 0xc0c98
je 0x40166
movq %r14, 0x10(%rsp)
addq %r15, 0x118(%rbx)
movq 0x28(%rbx), %rax
movq 0x30(%rbx), %r14
cmpq %rax, %r14
jbe 0x40009
xorl %r14d, %r14d
jmp 0x40203
addq %r14, %r15
cmpq %rax, %r15
cmovaeq %rax, %r15
movq $0x0, 0x8(%rsp)
movq %r15, %rbp
subq %r14, %rbp
cmpl $0x0, 0x5c(%rbx)
je 0x400a4
movq 0x68(%rbx), %rax
leaq 0x1000(%rax), %r13
movq %r14, %rsi
subq %rax, %rsi
setb %al
cmpq %r13, %r14
setae %cl
orb %al, %cl
jne 0x40086
cmpq %r15, %r13
movq %r15, %r12
cmovbq %r13, %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, %rbp
je 0x401fe
addq %r12, %r14
movq %r14, 0x10(%rsp)
movq %r13, %r14
jmp 0x40089
movq %r14, %r13
movl $0x0, 0x5c(%rbx)
movl 0x58(%rbx), %edi
incq 0xe0(%rbx)
movq %r13, %rsi
xorl %edx, %edx
callq 0x23550
movq %r15, %rax
cmpq $0xfff, %rbp            # imm = 0xFFF
ja 0x400ee
movl $0x1, 0x5c(%rbx)
cmpq $0x0, 0x60(%rbx)
jne 0x400cc
movl $0x1000, %edi           # imm = 0x1000
callq 0x2dff4
movq %rax, 0x60(%rbx)
movq %r14, 0x68(%rbx)
movl $0x1000, %edx           # imm = 0x1000
leaq (%r14,%rdx), %rcx
movq 0x28(%rbx), %rax
movq %rax, %rbp
subq %r14, %rbp
cmpq %rax, %rcx
cmovbq %rcx, %rax
cmovbeq %rdx, %rbp
cmpq 0x78(%rbx), %r14
jb 0x400fd
cmpq 0x80(%rbx), %rax
jbe 0x40127
movq %rbx, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x4171d
testl %eax, %eax
je 0x401e6
movl 0x58(%rbx), %edi
incq 0xe0(%rbx)
movq %r14, %rsi
xorl %edx, %edx
callq 0x23550
cmpl $0x0, 0x5c(%rbx)
movl 0x58(%rbx), %edi
je 0x401a6
movq 0x60(%rbx), %rsi
movdqu 0xe8(%rbx), %xmm0
movq %rbp, %xmm1
movdqa 0x54997(%rip), %xmm2    # 0x94ae0
punpcklqdq %xmm1, %xmm2    # xmm2 = xmm2[0],xmm1[0]
paddq %xmm0, %xmm2
movdqu %xmm2, 0xe8(%rbx)
movq %rbp, %rdx
callq 0x234a5
jmp 0x4001c
movq 0x10(%rbx), %rdi
leaq 0x54cc6(%rip), %rsi     # 0x94e37
callq 0x6a50
testl %eax, %eax
je 0x3ffe8
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, %r14
addq %r14, 0x30(%rbx)
addq %r14, 0x118(%rbx)
jmp 0x40203
movdqu 0xe8(%rbx), %xmm0
movq %rbp, %xmm1
movdqa 0x54925(%rip), %xmm2    # 0x94ae0
punpcklqdq %xmm1, %xmm2    # xmm2 = xmm2[0],xmm1[0]
paddq %xmm0, %xmm2
movdqu %xmm2, 0xe8(%rbx)
movq 0x10(%rsp), %rsi
movq %rbp, %rdx
callq 0x234a5
addq %rbp, 0x30(%rbx)
movq 0x8(%rsp), %r14
addq %rbp, %r14
jmp 0x40203
leaq 0x54d88(%rip), %rsi     # 0x94f75
xorl %r14d, %r14d
movl $0x4, %edi
xorl %eax, %eax
callq 0x427e5
jmp 0x40203
movq 0x8(%rsp), %r14
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq